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

class DataMenuBasis
{
  
  public $exportadapter;
  public $addfstring;
  
  public $addfdatetime;
  public $urlparameter;

  
  public $sight;
  public $dataarrayheader;
  public $dataarraybody;
  public $dataarrayfooter;
  public $dataarraynodata;
  public $dataarraynocolumn;

  public $nrOfMainBodyElements = -1; 
  public $blaetternAry;
  
  public $seitenWahl;

  public function __construct($urlparameter)
  {
    $this->exportadapter = new ExpBasis();
    $this->addfstring = new StringOperations();
    $this->addfdatetime = new DateTimeOperations();
    $this->urlparameter = $urlparameter;
  }

  
  public function content()
  {
    $this->mainmenu();
  }

  public function _init()
  {
    $this->getsight();
    $this->getdataarrayheader();
    $this->getdataarraybody();
    $this->getdataarrayfooter();
    $this->getdataarraynodata();
    $this->getdataarraynocolumn();
  }

  
  public function getContent()
  {
    $this->_init();

    $result = $this->getheader();
    if (count($this->dataarraybody) > 0) {
      $result .= $this->getbody();
    } else {
      $result .= $this->getnodata();
    }
    $result .= $this->getfooter();

    return $result;
  }

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

    $this->showheader();
    if (count($this->dataarraybody) > 0) {
      $this->showbody();
    } else {
      $this->shownodata();
    }
    $this->showfooter();
  }

  
  public function getsight($sightid = 0)
  {
    $this->sight = SightsAPI::getSight($sightid);
  }

  public function getdataarrayheader()
  {
    $this->dataarrayheader = array(new Datamodul());
  }

  public function getdataarraybody()
  {
    $this->dataarraybody = array(new Datamodul());
  }

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

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

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

  protected function getExportAdapterHeader()
  {
    return $this->exportadapter;
  }

  protected function getExportAdapterBody()
  {
    return $this->exportadapter;
  }

  protected function getExportAdapterFooter()
  {
    return $this->exportadapter;
  }

  protected function getExportAdapterNoData()
  {
    return $this->exportadapter;
  }

  protected function getExportAdapterNoColumn()
  {
    return $this->exportadapter;
  }

  
  
  
  

  
  public function getheader($pSgtVars = array())
  {
    return $this->renderSightPart($this->sight->header, $this->getExportAdapterHeader(), $this->dataarrayheader, $pSgtVars, true, false);
  }

  public function showheader()
  {
    echo $this->getheader();
  }

  
  public function getbody($pSgtVars = array())
  {
    return $this->renderSightPart($this->sight->body, $this->getExportAdapterBody(), $this->dataarraybody, $pSgtVars, true, true);
  }

  public function showbody()
  {
    echo $this->getbody();
  }

  
  public function getfooter(bool $concatResultlist = false)
  {
    return $this->renderSightPart($this->sight->footer, $this->getExportAdapterFooter(), $this->dataarrayfooter, array(), true, $concatResultlist);
  }

  public function showfooter()
  {
    echo $this->getfooter();
  }

  
  public function getnodata()
  {
    return $this->renderSightPart($this->sight->nodata, $this->getExportAdapterNoData(), $this->dataarraynodata, array(), false, false);
  }

  public function shownodata()
  {
    echo $this->getnodata();
  }

  
  public function getnocolumn()
  {
    return $this->renderSightPart($this->sight->nocolumn, $this->getExportAdapterNoColumn(), $this->dataarraynocolumn, array(), false, false);
  }

  public function shownocolumn()
  {
    echo $this->getnocolumn();
  }

  
  protected function renderSightPart($sightCode, $exportAdapter, $exportResults, array $pSightVars = array(), bool $pagination = false, bool $concatResultlist = false)
  {
    $exportAdapter->export->initiate();
    foreach ($pSightVars as $sgtVarName => $sgtVarInhalt) {
      $exportAdapter->setSightInfo($sgtVarName, $sgtVarInhalt);
    }
    
    $exportAdapter->export->exportresults = $exportResults;
    if ($pagination) {
      $exportAdapter->export->setNrOfMainBodyElements($this->nrOfMainBodyElements);
    }
    $exportAdapter->export->setBlaetternAry($this->blaetternAry);
    
    if ($pagination && !empty($this->seitenWahl)) {
      $exportAdapter->export->setSeitenWahl($this->seitenWahl);
    }
    $exportAdapter->filestream = $sightCode;
    $exportAdapter->replaceVarNames();
    if (!empty($exportAdapter->resultlist)) { 
      if ($concatResultlist) {
        
        return implode('', array_slice($exportAdapter->resultlist, 0, $exportAdapter->export->resultCount()));
      } else {
        return $exportAdapter->resultlist[0];
      }
    } else {
      return '';
    }
  }

  public function initializecompletesight()
  {
    if (!isset($this->dataarrayheader)) {
      $this->dataarrayheader = array(new Datamodul());
    }
    if (!isset($this->dataarraybody)) {
      $this->dataarraybody = array(new Datamodul());
    }
    if (!isset($this->dataarrayfooter)) {
      $this->dataarrayfooter = array(new Datamodul());
    }
  }

  
  public function getsightcomplete($sightid, $expadapterBody, $expadapterHeader = null, $expadapterFooter = null)
  {
    if ($expadapterHeader === null) {
      $expadapterHeader = $expadapterBody;
    }

    if ($expadapterFooter === null) {
      $expadapterFooter = $expadapterBody;
    }

    $html = '';
    $this->getsight($sightid);

    
    $expadapterHeader->export->initiate();
    $html .= $this->renderSightPart($this->sight->header, $expadapterHeader, $this->dataarrayheader, array(), true);

    $this->exportadapter = $expadapterBody;
    $this->exportadapter->export->initiate();
    $html .= $this->getbody();

    $this->exportadapter = $expadapterFooter;
    $this->exportadapter->export->initiate();
    $html .= $this->getfooter();

    return $html;
  }

  public function showsightcomplete($sightid, $expadapterBody, $expadapterHeader = null, $expadapterFooter = null)
  {
    
    
    echo $this->getsightcomplete($sightid, $expadapterBody, $expadapterHeader, $expadapterFooter);
  }

  public function getContentAnker()
  {
    $settings = ToolSettingsAPI::getToolSettings(4);

    if ($settings->insertContentAnker) {
      $urlparam = new URLParameterNavigation();
      if (!$urlparam->isstatic) {
        return ' <a id="' . CleanOperations::cleanStringForHtml(ANKER_NAME_CONTENT) . '"></a>' . "\n";
      }
    }

    return '';
  }

  

  
  public function setExportSightInfo($pName, $pValue)
  {
    
    $this->exportadapter->setSightInfo($pName, $pValue);
  }

  
  public function getExportSightInfo($pName)
  {
    
    
    
    
    return $this->exportadapter->getSightInfo($pName);
  }

  protected function getSightPart($pSightId, $pSightpart, $pData)
  {
    $this->getsight($pSightId);
    $this->exportadapter->export->initiate();
    $this->exportadapter->export->exportresults = $pData;
    $this->exportadapter->export->setNrOfMainBodyElements(count($pData));
    switch ($pSightpart) {
      case WB_SIGHT_PART_HEADER:
        $this->exportadapter->filestream = $this->sight->header;
        break;
      case WB_SIGHT_PART_BODY:
        $this->exportadapter->filestream = $this->sight->body;
        break;
      case WB_SIGHT_PART_FOOTER:
        $this->exportadapter->filestream = $this->sight->footer;
        break;
      case WB_SIGHT_PART_NOCOLUMN:
        $this->exportadapter->filestream = $this->sight->nocolumn;
        break;
      case WB_SIGHT_PART_NODATA:
        $this->exportadapter->filestream = $this->sight->nodata;
        break;
    }
    $this->exportadapter->replaceVarNames();
    if (count($this->exportadapter->resultlist) > 0) {
      return implode("\n", $this->exportadapter->resultlist);
    } else {
      return '';
    }
  }
}