<?php
/**
 * @copyright (c) Kufer Software GmbH, Oderstraße 7, D-84453 Mühldorf
 */



abstract class PageGrafikBase extends PageBaseWT
{
  protected $_farblegendary;
  protected $_printversion;
  
  
  protected $_detailPageId;

  
  protected $_kursIdAry4User;
  
  
  protected $_setLink2OtherTool;
  
  protected $_tab4Termin;

  public function __construct($pTool, $pPageId, $pDetailPageId, $pSetLink2OtherTool = false, $pKursIdAry4User = array(), $pTab4Termin = '')
  {
    
    parent::__construct('Raumübersicht', $pPageId, $pTool);

    $this->_farblegendary = array();
    $this->_farblegendary[''] = $this->toolSettings->farbenorm;
    $this->_farblegendary['X'] = $this->toolSettings->farbeX;
    $this->_farblegendary['A'] = $this->toolSettings->farbeA;
    $this->_farblegendary['N'] = $this->toolSettings->farbeN;
    $this->_farblegendary['Z'] = $this->toolSettings->farbeZ;
    $this->_farblegendary['V'] = $this->toolSettings->farbeV;
    $this->_farblegendary['E'] = $this->toolSettings->farbeE;
    $this->_farblegendary['P'] = $this->toolSettings->farbeP;
    $this->_farblegendary['I'] = $this->toolSettings->farbeI;
    $this->_farblegendary['S'] = $this->toolSettings->farbeS;
    $this->_farblegendary['1K'] = $this->toolSettings->farbe1K;

    
    $this->_detailPageId = $pDetailPageId;
    $this->_printversion = GetOperations::getVarValue('print', false);

    $this->_kursIdAry4User = $pKursIdAry4User;
    $this->_setLink2OtherTool = $pSetLink2OtherTool;
    $this->_tab4Termin = $pTab4Termin;
  }

  public function requiresFullScreen()
  {
    if (!$this->_printversion) {
      return DARSTELLUNG_MIT_BANNER;
    } else {
      return DARSTELLUNG_OHNE_BANNER;
    }
  }

  
  public function getContentHeader()
  {
    return '';
  }

  
  public function getLegendeBelVork($result, $sperrleg = false, $firstKT = false)
  {
    $defsett = new ToolRaumUebersichtSettings();
    $artlegendary = $defsett->legend_arry['art'];

    
    
    $countres = count($result);
    if ($countres == 0 && !$sperrleg) {
      return '';
    }

    
    
    $legend
      = '<div class="legende">' . "\n"
      
      
      
      
      . '<table style="width: 125px;" border= "1" cellpadding="2" cellspacing="0">' . "\n"
      . '<tbody>' . "\n"
      . '<tr>' . "\n"
      . '<th>&nbsp; Farblegende</th>' . "\n"
      . '</tr>' . "\n";
    
    $vork_ary = array();
    
    
    for ($i = 0; $i < $countres; $i++) {
      if (!in_array($result[$i]->ausfall, $vork_ary)) {
        $vork_ary[] = $result[$i]->ausfall;
      }
    }
    
    
    foreach ($vork_ary as $vork_wert) {
      
      
      
      
      if ($this->_farblegendary[$vork_wert] != 'WHITE') {
        $z = ($vork_wert != '') ? ': ' : '  ';
        $legend .= "<tr><td style='text-align: center; background-color: " . CleanOperations::cleanStringForHtml($this->_farblegendary[$vork_wert]) . ";'>
        <font size='-1'><a> " . CleanOperations::cleanStringForHtml($vork_wert . $z . $artlegendary[$vork_wert]) . '</a></font></td> </tr>' . "\n";
      }
    }
    
    
    if ($sperrleg == true && $this->_farblegendary['S'] != 'WHITE') {
      $legend .= "<tr><td style='text-align: center; background-color: " . CleanOperations::cleanStringForHtml($this->_farblegendary['S']) . ";'>
      <font size='-1'><a> S: " . CleanOperations::cleanStringForHtml($artlegendary['S']) . '</a></font></td> </tr>' . "\n";
    }
    
    
    if ($firstKT == true && $this->_farblegendary['1K'] != 'WHITE') {
      $legend .= "<tr><td style='text-align: center; background-color: " . CleanOperations::cleanStringForHtml($this->_farblegendary['1K']) . ";'>
      <font size='-1'><a> 1K: " . CleanOperations::cleanStringForHtml($artlegendary['1K']) . '</a></font></td> </tr>' . "\n";
    }
    $legend .= "</tbody></table>\n";
    
    $legend .= "</div>\n";

    return $legend;
  }

  
  public function getLegendeBel()
  {
    
    
    
    
    $legend = '<div class="legende">' . "\n";
    
    
    
    
    

    if ($this->_farblegendary[''] != 'WHITE' || $this->_farblegendary['X'] != 'WHITE' || $this->_farblegendary['A'] != 'WHITE' || $this->_farblegendary['Z'] != 'WHITE' || $this->_farblegendary['V'] != 'WHITE'
      || $this->_farblegendary['N'] != 'WHITE' || $this->_farblegendary['E'] != 'WHITE' || $this->_farblegendary['P'] != 'WHITE' || $this->_farblegendary['I'] != 'WHITE' || $this->_farblegendary['1K'] != 'WHITE' || $this->_farblegendary['S'] != 'WHITE') {
      $legend
        .= "<table style= ' width: 125px;'" 
        . " border='1' cellpadding='2' cellspacing='0'>
        <tbody>"
        . '<tr>'
        . '<th>&nbsp; Farblegende</th>
        </tr>';

      $zustandary = array('' => ' normal',
             'X' => 'X:  durchgef&uuml;hrt',
             'A' => 'A:  Ausfall',
             'Z' => 'Z:  Zusatztag',
             'V' => 'V:  Vorbesprechung',
             'N' => 'N:  Nachbesprechung',
             'E' => 'E:  Elternabend',
             'P' => 'P:  Pr&uuml;fung',
             'I' => 'I: Informationsabend',
             '1K' => '1K: erster Kurstag',
             'S' => 'S:  Sperre');


      foreach ($zustandary as $kurz => $zustand) {
        
        if (strcmp($this->_farblegendary[$kurz], 'WHITE') != 0) {
          $legend .= "<tr><td style='text-align: center; background-color: " . CleanOperations::cleanStringForHtml($this->_farblegendary[$kurz]) . ";'>
          <font size='-1'>" . $zustand . '</font></td> </tr>';
        }
      }
    }
    $legend .= ' </tbody> </table>';
    
    $legend .= '</div>';

    return $legend;
  }

  public function _GetUhrzeitHinterlegungsButton($pTyp, $pFieldID)
  {
    return ToolsAPI::hlGetUhrzeitHinterlegungsButton($pTyp, $pFieldID);
  }

  
  protected function getBelegBlockInhalt($pKurstag, $pMedienText, $pQuerclass, $pKursKurzbez, $pRaumNR = '')
  {
    $medienspan = '';
    if ($pMedienText != '') {
      $medienspan = '<span class="medien">' . CleanOperations::cleanStringForHtml($pMedienText) . '</span>' . "\n";
    }

    
    $linkAllowed = (!$this->_setLink2OtherTool || in_array($pKurstag->knr, $this->_kursIdAry4User));

    
    if ($this->links['kursdetails'] == 2 && $linkAllowed) {
      
      $blockInhalt
      = '<a ' . $pQuerclass . ' href="' . CleanOperations::cleanStringForHtml($this->getKursDetailLink($pKurstag, '', $pRaumNR)) . '"  title="Kursdetails">' . "\n"
               . CleanOperations::cleanStringForHtml($pKurstag->knr . $pKursKurzbez) . '</a>' . "\n" . $medienspan;
    } else { 
      $blockInhalt = '<span ' . CleanOperations::cleanStringForHtml($pQuerclass) . ' title="Kursdetails">' . "\n" . CleanOperations::cleanStringForHtml($pKurstag->knr . $pKursKurzbez) . $medienspan;
    }

    
    if ($this->links['kursdetails_img'] && $linkAllowed) {
      $blockInhalt
      .= '<a ' . CleanOperations::cleanStringForHtml($pQuerclass) . ' href="' . CleanOperations::cleanStringForHtml($this->getKursDetailLink($pKurstag, $this->_tab4Termin, $pRaumNR)) . '">' . "\n"
               . '<img src="images/kurstageg.gif"'
               . ' title="Übersicht der Kurstermine" alt="Übersicht der Kurstermine" onmousemove="ChangeImg(this,Kurstage)"'
               . ' onmouseout="ChangeImg(this,KurstageGray)" border="0" height="16" width="16" />'
               . '</a>' . "\n";
    }

    return $blockInhalt;
  }

  

  
  protected function getSperrGrund($pSperrEntry, $pToolId, $pStyle4margintop, $pRaumNr)
  {
    $sperrgrund = '<span class="sperre">' . CleanOperations::cleanStringForHtml($pSperrEntry->begruendung) . '</span>' . "\n";

    
    
    
    foreach ($pSperrEntry->kollisionen as $kollision) {
      
      $linkAllowed = (!$this->_setLink2OtherTool || in_array($kollision, $this->_kursIdAry4User));

      $kollKursText = '<span>' . CleanOperations::cleanStringForHtml($kollision) . '</span>';
      
      if ($linkAllowed) {
        $kollKursText = '<a class="sperre" href="' . CleanOperations::cleanStringForHtml($this->getDetailsLink($pToolId, $kollision, $this->_detailPageId, $pRaumNr)) . '" '
                        . 'title="Kursdetails">' . CleanOperations::cleanStringForHtml($kollision) . '</a>';
      }

      $sperrgrund .= '<span ' . $pStyle4margintop . '>Kollision m. ' . $kollKursText
      
      
                    . "\n" . '</span>' . "\n";

      
      if ($this->links['kursdetails_img'] && $linkAllowed) {
        $sperrgrund .= '<a class="sperre" ' . $pStyle4margintop . ' href="'
                      . CleanOperations::cleanStringForHtml($this->getDetailsLink($pToolId, $kollision, $this->_detailPageId, $pRaumNr, $this->_tab4Termin))
                      . '" target="_blank" title="Übersicht der Kurstermine">[Term.]</a>' . "\n";
      }
    } 

    return $sperrgrund;
  }

  

  
  public function _getKollisionen($pKtind, $pCountsp, $pTabind, $pResary, $pSperary, $pSearch, $pTabary, $darst)
  {
    
    
    for ($j = 0; $j < $pCountsp; $j++) {
      
      if ($darst == 'tag') {
        $sperr_ind = $pTabary[$j];
      }
      
      else { 
        $sperr_ind = $pSearch->getArrayIndexbyfieldname($pTabary, 'id', $pSperary[$j]->ortnr);
      }
      if ($sperr_ind == $pTabind) {
        if (($pResary[$pKtind]->zeit_beg >= $pSperary[$j]->bisuhr)
           || ($pResary[$pKtind]->zeit_ende <= $pSperary[$j]->vonuhr)) {
          
        } else { 
               
               
          if (!in_array($pResary[$pKtind]->knr, $pSperary[$j]->kollisionen)) {
            $pSperary[$j]->kollisionen[] = $pResary[$pKtind]->knr;
          }
        }
      } 
    }

    return $pSperary;
  }

  

  
  protected function _isFirstKurstag($pKtag, $pKtary)
  {
    
    
    
    usort($pKtary, array('SortFunctions', 'sortKurstageByKnrBeginn'));
    
    
    $Knrary = array();
    $Ktidary = array();
    foreach ($pKtary as $ktag) {
      if (!in_array($ktag->knr, $Knrary)) {
        $Knrary[] = $ktag->knr;
        $Ktidary[] = $ktag->id;
      }
    }

    
    if (in_array($pKtag->id, $Ktidary)) {
      return true;
    } else {
      return false;
    }
  }

  

  
  protected function getKursDetailLink($pKurstag, $pTab = '', $pRaumNR = '')
  {
    $toolId = $this->tool->getToolId();
    if (!empty($pRaumNR)) {
      $raumNr = $pRaumNR;
    } else {
      $raumNr = $pKurstag->kursort->id;
    }

    return $this->getDetailsLink($toolId, $pKurstag->knr, $this->_detailPageId, $raumNr, $pTab);
  }

  
  protected function getDetailsLink($pToolId, $pKnr, $pSectionId, $pRaumNr, $pTab = '')
  {
    $toolId = $pToolId;
    
    if ($toolId == \ToolsAPI::TOOL_RAUMBELEGUNG_MA) {
      $toolId = \ToolsAPI::TOOL_KURSPLANUNG_MA;
    }

    
    $urlParamAry = array('tool_id' => $toolId, 'toolsection_id' => $pSectionId, 'r-nr' => $pRaumNr, 'nr' => $pKnr, 'fromOtherTool' => '1');

    return $this->getUrlParamsForFile($urlParamAry, 'usertools.php', $pTab); 
  }

  

  
  protected function getActionLink($pRaumNr = '')
  {
    
    
    
    
    
    $linkAry = array('toolsection_id' => $this->id);
    if ($pRaumNr !== '') {
      $linkAry['nr'] = $pRaumNr;
    }

    return $this->getUrlParamsForFile($linkAry);
  }

  protected function getActionLink4Raum()
  {
    
    
    return $this->getUrlParamsForFile(array('toolsection_id' => $this->id));
  }

  protected function getGebFormLink($pForm, $pGeb, $pDateForm)
  {
    
    return $this->getUrlParamsForFile(array('toolsection_id' => $this->id, 'gebaeude' => $pGeb, 'form' => $pForm,
                                         'dateForm' => $pDateForm));
  }

  protected function getGebGrafikLink4($pForm, $pGeb, $pShowGrafik)
  {
    
    return $this->getUrlParamsForFile(array('toolsection_id' => $this->id, 'gebaeude' => $pGeb, 'form' => $pForm,
                                         'showGrafik' => $pShowGrafik));
  }

  public function getJSDeclarations()
  {
    $result = parent::getJSDeclarations();
    $result .= 'registerNamespace("KuferTools.Grafik");' . "\n";
    $result .= JsOperations::getDeclarationInt('KuferTools.ToolSectionId', $this->id);
    $result .= 'registerNamespace("KuferTools.Grafik.Datum");' . "\n";
    

    
    
    $format = GetOperations::getVarValue('form');
    if (!empty($format)) {
      $result .= JsOperations::getDeclarationInt('KuferTools.Grafik.Format', $format);
    }

    return $result;
  }

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

    return $fileAry;
  }

  
  protected function getUrlParamsForFile($pParams, $pFile = 'usertools.php', $pAnchor = '')
  {
    $params = $this->getUrlParams($pParams);

    return $pFile . ($params != '' ? '?' : '') . $params . ($pAnchor != '' ? '#' . urlencode($pAnchor) : '');
  }

  
  protected function getUrlParams($pParams)
  {
    if (!is_array($pParams)) {
      return '';
    }
    $result = '';
    if (!isset($pParams['tool_id'])) {
      $result .= '&tool_id=' . $this->tool->getToolId();
    }
    foreach ($pParams as $name => $value) {
      if ($value != '') {
        $result .= "&$name=" . urlencode($value);
      }
    }

    return StringOperations::substr($result, 1);
  }
}