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


class CategoryAPI
{
  
  public $toolsettings;

  
  public $dbwcat;

  
  public function __construct()
  {
    $this->toolsettings = ToolSettingsAPI::getToolSettings(4);
    $this->dbwcat = new DBWorkCategories();
  }

  
  public function getTopCategories4NavMenu($pKatType, $pKathaupt = -1, $pNotEmpty = false)
  {
    if ($pNotEmpty) {
      $categories = $this->dbwcat->getMasterCategoriesByTypeNotEmpty($pKatType);
    } else {
      $categories = $this->dbwcat->getMasterCategoriesByType($pKatType);
    }

    return $this->formatTopCategories4NavMenu($categories, $pKatType, $pKathaupt, $pNotEmpty);
  }

  public function formatTopCategories4NavMenu($pCategories, $pKatType, $pKathaupt, $pNotEmpty)
  {
    $retour = '';
    foreach ($pCategories as $cat) {
      $urlparam = new URLParameterKategorie();
      if ($pKathaupt == -1) {
        $urlparam->kathaupt = $pKatType;
      } else {
        $urlparam->kathaupt = $pKathaupt;
      }

      $aktkatid = $urlparam->katid;

      $urlparam->katid = $cat->id;
      $urlparam->katvaterid = $cat->owner;
      $urlparam->katname = $cat->text;
      $urlparam->implodeSettings();

      $cssClass = 'navmenu_topkategorien typ' . CleanOperations::cleanInt($pKatType);

      if ($aktkatid == $cat->id) {
        $cssClass .= ' active';
        $additionalCssClass = ''; 
      } else {
        $additionalCssClass = ' highlight_ein';
      }

      $retour .= "<li class='$cssClass' ";
      if (!$this->toolsettings->noJavaScript) {
        $retour .= "onmouseover=\"this.className='$cssClass $additionalCssClass';\" onmouseout=\"this.className='$cssClass';\"";
      }
      $retour .= "><a id='kat" . $cat->id . "' class='$cssClass' href='" . CleanOperations::cleanStringForHtml(INDEXSCRIPT) . '?urlparameter='
                  . CleanOperations::cleanStringForHtml($urlparam->stream)
                  . "' title='Veranstaltungsübersicht " . CleanOperations::cleanStringForHtml($cat->text)
                  . " öffnen' >";
      $retour .= CleanOperations::cleanStringForHtml($cat->text) . '</a>';
      $retour .= '</li>' . "\n";
    }
    $retour .= '';

    return $retour;
  }

  
  public function getTreeCategories4NavMenu(
    $pKatid,
    $pKattype,
    $pLastChild,
    $pChildString,
    $pKathaupt,
    $pShowKathauptAlt = false,
    $pSuppressID = false
  ) {
    $cats = array();
    if ($pKatid == 0) {
      
      if (!$this->toolsettings->noObersteKategorie || $pChildString == '') {
        $cats = $this->dbwcat->getCategoriesByTypeStructured($pKattype, $this->toolsettings->showEmptyCategories);
      } else {
        return $pChildString;
      }
    } else {
      
      if ($this->toolsettings->showEmptyCategories) {
        
        $cats = $this->dbwcat->getChildCategoriesByType($pKatid, $pKattype);
      } else {
        
        
        $semester = '';
        if ($this->toolsettings->semlist != '') {
          $stringop = new StringOperations();
          $semester = ' AND kurs.semester in ('
              . $stringop->convertCommaSeparatedList2InList($this->toolsettings->semlist) . ') ';
        }
        $cats = $this->dbwcat->getChildCategoriesByTypeNotEmpty($pKatid, $pKattype, $semester);
      }
    }
    $retour = '';
    if (count($cats) > 0) {
      

      
      foreach ($cats as $category) {
        
        $urlparam = new URLParameterKategorie();

        $aktkathaupt = $urlparam->kathaupt; 

        $urlparam->kathaupt = $pKathaupt;
        $urlparam->implodeSettings();

        $cleanedKattype = CleanOperations::cleanInt($pKattype);

        $cssClass = 'navmenu_kategorien typ' . $cleanedKattype;

        if ($pKatid == 0) {
          $cssClass = 'navmenu_kategorien typ' . $cleanedKattype . ' top';
        }
        if ($category->id == $pLastChild) {
          $cssClass = 'navmenu_kategorien typ' . $cleanedKattype . ' pfad';
        }

        
        $aktkatid = 0;
        $aktkatid = $urlparam->katid;
        if ($aktkathaupt == $pKathaupt) {
          $aktkathaupt = $urlparam->kathauptalt;
        }

        if ($pShowKathauptAlt) {
          
          
          
          if (($aktkathaupt != 201) && ($aktkathaupt != 200)) {
            $urlparam->kathauptalt = $aktkathaupt;
          } 
          else {
            $urlparam->kathauptalt = '';
          }
        } else {
          $urlparam->kathauptalt = '';
        }
        $urlparam->katid = $category->id;
        $urlparam->katvaterid = $category->owner;
        $urlparam->katname = StringOperations::cleanStringForUrl($category->text);
        $urlparam->implodeSettings();

        $idPart = '';
        if ($pSuppressID) {
          $idPart = 'id="kat' . CleanOperations::cleanInt($category->id) . '"';
        }

        if ($aktkatid == $category->id) {
          $cssClass = 'navmenu_kategorien typ' . $cleanedKattype . ' active';

          
          $retour .= "<li class='$cssClass' ";
          if (!$this->toolsettings->noJavaScript) {
            $retour .= "onmouseover=\"this.className='$cssClass';\" onmouseout=\"this.className='$cssClass';\"";
          }
          $retour .= '><a ' . $idPart . " class='$cssClass' href='" . CleanOperations::cleanStringForHtml(INDEXSCRIPT) . '?urlparameter='
                      . CleanOperations::cleanStringForHtml($urlparam->stream) 
                      . "' title='Veranstaltungsübersicht " . CleanOperations::cleanStringForHtml($category->text)
                      . " öffnen' >";
        } else {
          $retour .= "<li class='$cssClass' ";
          if (!$this->toolsettings->noJavaScript) {
            $retour .= "onmouseover=\"this.className='$cssClass highlight_ein';\" onmouseout=\"this.className='$cssClass';\"";
          }
          $retour .= '><a ' . $idPart . " class='$cssClass' href='" . CleanOperations::cleanStringForHtml(INDEXSCRIPT) . '?urlparameter='
                      . CleanOperations::cleanStringForHtml($urlparam->stream) 
                      . "' title='Veranstaltungsübersicht " . CleanOperations::cleanStringForHtml($category->text)
                      . " öffnen' >";
        }

        $retour .= CleanOperations::cleanStringForHtml($category->text) . '</a>';

        if (($category->id == $pLastChild) && ($pChildString != '')) { 
          $retour
            .= "\n " . '<ul class="navmenu_kategorien typ' . $cleanedKattype . '">' . "\n"
            . $pChildString
            . '</ul>';
        }
        $retour .= "</li>\n";
      }
    }
    $cat = $this->dbwcat->getCategory($pKatid);
    if ($pKatid != 0) {
      return
        $this->getTreeCategories4NavMenu($cat->owner, $pKattype, $pKatid, $retour, $pKathaupt, $pSuppressID);
    } else {
      return $retour;
    }
  }

  
  public function getParentDepth2ID($pKatid, $pMasterCategories = null)
  {
    if ($pKatid <= 0) {
      return -1;
    }
    $cat = $this->dbwcat->getCategory($pKatid);
    if ($cat != null) {
      if ($cat->owner == 0) {
        return '';
      } else {
        if ($pMasterCategories == null) { 
          $pMasterCategories = $this->dbwcat->getMasterCategoriesIDsByType($cat->kategorieTyp);
        }
        $fathercat = $this->dbwcat->getCategory($cat->owner);
        if ((!$fathercat->isEmpty()) && ($fathercat->owner == 0)) {
          return $cat->id;
        }
        if ((!$fathercat->isEmpty()) && (in_array($fathercat->owner, $pMasterCategories))) {
          return $fathercat->id;
        }
      }

      return $this->getParentDepth2ID($cat->owner, $pMasterCategories);
    }

    return -1;
  }

  
  public function getCategoriesByDepth($pDepth, $pCategoryType = 1)
  {
    if ($pDepth == 0) {
      return null;
    }
    if ($pDepth === 1) {
      return $this->dbwcat->getMasterCategoriesIDsByType($pCategoryType);
    }
    $naechsteKategorienDerenKinderGeholtWerden = $this->dbwcat->getMasterCategoriesByType($pCategoryType);

    for ($i = 1; $i < $pDepth; $i++) {
      $vorbereitungNaechsteRunde = array();
      foreach ($naechsteKategorienDerenKinderGeholtWerden as $cat) {
        echo "$cat->id ";
        $vorbereitungNaechsteRunde = array_merge($vorbereitungNaechsteRunde, $this->dbwcat->getChildCategories($cat->id));
      }
      $naechsteKategorienDerenKinderGeholtWerden = $vorbereitungNaechsteRunde;
    }

    return $naechsteKategorienDerenKinderGeholtWerden;
  }

  
  public function getParentDepth1ID($pKatid)
  {
    $cat = $this->getParentDepth1($pKatid);
    if ($cat->isEmpty()) {
      return -1;
    } else {
      return $cat->id;
    }
  }

  public function getParentDepth1($pKatid)
  {
    $katid = Type::getInt($pKatid, 0);
    if ($katid <= 0) {
      return $this->dbwcat->getNewObject();
    }
    $cat = $this->dbwcat->getCategory($katid);
    if ($cat != null) {
      if ($cat->owner == 0) {
        return $cat;
      } else {
        return $this->getParentDepth1($cat->owner);
      }
    }

    return $this->dbwcat->getNewObject();
  }

  
  public function getParentCategories($pKatid)
  {
    $katid = Type::getInt($pKatid, 0);
    if ($katid <= 0) {
      return false;
    }
    $result = array();
    $cat = $this->dbwcat->getCategory($katid);
    if ($cat != null) {
      $result[] = $cat;
      if ($cat->owner == 0) {
        return $result;
      } else {
        return array_merge($result, $this->getParentCategories($cat->owner));
      }
    }

    return false;
  }

  
  public function getAussenstellen4Category($pKatID)
  {
    $dbwkurse = new DBWorkKurseSmall();
    $knrs = $dbwkurse->getSearchSQLCategoryRekursiv('', $pKatID, $this->dbwcat, 0);
    if ($knrs == '') {
      return array();
    }

    return $dbwkurse->getDistinctAussenstByKnrs($knrs);
  }

  
  public function obersteKategorie($pBool)
  {
    $this->toolsettings->noObersteKategorie = !$pBool;
  }

  public static function getKathauptForCategoryType($pCategoryType)
  {
    if ($pCategoryType == 3) {
      return 600;
    }



    return $pCategoryType;
  }

  public static function getStandardUrlparam($category, $withSeo = SEARCHENGINE_FUNCTIONS)
  {
    $urlparam = new URLParameterNavigation(false);
    $urlparam->kathaupt = CategoryAPI::getKathauptForCategoryType($category->kategorieTyp);
    $urlparam->katid = $category->id;
    $urlparam->katvaterid = $category->owner;
    if ($withSeo) {
      $urlparam->katname = StringOperations::cleanStringForUrl($category->text);
    }

    return $urlparam;
  }
}