<?php
/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */

class WBAnmeldungNormal extends WBAnmeldung
{
  public $epayinfo;

  
  public $step = 1;

  
  public $pflichtfeldFehlerArray = array();

  
  public $formatFehlerArray = array();

  
  public $anmeldeart;

  public function __construct($pUrlparameter)
  {
    parent::__construct($pUrlparameter);

    $valStep = intval(SessionOperations::getVarValueWk('last_validated_step', 0));
    $aktStep = (isset($this->urlparameter->schritt)) ? intval($this->urlparameter->schritt) : 1;
    $aktStep = ($aktStep <= 0) ? 1 : $aktStep;
    if ($valStep >= $aktStep) { 
      $this->step = $valStep = $aktStep;
      SessionOperations::setVarValueWk('last_validated_step', $valStep);
    }
    $this->_checkStep($aktStep, $valStep);

    if ($this->urlparameter->delattach != '') {
      SessionOperations::delUploadedFile('', $this->urlparameter->delattach);
    }
  }

  
  public function _checkStep($ziel, $geprüft = 0)
  {
    if (!is_numeric($ziel) || $ziel <= 0) {
      $ziel = 1;
    }

    
    if ($ziel >= 1 && $geprüft < 1) {
      if ($this->toolsettings->nurnormaleanmeldearten && $ziel == 1) {
        $ziel = 2;
        $this->step = 2;
        SessionOperations::setVarValueWk('last_validated_step', 2);
        $url = new URLParameterAnmeldungArt();
        
        
        $url->anmart = 1;
        $url->schritt = 2;
        $url->implodeSettings();
        $_REQUEST['urlparameter'] = $url->stream;
        $this->urlparameter->anmart = 1;
      } else {
        $this->step = 1;
        SessionOperations::setVarValueWk('last_validated_step', 1);
      }
    }

    
    if ($ziel >= 2 && $geprüft < 2) {
      $this->step = 2;
      SessionOperations::setVarValueWk('last_validated_step', 2);
    }

    
    if ($ziel >= 3 && $geprüft < 3) {
      
      if ($ziel == 3 && $_SERVER['REQUEST_METHOD'] == 'POST') {
        $this->getTeilnehmerFromRequest2Session();
        $this->getBelegungFromRequest2Session();

        
        $mehrfachsimpelpost = PostOperations::getVarValue('tedit_mehrfachsimpel');
        if ($mehrfachsimpelpost != ''
            && is_numeric($mehrfachsimpelpost)
            && $mehrfachsimpelpost != 0) {
          
          
          
          
          SessionOperations::setVarValueAnm('simpleweitere', 1);
          SessionOperations::setVarValueAnm(ANMELDEART, ANMELDEART_INTERN_MEHRFACH);
          SessionOperations::setVarValueWk('gAnzahl_Mehrfach', $mehrfachsimpelpost);
        }
      }
      $this->getSight(62);
      if (!$this->checkEingaben()) {
        $this->step = 2;
        SessionOperations::setVarValueWk('last_validated_step', 2);

        return;
      }



      $this->step = 3;
      SessionOperations::setVarValueWk('last_validated_step', 3);
    } 

    
    if ($ziel >= 4 && $geprüft < 4) {
      $this->step = 4;
      SessionOperations::setVarValueWk('last_validated_step', 4);
    }

    
    if ($ziel >= 6 && $geprüft <= 6) {
      $this->step = 6;
      SessionOperations::setVarValueWk('last_validated_step', 5);
    }
    if ($ziel >= 99 && $geprüft <= 99) {
      $this->step = 99;
      SessionOperations::setVarValueWk('last_validated_step', 99);
    }
  }

  
  public function checkEingaben($pCheck = '')
  {
    if ($pCheck instanceof WBAnmeldungCheckHauptTN) {
      $checkTn = $pCheck;
    } else {
      $checkTn = new WBAnmeldungCheckHauptTN();
    }
    $this->addAdditionalValidators($checkTn);
    $checkTn->vorlage = $this->getVorlageForMainCheck();
    
    if (!$checkTn->check()) {
      $this->pflichtfeldFehlerArray = array_merge($this->pflichtfeldFehlerArray, $checkTn->pflichtfeldFehlerArray);
      $this->formatFehlerArray = array_merge($this->formatFehlerArray, $checkTn->formatFehlerArray);
    }

    
    
    
    
    
    
    if (StringOperations::strpos($checkTn->vorlage, '{%bedit_belegungsdaten%}') !== false) {
      $this->checkFreiDefbarFelder();
    }

    
    $this->checkBedit();

    return (count($this->pflichtfeldFehlerArray) === 0 && count($this->formatFehlerArray) === 0);
  }

  protected function getVorlageForMainCheck()
  {
    return $this->sight->body;
  }

  private function checkBedit()
  {
    $checkKursInfo = new WBAnmeldungCheckKursInfo();
    $checkKursInfo->vorlage = $this->sight->body;
    if (!$checkKursInfo->check()) {
      $this->pflichtfeldFehlerArray = array_merge($this->pflichtfeldFehlerArray, $checkKursInfo->pflichtfeldFehlerArray);
      $this->formatFehlerArray = array_merge($this->formatFehlerArray, $checkKursInfo->formatFehlerArray);
    }
  }

  private function checkFreiDefbarFelder()
  {
    $sight = SightsAPI::getSight(426);
    $checkFreifelder = new WBAnmeldungCheckFreifelder();
    $sightPartAry = array('header', 'body', 'footer');
    $check = true;

    foreach ($sightPartAry as $sightPart) {
      $checkFreifelder->vorlage = $sight->{$sightPart};
      $check &= $checkFreifelder->checkFreiFelder('bedit');
    }

    if (!$check) {
      $this->pflichtfeldFehlerArray = array_merge($this->pflichtfeldFehlerArray, $checkFreifelder->pflichtfeldFehlerArray);
    }
  }

  private function _getClickLink($pUrl, $pContent)
  {
    return '<a class="clickadresse" href="' . CleanOperations::cleanStringForHtml($pUrl) . '">' . CleanOperations::cleanStringContainingHtml($pContent) . '</a>';
  }

  
  public function setClickPath()
  {
    if (DBWorkBASysSettings::isAugsburgKolping()) {
      $kurse = $this->dbwWk->getKurse();

      
      if (count($kurse) === 0) {
        $knr = $this->urlparameter->knradd;
        $kurs = WBKursStore::getKurs($knr);
      } else {
        $kurs = $kurse[0];
        $knr = $kurse[0]->id;
      }

      $dbw = new DBWorkCategories();
      $cat = $dbw->getFirstCategoryForKurs($knr);
      $cats = $dbw->getParentCategories($cat);
      $vorgExist = false;
      
      $path = '';
      foreach ($cats as $cat) {
        
        $part = str_replace(' ', '-', $cat->text);
        $part = str_replace('--', '-', $part);
        $path .= StringOperations::strtolower($part) . '/';
        $link = $this->_getClickLink($path, $cat->text);
        $this->clickpfad .= $this->_buildClickString(
          $link,
          1,
          1,
          false,
          false,
          $vorgExist
        );
        $vorgExist = true;
      }
      $this->clickpfad .= $this->_buildClickString(
        $this->_getClickLink(
          INDEXSCRIPT . '?' . (GetOperations::getVarValue('alteid') != '' ? 'id=' . GetOperations::getVarValue('alteid') . '&amp;' : '') . 'urlparameter=' . $this->urlparameter->getEmptyStreamWith(
            array('kathaupt', 'knr', 'kursname'),
            array('11', $kurs->id, $kurs->getVarValue('kurzbez_titel'))
          ),
          $kurs->getVarValue('kurzbez_titel')
        ),
        1,
        1,
        true,
        true,
        $vorgExist
      );

      return;
    }
    
    
    
    
    
    
    
    
    
    $vorgExist = false;
    if (!empty($this->toolsettings->titleKursBereich)
        || !empty($this->toolsettings->anmeldeBereichTitel)) {
      $vorgExist = true;
    }
    $anmeldeartText = '';
    if (!$this->toolsettings->nurnormaleanmeldearten) {
      $anmeldeartText = $this->toolsettings->anmeldeArtText;
    }
    switch ($this->step) {
      default:
      case 1:
        
        $this->clickpfad = $this->_buildClickString(
          $anmeldeartText,
          1,
          1,
          false,
          true,
          $vorgExist
        );
        break;
      case 2:
        
        $this->clickpfad = $this->_buildClickString(
          $anmeldeartText,
          1,
          1,
          true,
          false,
          $vorgExist
        );
        $vorgExist = ($vorgExist || (!empty($anmeldeartText)));
        $this->clickpfad .= $this->_buildClickString(
          $this->toolsettings->anmeldeEingTextHaupt,
          1,
          2,
          false,
          true,
          $vorgExist
        );
        break;
      case 3:
        
        $this->clickpfad = $this->_buildClickString(
          $anmeldeartText,
          1,
          1,
          true,
          false,
          $vorgExist
        );
        $vorgExist = ($vorgExist || (!empty($anmeldeartText)));
        $this->clickpfad .= $this->_buildClickString(
          $this->toolsettings->anmeldeEingTextHaupt,
          1,
          2,
          true,
          false,
          $vorgExist
        );
        $vorgExist = ($vorgExist || (!empty($this->toolsettings->anmeldeEingTextHaupt)));
        $this->clickpfad .= $this->_buildClickString(
          $this->toolsettings->anmeldeControlText,
          1,
          3,
          false,
          true,
          $vorgExist
        );
        break;
      case 4:
        $this->clickpfad .= $this->_buildClickString(
          $this->toolsettings->anmeldeConfirmText,
          1,
          4,
          false,
          true,
          $vorgExist
        );
        break;
    }
  }

  
  public function content()
  {
    
    if (SessionOperations::getVarValueAnm('simpleweitere') < 1) {
      SessionOperations::setVarValueAnm(ANMELDEART, ANMELDEART_INTERN_NORMAL);
    }

    switch ($this->step) {
      default:
      case 1:
        $oldMtid = SessionOperations::getVarValueAnm('epay_anm_mtid');
        if (!empty($oldMtid)) {
          $oldPayonline = ePaymentApi::getPayonlineProviderKeyFromSession();
          ePaymentApi::handleAlteMtidInSession($oldMtid, $oldPayonline);
        }

        $this->showsightcomplete(60, new ExpTeilnehmer());
        break;
      case 2: 
        $this->dataarraybody = SessionOperations::getTeilnehmerFromSession();
        if (count($this->pflichtfeldFehlerArray) > 0
        || count($this->formatFehlerArray) > 0) {
          $this->warnungpflichtfelder(62, new ExpTeilnehmer());
        } else {
          $this->showsightcomplete(62, new ExpTeilnehmer());
        }
        break;
      case 3: 
        $this->dataarrayheader = SessionOperations::getTeilnehmerFromSession();
        $this->anmeldungbestaetigen();
        break;
      case 4:
        if (ePaymentApi::isAnyPayonlineInSession() || (isset($this->urlparameter->payOnline) && $this->urlparameter->payOnline)) {
          
          $this->ePaymentCommit($this->urlparameter);
        } else {
          
          $this->anmeldungabschliessen();
        }
        
        SessionOperations::setVarValueAnm(ANMELDEART, null);
        break;
      case 6: 
        $this->ePaymentExecute($this->urlparameter);
        break;
      case 99:
        $this->doSchritt99($this->urlparameter);
        break;
    }
  }

  
  public function _anmeldungbestaetigenPersonenPart()
  {
    $retour = '';
    $this->dataarraybody = SessionOperations::getTeilnehmerFromSession();
    $retour .= $this->getsightcomplete(61, new ExpTeilnehmer());

    return $retour;
  }

  
  public function _anmeldungbestaetigenKursPart($pKurse)
  {
    $retour = '';
    $this->dataarraybody = $pKurse;
    $retour .= $this->getsightcomplete(69, new ExpKursuebersicht());

    return $retour;
  }

  
  public function _getNichtAbsendbarSightID()
  {
    return 67;
  }

  public static function anmeldungNichtAbsendbarAbbruch($pSightId)
  {
    if (!SessionOperations::mailAbsendbar()) {
      $dmbasis = new DataMenuBasis(null);
      $dmbasis->exportadapter = new ExpBasis();
      $dmbasis->getsight($pSightId);
      $dmbasis->dataarraynodata = array(new Datamodul());
      echo $dmbasis->getnodata();

      return true;
    }

    return false;
  }

  
  public function anmeldungabschliessen($pMessage = '', $pSplitAnmeldungKurs = false)
  {
    $objsettings = ToolSettingsAPI::getToolSettings(4);
    
    if ($this->anmeldungNichtAbsendbarAbbruch($this->_getNichtAbsendbarSightID())) {
      return false;
    }

    $anmeld = $this->getBuchungsVorgang();
    
    $anmeld->createTNInfo();
    if (!empty($this->epayinfo)) {
      $anmeld->setEPayInfo($this->epayinfo);
    }

    $anmeld->setKurseBySession();

    $anmeld = $this->anmeldungabschliessenHookAfterSetKurse($anmeld);

    $anmeld->setBuchungsVars(); 

    $anmeld->buchungStep1(); 
    

    $erfolgreich = $anmeld->buchungStep2();
    $this->kurse_anmstatus = $anmeld->kurse_anmstatus;

    if ($pSplitAnmeldungKurs) {
      $htmlbest = $anmeld->getAnmeldebestaetigungHTML($pMessage, true);
    } else {
      $htmlbest = $anmeld->getAnmeldebestaetigungHTML($pMessage);
    }

    if (SessionOperations::getVarValueAnm('payonline', false)) {
      
      
      

      SessionOperations::setVarValueAnm('mailBest', $anmeld->generiereMailBesttext());

      
      if (SessionOperations::getVarValueAnm('epay_schnittstelle') != '' && strlen(SessionOperations::getVarValueAnm('epay_schnittstelle')) > 1) {
        SessionOperations::setVarValueAnm('htmlBest', $htmlbest);
        $anmeld->sendTNBestaetigung();
        $anmeld->sendBestaetigungKopie();
      }
    } else {
      echo $htmlbest;
      $anmeld->sendTNBestaetigung();
      $anmeld->sendBestaetigungKopie();
      
      
      $anmeld->cleanUp();
    }
    
    if ($objsettings->showOriginalAnmeldebestaetigungDruck) {
      SessionOperations::setVarValueDruck('druckversion', $htmlbest);
    } else {
      SessionOperations::setVarValueDruck('druckversion', StringOperations::getBestaetigungBody($anmeld->besttext));
    }
    if (!$erfolgreich) {
      return false;
    }

    return true;
  }

  protected function getBuchungsVorgang()
  {
    return new BuchungsvorgangWEBBASys();
  }

  
  protected function anmeldungabschliessenHookAfterSetKurse($pBuchungsVorgang)
  {
    return $pBuchungsVorgang;
  }

  
  public function warnungpflichtfelder($sightid, $expadapter)
  {
    $this->getsight($sightid);
    $this->exportadapter = $expadapter;
    $this->exportadapter->export->initiate();
    $this->exportadapter->setFehler($this->pflichtfeldFehlerArray, $this->formatFehlerArray);
    $this->showheader();
    if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'GET') {
      echo '<span class="noget">Benutzen Sie zur Navigation bitte nur die dafür vorgesehenen Schaltflächen!</span>';
    } else {
      $this->shownodata();
    }

    $this->sight->body = str_replace(
      '{%sbtn_wizzardweiter%}',
      '{%sbtn_wizzardnichtweiter%}',
      $this->sight->body
    );

    echo '<ul class="warnungpflichtfelder">';
    foreach ($this->pflichtfeldFehlerArray as $meldung) {
      if ($meldung != '') {
        echo '<li class="warnungpflichtfelder">' . CleanOperations::cleanStringContainingHtml($meldung) . '</li>';
      }
    }

    
    echo $this->warnungValidierung($this->exportadapter->varlist);

    echo '</ul>';

    $this->showbody();
    $this->showfooter();
  }

  
  public function warnungValidierung($pArr)
  {
    $ret = '';
    if (count($this->formatFehlerArray) == 0) {
      return $ret;
    }
    foreach ($this->formatFehlerArray as $value) {
      $ret .= "  <li class='warnungvalidierung'>" . CleanOperations::cleanStringContainingHtml($value) . "</li>\n";
    }

    return $ret;
  }

  
  public function getTeilnehmerFromRequest2Session($pRequestPraefix = 'tedit', $pSessionSuffix = '')
  {
    SessionOperations::getTeilnehmerFromRequest2Session($pRequestPraefix, $pSessionSuffix);

    $sessionSuffix = ($pSessionSuffix !== '' && !StringOperations::startsWith($pSessionSuffix, '_')) ? '_' . $pSessionSuffix : $pSessionSuffix;

    
    $blzsepa = SessionOperations::getWKBankart();
    $iban = SessionOperations::getVarValueWk('w_iban', '');
    $bic = SessionOperations::getVarValueWk('w_bic', '');
    $blz = SessionOperations::getVarValueWk('w_blz', '');
    $bankbez = $this->getBankbezeichnung($blzsepa, $iban, $bic, $blz);

    if ($bankbez != '') {
      SessionOperations::setVarValueWk('w_bankbez', $bankbez);
    }

    if ($bic == '' && $iban != '') {
      $bic = BankDatenOperations::getBicFromIban($iban);
      SessionOperations::setVarValueWk('w_bic', $bic);
    }

    if ($this->toolsettings->enforceNewslErlaubt) {
      
      $newslSessionName = 'w_newsl_erlaubt' . $sessionSuffix;
      if (SessionOperations::getVarValueWk($newslSessionName) === '') {
        SessionOperations::setVarValueWk($newslSessionName, 'N');
      }
    }

    
    
    $zimmerselected = SessionOperations::getVarValueWk('w_uw_zimmerkursselect', '');
    if ($zimmerselected != '') {
      $dbwkurse = new DBWorkKurseSmall();
      $kurse = $this->dbwWk->getKurse();
      if (count($kurse) == 1) {
        $knrInWk = $kurse[0]->id;
        $knrToAdd = $dbwkurse->getZimmerUnterKurs($knrInWk, $zimmerselected);
        $this->dbwWk->addEntry($knrToAdd);
        $this->warenkorb = $this->dbwWk->getEntries();
      }
    }
  }

  public function getBelegungFromRequest2Session($pRequestPraefix = 'bedit', $pSessionSuffix = '')
  {
    SessionOperations::getBelegungFromRequest2Session($pRequestPraefix, $pSessionSuffix);

    
    $blzsepa = SessionOperations::getWKBankart(true);
    $iban = SessionOperations::getVarValueWk('w_abw_iban_b', '');
    $bic = SessionOperations::getVarValueWk('w_abw_bic_b', '');
    $blz = SessionOperations::getVarValueWk('w_abw_blz_b', '');
    $bankbez = $this->getBankbezeichnung($blzsepa, $iban, $bic, $blz);

    if ($bankbez != '') {
      SessionOperations::setVarValueWk('w_abw_bankbez_b', $bankbez);
    }

    if ($this->toolsettings->enforceNewslErlaubt) {
      
      $newslSessionName = 'w_abw_newslerlaubt_b';
      if (SessionOperations::getVarValueWk($newslSessionName) === '') {
        SessionOperations::setVarValueWk($newslSessionName, 'N');
      }
    }
  }

  public function getBelegungWeitererFromRequest2Session($pRequestPraefix = 'bedit', $pSessionSuffix = '')
  {
    SessionOperations::getBelegungKursDataFromRequest2Session($pRequestPraefix, $pSessionSuffix);
  }

  public function setBackToStep($pStep)
  {
    $url = new URLParameterAnmeldungZusatzPerson();
    
    
    $url->anmart = $this->anmeldeart;
    $url->schritt = $pStep;
    $url->implodeSettings();
    $_REQUEST['urlparameter'] = $url->stream;
    $this->urlparameter->schritt = $pStep;
  }

  
  protected function addAdditionalValidators($pCheck)
  {
    $settings = ToolSettingsAPI::getToolSettings(4);
    if ($settings->empfohlenesAlterPflichtBeiHauptTn) {
      $validator = new GebdatAltersbereichValidator();
      $pCheck->addAdditionalValidator($validator);
    }
  }
}