<?php

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


class DataMenuAnmeldungVerification extends DataMenuMainTopLeft
{
  public $urlparameter;

  public function __construct($urlparameter)
  {
    parent::__construct($urlparameter);
  }

  
  public function mainmenu()
  {
    $this->prepareMainmenu();
    if ($this->toolsettings->showClickpfad) {
      echo $this->getClickPath();
    }
    echo '<div class="hauptseite">' . "\n" . $this->getContentAnker();

    if (PostOperations::getVarValue('verify')) {
      $this->step2(); 
    } else {
      $this->step1(); 
    }
    echo "</div>\n\n";
  }

  
  public static function getVerifyObject($verify)
  {
    $toolsettings = ToolSettingsAPI::getToolSettings(0);
    $filepath = $toolsettings->getVarValue('emailfilepath');

    $xmlobj = null;
    $verify = base64_decode($verify);
    if ($verify != '') {
      if (file_exists($filepath . DIRECTORY_SEPARATOR . $verify . DATEIENDUNG_VERIFICATION)) {
        $contents = file_get_contents($filepath . DIRECTORY_SEPARATOR . $verify . DATEIENDUNG_VERIFICATION);
        
        $contents = preg_replace_callback('/(<(\\/)?\\b([A-Z_]+)\\b>)/', function($word) {
          return strtolower($word[1]);
        }, $contents);
        $xmlobj = new SimpleXMLElement($contents);
        $xmlobj = json_decode(json_encode($xmlobj)); 
      }
    }

    return $xmlobj;
  }

  public static function xmlTnToTeilnehmerPflicht($tn)
  {
    $encode = new EncodeCharsetFromTo(UTF_8, CodingOperations::getInternalEncoding());

    return Datamodul::toDatamodul('TeilnehmerPflicht', $tn, array(), array(), array($encode, 'encode'));
  }

  
  private function step1()
  {
    $xmlobj = self::getVerifyObject($this->urlparameter->verify);
    if (!is_null($xmlobj)) {
      $hauptteilnehmer = self::xmlTnToTeilnehmerPflicht($xmlobj->anmeldung->stammdaten);
      $this->exportadapter = new ExpTeilnehmer();
      $this->dataarrayheader = array($hauptteilnehmer);
      $this->showheader();
      $kurseAusXml = $xmlobj->anmeldung->kurse->kurs; 
      if (!is_array($kurseAusXml)) {
        $kurseAusXml = array($kurseAusXml);
      } 

      $kursnummern = array_map(function($el) {return $el->kursnummer;}, $kurseAusXml);
      $dbwkurse = new DBWorkKurse();
      $kurse = $dbwkurse->getKurseByKursnummern($kursnummern);
      $this->exportadapter = new ExpKurse();
      $this->dataarraybody = $kurse;
      $this->showbody();

      $this->showfooter();

      echo '<form class="verifizieren-form" action="" method="POST">';
      echo '<input type="hidden" name="verify" value="' . $this->urlparameter->verify . '">';
      echo '<input class="verifizieren-submit" type="submit" value="verifizieren">';
      echo '</form>';
    } else {
      $this->shownodata();
    }
  }

  
  private function step2()
  {
    $toolsettings = ToolSettingsAPI::getToolSettings(0);
    $filepath = $toolsettings->getVarValue('emailfilepath');

    $success = false;
    $verify = base64_decode(PostOperations::getVarValue('verify'));
    if (file_exists($filepath . DIRECTORY_SEPARATOR . $verify . DATEIENDUNG_VERIFICATION)) {
      $success = rename($filepath . DIRECTORY_SEPARATOR . $verify . DATEIENDUNG_VERIFICATION, $filepath . DIRECTORY_SEPARATOR . $verify . '.xml');
      if ($success) {
        $this->shownocolumn();
      }
    }
    if (!$success) {
      $this->shownodata();
    }
  }

  public static function getWeitereArrayForSight($pDatamodul)
  {
    $urlparam = new URLParameterVerification();
    $xmlobj = DataMenuAnmeldungVerification::getVerifyObject($urlparam->verify);
    if (is_null($xmlobj)) {
      return '';
    }
    $kurseAusXml = $xmlobj->anmeldung->kurse->kurs;
    if (!is_array($kurseAusXml)) {
      $kurseAusXml = array($kurseAusXml);
    }
    $weitereAry = array();
    foreach ($kurseAusXml as $kursAusXml) {
      if ($kursAusXml->kursnummer == $pDatamodul->id) {
        if (isset($kursAusXml->weitereanm->weiteranm)) {
          if (!is_array($kursAusXml->weitereanm->weiteranm)) {
            $kursAusXml->weitereanm->weiteranm = array($kursAusXml->weitereanm->weiteranm);
          }
          foreach ($kursAusXml->weitereanm->weiteranm as $weiterer) {
            if (isset($weiterer->weiterstamm)) { 
              $weitereAry[] = DataMenuAnmeldungVerification::xmlTnToTeilnehmerPflicht($weiterer->weiterstamm);
            }
          }
        }
      }
    }

    return $weitereAry;
  }

  public static function getAbwZahlerSightpart($pDatamodul)
  {
    $urlparam = new URLParameterVerification();
    $xmlobj = DataMenuAnmeldungVerification::getVerifyObject($urlparam->verify);
    if (is_null($xmlobj)) {
      return '';
    }
    $kurseAusXml = $xmlobj->anmeldung->kurse->kurs;
    if (!is_array($kurseAusXml)) {
      $kurseAusXml = array($kurseAusXml);
    }
    $abwZahler = null;
    foreach ($kurseAusXml as $kursAusXml) {
      if ($kursAusXml->kursnummer == $pDatamodul->id) {
        $abwZahler = self::getAbwZahlerTnPflichtAusStdObject($kursAusXml);
      }
    }
    $dmbasis = new DataMenuBasis(new URLParameterBasis());
    $defaultExp = new ExpTeilnehmer();
    $dmbasis->getsight('751');

    if ($abwZahler == null) {
      return '';
    }
    $dmbasis->exportadapter = $defaultExp;
    $dmbasis->dataarraynocolumn = array($abwZahler);

    return $dmbasis->getnocolumn();
  }

  private static function getAbwZahlerTnPflichtAusStdObject($xmlObj)
  {
    if (!isset($xmlObj->abw_name) || $xmlObj->abw_name == '') {
      return null;
    }
    $tn = new TeilnehmerPflicht();
    $encode = new EncodeCharsetFromTo(UTF_8, CodingOperations::getInternalEncoding());
    $mapping = array('abw_bankbez' => 'bankbez', 'abw_name' => 'name', 'abw_vorname' => 'vorname', 'abw_geschlecht' => 'geschlecht',
         'abw_gebdat' => 'gebdat', 'abw_strasse' => 'strasse', 'abw_ort' => 'ort', 'abw_email' => 'email', 'abw_telefon' => 'tel',
         'abw_tnr' => 'id');
    foreach ($xmlObj as $schluessel => $wert) {
      if (isset($mapping[$schluessel])) {
        $tn->setValue2Var($mapping[$schluessel], $encode->encode($wert));
      }
    }

    return $tn;
  }

  public function getsight($sightid = 750)
  {
    parent::getSight($sightid);
  }

  public function _getClickPath($pName = 'Verifizierung')
  {
    $this->clickpfad = '';
    $this->toolsettings = ToolSettingsAPI::getToolSettings(4);
    if ($this->toolsettings->titleKursBereich != '') {
      $this->clickpfad = '<span class="clickadresse"> ' . CleanOperations::cleanStringForHtml(stripslashes($this->toolsettings->titleKursBereich)) . ' </span>' . PHP_EOL;
      $this->clickpfad .= '<span class="trennzeichen"> ' . CleanOperations::cleanStringContainingHtml($this->toolsettings->trennzeichenKategorie) . ' </span>' . PHP_EOL;
    }
    $this->clickpfad .= $pName;

    return $this->clickpfad;
  }
}