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


class SemesterAPI
{
  
  public $dbwsem;
  
  public $hiddenstr = '';
  
  public $html;

  
  public function __construct()
  {
    $this->dbwsem = new DBWorkHLSemester();
    $this->stringop = new StringOperations();
    $this->html = new HTMLOperations();
  }

  
  public function getForm4Semester()
  {
    
    
    $urlnav = new URLParameterNavigation();
    $urlnav->implodeSettings();
    $actionurl = $this->stringop->getHttpWithoutParams($this->stringop->getURLwithParams(true))
        . '?urlparameter=' . $urlnav->getStreamWith(array(), array());
    $sys_sem = '';
    $sys_sem .= "<form action='" . CleanOperations::cleanStringForHtml($actionurl) . "' method=\"post\">";
    $sys_sem .= $this->getList4Semester($actionurl);
    $sys_sem .= $this->html->CreateInputTag(
      'semesterids',
      'hidden',
      '',
      '',
      $this->hiddenstr
    );

    return $sys_sem;
  }

  
  public function getList4Semester($pUrl)
  {
    
    $semesterary = $this->dbwsem->getAll();
    $wk = new DBWorkWarenkorbSemesterFilter();
    
    $wkbezids = $wk->getEntriesIDSerialized();
    $hiddenstr = '';
    $retour = "\n " . '<ul class = "semester">' . "\n";

    foreach ($semesterary as $semester) {
      $sem_id = $semester->id;
      $sem_bez = $semester->sem;
      $hiddenstr .= CleanOperations::cleanInt($sem_id) . FILTER_FORM_DELIMITER;

      $semester_checked = in_array($sem_bez, $wkbezids);
      $retour .= "\n " . '<li class = "semester">' . "\n"
                . $this->html->CreateInputTag(
                  'semester' . $sem_id,
                  'checkbox',
                  '',
                  '',
                  $sem_id,
                  '',
                  $semester_checked
                );
      $retour .= "<a class = 'semester' href='" . CleanOperations::cleanStringForHtml($pUrl) . 'filtersem:' . CleanOperations::cleanInt($sem_id) . ";'>";

      $retour .= 'Semester ' . CleanOperations::cleanStringForHtml($sem_bez) . "</a>\n";
      $retour .= "</li>\n";
    }
    $retour .= "</ul>\n";

    $this->hiddenstr .= $hiddenstr;

    return $retour;
  }

  

  
  public function getSemesterSelect4KFS()
  {
    $semesterary = $this->dbwsem->getAll();
    if (count($semesterary) == 0) {
      return '';
    }

    $settings = ToolSettingsAPI::getToolSettings(4);
    $allowedsems = explode(',', $settings->semlist); 

    $value = SessionOperations::getVarValueSuche('kfs_semester');
    $retour = '<select name="kfs_semester_select" class="kfs_semester_select" id="kfs_semester_selectid">';
    $retour .= '  <option value="-1">- keine Auswahl -</option>';
    $options = '';
    foreach ($semesterary as $sem) {
      if (in_array($sem->sem, $allowedsems)) {
        $sel = ($sem->sem == $value ? 'selected' : '');
        $options .= '  <option value="' . CleanOperations::cleanStringForHtml($sem->sem) . '"  ' . $sel . '>' . CleanOperations::cleanStringForHtml($sem->sem) . '</option>';
      }
    }
    if ($options === '') { 
      return '';
    }
    $retour .= $options . '</select>';

    return $retour;
  }
}