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


class DataMenuTermineSelf extends DataMenuSelfBlaettern
{
  public $dbwKurse;
  
  public $callFromExport = false;

  public function __construct($urlparameter)
  {
    $this->dbwKurse = new DBWorkKurseINetAktivFilter();
    DataMenuKurse::__construct($urlparameter);
    $this->dbwork = new DBWorkKurseTageWB();
    $this->exportadapter = new ExpKurstageOnlyDifferent();
    $this->datensaetzeProSeite = $this->settings->kurstageproseite;

    
    
    
    $this->kursterminblaetternapi = new DSblaetternAPI();

    if (isset($_REQUEST['kfs_ktage_dat1']) && $_REQUEST['kfs_ktage_dat1'] != '') {
      if (DateTimeOperations::isValidDate($_REQUEST['kfs_ktage_dat1'])) {
        $_REQUEST['kfs_ktage_dat1'] = $this->addfdatetime->getValidDateString($_REQUEST['kfs_ktage_dat1']);
      } else {
        $_REQUEST['kfs_ktage_dat1'] = '';
      }
      SessionOperations::setVarValueSuche('kfs_ktage_dat1', $_REQUEST['kfs_ktage_dat1']);
    } else {
      SessionOperations::setVarValueSuche('kfs_ktage_dat1', '');
    }
    if (isset($_REQUEST['kfs_ktage_dat2']) && $_REQUEST['kfs_ktage_dat2'] != '') {
      if (DateTimeOperations::isValidDate($_REQUEST['kfs_ktage_dat2'])) {
        $_REQUEST['kfs_ktage_dat2'] = $this->addfdatetime->getValidDateString($_REQUEST['kfs_ktage_dat2']);
      } else {
        $_REQUEST['kfs_ktage_dat2'] = '';
      }
      SessionOperations::setVarValueSuche('kfs_ktage_dat2', $_REQUEST['kfs_ktage_dat2']);
    } else {
      SessionOperations::setVarValueSuche('kfs_ktage_dat2', '');
    }
  }

  
  public function getBlaettern()
  {
    $retAry = DataMenuSelfBlaettern::getBlaettern();

    $urlParamNav = new URLParameterNavigation();
    $urlParamNav->suchesetzen = '';
    $urlParamNav->implodeSettingsbyType($this->urlparameter);
    $aktknr = $urlParamNav->knr;
    
    
    $retAry[6] = $this->getKursBlaetternLink($urlParamNav, $aktknr, 'backward');
    $retAry[7] = $this->getKursBlaetternLink($urlParamNav, $aktknr, 'forward');

    return $retAry;
  }

  
  public function getKursBlaetternLink($pUrlParamNav, $pAktknr, $pDirection)
  {
    $retVal = '';
    $blaettAry = $this->kursterminblaetternapi->getBlaetternAry($pAktknr, $pDirection);
    if ($blaettAry[0] != '') {
      $pUrlParamNav->knr = $blaettAry[0];
      
      $pUrlParamNav->blid = '';
      $pUrlParamNav->blkeep = '';
      $pUrlParamNav->browse = ''; 
      $pUrlParamNav->implodeSettings();
      $retVal .= INDEXSCRIPT . '?urlparameter=' . $pUrlParamNav->stream;
      $retVal .= $this->getKursnameForBlaetternLink($pUrlParamNav->knr);
    }

    return $retVal;
  }

  
  public function getForwBlaetternLink($pCountBeh, $pUrlParamNav)
  {
    $retVal = '';
    if ($pCountBeh > 0) {
      $retVal .= parent::getForwBlaetternLink($pCountBeh, $pUrlParamNav);
      if (isset($_REQUEST['kfs_ktage_dat1'])) {
        $retVal .= '&kfs_ktage_dat1=' . $_REQUEST['kfs_ktage_dat1'];
      }
      if (isset($_REQUEST['kfs_ktage_dat2'])) {
        $retVal .= '&kfs_ktage_dat2=' . $_REQUEST['kfs_ktage_dat2'];
      }
    }

    return $retVal;
  }

  
  public function getBackwBlaetternLink($pCountBef, $pUrlParamNav, $pAktSeite = '')
  {
    $retVal = '';
    if ($pCountBef > 0) {
      $retVal .= parent::getBackwBlaetternLink($pCountBef, $pUrlParamNav);
      if (isset($_REQUEST['kfs_ktage_dat1'])) {
        $retVal .= '&kfs_ktage_dat1=' . $_REQUEST['kfs_ktage_dat1'];
      }
      if (isset($_REQUEST['kfs_ktage_dat2'])) {
        $retVal .= '&kfs_ktage_dat2=' . $_REQUEST['kfs_ktage_dat2'];
      }
    }

    return $retVal;
  }

  
  public function getAnzahlBodyElements()
  {
    $this->setKursSearch(); 

    $anzahl = 0;

    $kurs = $this->getKurs();
    if ($kurs != null && !$kurs->isEmpty()) {
      
      $anzahl = $this->dbwork->getKurstageCountbyKursNROnlyDifferent($this->addfstring->getfilledStr($this->urlparameter->knr, '-1'));
    }

    return $anzahl;
  }

  
  public function _getClickPath($pName = 'Kurstermine', $pPHP53DummyParam = '')
  {
    DataMenuMain::_getClickPath();

    $tmp = CleanOperations::cleanStringForHtml($pName);
    if ($this->toolsettings->clickKurstermineText != '') {
      $tmp = '<span class="trennzeichen"> ' . CleanOperations::cleanStringContainingHtml($this->toolsettings->trennzeichenKategorie) . ' </span>';
      $tmp .= '<span class="clickadrbold">' . CleanOperations::cleanStringForHtml($this->toolsettings->clickKurstermineText) . '</span>';
    } else {
      $kurs = $this->dbwKurse->getKurs($this->urlparameter->knr);
      if (!$kurs->isEmpty()) {
        $tmp = '<span class="trennzeichen"> ' . CleanOperations::cleanStringContainingHtml($this->toolsettings->trennzeichenKategorie) . ' </span>';
        $tmp .= '<span class="clickadrbold">' . CleanOperations::cleanStringForHtml($kurs->getVarValue('kurzbez_titel')) . '</span>';
      }
    }
    
    return DataMenuSelf::_getClickPath($tmp);
  }

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

  
  
  public function mainmenu()
  {
    
    $this->getsight();
    
    
    $this->nrOfMainBodyElements = $this->getAnzahlBodyElements();
    
    if (!empty($this->settings->maxAnzTermineProSeite) && ($this->settings->maxAnzTermineProSeite > 0)) {
      $maxTermineproSeite = $this->settings->maxAnzTermineProSeite;
    } else {
      $maxTermineproSeite = DataMenuTermine::MAXANZTERMINE;
    }
    if (($this->nrOfMainBodyElements > 0) && ($this->nrOfMainBodyElements <= $maxTermineproSeite)) {
      
      
      
      
      $this->getdataarraybody();
      $this->getdataarrayheader();
      $this->getdataarrayfooter();
    }
    
    
    $this->blaetternAry = $this->getBlaettern();
    $this->getdataarraynodata();
    $this->getdataarraynocolumn();

    if (!$this->callFromExport) {
      if ($this->toolsettings->showClickpfad) {
        
        echo $this->getClickPath();
      }
      echo "<div class='hauptseite'>\n" . $this->getContentAnker();
      $this->statusmenu();
    }

    
    StatistikAPI::writeKursdetailsAufrufOnlyDiff(new StatistikParameter('kursnr', $this->urlparameter->knr));
    if ($this->nrOfMainBodyElements > 0) {
      
      
      if ($this->nrOfMainBodyElements > $maxTermineproSeite) {
        $this->shownocolumn();
      } else {
        $expKurse = new ExpKurse();
        $expKurstage = $this->exportadapter;
        $this->exportadapter = $expKurse;
        $this->showheader();
        $this->exportadapter = $expKurstage;
        $this->showbody();
        $this->exportadapter = $expKurse;
        $this->showfooter();
        $this->exportadapter = $expKurstage;
      }
    } else {
      if (!NO_CLEAR_BEFORE_HAUPTSEITE_KURSE) {
        echo "<div style='clear: left;'></div>";
      }
      $this->shownodata();
    }

    if (!$this->callFromExport) {
      echo "</div>\n\n";
    }
  }

  private function getKurs()
  {
    static $kurs = null;
    if (is_null($kurs) || $this->callFromExport) {
      $this->urlparameter->explodeSettingStream();
      $kurs = $this->dbwKurse->getKurs($this->addfstring->getfilledStr($this->urlparameter->knr, '-1'));
    }

    return $kurs;
  }

  public function getdataarrayheader()
  {
    $this->dataarrayheader[0] = $this->getKurs();
  }

  
  public function getdataarraybody($pZeitBereich = 'noRestrict')
  {
    
    $this->setKursSearch();
    $this->urlparameter->explodeSettingStream();
    if (is_numeric($this->urlparameter->blid)) {
      $ktId = $this->urlparameter->blid;
    } else {
      $ktId = -1;
    }

    $this->dataarraybody
           = $this->dbwork->getKurstagebyKursNROnlyDifferentLimit(
             $this->addfstring->getfilledStr($this->urlparameter->knr, '-1'),
             $this->dbwork->getKurstag($ktId),
             $this->datensaetzeProSeite,
             ($this->addfstring->getfilledStr($this->urlparameter->browse, 'forward') == 'forward')
           );

    $this->setCountVars();
  }

  public function getdataarrayfooter()
  {
    $this->dataarrayfooter = array($this->getKurs());
  }

  
  public function getdataarraynodata()
  {
    $this->dataarraynodata = array($this->getKurs());
  }

  
  public function getnodata()
  {
    
    $tmpexpadapter = $this->exportadapter;
    
    $this->getsight(14);
    $this->exportadapter = new ExpKurse();
    $retour = parent::getnodata();
    
    $this->exportadapter = $tmpexpadapter;

    return $retour;
  }

  
  public function getdataarraynocolumn()
  {
    $this->dataarraynocolumn = array($this->getKurs());
  }

  
  public function getnocolumn()
  {
    
    $tmpexpadapter = $this->exportadapter;
    $this->getsight(14);
    $this->exportadapter = new ExpKurse();
    $retour = parent::getnocolumn();
    
    $this->exportadapter = $tmpexpadapter;

    return $retour;
  }

  
  public function get2ndColumn()
  {
    
    $tmpsight = $this->sight;
    $tmpexpadapter = $this->exportadapter;
    
    
    $this->getsight(50);
    $this->exportadapter = new ExpKategorien();
    $this->dataarraynodata = array(new Datamodul());
    $retour = parent::getnodata();
    
    $this->sight = $tmpsight;
    $this->exportadapter = $tmpexpadapter;

    return $retour;
  }

  
  public function setKursSearch()
  {
    $this->dbwork->newSearch();
    if (isset($_REQUEST['kfs_ktage_dat1'])) {
      $this->dbwork->fromobject->datum = $_REQUEST['kfs_ktage_dat1'];
    }
    if (isset($_REQUEST['kfs_ktage_dat2'])) {
      $this->dbwork->toobject->datum = $_REQUEST['kfs_ktage_dat2'];
    }
    if ($this->toolsettings->versteckeKurstageWieImRaumanzeigeprogramm) {
      $this->dbwork->fromobject->ranzprognoanz = true;
    }
    if ($this->toolsettings->versteckeKurstageOhneMinuten) {
      $this->dbwork->fromobject->minuten = 1;
    }
    $status = StringOperations::filterValidChars(trim($this->toolsettings->kurstageAnhandStatusAusblenden), 'XAZVNEPIKD-');
    if ($status != '') {
      $this->dbwork->filterExcludeStatus();
      $this->dbwork->fromobject->ausfall = $status;
    }
    
  }
}