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

class DataMenuKursDetailsSelf extends DataMenuSelf
{
  public $dbwkurse;
  public $kursblaetternapi;

  
  public function __construct($urlparameter)
  {
    
    
    parent::__construct($urlparameter);
    
    $this->dbwkurse = new DBWorkKurseINetAktivFilter();
    
    $this->exportadapter = new ExpKurse();
    $this->kursblaetternapi = new DSblaetternAPI();
  }

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

    $tmp = $pName;
    if ($this->toolsettings->clickKursdetailsText != '') {
      $tmp = '<span class="trennzeichen"> ' . CleanOperations::cleanStringContainingHtml($this->toolsettings->trennzeichenKategorie) . ' </span>';
      $tmp .= '<span class="clickadrbold">' . CleanOperations::cleanStringForHtml($this->toolsettings->clickKursdetailsText) . '</span>';
    } else {
      $kurs = $this->dbwkurse->getKurs($this->getKnr());
      if (!$kurs->isEmpty()) {
        $tmp = '<span class="trennzeichen"> ' . CleanOperations::cleanStringContainingHtml($this->toolsettings->trennzeichenKategorie) . ' </span>';

        $kurs4html = new CleanDatamodulForHtml($kurs);
        $text = $kurs4html->getVarValue('kurzbez_titel');

        $sett = BASysSettingsAPI::getBASysSettings();
        if ($sett->vhsbezeichnung === 'Verl - Harsewinkel - Schloß Holte-Stukenbrock') {
          $text = $kurs4html->getVarValue('titel_zeile1') . ($kurs4html->getVarValue('titel_untertitel') !== '' ? '<br />' : '') . $kurs4html->getVarValue('titel_untertitel');
        }

        $tmp .= '<span class="clickadrbold">' . $text . '</span>';

        
        if (DBWorkBASysSettings::isAugsburgKolping() && GetOperations::getVarValue('showclick') != '') {
          $dbw = new DBWorkCategories();
          $cat = $dbw->getFirstCategoryForKurs($kurs->id);
          $cats = $dbw->getParentCategories($cat);
          
          
          $path = '';
          $catPath = '';
          foreach ($cats as $cat) {
            
            $part = str_replace(' ', '-', $cat->text);
            $part = str_replace('--', '-', $part);
            $path .= StringOperations::strtolower($part) . '/';
            $link = '<a class="clickadresse" href="' . CleanOperations::cleanStringForHtml($path) . '">' . CleanOperations::cleanStringForHtml($cat->text) . '</a>';
            if ($catPath !== '') {
              $catPath .= "   <span class='trennzeichen'> " . CleanOperations::cleanStringContainingHtml($this->toolsettings->trennzeichenKategorie) . "</span>\n";
            }

            $catPath .= "   <span class='clickadresse'> " . $link . "</span>\n";
          }

          return $catPath . $tmp;
        }
      }
    }

    if (($this->toolsettings->clickKursdetailsMitKategorie || $this->toolsettings->clickKursdetailsNurMitKategorie) && !SessionOperations::lastStepWasSearch()) {
      $katid = $this->urlparameter->katid;
      if ($katid == '') {
        
        $up = IndexOperations::decodeToUrlParameter(SessionOperations::getVarValueIntern('history_referer', ''));
        $katid = $up->katid;
      }
      if ($katid == '') {
        $up = IndexOperations::decodeToUrlParameter(SessionOperations::getLastHistoryStep());
        $katid = $up->katid;
      }
      if ($this->toolsettings->clickKursdetailsNurMitKategorie) {
        $tmp = $this->computeCatpath($katid);
      } else {
        $tmp = $this->computeCatpath($katid) . $tmp;
      }
    }
    
    return DataMenuSelf::_getClickPath($tmp);
  }

  private function computeCatpath($katid = 0)
  {
    if (!is_numeric($katid) || $katid <= 0) {
      $dbw = new DBWorkCategories();
      $cat = $dbw->getFirstCategoryForKurs($this->getKnr());
      $katid = $cat->id;
    }

    return $this->getCategoryPath($this->addfstring->getfilledStr($katid, '0'), '', false);
  }

  public function mainmenu()
  {
    $this->prepareMainmenu();
    if ($this->toolsettings->showClickpfad) {
      echo $this->getClickPath();
    }
    echo "<div class='hauptseite'>\n";
    $this->statusmenu();
    echo $this->getContentAnker();
    
    if (count($this->dataarraybody) == 1 && !$this->dataarraybody[0]->isEmpty()) {
      
      StatistikAPI::writeKursdetailsAufrufOnlyDiff(new StatistikParameter('kursnr', $this->getKnr()));

      $this->showheader();
      $this->showbody();
      $this->showfooter();
    }
    
    else {
      $this->shownodata();
    }
    echo '</div>';
  }

  public function prepareMainmenu($pZeitBereich = 'noRestrict')
  {
    if ($this->settings->kursDetailsBeiAufrufAktualisieren) {
      $this->aktualisiereKursDatenAusDmz();
    }
    parent::prepareMainmenu($pZeitBereich);
  }

  private function aktualisiereKursDatenAusDmz()
  {
    try {
      $abgleichApi = new AbgleichAPI();

      $data = new \XMLNode('data');
      $data->addChild(new \XMLNode('knr', $this->getKnr()));
      $data->addChild(new \XMLNode('getOnlySimpleKursObj', '1'));
      $result = $abgleichApi->sendeNachricht(
        AbgleichAPI::ABGLEICH_GET_KURS,
        new Nutzlast($data)
      );

      $valuesFromDmz = json_decode($result->reason, true)['Kurs'];

      $kursWebDb = $this->dbwkurse->getKurs($this->getKnr());

      $varTakeOver = array('AnmEnde' => 'anm_ende', 'BeginnDat' => 'beginn_dat', 'EndeDat' => 'ende_dat', 'Kurzbez' => 'kurzbez', 'Titel' => 'titel', 'Beginn' => 'beginn', 'Ende' => 'ende', 'Info' => 'info', 'Webinfo' => 'webinfo', 'Vorspann' => 'vorspann', 'Nachspann' => 'nachspann', 'Keinewebanmeld' => 'keinewebanmeld', 'Individtermine' => 'individtermine', 'Material' => 'material', 'Ort' => 'ort', 'KartKey' => 'kart_key', 'Dauer' => 'dauer', 'Stundenanz' => 'stundenanz', 'TeilMin' => 'teil_min', 'TeilMax' => 'teil_max', 'TEinzel' => 'teinzel', 'Ausfall' => 'ausfall', 'Webflags' => 'webflags');
      $valueChanged = $kursWebDb->updateByDmzValues($varTakeOver, $valuesFromDmz);

      if ($valueChanged) {
        $this->dbwkurse->updateKurs($kursWebDb, true, true);
        
        $this->dbwkurse->getKurs($this->getKnr(), true);
        WBKursStore::getKurs($this->getKnr(), true);
      }
    } catch (Exception $e) {
    }
  }

  
  public function getBlaettern()
  {
    $retAry = array('', '', '', '', '', '', '', '');
    $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->kursblaetternapi->getBlaetternAry($pAktknr, $pDirection);
    if ($blaettAry[0] != '') {
      $pUrlParamNav->knr = $blaettAry[0];
      $pUrlParamNav->browse = '';    
      $pUrlParamNav->blid = '';
      $pUrlParamNav->blkeep = '';
      $pUrlParamNav->implodeSettings();
      $retVal .= INDEXSCRIPT . '?urlparameter=' . $pUrlParamNav->stream;
      $retVal .= $this->getKursnameForBlaetternLink($pUrlParamNav->knr);
    }

    return $retVal;
  }

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

  private function getKnr()
  {
    return $this->addfstring->getfilledStr($this->urlparameter->knr, '-1');
  }

  public function getdataarrayheader()
  {
    $this->urlparameter->explodeSettingStream();
    $this->dataarrayheader = array($this->dbwkurse->getKurs($this->getKnr()));
  }

  public function getdataarraybody($pKompat = 'noRestrict')
  {
    $this->urlparameter->explodeSettingStream();
    $this->dataarraybody = array($this->dbwkurse->getKurs($this->getKnr()));
  }

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

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

    return $retour;
  }
}