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

class DataMenuAnmeldungNeuKontrolle extends DataMenuMainTopLeft
{
  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();
    SessionOperations::setVarValueAnm('anminfomessages', array());
    $fehlerArray = array();
    $anminfo = PostOperations::getAnmInfo();

    $this->statusmenu();

    $this->exportadapter = new ExpTeilnehmer();
    if ($anminfo != null) {
      $anminfo->hauptTn = $this->prepareHauptTnForSession($anminfo->hauptTn);
      $this->setHauptTnToSession($anminfo->hauptTn);
    }
    $this->dataarrayheader = SessionOperations::getTeilnehmerFromSession();
    $this->getsight();
    $this->showheader();
    $this->exportadapter = new ExpKurse();
    $dataarraybody = array();
    if ($anminfo != null) {
      foreach ($anminfo->kurse as $kurs) {
        $subclasses = array(
            'zusatztexte' => 'Kurszusatztexte',
            'basysbenutzer_verwaltung_ma' => 'BASysBenutzer',
            'hauptdozent' => 'Dozent',
            'email' => 'Kommunikation',
            'email2' => 'Kommunikation',
            'tel' => 'Kommunikation',
            'tel2' => 'Kommunikation',
            'mobil' => 'Kommunikation',
            'mobil2' => 'Kommunikation',
            'fax' => 'Kommunikation',
            'fax2' => 'Kommunikation');
        $kurs = Datamodul::toDatamodul('Kurs', $kurs, $subclasses);
        array_push($dataarraybody, $kurs);
      }
      $this->dataarraybody = $dataarraybody;
    }


    SessionOperations::setVarValueAnm('anminfo', $anminfo);
    $this->showbody();
    $this->exportadapter = new ExpKurse();
    $this->showfooter();

    if ($anminfo != null) {
      
      $dbwwk = new DBWorkWarenkorbKurse();
      $kurse = $dbwwk->getKurse();
      $kursnummernWK = array();
      foreach ($kurse as $kurs) {
        $kursnummernWK[] = $kurs->id;
      }
      $kursnummernAnminfo = array();
      foreach ($anminfo->kurse as $kurs) {
        $kursnummernAnminfo[] = $kurs->knr;
      }
      $zuLoeschen = array_diff($kursnummernWK, $kursnummernAnminfo);
      foreach ($zuLoeschen as $knr) {
        $dbwwk->delEntry($knr);
      }

      if (count($zuLoeschen) > 0) {
        SessionOperations::setVarValueAnm('anminfo', $anminfo);
      } else {
        
      }
      $this->exportadapter = new ExpTeilnehmer();
      $fehlerArray = $this->checkHauptTn();
    } else { 
      $fehlerArray = array('emptyAnminfo' => 'emptyAnminfo');
    }

    $this->submitForm();

    if (count($fehlerArray) > 0) {
      SessionOperations::setVarValueAnm('anminfomessages', $fehlerArray);
      $this->submitFormClick();
    }

    echo "</div>\n\n";
  }

  

  private function submitForm()
  {
    $t3part = '';
    if (GetOperations::getVarValue('id') != '') {
      $t3part = 'id=' . CleanOperations::cleanInt(GetOperations::getVarValue('id')) . '&amp;';
    }
    $url = 'index.php?' . $t3part . 'kathaupt=500';

    echo '<form action="' . $url . '" method="POST" id="backform">';
    echo '<input type="submit" id="backsubmit" class="backsubmit btn btn-theme" value="zurück">';
    echo '</form>';
  }

  private function submitFormClick()
  {
    echo '<script type="text/javascript">
    document.getElementById("backform").submit();
    </script>';
  }

  private function prepareHauptTnForSession($pHaupTn)
  {
    $blzsepa = SessionOperations::getWKBankart();
    $iban = SessionOperations::getVarValueWk('w_iban', '');
    $bic = SessionOperations::getVarValueWk('w_bic', '');
    $blz = SessionOperations::getVarValueWk('w_blz', '');
    $bankbez = WBAnmeldung::getBankbezeichnung($blzsepa, $iban, $bic, $blz);

    if ($bankbez != '') {
      $pHaupTn->bankbez = $bankbez;
    }

    return $pHaupTn;
  }

  private function setHauptTnToSession($pHauptTn)
  {
    $subclasses = array(
      'fax' => 'Kommunikation',
      'fax2' => 'Kommunikation',
      'fax3' => 'Kommunikation',
      'email' => 'Kommunikation',
      'tel' => 'Kommunikation',
      'tel2' => 'Kommunikation',
      'mobil' => 'Kommunikation',
      'mobil2' => 'Kommunikation',
      'email2' => 'Kommunikation');
    $hauptTn = Datamodul::toDatamodul('TeilnehmerPflicht', $pHauptTn, $subclasses);
    SessionOperations::setDatamodulToSession($hauptTn, 'wk_w_');
    SessionOperations::setVarValueWk('w_zahlart_b', $pHauptTn->zahlart);
  }

  private function checkHauptTn()
  {
    $checkHauptTn = new WBAnmeldungCheckHauptTN();
    $this->getsight(504);
    $checkHauptTn->vorlage = $this->getheader() . $this->getbody() . $this->getfooter();
    $checkHauptTn->check();

    return $checkHauptTn->pflichtfeldFehlerArray;
  }

  public function leseJsonNeuesAnmeldeverfahren()
  {
  }

  public function getsight($pSightId = 508)
  {
    parent::getsight($pSightId);
  }

  
  public function statusmenu($pPHP53DummyParam = '')
  {
    $tagnameFuerStatusmeldung = $this->getTagnameFuerStatusmeldung();
    $beginTag = '';
    $endTag = '';
    if ($tagnameFuerStatusmeldung != '') {
      $beginTag = "   <$tagnameFuerStatusmeldung class='statusmeldung'>";
      $endTag = "</$tagnameFuerStatusmeldung>";
    }
    if ($this->status1 == '' && $this->status2 == '') {
      return;
    }
    echo "  <div class='hauptseite_status'>\n";
    if ($this->status1 != '') {
      echo "   <h2 class='statusadresse'>" . $this->addfstring->getfilledStr($this->status1, BR) . "</h2>\n";
    }
    echo $beginTag . $this->addfstring->getfilledStr(CleanOperations::cleanStringContainingHtml($this->status2), BR) . $endTag . "\n";
    echo "  </div>\n\n";
  }
}