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

class DataMenuKursFinderAjaxResult extends DataMenuSucheKurse
{
  public function getClickPath()
  {
    return '';
  }

  
  public function statusmenu($pTrennbeg = false)
  {
    return;
  }

  protected function openTagHauptseite()
  {
  }

  protected function closingTagHauptseite()
  {
  }

  
  

  public function getdataarraybody($pZeitBereich = 'noRestrict')
  {
    $param = new URLParameterFilter();
    if ($param->clearAllKatfilter()) {
      SessionOperations::resetKursKategorieRestrictions();
    }
    $this->setKursSearch();
    SessionOperations::applyKursKategorieRestrictions($this->dbwkurse->fromobject, $this->dbwkurse->toobject);
    $sw = new \Stopwatch(true);
    parent::getdataarraybody();
    $sw->stop();
    Logger::logg('Zeit in DataMenuKursFinderAjaxResult::getdataarraybody(): ' . $sw->elapsed(), 400);
  }

  public function mainmenu()
  {
    $this->prepareMainmenu();

    $sw = new \Stopwatch(true);
    
    if (count($this->dataarraybody) > 0) {
      $this->showheader();
      $this->showbody();
      $this->showfooter();
    }
    
    else {
      $this->shownocolumn();
    }
    $sw->stop();
    Logger::logg('Zeit in DataMenuKursFinderAjaxResult::mainmenu() ohne getdataarraybody(): ' . $sw->elapsed(), 410);
  }

  public function getbody($pSgtVars = array())
  {
    $cache = Kufer\Web\KuferWEB\Api\HtmlCacheFile::getInstance();
    $results = array();
    $kurseToRender = array();

    
    
    
    
    foreach ($this->dataarraybody as $kurs) {
      $cachedKursData = $cache->get($this->getCacheKey4Knr($kurs->id));
      if (is_null($cachedKursData)) {
        $kurseToRender[] = $kurs;
      } else {
        $results[$kurs->id] = $cachedKursData;
      }
    }

    
    $results = $this->renderAndCacheKurse($kurseToRender, $results);

    
    $tmp = '';
    foreach ($this->dataarraybody as $kurs) {
      $tmp .= $results[$kurs->id];
    }

    return $tmp;
  }

  private function renderAndCacheKurse($kurseToRender, $results)
  {
    $cache = Kufer\Web\KuferWEB\Api\HtmlCacheFile::getInstance();
    $this->exportadapter->export->initiate();
    $this->exportadapter->export->exportresults = $kurseToRender;
    $this->exportadapter->export->setNrOfMainBodyElements($this->nrOfMainBodyElements);
    $this->exportadapter->export->setBlaetternAry($this->blaetternAry);
    if (!empty($this->seitenWahl)) {
      $this->exportadapter->export->setSeitenWahl($this->seitenWahl);
    }
    $this->exportadapter->filestream = $this->sight->body;
    $this->exportadapter->replaceVarNames();
    for ($i = 0; $i < $this->exportadapter->export->resultCount(); $i++) {
      
      $currentCacheKey = $this->getCacheKey4Knr($kurseToRender[$i]->id);
      try {
        $cache->set($currentCacheKey, $this->exportadapter->resultlist[$i], $this->getTtl());
      } catch (\InvalidArgumentException $e) {
        Logger::logg('Dem HtmlCache wurde kein gültiger cacheKey übergeben (' . $currentCacheKey . ')', 4401);
      }
      $results[$kurseToRender[$i]->id] = $this->exportadapter->resultlist[$i];
      Logger::logg('kurs gecached: ' . $kurseToRender[$i]->id . " ($currentCacheKey)", 421);
    }

    return $results;
  }

  private function getCacheKey4Knr($knr)
  {
    return 'k_list_' . sha1($knr);
  }

  protected function getTtl()
  {
    return 3600;
  }
}