<?php
/**
 * @copyright (c) Kufer Software GmbH, Oderstraße 7, D-84453 Mühldorf
 */

class DataMenuLoginMeineDokumente extends DataMenuMainTopLeft
{
  protected $exportadapterStammhoerer;
  protected $exportadapterDatDoc;

  private $searchOptions;

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

    $this->exportadapterStammhoerer = $this->exportadapter = new ExpStammhoerer();
    $this->exportadapterDatDoc = new ExpDatDoc();

    $this->searchOptions = new MeineDokumenteSearchOptions();
  }

  public function _getClickPath($pName = 'Teilnehmer-Login - Meine Dokumente')
  {
    $this->_setStdClickPath($pName, 'clickKundenLoginMeineDokumente');

    return $this->clickpfad;
  }

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

  public function mainmenu()
  {
    if (SessionOperations::getWbLogin() == null) {
      $this->getsight(); 

      if ($this->toolsettings->showClickpfad) {
        echo $this->getClickPath();
      }

      echo '<div class="hauptseite">' . "\n";
      $this->_showNichtEingeloggt();
      echo '</div>';
    } elseif ($this->isDokumentDownload()) {
      if (!$this->isDownloadLinkManipulated()) {
        $datDoc = $this->getDatDocObj();
        $file = $this->getDokumentPerDMZ($datDoc);
      }
      if (is_null($file)) {
        $idParam = '';
        if (TYPO3_CMS_USED) {
          $id = CleanOperations::cleanStringForHtml(GetOperations::getVarValue('id'));
          $idParam = '&id=' . $id;
        }
        $zielurl = INDEXSCRIPT . '?kathaupt=407' . $idParam . '&filterBeginn=' . $this->searchOptions->vondat . '&dokument_fehler=1';
        header('Location:' . $zielurl);
      } else {
        RequestOperations::setRequestHeaderByFilename($file->DateiName);
        echo base64_decode($file->ContentBase64);
      }
      die();
    } else {
      parent::mainmenu();
    }
  }

  public function getdataarrayheader()
  {
    $this->dataarrayheader = array(SessionOperations::getWbLogin());
  }

  
  public function getdataarraybody()
  {
    try {
      $datDocAry = $this->getDokumentenListe();
      $datDocAry = $this->sortiereDokumenteNachKnr($datDocAry);
    } catch (Exception $e) {
      Logger::logg('KuferWEB: Fehler beim Auslesen der Dokumentenliste: ' . $e->getMessage(), 252);
      $datDocAry = array();
    }

    $this->dataarraybody = $datDocAry;
  }

  public function getdataarrayfooter()
  {
    $this->dataarrayfooter = array(SessionOperations::getWbLogin());
  }

  public function getdataarraynocolumn()
  {
    $this->dataarraynocolumn = array(SessionOperations::getWbLogin());
  }

  public function showheader()
  {
    $tmp = $this->exportadapter;
    $this->exportadapter = $this->exportadapterStammhoerer;
    echo $this->getheader();
    $this->exportadapter = $tmp;
  }

  public function showbody()
  {
    $tmp = $this->exportadapter;
    $this->exportadapter = $this->exportadapterDatDoc;
    echo $this->getbody();
    $this->exportadapter = $tmp;
  }

  public function showfooter()
  {
    $tmp = $this->exportadapter;
    $this->exportadapter = $this->exportadapterStammhoerer;
    echo $this->getfooter();
    $this->exportadapter = $tmp;
  }

  private function _showNichtEingeloggt()
  {
    $this->getdataarraynocolumn();
    $tmp = $this->exportadapter;
    $this->exportadapter = $this->exportadapterStammhoerer;
    $this->shownocolumn();
    $this->exportadapter = $tmp;
  }

  
  private function getDokumentenListe()
  {
    $datDoc = $this->getDatDocObj4Liste();
    $datDoc->kurs_beginn_dat = $this->searchOptions->vondat;
    $datDoc->belegung_status = $this->searchOptions->status;
    $datDoc->typen = $this->searchOptions->typen;

    return DatDocAPI::getDokumentenListeFromDmz($datDoc);
  }

  
  private function getDatDocObj4Liste()
  {
    $wblogin = SessionOperations::getWbLogin();

    $datDoc = new Dat_doc();
    $datDoc->nr = $wblogin->tnr;

    return $datDoc;
  }

  
  private function getDatDocObj()
  {
    $wblogin = SessionOperations::getWbLogin();

    $datDoc = new Dat_doc();
    $datDoc->nr = $wblogin->tnr;
    $datDoc->lfdnr = RequestOperations::getVarValue('lfdnr');
    $datDoc->typ = RequestOperations::getVarValue('typ');

    return $datDoc;
  }

  private function getDokumentPerDMZ($datDoc)
  {
    try {
      return DatDocAPI::getDokumentFromDmz($datDoc);
    } catch (Exception $e) {
      Logger::logg('KuferWEB: Fehler beim Dokumentendownload: ' . $e->getMessage(), 251);

      return null;
    }
  }

  private function isDokumentDownload()
  {
    return GetOperations::getVarValue('action') == 'getDokument';
  }

  public function isDateiDownload()
  {
    return $this->isDokumentDownload();
  }

  private function isDownloadLinkManipulated()
  {
    $xsrf = Xsrf::checkAndUseXsrfFromGet('wbl_get_dokument', RequestOperations::getVarValue('lfdnr'));
    $typAry = explode(',', $this->searchOptions->typen);

    if ($xsrf == Xsrf::KEY_NOT_OK) {
      return true;
    }
    if (!in_array(RequestOperations::getVarValue('typ'), $typAry)) {
      return true;
    }

    return false;
  }

  
  private function sortiereDokumenteNachKnr($pDatDocAry)
  {
    usort($pDatDocAry, function($datDoc1, $datDoc2) {
      return strcmp($datDoc1->knr, $datDoc2->knr);
    });

    return $pDatDocAry;
  }
}

class MeineDokumenteSearchOptions extends MeineKurseSearchOptions
{
  
  public $typen;

  
  public function setSearchOptions($pEnforceStandardFilter = false)
  {
    parent::setSearchOptions($pEnforceStandardFilter);

    $einstellungen = ToolSettingsAPI::getToolSettings(4);
    $this->typen = 'R,M,B,P_A,Z';
    if ($einstellungen->kundenLoginDokumentTyp != '') {
      $this->typen = $einstellungen->kundenLoginDokumentTyp;
    }
  }
}