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

class WBAnmeldungKinder extends WBAnmeldungWeiterePersonen
{
  public function __construct($urlparameter)
  {
    $this->anmeldeart = 3;
    parent::__construct($urlparameter);
  }

  
  public function _checkStep($ziel, $geprüft = 0)
  {
    if (!System::isKinderAnmeldungErlaubt()) {
      $this->step = 1;

      return;
    }

    if (!is_numeric($ziel) || $ziel <= 0) {
      $ziel = 1;
    }

    if ($ziel >= 1 && $geprüft < 1) {
      $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) {
      $tmpurlparam = new URLParameterAnmeldungZusatzPerson();
      
      if ($geprüft < 3) {
        if ($ziel == 3 && $_SERVER['REQUEST_METHOD'] == 'POST' && $tmpurlparam->addhauptperson == 1) {
          $this->getTeilnehmerFromRequest2Session();
          $this->getBelegungFromRequest2Session();
        }
        $this->getSight(62);
        if (!$this->checkEingaben()) {
          $this->step = 2;

          return;
        }
        SessionOperations::setVarValueWk('last_validated_step', 3);
      }
      
      if ((3 == $ziel && $_SERVER['REQUEST_METHOD'] == 'POST' && $tmpurlparam->addperson == 1)
        || (4 == $ziel && isset($_REQUEST['kiedit_vorname']) && trim($_REQUEST['kiedit_vorname']) != '')) {
        $this->getKindFromRequest2Session('kiedit', 'k');
        $this->addPerson = true;
        $ziel = 3;
        $this->step = 3;
        $this->setBackToStep(3);
      } elseif ($tmpurlparam->delperson != '') {
        if (!static::$alreadyDeleted) {
          $this->delWeiterePersonFromSession(
            $tmpurlparam->delperson,
            $this->getPersonObject(),
            'gAnzahl_Kinder',
            'w',
            'k'
          );
          static::$alreadyDeleted = true;
        }
        $this->step = 3;

        return;
      }
      $this->getSight(64);
      $check = $this->_getWbAnmeldungCheck();
      $check->vorlage = $this->sight->footer;
      if (!$check->check()) {
        
        
        
        
        
        if (SessionOperations::getVarValueWk('gAnzahl_Kinder', 0) >= 1 && !$this->addPerson); 
      else {
          if ($tmpurlparam->addhauptperson != 1) {
            $this->addPerson = true;
          } 
        $this->pflichtfeldFehlerArray = $check->pflichtfeldFehlerArray;
        $this->formatFehlerArray = $check->formatFehlerArray;
        $this->step = 3;

        return;
      }
      }
      $this->step = 3;
      
      
    }

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

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

  
  public function content()
  {
    SessionOperations::setVarValueAnm(ANMELDEART, ANMELDEART_INTERN_ELTERNKIND);
    $isFehlerAufgetreten
        = (count($this->pflichtfeldFehlerArray) > 0
            || count($this->formatFehlerArray) > 0);
    switch ($this->step) {
      default:
      case 1:
      





        $this->showsightcomplete(60, new ExpTeilnehmer());
        break;
      case 2: 
        $this->dataarraybody = SessionOperations::getTeilnehmerFromSession();
        if ($isFehlerAufgetreten) {
          $this->warnungpflichtfelder(62, new ExpTeilnehmer());
        } else {
          $this->showsightcomplete(62, new ExpTeilnehmer());
        }
        break;
      case 3: 
        if ($this->addPerson) {
          if ($isFehlerAufgetreten) {
            $this->warnungpflichtfelder(77, new ExpKinder());
            break;
          } else {
            $this->addWeiterePerson2Session(
              $this->getPersonObject(),
              'gAnzahl_Kinder',
              'w',
              'k'
            );
            $this->delEditPersonFromSession($this->getPersonObject(), 'w', 'k');
          }
        }
        
        
        if ($isFehlerAufgetreten || SessionOperations::getVarValueWk('gAnzahl_Kinder', 0) < 1) {
          $this->getKinderMenu(true);
        } else {
          $this->getKinderMenu();
        }
        break;
      case 4: 
        if (isset($this->urlparameter->payOnline) && $this->urlparameter->payOnline) {
          $this->ePaymentStart($this->urlparameter);
        } else {
          $this->dataarrayheader = SessionOperations::getTeilnehmerFromSession();
          $this->anmeldungbestaetigen();
        }
        break;
      case 5: 
        if (ePaymentApi::isAnyPayonlineInSession() || (isset($this->urlparameter->payOnline) && $this->urlparameter->payOnline)) {
          $this->ePaymentCommit($this->urlparameter);
        } else {
          $this->anmeldungabschliessen();
        }
        SessionOperations::setVarValueAnm(ANMELDEART, null);
        break;
      case 6: 
        if (isset($this->urlparameter->payOnline) && $this->urlparameter->payOnline) {
          $this->ePaymentExecute($this->urlparameter);
        }
        break;
      case 99:
        if (isset($this->urlparameter->payOnline) && $this->urlparameter->payOnline) {
          $this->doSchritt99($this->urlparameter);
        }
        break;
    }
  }

  
  public function _anmeldungbestaetigenPersonenPart()
  {
    $retour = '';

    
    $this->dataarraybody = SessionOperations::getTeilnehmerFromSession();
    $retour .= $this->getsightcomplete(61, new ExpTeilnehmer());

    
    $this->dataarraybody = SessionOperations::getWeitereFromSession();
    if (count($this->dataarraybody) > 0) {
      $retour .= $this->getsightcomplete(224, new ExpKinder());
    }

    return $retour;
  }

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

    return $retour;
  }

  
  public function getKinderMenu($pNichtWeiter = false)
  {
    $this->dataarraybody = SessionOperations::getKinderFromSession();
    $this->nrOfMainBodyElements = count($this->dataarraybody);
    $this->showsightcomplete(64, new ExpKinderRekursiv(), $pNichtWeiter);
  }

  
  public function getKindFromRequest2Session($pRequestPraefix = 'kiedit', $pSessionSuffix = 'k')
  {
    $requestPraefix = ($pRequestPraefix != '' ? $pRequestPraefix . '_' : '');
    $sessionSuffix = ($pSessionSuffix != '' ? '_' . $pSessionSuffix : '');

    
    $tmpdbwkind = new DBWorkKinder();
    $tmptn = $tmpdbwkind->getNewObject();
    
    for ($i = 0;$i < $tmptn->getVarCount();$i++) {
      if (isset($_REQUEST[$requestPraefix . $tmptn->getVarName($i)])) {
        $value = trim($_REQUEST[$requestPraefix . $tmptn->getVarName($i)]);
      } else {
        $value = '';
      } 

      
      SessionOperations::setVarValueWk('w_' . $tmptn->getVarName($i) . $sessionSuffix, $value);
    }
  }

  
  public function getPersonObject()
  {
    return new Kind();
  }

  
  protected function _getWbAnmeldungCheck()
  {
    return new WBAnmeldungCheckKind();
  }
}