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

class DataMenuKategorien extends DataMenuMain
{
  
  public $dbwCat;

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


    $this->dbwCat = new DBWorkCategories();
    $this->dbwCat->onlyAussenstelle = SessionOperations::getKatAsFilter();
    $this->exportadapter = new ExpKategorien();
  }

  
  protected function getTopCategory()
  {
    $dbwtexte = new DBWorkTexte();
    $type = $this->urlparameter->kathaupt == 1 ? TEXTE_TOPCATEGORY : TEXTE_TOPSONDERCATEGORY;
    $catType = $this->urlparameter->kathaupt == 1 ? 1 : 2;
    $text = $dbwtexte->getTextById($type);

    $cat = new Category();
    $cat->id = 0;
    $cat->text = 'Oberste Kategorie-Wurzel';
    $cat->beschreibung = nl2br($text->wert);
    $cat->beschreibungorig = $text->wert;
    $typo3Filter = Typo3Filter::getInstance();
    $aussenstellenIds = $typo3Filter->getFilterAussenstellen();
    if ($aussenstellenIds == '' || count($aussenstellenIds) === 0) {
      $cat->subCategories = $this->dbwCat->getCategoriesByTypeStructured($catType, $this->toolsettings->showEmptyCategories);
    } else { 
      $cat->subCategories = $this->dbwCat->getCategoriesByTypeStructured($catType, $this->toolsettings->showEmptyCategories, true);
    }
    $cat->countKurseComplete = 0;
    foreach ($cat->subCategories as $subCat) {
      $cat->countKurseComplete += $subCat->countKurseComplete;
    }
    $cat->level = 0;

    return $cat;
  }

  
  protected function getCurrentCategory()
  {
    if (!isset($this->urlparameter->katid)) {
      $katid = -1;
    } else {
      $katid = $this->urlparameter->katid;
    }
    static $cache = array();
    if (!isset($cache[$katid])) {
      if ($katid > -1) {
        $tmp = $this->dbwCat->getCategoryStructured($katid, $this->toolsettings->showEmptyCategories);
        $cache[$katid] = ($tmp !== false) ? $tmp : null;
      } else {
        $cache[$katid] = $this->getTopCategory();
      }
    }

    return $cache[$katid];
  }

  protected function showCategories($pDataHeader, $pDataBody, $pDataFooter)
  {
    $this->dataarrayheader = $pDataHeader;
    $this->dataarraybody = $pDataBody;
    $this->dataarrayfooter = $pDataFooter;
    $this->showheader();
    $this->showbody();
    $this->showfooter();
  }

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

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

    
    if ($this->toolsettings->categoryType == CATEGORYOLD) {
      $currentCat = $this->getCurrentCategory();

      
      
      $minLevel = $currentCat->level + 1 - $this->toolsettings->anzahlSpalten;

      
      if ($minLevel <= 0) {
        $owner = $this->getTopCategory();
        $this->showCategories(array($owner), $owner->subCategories, array($owner));
      }

      
      
      $ary = $currentCat->ownerList;
      if ($currentCat->id > 0) {
        $ary[] = $currentCat->id;
      }
      foreach ($ary as $id) {
        $owner = $this->dbwCat->getCategoryStructured($id, $this->toolsettings->showEmptyCategories);
        if ($minLevel <= $owner->level) {
          $this->showCategories(array($owner), $owner->subCategories, array($owner));
        }
      }

    
    } else {
      $this->showCategories($this->dataarrayheader, $this->dataarraybody, $this->dataarrayfooter);
    }
  }

  public function showheader()
  {
    DataMenu::showheader();
  }

  public function showfooter()
  {
    DataMenu::showfooter();
  }

  public function getdataarrayheader()
  {
    $cat = $this->getTopCategory();
    $this->dataarrayheader = array($cat);
  }

  public function getdataarraybody()
  {
    $topCategory = $this->getTopCategory();
    $this->dataarraybody = $topCategory->subCategories;
  }

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

  public function menukategorie($katid = -1, $count = 0, $pKatType = 0)
  {
  }
}


class DataMenuKategorienM28Style extends DataMenuKategorien
{
  public function mainmenu()
  {
    $this->prepareMainmenu();

    $this->showCategories($this->dataarrayheader, $this->dataarraybody, $this->dataarrayfooter);
  }

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

  public function getdataarraybody()
  {
    $cat = $this->getCurrentCategory();
    $this->dataarraybody = $cat->subCategories;
  }

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