<?php

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

if (!defined('BASEDIR')) {
  include_once '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . 'constants.php';
}


class WBAnmeldungCheckWunsch extends WBAnmeldungCheck
{
  public function __construct($pDatamodul)
  {
    parent::__construct();
    $this->datamodul = $pDatamodul;
    $this->exportAdapter = new ExpWunschzettel();
    $this->exportAdapter->initiate();
    $this->exportAdapter->export->initiate();
    $this->exportAdapter->export->exportresults = array($this->datamodul);
    $this->exportAdapter->export->selectResultset(0);
  }

  public function _checkPflichtfeld($pVarName, $pSessPraefix = 'w', $pSessSuffix = '', $pCheckKonto = true, $pCheckSepa = true)
  {
    if ($pVarName == 'captcha' || $pVarName == 'captcha_ohnebr') {
      $capi = \Kufer\Web\Classes\Captchas\CaptchaFactory::getCaptcha4KuferWEB();

      return $capi->captcha->isValid($this->datamodul, true, 'wishedit');
    }

    
    if ($pVarName == 'teilnehmerart') {
      $pVarName = 'is_dozent';
    }



    
    $sName = $pVarName;

    
    if (trim(PostOperations::getVarValue('wishedit_' . $sName)) == '') {
      return false;
    }

    return true;
  }

  
  public function check()
  {
    if ($this->exportAdapter == null) {
      return false;
    }
    $this->prepareVorlage();
    $pflichtFelder = $this->getPflichtFelder(); 
    

    foreach ($pflichtFelder as $key => $value) {
      
      if (!$this->_checkPflichtFeld($value)) {
        $this->pflichtfeldFehlerArray[$key] = $this->getFehlerMeldung($key);

        continue;
      }
      
    }
    
    if ($this->datamodul != null) {
      $this->_checkValidierung();
    }

    if (count($this->pflichtfeldFehlerArray) > 0 || count($this->formatFehlerArray) > 0) {
      return false;
    }

    return true;
  }

  public function _checkValidierung($pSessPraefix = 'w', $pSessSuffix = '', $pAbwBankdaten = false, $pAlleDatenfelder = false)
  {
    if ($this->datamodul != null) {
      for ($i = 0; $i < count($this->datamodul); $i++) {
        $value = '';
        $sName = $this->datamodul[$i];
        if (trim(PostOperations::getVarValue($sName)) != '') {
          $value = PostOperations::getVarValue($sName);
        }
        if ($value !== '') {
          $tmp = $this->validation->isValidVar($sName, $value);
          if ($tmp !== true) {
            $msg = $this->getFehlerMeldung($tmp);
            
            $key = array_search($tmp, $this->varnamenMapping);
            if ($msg == $tmp && ($key !== false && $key !== null)) {
              $msg = $this->getFehlerMeldung($key);
            }
            
            $this->formatFehlerArray[$tmp] = $msg;
          }
        }
      }
    }
  }
}