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


class ToolRaumAnzeige extends ToolBase
{
  
  private $_user;

  public function __construct()
  {
    parent::__construct();
    $this->toolTitle = 'was ist heute wo';
    $this->_user = SessionOperations::getCurrentWebToolsUser();
  }

  
  public function init($pPageId)
  {
    

    switch ($pPageId) {
      default:
      case 292:

        
        if ($this->_user->isAdmin() || $this->_user->dnr > -1
          || $this->_user->anr > -1 || $this->_user->ben > -1) {
          $dbwkurstage = new DBWorkKurseTage();
        } else { 

          $dbwkurstage = new DBWorkKurseTageWTINetAktivFilter(1, true);
        }
        $dbwkurstage->newSearch();
        $dbwkurstage->setBuildSeitenindex(true);

        
        $urlParameter = array();
        $urlParameter['toolsection_id'] = $pPageId;


        
        if (RequestOperations::getVarValue('datum') != '' && DateTimeOperations::isValidDate(RequestOperations::getVarValue('datum'))) {
          $datum = RequestOperations::getVarValue('datum');
        } else {
          $datum = DateTimeOperations::getDateNow();
        }
        $urlParameter['datum'] = $datum;
        $dbwkurstage->fromobject->datum = $datum;

        
        $bisdatum = '';
        
        if ($this->toolSettings->weiterekurstage > 0) {
          $bisdatum = DateTimeOperations::getNextDate($datum, $this->toolSettings->weiterekurstage);
          $dbwkurstage->toobject->datum = $bisdatum;
        }
        
        else {
          $dbwkurstage->toobject->datum = $datum;
        }

        $filterGesetzt = false;
        
        if (RequestOperations::getVarValue('aussenstelle') != '') {
          $aussenstelle = RequestOperations::getVarValue('aussenstelle');
          $dbwkurstage->fromobject->kurs->aussenst = $aussenstelle;
          $urlParameter['aussenstelle'] = $aussenstelle;
          $filterGesetzt = true;
        }

        
        if (RequestOperations::getVarValue('gebaeude') != '') {
          $gebaeude = RequestOperations::getVarValue('gebaeude');
          $dbwkurstage->fromobject->kurs->ort = $gebaeude;
          $urlParameter['gebaeude'] = $gebaeude;
          $filterGesetzt = true;
        }

        
        if (RequestOperations::getVarValue('kursort') != '') {
          $kursort = RequestOperations::getVarValue('kursort');
          $dbwkurstage->fromobject->ort = $kursort;
          $urlParameter['kursort'] = $kursort;
          $filterGesetzt = true;
        }

        
        if (RequestOperations::getVarValue('uhrzeit') != '') {
          $uhrzeit = RequestOperations::getVarValue('uhrzeit');
          $dbwkurstage->fromobject->zeit_beg = $uhrzeit;
          $urlParameter['uhrzeit'] = $uhrzeit;
          $filterGesetzt = true;
        }

        
        $dozentClicked = false;
        if (RequestOperations::getVarValue('dozentnr') != '') {
          $dozentNr = RequestOperations::getVarValue('dozentnr');
          $dbwkurstage->fromobject->dnr = $dozentNr;
          $dbwkurstage->toobject->dnr = $dozentNr;
          $dozentClicked = $dozentNr >= 0;
          $urlParameter['dozentnr'] = $dozentNr;
          $filterGesetzt = true;
        }

        
        if ($this->toolSettings->abweichendeKtStatus) {
          
          $ktstatus = $this->toolSettings->ktstatus;
          
          $colorktstatus = $this->toolSettings->colorktstatus;
        } else {
          
          $ktstatus = '-XZNPVEI';
          $colorktstatus = '#000000:#000000:#000000:#000000:#000000:#000000:#000000:#000000:';
        }
        $dbwkurstage->fromobject->ausfall = $ktstatus;
        $dbwkurstage->toobject->ausfall = $ktstatus;

        
        $dbwkurstage->orderby = 'datum, kt.zeit_beg, k.kurzbez, k.titel ';

        
        $pageLinks = array();
        $pageLinks['self'] = $this->getUrlParamsForFile($urlParameter);
        
        $pageLinks['filter'] = $pageLinks['self'];

        if ($filterGesetzt) {
          $pageLinks['filteraus'] = $this->getUrlParamsForFile(array('toolsection_id' => $pPageId));
        }

        
        $urlParameter['datum'] = DateTimeOperations::getNextDate($datum, 1);
        $pageLinks['tagvor'] = $this->getUrlParamsForFile($urlParameter);

        
        $urlParameter['datum'] = DateTimeOperations::getNextDate($datum, -1);
        $pageLinks['tagzurueck'] = $this->getUrlParamsForFile($urlParameter);

        $alleIds = $dbwkurstage->getKurstageIds();
        $data = $this->getData($dbwkurstage, $alleIds, $this->toolSettings->kurstageproseite);
        $anzahlAlleKurstage = count($alleIds);

        
        $page = new PageRaumAnzeige(
          'was ist heute wo',
          $pPageId,
          $this,
          $data,
          $datum,
          $ktstatus,
          $colorktstatus,
          $this->_user->dnr,
          $dozentClicked,
          $bisdatum,
          $anzahlAlleKurstage,
          $this->toolSettings->kurstageproseite
        );
        $page->setLinks($pageLinks);
      break;
    }

    if ($page == null) {
      throw new Exception('ungültige PageId');
    }

    $this->currentPage = $page;
  }

  public function getToolId()
  {
    return \ToolsAPI::TOOL_RAUMANZEIGE;
  }
}  

class PageRaumAnzeige extends BrowseableObjectsPageBaseWT
{
  private $_data;
  private $_date;
  private $_stati;
  private $_statiColors;
  private $_aussenstellen;
  private $_orte;
  private $_gebaeude;
  private $_dozent;
  private $_dozentClicked;
  
  private $_bisdate;

  public function __construct($pPageTitle, $pPageId, $pTool, $pData, $pDate, $pStati, $pStatiColors, $pDozent = false, $pDozentClicked = false, $pBisDate = '', $pAnzahlAllerKurstage = 1, $pAnzahlKurstageProSeite = 1)
  {
    
    parent::__construct($pPageTitle, $pPageId, $pTool, $pAnzahlAllerKurstage, $pAnzahlKurstageProSeite);
    $this->_data = $pData;
    $this->_date = $pDate;
    $this->_stati = $pStati;
    $this->_statiColors = $pStatiColors;
    $this->_dozent = $pDozent;
    $this->_dozentClicked = $pDozentClicked;

    
    $dbwAussenst = new DBWorkHLAussenstellen();
    $this->_aussenstellen = $dbwAussenst->getAussenstellen();
    
    $dbwKursorte = new DBWorkKursorte();
    $this->_orte = $dbwKursorte->getKursorte();
    
    $dbwGebaeude = new DBWorkGebaeude();
    $this->_gebaeude = $dbwGebaeude->getGebaeude();
    
    $this->_bisdate = $pBisDate;
  }

  public function getSpecialJS()
  {
    $fileAry = parent::getSpecialJS();
    $fileAry[] = 'js/raumanzeige.js';

    return $fileAry;
  }

  
  public function getContentHeader()
  {
    $inhalt = '';
    if (count($this->_data) > 0) {
      if ($this->_bisdate == '') {
        $inhalt .= 'Übersicht der am ' . $this->_date . ' stattfindenden Termine';
      } else {
        $inhalt .= 'Übersicht der vom ' . $this->_date . ' bis zum ' . $this->_bisdate . ' stattfindenden Termine';
      }
    }

    return $inhalt;
  }

  public function getContent()
  {
    $result = '<div id="main_content">' . "\n";

    if (!(count($this->_data) == 0 && $this->allgSettings->buttonleite_position == BUTTONBAR_BOTH)
       && ($this->allgSettings->buttonleite_position == BUTTONBAR_TOP || $this->allgSettings->buttonleite_position == BUTTONBAR_BOTH)) {
      $result .= $this->_getButtonLeiste();
    }


    $counter = 0;

    $colors = array();
    if (StringOperations::strlen($this->_statiColors) > 0) {
      $tmp = explode(':', $this->_statiColors);
      $anzahlStati = StringOperations::strlen($this->_stati);
      for ($i = 0; $i < $anzahlStati; $i++) {
        $colors[$this->_stati[$i]] = $tmp[$i];
      }
    }
    
    
    
    
    $dbwkt = new DBWorkKurseTage();
    
    $kurstag0 = $dbwkt->getNewObject();
    $headBuilder = new SightBuilderUebersicht($this->tool->getToolId(), 331, $kurstag0, true);
    $headBuilder->weitereKlasseFuerSightDiv = 'raumanzeige header';

    $content = '';
    foreach ($this->_data as $kurstag) {
      $builder = new SightBuilderUebersicht($this->tool->getToolId(), 292, $kurstag, true);
      $builder->weitereKlasseFuerSightDiv = 'raumanzeige';
      $evenClass = ($counter % 2 == 0) ? ' farbe_sicht_erstezeile' : '';
      $content .= '<div class="content_zeile' . $evenClass;
      $vergleichswert = $kurstag->ausfall != '' ? $kurstag->ausfall : '-';
      if (StringOperations::strpos($this->_stati, $vergleichswert) !== false) {
        if ($colors[$vergleichswert] != '#000000') {
          $content .= ' toColor" style="color: ' . $colors[$vergleichswert] . ';';
        }
      }
      $content .= '">' . "\n";
      $content .= $builder->buildSight();
      $content .= '</div>' . "\n";
      $counter++;
    }

    
    
    
    
    $result .= '<script type="text/javascript">var divs = $(".content_zeile.toColor");' . "\n";
    $result .= 'divs.each(function(index, item) {' . "\n";
    $result .= '$item = $(item); newColor = $item.css("color"); ' . "\n";
    $result .= '$item.find(".raumanzeige span").each(function(innerindex, inneritem){$(inneritem).css("color", newColor);});});';
    $result .= '</script>' . "\n";

    if ($counter == 0) { 
      $result .= $this->_getNotice();
    } else {
      $result .= $headBuilder->buildSight();
      $result .= $content;
    }

    if ($this->allgSettings->buttonleite_position != BUTTONBAR_TOP) {
      $result .= $this->_getButtonLeiste();
    }
    $result .= '</div>' . "\n";

    return $result;
  }

  
  protected function _getButtonLeiste()
  {
    $result = '<div class="main_buttons">';

    $result .= $this->_getTageBlaettern();

    if (isset($this->links['filter'])) {
      $result .= '<a href="#" title="Suchfilter" class="filter_button_raumanzeige ';
    }
    
    if (isset($this->links['filteraus']) && isset($this->links['filteraus']) != '') {
      $result .= ' nomargin_right';
    }
    $result .= '"><img src="layout/button_main_search_filter.gif" title="Filter" alt="Filter"></a>';

    if (isset($this->links['filteraus']) && isset($this->links['filteraus']) != '') {
      $result .= "\n";
      $result .= '<a class="filter_aus_raumanzeige" href="' . $this->links['filteraus'] . '" title="Filter leeren"><img src="layout/button_none.gif" alt=""></a>';
    }

    $result .= $this->getNavigaton();

    $result .= "\n" . '</div>' . "\n";


    static $isDialogSet = false;
    if (!$isDialogSet) {
      $result .= $this->getFilterDialog();
      $isDialogSet = true;
    }

    return $result;
  }

  private static function buildHiddenInputs4Params($paramArray)
  {
    $retour = '';
    foreach ($paramArray as $key => $value) {
      $retour .= '<input type="hidden" name="' . StringOperations::htmlspecialchars($key, ENT_QUOTES) . '" value="' . StringOperations::htmlspecialchars($value, ENT_QUOTES) . '">';
    }

    return $retour;
  }

  private function getFilterDialog()
  {
    
    
    $paramAry = StringOperations::getUrlParams($this->links['self']);

    $kursort = '';
    if (isset($paramAry['kursort'])) {
      $kursort = urldecode($paramAry['kursort']);
      unset($paramAry['kursort']);
    }

    $aussenstelle = '';
    if (isset($paramAry['aussenstelle'])) {
      $aussenstelle = urldecode($paramAry['aussenstelle']);
      unset($paramAry['aussenstelle']);
    }

    $gebaeude = '';
    if (isset($paramAry['gebaeude'])) {
      $gebaeude = urldecode($paramAry['gebaeude']);
      unset($paramAry['gebaeude']);
    }

    $datum = '';
    if (isset($paramAry['datum'])) {
      $datum = urldecode($paramAry['datum']);
      unset($paramAry['datum']);
    }

    $uhrzeit = '';
    if (isset($paramAry['uhrzeit'])) {
      $uhrzeit = urldecode($paramAry['uhrzeit']);
      unset($paramAry['uhrzeit']);
    }

    if (isset($paramAry['dozentnr'])) {
      unset($paramAry['dozentnr']);
    }

    $sprachsettings = new SprachSettingsObject();

    
    $result = '<div id="dialogFilterRaumAnz" style="display: none;">' . "\n";
    $result .= '<form id="submitFilterRaumAnz" method="get" action="' . $this->links['self'] . '">' . "\n";
    $result .= '  <table>' . "\n";
    $result .= '    <tbody>' . "\n";
    $result .= '      <tr>' . "\n";
    $result .= '        <td width="400px">Außenstelle:</td>' . "\n";
    $result .= '        <td><select class="raumAnzDialogInput" name="aussenstelle" id="dialogFilterRaumAnz_aussenst"><option></option>';
    foreach ($this->_aussenstellen as $value) {
      $result .= '<option';
      $result .= $value->bezeichner == $aussenstelle ? ' selected ' : '';
      $result .= '>' . $value->bezeichner . '</option>';
    }
    $result .= '            </select></td>' . "\n";
    $result .= '      </tr>' . "\n";
    $result .= '      <tr>' . "\n";
    $result .= '        <td>Gebäude:</td>' . "\n";
    $result .= '        <td><select class="raumAnzDialogInput" name="gebaeude" id="dialogFilterRaumAnz_gebaeude"><option></option>';
    foreach ($this->_gebaeude as $value) {
      $result .= '<option';
      $result .= $value->name == $gebaeude ? ' selected ' : '';
      $result .= '>' . $value->name . '</option>';
    }
    $result .= '            </select></td>' . "\n";
    $result .= '      </tr>' . "\n";
    $result .= '      <tr>' . "\n";
    $result .= '        <td>' . $sprachsettings->Kursraum['Singular']['Nominativ'] . ':</td>' . "\n";
    $result .= '        <td><select class="raumAnzDialogInput" name="kursort" id="dialogFilterRaumAnz_ort"><option></option>';
    foreach ($this->_orte as $value) {
      $result .= '<option';
      $result .= $value->mktext == $kursort ? ' selected ' : '';
      $result .= '>' . $value->mktext . '</option>';
    }
    $result .= '            </select></td>';
    $result .= '      </tr>' . "\n";
    $result .= '      <tr>' . "\n";
    $result .= '        <td class="nowrap">ab folgender Uhrzeit:</td>' . "\n";
    $result .= '        <td><input class="time" type="text" name="uhrzeit" id="dialogFilterRaumAnz_uhrzeit" length="25" class="hasDatepicker" value="' . $uhrzeit . '"></td>' . "\n";
    $result .= '      </tr>' . "\n";
    $result .= '      <tr>' . "\n";
    $result .= '        <td class="nowrap">für folgendes Datum:</td>' . "\n";
    $result .= '        <td><input class="date" type="text" name="datum" id="dialogFilterRaumAnz_datum" length="25" class="hasDatepicker" value="' . $datum . '"></td>' . "\n";
    $result .= '      </tr>' . "\n";
    if ($this->_dozent >= 0) {
      $checked = $this->_dozentClicked ? 'checked ' : '';
      $result .= '      <tr>' . "\n";
      $result .= '        <td class="nowrap">nur meine Termine:</td>' . "\n";
      $result .= '        <td><input type="checkbox" name="dozentnr" id="dialogFilterRaumAnz_dozent" length="25" value="' . $this->_dozent . '"'
          . $checked . '></td>' . "\n";
      $result .= '      </tr>' . "\n";
    }
    $result .= '    </tbody>' . "\n";
    $result .= '  </table>' . "\n";
    $result .= PageRaumAnzeige::buildHiddenInputs4Params($paramAry);
    $result .= '</form>' . "\n";
    $result .= '</div>' . "\n";

    return $result;
  }

  private function _getTageBlaettern()
  {
    $result = '';
    
    if (isset($this->links['tagzurueck']) && $this->links['tagzurueck'] != '') {
      $result .= '<a href="' . $this->links['tagzurueck'] . '" title="einen Tag zur&uuml;ck bl&auml;ttern">';
      $result .= '<img src="layout/button_rueckblaetternTag.gif" alt="einen Tag zur&uuml;ck bl&auml;ttern">';
      $result .= '</a>';
    }
    if (isset($this->links['tagvor']) && $this->links['tagvor'] != '') {
      $result .= '<a href="' . $this->links['tagvor'] . '" title="einen Tag vorw&auml;rts bl&auml;ttern">';
      $result .= '<img src="layout/button_vorblaetternTag.gif" alt="einen Tag vorw&auml;rts bl&auml;ttern">';
      $result .= '</a>';
    }

    return $result;
  }

  
  private function _getNotice()
  {
    $result = '';
    $result .= '<div class="seitentitel"><h2>Achtung</h2></div>'
    . '<p class="default_message">Für den ' . $this->_date . ' wurden keine passenden Termine gefunden.' . BR_WT
    
    . '</p>';

    return $result;
  }
}