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


class PageGrafikFuerGebaeude extends PageGrafikBase
{
  private $_gebaeude;

  public function __construct($pTool, $pPageId, $pData, $pDetailPageId, $pTab4Termin = '', $pSetLink2OtherTool = false, $pKursIdAry4User = array())
  {
    parent::__construct($pTool, $pPageId, $pDetailPageId, $pSetLink2OtherTool, $pKursIdAry4User, $pTab4Termin);
    $this->_gebaeude = $pData;
  }

  
  public function requiresFullScreen()
  {
    return 3;
  }

  public function getContent()
  {
    $toolId = $this->tool->getToolId(); 
    $retour = '';
    
    $grafik_anz = true;
    
    if (GetOperations::getVarValue('form') == '2') { 
      $tmp = new TimeTableRaeumeVertical();
      $usehor = true;
      
      $tmp->setCellSize(
        $this->toolSettings->cellheight_vert,
        $this->toolSettings->cellwidth_vert,
        $this->toolSettings->cellwidth_header_vert,
        $this->toolSettings->cellheight_header_vert
      );
      
      
      if (!empty($this->toolSettings->sperrblock_height_vert)) {
        $sperrblock_geb_size = $this->toolSettings->sperrblock_height_vert;
      } else {
        $sperrblock_geb_size = '40';
      }
      if (!empty($this->toolSettings->belblock_height_vert)) {
        $belblock_geb_size = $this->toolSettings->belblock_height_vert;
      } else {
        $belblock_geb_size = '58';
      }
    } elseif (GetOperations::getVarValue('form') == '1') { 
      $tmp = new TimeTableRaeume();
      $usehor = false;

      if (DBWorkBASysSettings::isHerneHospital()) {
        
        $usehor = true;
      }

      $tmp->setCellSize(
        $this->toolSettings->cellwidth_hori,
        $this->toolSettings->cellheight_hori,
        $this->toolSettings->cellheight_header_hori,
        $this->toolSettings->cellwidth_header_hori
      );
      
      if (!empty($this->toolSettings->sperrblock_width_hori)) {
        $sperrblock_geb_size = $this->toolSettings->sperrblock_width_hori;
      } else {
        $sperrblock_geb_size = '110';
      }
      if (!empty($this->toolSettings->belblock_width_hori)) {
        $belblock_geb_size = $this->toolSettings->belblock_width_hori;
      } else {
        $belblock_geb_size = '133';
      }
    } else {
      
      
      
      
      $grafik_anz = false;
    }

    
    $starttime = GetOperations::getVarValue('starttime');
    $endtime = GetOperations::getVarValue('endtime');

    
    if ($starttime != '') {
      if ($grafik_anz) {
        $tmp->setStarttime($starttime);
      }
    } else {
      $starttime = DateTimeOperations::getFloatasTimeString(STARTZEIT, true);
    }



    if ($endtime != '') {
      if ($grafik_anz) {
        $tmp->setEndtime($endtime);
      }
    } else {
      $endtime = DateTimeOperations::getFloatasTimeString(ENDEZEIT, true);
    }

    $getkt = new DBWorkKurseTage();
    $getko = new DBWorkKursorte();
    
    
    $getov = new DBWorkOrtVerf();
    $dat = new DateTimeOperations();
    $search = new SearchOperations();
    
    
    
    $gebaeude = $this->_gebaeude;

    
    
    
    

    $firstid = '';
    $forward = true;
    $kolimit = 599; 
    $getko->newSearch();
    $getko->orderby = $this->toolSettings->sortraeume;
    $getko->orderbyasc = $this->toolSettings->sortraeumeasc;
    
    
    
    $getko->fromobject->schule = $gebaeude;
    
    
    $usr = SessionOperations::getCurrentWebToolsUser();
    
    
    $restrict = false;
    
    if ($toolId == \ToolsAPI::TOOL_RAUMBELEGUNG_HM) {
      if ($this->toolSettings->nurzugeordneteorte) {
        $restrict = true;
      }
    }

    
    

    $firstidobj = null;
    if (!empty($firstid)) {
      $firstidobj = $getko->getKursort($firstid);
    }
    $kursort_ary = $getko->getKursorteLimit($firstidobj, $kolimit, $forward, $usr->id, $usr->admin, $restrict);
    
    
    if (empty($_REQUEST['DatVon'])) {
      $_REQUEST['DatVon'] = $dat->getDateNow();
    }

    
    

    if (GetOperations::getVarValue('dateForm') == DATE_TAG_VOR) {
      $_REQUEST['DatVon'] = $dat->getNextDate($_REQUEST['DatVon'], 1);
    } elseif (GetOperations::getVarValue('dateForm') == DATE_TAG_ZUR) {
      $_REQUEST['DatVon'] = $dat->getNextDate($_REQUEST['DatVon'], -1);
    } elseif (GetOperations::getVarValue('dateForm') == DATE_WO_VOR) {
      $_REQUEST['DatVon'] = $dat->getNextDate($_REQUEST['DatVon'], 7);
    } elseif (GetOperations::getVarValue('dateForm') == DATE_WO_ZUR) {
      $_REQUEST['DatVon'] = $dat->getNextDate($_REQUEST['DatVon'], -7);
    }
    $wochentag = $dat->getWeekDay($_REQUEST['DatVon']);


    
    
    
    
    
    
    

    
    $retour .= $this->_getBelegungUeber($gebaeude);


    
    
    
    
    
    
    
    

    
    
    
    
    

    if ($grafik_anz) {
      $tmp->startleft = 10;
      $tmp->starttop = 120;

      $retour .= '<div class="text4tag" style="left:' . CleanOperations::cleanInt($tmp->startleft + 40) . 'px;">' . "\n" . CleanOperations::cleanStringForHtml($wochentag)
                   . ', ' . CleanOperations::cleanDateString($_REQUEST['DatVon'], '') . '</div>' . "\n";

      
      $getkt->newSearch();
      $getov->newSearch();
      
      
      if (($_REQUEST['DatVon'] != '') && (StringOperations::strlen($_REQUEST['DatVon']) == MAXLEN_DAT)) {
        $getkt->fromobject->datum = $_REQUEST['DatVon'];
        $getkt->toobject->datum = $_REQUEST['DatVon'];
        $getov->fromobject->vondat = $_REQUEST['DatVon'];
        $getov->toobject->bisdat = $_REQUEST['DatVon'];
      }

      $ktfirst = array();
      $result = array();
      $sperr_ary = array();
      foreach ($kursort_ary as $kursort) {
        $p_rnr = $kursort->id;
        $result = array_merge($result, $getkt->getKurstagebyGebaeude($p_rnr));
        $sperr_ary = array_merge($sperr_ary, $getov->getKursorteVerfbyRaumDat($p_rnr));
        
        
        $ktfirst = array_merge($ktfirst, $getkt->getKurstageBeginnbyRaum($p_rnr));
      }
      $countres = count($result);
      $countsperr = count($sperr_ary);
      $sperr_legend = false;
      if ($countsperr > 0) {
        $sperr_legend = true;
      }

      
      
      $tmp->ary = $kursort_ary;
      
      if (isset($this->links['veranstaltungsPlanungLink']) && $this->links['veranstaltungsPlanungLink'] != '') {
        $tmp->veranstaltungsPlanungLink = $this->links['veranstaltungsPlanungLink'];
      }

      $retour .= '<div class="grafikTable">' . "\n"; 

      $retour .= $tmp->createFramebyRange(GetOperations::getVarValue('DatVon'), GetOperations::getVarValue('DatBis'));

      $firstKT = false;
      
      
      
      for ($i = 0; $i < $countres; $i++) { 
          
        
        $medienausg = '';

        if ($usehor == true) { 
          $kurstitel = $result[$i]->kurs->kurzbez;
          
          if ($this->tool->isIhk()) {
            if (StringOperations::strtolower(StringOperations::substr(trim($kurstitel), 0, 16)) == 'raumreservierung') {
              $kurstitel = $result[$i]->bemerkungen;
            }
          } 
          $kursKurzbezAusgabe = ';&nbsp;' . StringOperations::substr($kurstitel, 0, 25)
          . (StringOperations::strlen($kurstitel) > 25 ? '...' : '');
          
          if ($this->toolSettings->medienanzeigen1 && $result[$i]->medien != '') {
            $medienausg = StringOperations::substr($result[$i]->medien, 0, 24) . (StringOperations::strlen($result[$i]->medien) > 24 ? '...' : '');
          }
          $querclass = 'class="quer"';
        } else { 
          $kursKurzbezAusgabe = '';
          if ($this->toolSettings->medienanzeigen1 && $result[$i]->medien != '') {
            $medienausg = StringOperations::substr($result[$i]->medien, 0, 35);
          }
          $querclass = '';
        }

        $str = '<div class="datausgabe">' . "\n";
        $str .= $this->getBelegBlockInhalt($result[$i], $medienausg, $querclass, $kursKurzbezAusgabe);
        $str .= '</div>' . "\n";
        
        
        $index = $search->getArrayIndexbyfieldname($kursort_ary, 'mktext', $result[$i]->ort);
        
        
        $sperr_ary = $this->_getKollisionen($i, $countsperr, $index, $result, $sperr_ary, $search, $kursort_ary, 'raum');
        
        $blocksize = $belblock_geb_size;

        
        
        
        
        if (!$this->_isFirstKurstag($result[$i], $ktfirst)) {
          $status4farblegend = $this->_farblegendary[$result[$i]->ausfall];
        } else {
          $firstKT = true;
          $status4farblegend = $this->_farblegendary['1K'];
        }
        
        if ($index !== -1) {
          $retour .= $tmp->addTimePanel($index, $result[$i]->zeit_beg, $result[$i]->zeit_ende, $str, $status4farblegend, $blocksize);
        }
      } 

      
      $weekdays = array(1 => 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag', 'Sonntag');
      $sperrfarbe = $this->_farblegendary['S'];
      $style4margintop = '';
      if (!$usehor) { 
        $style4margintop = 'style="margin-top:3px;"';
      }
      for ($i = 0; $i < $countsperr; $i++) {
        
        
        $sperr_ind = -1;
        
        if ($sperr_ary[$i]->wochentag == 0) {
          $sperr_ind = $search->getArrayIndexbyfieldname($kursort_ary, 'id', $sperr_ary[$i]->ortnr);
        } else {
          
          
          
          if ($weekdays[$sperr_ary[$i]->wochentag] == $dat->getWeekDay($_REQUEST['DatVon'])) {
            $sperr_ind = $search->getArrayIndexbyfieldname($kursort_ary, 'id', $sperr_ary[$i]->ortnr);
          }
        }

        $sperrgrund = $this->getSperrGrund($sperr_ary[$i], $toolId, $style4margintop, $sperr_ary[$i]->ortnr);


        
        
        $vonuhr = $dat->getFloatasTimeString($tmp->starttime);
        if (StringOperations::strlen($vonuhr) < 5) {
          $vonuhr = '0' . $vonuhr;
        }
        $vonuhr = max($vonuhr, $sperr_ary[$i]->vonuhr);
        
        $bisuhr = $dat->getFloatasTimeString($tmp->endtime);
        if (StringOperations::strlen($bisuhr) < 5) {
          $bisuhr = '0' . $bisuhr;
        }
        $bisuhr = min($bisuhr, $sperr_ary[$i]->bisuhr);
        
        if ($sperr_ind != -1) {
          
          $blocksize = $sperrblock_geb_size;
          $retour .= $tmp->addTimePanel($sperr_ind, $vonuhr, $bisuhr, $sperrgrund, $sperrfarbe, $blocksize);
        }
      }

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

      $panel_height = $tmp->getPanelheight();
      $retour .= "<div style = 'position:relative; visibility:hidden; height:" . CleanOperations::cleanInt($panel_height) . "px;'>";
      $retour .= "</div>\n";
      
      $retour .= $this->getLegendeBelVork($result, $sperr_legend, $firstKT);
    }
    else {
      $retour .= $this->getLegendeBel();
    }


    
    $retour
        .= "<form id='gebaeudegrafik' action='" . CleanOperations::cleanStringForHtml($this->getActionLink()) . "' method='POST'>\n"
        
          . "<table border='0'>"
           . "<tr valign='top'>"
            . '<td>'
               . '<span id="grafikwahl">Bitte wählen Sie den gewünschten Tag aus</span>' . BR_WT
               . '<div id="pfeilerueck">' . "\n"
               

               . '<input id="worueck" type="image" title="Eine Woche zurückblättern" src="layout/button_doppelpfeil_links.gif">' . "\n"

              

              . '<input id="tagrueck" type="image" title="Einen Tag zurückblättern" src="layout/button_pfeil_links_small.gif">' . "\n"
              . '</div>' . "\n"
              
              . '<div id="datumwahl4geb">' . "\n"
                . "<input type='text' maxlength='" . MAXLEN_DAT . "' value='" . CleanOperations::cleanDateString($_REQUEST['DatVon'], '') . "' size='9"
                . "' name='DatVon' class='hasDatePicker'>&nbsp;"
              . '</div>' . "\n"
              . '<div id="pfeilevor">' . "\n"
                
                 . '<input id="tagvor"  type="image" title="Einen Tag vorblättern" src="layout/button_pfeil_rechts_small.gif">' . "\n"
                
                . '<input id="wovor"  type="image" title="Eine Woche vorblättern" src="layout/button_doppelpfeil_rechts.gif">' . "\n"
              . '</div>' . "\n"
              . '<div id="zeitwahl4geb">' . "\n"
                . '<span>von:&nbsp;</span>' . "\n" . "<input type='text' maxlength='" . MAXLEN_TIME . "' value='" . CleanOperations::cleanTimeString($starttime, '')
                . "' size='3' name='starttime' id='starttime' />&nbsp;"
                . '<span>bis:&nbsp;</span>' . "\n" . "<input type='text' maxlength='" . MAXLEN_TIME . "' value='" . CleanOperations::cleanTimeString($endtime, '')
                . "' size='3' name='endtime' id='endtime' />"
                 . $this->_GetUhrzeitHinterlegungsButton(\HLDispatcher::HL_UHRZEITEN, 'starttime:endtime')
              . '</div>' . "\n";
    
    $retour .= '<div class="buttons4ausgabe">' . "\n";
    
    $retour .= '<div class="radiobuttons">' . "\n";
    
    $checkedquer = '';
    $checkedhoch = 'checked="checked"';
    if (GetOperations::isDefined('form') && GetOperations::getVarValue('form') == '2') {
      $checkedhoch = '';
      $checkedquer = 'checked="checked"';
    }
    $retour .= '<input type="radio" name="hochquer" value="1" ' . $checkedhoch . '>Hochformat' . "\n"
               . '<input type="radio" name="hochquer" value="2" ' . $checkedquer . '>Querformat' . "\n"
               . '</div>' . "\n";

    $retour .= '<div class="druckbuttons">' . "\n";

    
    $retour .= '<input id="generateList" name="Druck" type="image" src="layout/button_listegenerieren.gif">' . "\n";

    
    if ($grafik_anz) {
      $retour .= '<a href ="javascript:window.print();">'
                 . '<img class="druckbuttonHM" title="Druck der Kurstermine" src="layout/button_druck.gif" alt="Druck der Kurstermine" />'
                 . '</a>' . "\n";
    }

    $retour .= HTMLOperations::CreateBackButton(ToolsAPI::getPreviousToolsectionLink(), '', '', 'abbruch');
    $retour .= '</div>' . "\n" . '</div>' . "\n";
    $retour .= '</table>' . "\n" . '</form>' . "\n";

    return $retour;
  }

  

  
  private function _getBelegungUeber($gebaeude)
  {
    return "<table border='0' width='100%'>\n"
      . " <tr>\n"
      . "  <td width='50%' align='left'><font face='Arial' size='4'><b>" . CleanOperations::cleanStringForHtml($this->allgSettings->vhsname) . "</b></font></td>\n"
      . "  <td width='50%' align='right'><font size='3' face='Arial'><b>Stand: " . date('d.m.Y') . "</b></font></td>\n"
      . " </tr>\n"
      . " <tr>\n"
      . "  <td width='50%' align='left'><font face='Arial' size='4'><b>Grafik der Belegungen für: " . CleanOperations::cleanStringForHtml(implode(', ', $gebaeude)) . "</b></font></td>\n"
      . "  <td width='50%' align='right'></td>"
      . " </tr>\n </table>" . BR_WT;
  }

  

  public function getJSDeclarations()
  {
    $result = parent::getJSDeclarations();

    $gebaeudeUrl = '';
    foreach ($this->_gebaeude as $gebaeude) {
      $gebaeudeUrl .= 'gebaeude[]=' . urlencode($gebaeude) . '&';
    }

    $result .= JsOperations::getDeclarationString('KuferTools.Grafik.GebaeudeUrlParam', $gebaeudeUrl);
    
    $result .= JsOperations::getDeclarationInt('KuferTools.Grafik.Datum.WoZur', DATE_WO_ZUR);
    $result .= JsOperations::getDeclarationInt('KuferTools.Grafik.Datum.WoVor', DATE_WO_VOR);
    $result .= JsOperations::getDeclarationInt('KuferTools.Grafik.Datum.TagZur', DATE_TAG_ZUR);
    $result .= JsOperations::getDeclarationInt('KuferTools.Grafik.Datum.TagVor', DATE_TAG_VOR);

    return $result;
  }
}