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

/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */
class PageRaumuebersicht extends BrowseableObjectsPageBaseWT
{
  protected $_data;
  protected $_searchraum;
  protected $_captionPageId;
  
  protected $_addLinkAry;
  protected $_allowedSchablonIds;

  protected $_gebaeude;

  public function __construct(
    $pTool,
    $pData,
    $pRestrict,
    $pCaptionPageId,
    $pPageId,
    $pAddLinkAry,
    $countAllObjects = 1,
    $objectsPerPage = 1,
    $pAllowedSchablonIds = array()
  ) {
    parent::__construct('Raumübersicht', $pPageId, $pTool, $countAllObjects, $objectsPerPage);
    $this->_data = $pData;
    $this->_restrict = $pRestrict;

    $this->_captionPageId = $pCaptionPageId;

    $user = SessionOperations::getCurrentWebToolsUser();
    $dbwKursortGebaeude = new DBWorkKursortGebaeude();
    
    
    $this->_gebaeude = $dbwKursortGebaeude->getGebaeudeByUserID($user->id, $this->_restrict, !$this->toolSettings->raumNichtVerfuegbar);
    $this->_addLinkAry = $pAddLinkAry;
    
    $this->_allowedSchablonIds = $pAllowedSchablonIds;
  }

  
  public function getContentHeader()
  {
    $searchGebaeude = '';

    $searchApi = new GebaeudesuchAPI();
    $suchdm = $searchApi->getFromObjectBySession(null, $this->tool->getToolId());

    if ($suchdm->gebaeude != '') {
      $searchGebaeude = ' f&uuml;r ' . implode(', ', $suchdm->gebaeude);
    }

    return 'Raumübersicht' . $searchGebaeude;
  }

  public function getContent()
  {
    $menu = new Menu();


    $result = '';
    $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;
    
    $headBuilder = new SightBuilderUebersicht($this->tool->getToolId(), $this->_captionPageId, new Kursort(), true);
    $headBuilder->weitereKlasseFuerSightDiv = 'raumanzeige header';

    $content = '';

    if (count($this->_data) > 0) {
      foreach ($this->_data as $kursort) {
        
        $builder = new SightBuilderUebersicht($this->tool->getToolId(), $this->id, $kursort, true);

        
        
        $builder->setAddLinkPart($this->_addLinkAry);

        $builder->weitereKlasseFuerSightDiv = 'raumuebersicht';
        $evenClass = ($counter % 2 == 0) ? ' farbe_sicht_erstezeile' : '';
        $content .= '<div class="content_zeile ' . $evenClass . '">';
        $content .= '<div class="steuerelemente raumuebersicht">';
        if (isset($this->links['hmliste']) && $this->links['hmliste']) {
          $content .= '<input type="checkbox" id="o_' . CleanOperations::cleanInt($kursort->id) . '" name="' . CleanOperations::cleanInt($kursort->id) . '" class="kurscheckbox markable" value="' . CleanOperations::cleanInt($kursort->id) . '">' . BR_WT;
        }
        if (isset($this->links['raumdetails']) && $this->links['raumdetails'] != '') {
          $content .= $menu->getUserLink(
            '<img src="images/details.gif" alt="Zu den Details" title="Raumdetails">',
            $this->links['raumdetails'] . '&r-nr=' . CleanOperations::cleanInt($kursort->id)
          );
        }
        $content .= '</div>';
        $content .= $builder->buildSight();
        $content .= '</div>' . "\n";
        $counter++;
      }
    }
    $result .= $headBuilder->buildSight();

    $result .= $content;
    if ($this->allgSettings->buttonleite_position != BUTTONBAR_TOP) {
      $result .= $this->_getButtonLeiste();
    }

    
    if (isset($this->links['stdplangrafik']) && $this->links['stdplangrafik'] !== '') {
      $result .= $this->_getHmStundenplanDialog();
    }
    $result .= '</div>' . "\n";

    return $result;
  }

  
  protected function _getButtonLeiste()
  {
    $result = '<div class="main_buttons">';
    if (isset($this->links['markieren']) && $this->links['markieren']) {
      $result .= "<input type='image' name='markieren' value='1' src='layout/button_small_markieren.gif' border='0' "
                 . " title='Markieren von Räumen' alt='Markieren von Räumen' class=\"alleMarkieren\">\n";
    }
    $displayDialogMarkieren = true;

    if (isset($this->links['belegungsplan']) && $this->links['belegungsplan'] != '') {
      $result .= '<a href="' . CleanOperations::cleanStringForHtml($this->links['belegungsplan']) . '">';
      $result .= '<img  src="layout/button_grafgebplan.gif" border="0" title="Grafischer Belegungsplan" alt="Grafischer Belegungsplan" >';
      $result .= '</a>';
    }

    if (isset($this->links['hmliste']) && $this->links['hmliste']) {
      $result .= '<input type="image" name="hausmeisterlisten" value="1" src="layout/button_druck_hmlisteng.gif" border="0" '
              . ' title="Druck der Hausmeisterlisten" alt="Druck der Hausmeisterlisten" class="hmDruckListe">';
    }

    
    
    if (isset($this->links['stdplangrafik']) && $this->links['stdplangrafik'] !== '') {
      $result .= '<a href="' . CleanOperations::cleanStringForHtml($this->links['stdplangrafik']) . '" class="stdplangrafik">';
      $result .= '<img  src="layout/button_druck_stdplan.gif" border="0" title="Grafik für Stundenplan" alt="Grafik für Stundenplan" >';
      $result .= '</a>' . "\n";

      
    }


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

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

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

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

      $result .= '<div id="dialog_markieren_hm">'
        . '<div class="nowrap">'
        . '<input type="image" src="layout/button_raeume_markieren_alleg.gif" '
          . 'title="Alle Räume markieren" alt="Alle Räume markieren" '
          . 'onClick="$(\'#dialog_markieren_hm\').dialog(\'close\'); window.location.href=KuferTools.SelfLink+\'&markerAction=markiereAlle\'" '
          . 'onmouseout="ChangeImg(this,MarkierenRaeumeAlleGray)" onmousemove="ChangeImg(this,MarkierenRaeumeAlle)" >'
      . '<input type="image" src="layout/button_raeume_markieren_alle_nichtg.gif" '
        . 'title="Keinen Raum markieren" alt="Keinen Raum markieren" '
        . 'onClick="$(\'#dialog_markieren_hm\').dialog(\'close\'); window.location.href=KuferTools.SelfLink+\'&markerAction=loescheMarkierungen\'" '
        . 'onmouseout="ChangeImg(this,MarkierenRaeumeAlleNichtGray)" onmousemove="ChangeImg(this,MarkierenRaeumeAlleNicht)" >'
      . '</div>'
      . '<div class="nowrap">'
          . '<input type="image" src="layout/button_raeume_markieren_seiteg.gif" '
            . 'title="Räume dieser Seite markieren" alt="Räume dieser Seite markieren" '
            . 'onClick="$(\'input.markable\').prop(\'checked\', true); $(\'#dialog_markieren_hm\').dialog(\'close\')" '
            . 'onmouseout="ChangeImg(this,MarkierenRaeumeSeiteGray)" onmousemove="ChangeImg(this,MarkierenRaeumeSeite)" >'
      . '<input type="image" src="layout/button_raeume_markieren_seite_nichtg.gif" '
        . 'title="Räume dieser Seite nicht markieren" alt="Räume dieser Seite nicht markieren" '
        . 'onClick="$(\'input.markable\').prop(\'checked\', false); $(\'#dialog_markieren_hm\').dialog(\'close\')" '
        . 'onmouseout="ChangeImg(this,MarkierenRaeumeSeiteNichtGray)" onmousemove="ChangeImg(this,MarkierenRaeumeSeiteNicht)" >'
        . '</div>'
    . '</div>';
      $isDialogSet = true;
    }

    return $result;
  }

  
  private function _getHmStundenplanDialog($pElemId = 'hm_stundenplan_dialog')
  {
    $result = '<div id="' . CleanOperations::cleanStringForHtml($pElemId) . '">' . "\n";

    
    
    $actionLink = $this->links['stdplanformaction'];

    
    
    $datum = DateTimeOperations::getDateNow();
    $jahr = DateTimeOperations::getYearFromValidDate($datum);
    $monat = (int)StringOperations::substr($datum, 3, 2);
    $tag = (int)StringOperations::substr($datum, 0, 2);
    $kwobj = KalenderwochenAPI::getKalenderWochenNrfromDate($tag, $monat, $jahr);
    $kwjahr = $kwobj->kwjahr;
    $kwnr = $kwobj->kwnr;
    
    
    

    $kwary = KalenderwochenAPI::getKalenderWochebyNr($kwjahr, $kwnr);
    $selnropt = KalenderwochenAPI::getSelectOptions4KalWoNr($kwnr);
    $seljahropt = KalenderwochenAPI::getSelectedOptions4KalWoJahr($kwjahr);

    $disabled = false;
    $result .= '<form action="' . CleanOperations::cleanStringForHtml($actionLink) . '" method="post" name="formliste" class="stdplangrafik" target="_blank">' . "\n";

    
    $result .= '<div class = "kalwochennr">' . "\n" . '<label id="l_kalwochnr" for="kalwochnr">Woche:</label>' . "\n"
    . HTMLOperations::CreateSelectTag($selnropt, 'kalwochnr', '', 'kalwochnr', 1, '', false, $disabled) . '</div>';

    $result .= '<div class="kalwochenjahr">' . "\n" . '<label id="l_kalwochjahr" for="kalwochjahr">Kalenderjahr:</label>' . "\n"
    . HTMLOperations::CreateSelectTag($seljahropt, 'kalwochjahr', '', 'kalwochjahr', 1, '', false, $disabled) . '</div>';

    
    $datary = KalenderwochenAPI::getKalenderWochebyNr($kwjahr, $kwnr);
    $datvon = $datary['Mo'];
    $datbis = $datary['So'];

    
    $result .= HTMLOperations::CreateInputTag('kt_von', 'hidden', 'kt_von', '', $datvon, '', false, false, 10);
    $result .= HTMLOperations::CreateInputTag('kt_bis', 'hidden', 'kt_bis', '', $datbis, '', false, false, 10);

    
    $vonfield = HTMLOperations::CreateInputTag('datbereich_von', 'text', 'datbereich_von', 'date', $datvon, '', false, true, 10);
    $bisfield = HTMLOperations::CreateInputTag('datbereich_bis', 'text', 'datbereich_bis', 'date', $datbis, '', false, true, 10);
    $result .= "<div class = 'datbereich_vonbis'>"
                  . "<div class = 'datbereich_von'>von:$vonfield</div>"
                  . "<div class = 'datbereich_bis'>bis:$bisfield</div>"
                . '</div>';


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

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

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


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

    return $result;
  }

  

  
  private function _getBoxes4PrintLayout()
  {
    
    $this->toolSettings->kurstitelAnzeige = true;

    $printLayout = '<div class = "printLayoutDiv">' . "\n";
    $printLayout .= '<div class ="printLayoutText">Ausgabe mit:</div>' . "\n";
    $printLayout .= '<div class="printLayoutBoxes oben">' . "\n";
    $printLayout .= '<div class = "kurstitelBox">' . "\n";
    $checkedKurstitel = ($this->toolSettings->kurstitelAnzeige) ? 'checked' : '';
    $printLayout .= HTMLOperations::CreateInputTag('kurstitel', 'checkbox', 'kurstitel', '', '', '', $checkedKurstitel, '', '')
    . '<label id="l_kurstitel" for="kurstitel">Kurstitel</label>' . "\n"
    . '</div>' . "\n";
    $printLayout .= '<div class = "tnzahlBox">' . "\n";
    $checkedTnAnz = (isset($this->toolSettings->tnzahlAnzeige) && $this->toolSettings->tnzahlAnzeige) ? 'checked' : '';
    $printLayout .= HTMLOperations::CreateInputTag('tnzahl', 'checkbox', 'tnzahl', '', '', '', $checkedTnAnz, '', '')
    . '<label id="l_tnzahl" for="tnzahl">Teilnehmeranzahl</label>' . "\n"
    . '</div>' . "\n"
    . '</div>' . "\n"; 

    $printLayout .= '<div class="printLayoutBoxes unten">' . "\n";
    $printLayout .= '<div class = "dozBox">' . "\n";
    $checkedDoz = (isset($this->toolSettings->dozAnzeige) && $this->toolSettings->dozAnzeige) ? 'checked' : '';
    $printLayout .= HTMLOperations::CreateInputTag('doz', 'checkbox', 'doz', '', '', '', $checkedDoz, '', '')
    . '<label id="l_doz" for="doz">Dozent</label>' . "\n"
    . '</div>' . "\n";

    $printLayout .= '<div class = "leerRaumBox">' . "\n";
    $checkedLeerRaum = (isset($this->toolSettings->leerRaumAnzeige) && $this->toolSettings->leerRaumAnzeige) ? 'checked' : '';
    $printLayout .= HTMLOperations::CreateInputTag('leerRaum', 'checkbox', 'leerRaum', '', '', '', $checkedLeerRaum, '', '')
    . '<label id="l_leerRaum" for="leerRaum">leeren Räumen</label>' . "\n"
    . '</div>' . "\n";
    $printLayout .= '</div>' . "\n"; 
    $printLayout .= '</div>' . "\n"; 

    return $printLayout;
  }

  

  
  private function _getSchablonen4StdPlan()
  {
    $dbwschabl = new DBWorkBelplan_Schabl();
    
    

    
    $schablonen = $dbwschabl->getSchablonenbyId($this->_allowedSchablonIds);

    
    $selSchablOpt = self::getSelectOptions4Schablone($schablonen, 1);
    $disabled = false;

    $schablonenWahlfeld = '';
    $schablonenWahlfeld .= '<div class = "schablone">' . "\n" . '<label id="l_schablone" for="schablone">Schablone:</label>' . "\n"
    . HTMLOperations::CreateSelectTag($selSchablOpt, 'schablone', '', 'schablone', 1, '', false, $disabled) . '</div>';

    return $schablonenWahlfeld;
  }

  

  
  public static function getSelectOptions4Schablone($pSchablonen, $pId)
  {
    $optary = array();
    if (count($pSchablonen) > 0) {
      foreach ($pSchablonen as $schablone) {
        $selected = ($pId == $schablone->id ? 'selected' : '');
        $optary[] = "<option $selected value=" . CleanOperations::cleanStringForHtml($schablone->id) . '>' . CleanOperations::cleanStringForHtml($schablone->beschreibung) . '</option>';
      }
    } 
    else {
      $optary[] = '<option value=0>keine Schablone vorhanden</option>';
    }

    return $optary;
  }

  
  private function _getBoxes4WeekDays()
  {
    
    $defaultBoxId = 1;
    $this->toolSettings->wochentagRange = $defaultBoxId;
    $checked = '';
    $wochenTagAry = array(1 => 'Montag bis Freitag', 'Montag bis Samstag', 'Montag bis Sonntag', 'Freitag bis Sonntag');
    

    $wochentage = '<div class = "wochentageDiv">' . "\n";
    $wochentage .= '<div class ="wochentageText">angezeigte Tage:</div>' . "\n";

    $wochentage .= '<div class="wochentageBoxes">' . "\n";

    foreach ($wochenTagAry as $wotagKey => $wotagName) {
      $checked = ($this->toolSettings->wochentagRange == $wotagKey ? 'checked' : '');
      $wochentage .= '<div class="wochentageRadiobtn">' . "\n"
                   . HTMLOperations::CreateInputTag('wochentagRange', 'radio', 'wotag' . $wotagKey, '', $wotagKey, '', $checked, '')
                   . '<label for="wotag' . $wotagKey . '">' . $wotagName . '</label>' . "\n" . '</div>' . "\n";
      
   
    }
    $wochentage .= '</div>' . "\n"; 
    $wochentage .= '</div>' . "\n"; 

    return $wochentage;
  }

  

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

    return $retour;
  }

  
  private function getFilterDialog()
  {

    
    $paramAry = StringOperations::getUrlParams($this->links['self']);
    unset($paramAry['gebaeude'], $paramAry['gebaeude[]']);

    $paramAry['markerAction'] = 'loescheMarkierungen';

    $searchApi = new GebaeudesuchAPI();
    $suchdm = $searchApi->getFromObjectBySession(null, $this->tool->getToolId());
    $gebaeude = is_array($suchdm->gebaeude) ? $suchdm->gebaeude : array();

    $url = new URL($this->links['self']);
    $url->removeParameter('gebaeude[]');

    $result = '<div id="dialogFilterRaumuebersicht" style="display: none;">' . "\n";
    $result .= '<form id="submitFilterRaumuebersicht" method="get" action="' . CleanOperations::cleanStringForHtml($url->getUrlRelative()) . '">' . "\n";
    $result .= '  <table>' . "\n";
    $result .= '    <tbody>' . "\n";
    $result .= '      <tr>' . "\n";
    $result .= '        <td>Gebäude:</td>' . "\n";
    if ($this->toolSettings->filterMehrereGebaeude) {
      $multiple = ' multiple="multiple" ';
      $emptyOption = '';
    } else {
      $multiple = '';
      $emptyOption = '<option></option>';
    }

    $result .= '        <td><select class="raumuebersichtDialogInput" name="gebaeude[]" id="dialogFilterRaumuebersicht_gebaeude" ' . $multiple . '>' . $emptyOption;
    foreach ($this->_gebaeude as $value) {
      if ($value->schule != '') { 
        $result .= '<option';
        $result .= in_array($value->schule, $gebaeude) ? ' selected ' : '';
        $result .= '>' . CleanOperations::cleanStringForHtml($value->schule) . '</option>';
      }
    }
    $result .= '            </select></td>' . "\n";
    $result .= '      </tr>' . "\n";
    $result .= '    </tbody>' . "\n";
    $result .= '  </table>' . "\n";
    $result .= self::buildHiddenInputs4Params($paramAry);
    $result .= '</form>' . "\n";
    $result .= '</div>' . "\n";

    return $result;
  }

  

  public function getJSDeclarations()
  {
    $result = parent::getJSDeclarations();
    $result .= " registerNamespace('KuferTools');";
    $result .= " registerNamespace('KuferTools.Raumuebersicht');";

    $result .= $this->getJSForIds($this->_data);

    return $result;
  }

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

    return $fileAry;
  }

  public function getSpecialCss()
  {
    return parent::getSpecialCss();
  }
}