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


class ToolHausmeisterKurse extends ToolBase
{
  private $_user;
  private $_dbwkurse;

  public function __construct()
  {
    parent::__construct();
    $this->toolTitle = 'Kurse in meinen R&auml;umen';

    

    $dbwkurse = new DBWorkKurseWT();
    
    

    $dbwkurse->setOrderBySettings($this->toolSettings);

    $this->_dbwkurse = $dbwkurse;
    $this->_user = SessionOperations::getCurrentWebToolsUser();
  }

  

  public function init($pPageId)
  {
    
    switch ($pPageId) {
      default:
      case 371: 
          $page = $this->_handleKursuebersicht();
          break;
    }

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

  

  
  protected function handleKursFilterVorbelegen()
  {
  }

  private function _handleKursuebersicht()
  {
    $dbwKurseOrte = new DBWorkKursorte(1, true);

    $pageId = 371;

    
    $datumVon = DateTimeOperations::getFirstDayOfCurrentWeek();
    $datumBis = DateTimeOperations::getNextDate($datumVon, 13);
    $knrVon = $knrBis = $kursTitel = '';
    if (GetOperations::isDefined('einstieg')) {
      if ($this->toolSettings->kursFilterVorbelegen) {
        $knrVon = $this->toolSettings->knrVon;
        $knrBis = $this->toolSettings->knrBis;
      }
      if ($this->toolSettings->kursFilterKursbeginnVorbelegen) {
        $datumVonBisAry = ToolsAPI::getVorbelegungDatumVonBis($this->toolId);
        $datumVon = $datumVonBisAry['von'];
        $datumBis = $datumVonBisAry['bis'];
      }
    } else {
      $knrVon = '0';
      $knrBis = 'ZZ';
    }

    if (GetOperations::isDefined('searchkurs')) {
      if (PostOperations::isDefined('filter_datumvon')) { 
        $datumVon = PostOperations::getVarValue('filter_datumvon');
        $datumBis = PostOperations::getVarValue('filter_datumbis');
        $knrVon = PostOperations::getVarValue('filter_knrvon');
        $knrBis = PostOperations::getVarValue('filter_knrbis');
        $kursTitel = PostOperations::getVarValue('filter_kurstitel');

        SessionOperations::setVarValueWTIntern('ssearch_kursbeginnzeitraum_von', $datumVon);
        SessionOperations::setVarValueWTIntern('ssearch_kursbeginnzeitraum_bis', $datumBis);
        SessionOperations::setVarValueWTIntern('ssearch_kursnummernbereich_von', $knrVon);
        SessionOperations::setVarValueWTIntern('ssearch_kursnummernbereich_bis', $knrBis);
        SessionOperations::setVarValueWTIntern('ssearch_kurstitel', $kursTitel);
      } else {
        $datumVon = SessionOperations::getVarValueWTIntern('ssearch_kursbeginnzeitraum_von', $datumVon);
        $datumBis = SessionOperations::getVarValueWTIntern('ssearch_kursbeginnzeitraum_bis', $datumBis);
        $knrVon = SessionOperations::getVarValueWTIntern('ssearch_kursnummernbereich_von', $knrVon);
        $knrBis = SessionOperations::getVarValueWTIntern('ssearch_kursnummernbereich_bis', $knrBis);
        $kursTitel = SessionOperations::getVarValueWTIntern('ssearch_kurstitel', $kursTitel);
      }
    } else {
      SessionOperations::setVarValueWTIntern('ssearch_kursbeginnzeitraum_von', $datumVon);
      SessionOperations::setVarValueWTIntern('ssearch_kursbeginnzeitraum_bis', $datumBis);
      SessionOperations::setVarValueWTIntern('ssearch_kursnummernbereich_von', $knrVon);
      SessionOperations::setVarValueWTIntern('ssearch_kursnummernbereich_bis', $knrBis);
      SessionOperations::setVarValueWTIntern('ssearch_kurstitel', $kursTitel);
    }

    $dbwKurstage = new DBWorkKurseTageMin(1, true);
    $dbwKurstage->newSearch();
    $dbwKurstage->fromobject->datum = $datumVon;
    $dbwKurstage->toobject->datum = $datumBis;
    $dbwKurstage->fromobject->kurs->id = $knrVon;
    $dbwKurstage->toobject->kurs->id = $knrBis;
    $dbwKurstage->fromobject->kurs->kurzbez = $kursTitel;

    $dbwKurstage->fromobject->kurs->semester = $this->toolSettings->semlist;
    $dbwKurstage->toobject->kurs->semester = '';
    $dbwKurstage->dbwkurse->justNoAusfall = $this->toolSettings->keineAusgefallenenKurse;
    

    $this->updateAndGetSelectedIds();
    $restrict = isset($this->toolSettings->nurzugeordneteorte) ? $this->toolSettings->nurzugeordneteorte : false;

    $raumVerfuegbarkeit = false;
    $alleIds = $this->getAlleIdsKurs4Hausmeister($this->_dbwkurse, $dbwKurstage, $dbwKurseOrte, $restrict, $raumVerfuegbarkeit);
    
    SessionOperations::setVarValueWTIntern('totalkursdatastr_' . \ToolsAPI::TOOL_RAUMBELEGUNG_HM, implode(':', $alleIds));
    $data = $this->getData($this->_dbwkurse, $alleIds, $this->toolSettings->kurseproseite);
    $anzahlAllerKurse = count($alleIds);

    
    $pageLinks['kursdetails'] = \SecurityOperations::privilegAllowed($this->_user->getIdentifier(), $this->getToolId(), '371.0.1')
              && (\SecurityOperations::privilegAllowed($this->_user->getIdentifier(), 5, '341.1.1')
              || \SecurityOperations::privilegAllowed($this->_user->getIdentifier(), 5, '341.1.2')
              || \SecurityOperations::privilegAllowed($this->_user->getIdentifier(), 5, '341.1.3')
              || \SecurityOperations::privilegAllowed($this->_user->getIdentifier(), 5, '341.1.4')
              || \SecurityOperations::privilegAllowed($this->_user->getIdentifier(), 5, '341.1.5'));

    $pageLinks['filter'] = \SecurityOperations::privilegAllowed($this->_user->getIdentifier(), $this->getToolId(), $pageId . '.0.2');
    
    $pageLinks['teilnehmer'] = (\SecurityOperations::privilegAllowed($this->_user->getIdentifier(), $this->getToolId(), $pageId . '.0.3')
                && (\SecurityOperations::privilegAllowed($this->_user->getIdentifier(), 5, '240.0.2')
              || \SecurityOperations::privilegAllowed($this->_user->getIdentifier(), 5, '240.0.4')
              || \SecurityOperations::privilegAllowed($this->_user->getIdentifier(), 5, '240.0.6')
              || \SecurityOperations::privilegAllowed($this->_user->getIdentifier(), 5, '240.0.8')
              || \SecurityOperations::privilegAllowed($this->_user->getIdentifier(), 5, '240.0.10')));

    
    $hasButton4Checkbox = ($pageLinks['teilnehmer']);

    
    $pageLinks['markieren'] = (\SecurityOperations::privilegAllowed($this->_user->getIdentifier(), $this->getToolId(), $pageId . '.0.4') && $hasButton4Checkbox);
    $pageLinks['checkboxen'] = $hasButton4Checkbox;

    $pageLinks['self'] = $this->getUrlParamsForFile(array('toolsection_id' => 371, 'searchkurs' => 1));
    
    
    $page = new PageHausmeisterKursuebersicht($this, $data, $datumVon, $datumBis, $knrVon, $knrBis, $kursTitel, $anzahlAllerKurse, $this->toolSettings->kurseproseite);
    $page->setLinks($pageLinks);

    return $page;
  }

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

class PageHausmeisterKursuebersicht extends BrowseableObjectsPageBaseWT
{
  private $_data;
  private $_datumVon;
  private $_datumBis;
  private $_knrVon;
  private $_knrBis;
  private $_kursTitel;

  public function __construct($pTool, $pData, $pDatumVon, $pDatumBis, $pKnrVon = '0', $pKnrBis = 'ZZ', $pKursTitel = '', $countAllObjects = 1, $objectsPerPage = 1)
  {
    parent::__construct('Kurs&uuml;bersicht', 371, $pTool, $countAllObjects, $objectsPerPage);
    $this->_data = $pData;
    $this->_datumVon = $pDatumVon;
    $this->_datumBis = $pDatumBis;
    $this->_knrVon = $pKnrVon;
    $this->_knrBis = $pKnrBis;
    $this->_kursTitel = $pKursTitel;
  }

  
  public function getContentHeader()
  {
    return 'Kurs&uuml;bersicht im Zeitraum vom ' . $this->_datumVon . ' bis ' . $this->_datumBis;
  }

  public function getContent()
  {
    $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();
    }

    $menu = new Menu();
    $allowDetails = (isset($this->links['kursdetails']) && $this->links['kursdetails'] ? 2 : 0);
    $sprachsettings = new SprachSettingsObject();


    $counter = 0;
    if (count($this->_data) > 0) {
      foreach ($this->_data as $kurs) {
        $builder = new SightBuilderUebersicht($this->tool->getToolId(), $this->id, $kurs, true);
        $builder->weitereKlasseFuerSightDiv = 'hmkurse';
        $evenClass = ($counter % 2 == 0) ? ' farbe_sicht_erstezeile' : '';
        $result .= '<div class="content_zeile' . $evenClass . '">';
        $result .= '<div class="steuerelemente kursuebersicht_tool32">';
        if (isset($this->links['checkboxen']) && $this->links['checkboxen']) {
          $result .= '<input type="checkbox" id="k_' . CleanOperations::cleanStringForHtml($kurs->id) . '"  name="' . CleanOperations::cleanStringForHtml($kurs->id) . '" class="kurscheckbox markable" value="' . CleanOperations::cleanStringForHtml($kurs->id) . '">' . BR_WT;
        }
        $result .= $menu->getUserLink(
          '<img src="images/kursdetails.gif" alt="Zu den Details" title="' . CleanOperations::cleanStringForHtml($sprachsettings->KursZusammengesetzt['Singular']['Nominativ']) . 'details">',
          'usertools.php?tool_id=5&toolsection_id=341&nr=' . $kurs->id,
          $allowDetails,
          false,
          false,
          'details_link'
        );
        $result .= '</div>';
        $result .= $builder->buildSight();
        $result .= '</div>' . "\n";
        $counter++;
      }
    } else { 
      if (!isset($this->_searchkurs) || $this->_searchkurs == '') {
        $result .= $this->getNoDataMessageDefault(1);
      } else {
        $result .= $this->getNoDataMessageDefault(2);
      }
    }
    if ($this->allgSettings->buttonleite_position != BUTTONBAR_TOP) {
      $result .= $this->_getButtonLeiste();
    }

    $result .= '</div>'; 

    return $result;
  }

  
  protected function _getButtonLeiste()
  {
    $sprachsettings = new SprachSettingsObject();
    $result = '<div class="main_buttons hm">';
    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 " . CleanOperations::cleanStringForHtml($sprachsettings->Kurs['Plural']['Akkusativ']) . "' alt='Markieren von " . CleanOperations::cleanStringForHtml($sprachsettings->Kurs['Plural']['Akkusativ']) . "' class=\"alleMarkieren\">\n";
      static $isDialogSet = false;
      if (!$isDialogSet) {
        $result .= $this->_getDialogMarkieren();
        $isDialogSet = true;
      }
    }


    if (isset($this->links['teilnehmer']) && $this->links['teilnehmer']) {
      $result .= '<input  class="leftspace tnListenDruckButton" type="image" name="teilnehmer" value="1" src="layout/button_90_druck_teilnehmer.gif" border="0" '
                . " title='Druck von " . CleanOperations::cleanStringForHtml($sprachsettings->Teilnehmer['Plural']['Nominativ']) . "listen' alt='Druck von " . CleanOperations::cleanStringForHtml($sprachsettings->Teilnehmer['Plural']['Nominativ']) . "listen'>\n";
    }

    if (isset($this->links['filter']) && $this->links['filter']) {
      $result .= '<input type="image" src="layout/button_main_search_filter.gif" title="Filter öffnen" alt="Filter" class="filter filter_hm_kursuebersicht">';
    }

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

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

    return $result;
  }

  
  private function _getDialogMarkieren()
  {
    $sprachsettings = new SprachSettingsObject();

    return '<div id="dialog_markieren">'
            . '<div class="nowrap">'
              . '<input type="image" src="layout/button_markieren_alleg.gif" '
              . 'title="Alle ' . CleanOperations::cleanStringForHtml($sprachsettings->Kurs['Plural']['Nominativ']) . ' markieren" alt="Alle ' . CleanOperations::cleanStringForHtml($sprachsettings->Kurs['Plural']['Nominativ']) . ' markieren" '
              . 'onClick="$(\'#dialog_markieren\').dialog(\'close\'); window.location.href=KuferTools.SelfLink+\'&markerAction=markiereAlle\'" '
              . 'onmouseout="ChangeImg(this,MarkierenAlleGray)" onmousemove="ChangeImg(this,MarkierenAlle)" >'
              . '<input type="image" src="layout/button_markieren_alle_nichtg.gif" '
              . 'title="' . CleanOperations::cleanStringForHtml($sprachsettings->Kurs['Plural']['Nominativ']) . ' nicht markieren" alt="' . CleanOperations::cleanStringForHtml($sprachsettings->Kurs['Plural']['Nominativ']) . ' nicht markieren" '
              . 'onClick="$(\'#dialog_markieren\').dialog(\'close\'); window.location.href=KuferTools.SelfLink+\'&markerAction=loescheMarkierungen\'" '
              . 'onmouseout="ChangeImg(this,MarkierenAlleNichtGray)" onmousemove="ChangeImg(this,MarkierenAlleNicht)" >'
            . '</div>'
            . '<div class="nowrap">'
              . '<input type="image" src="layout/button_markieren_seiteg.gif" '
              . 'title="' . CleanOperations::cleanStringForHtml($sprachsettings->Kurs['Plural']['Nominativ']) . ' dieser Seite markieren" alt="' . CleanOperations::cleanStringForHtml($sprachsettings->Kurs['Plural']['Nominativ']) . ' dieser Seite markieren" '
              . 'onClick="$(\'input.markable\').prop(\'checked\', true); $(\'#dialog_markieren\').dialog(\'close\')" '
              . 'onmouseout="ChangeImg(this,MarkierenSeiteGray)" onmousemove="ChangeImg(this,MarkierenSeite)" >'
              . '<input type="image" src="layout/button_markieren_seite_nichtg.gif" '
              . 'title="' . CleanOperations::cleanStringForHtml($sprachsettings->Kurs['Plural']['Nominativ']) . ' dieser Seite nicht markieren" alt="' . CleanOperations::cleanStringForHtml($sprachsettings->Kurs['Plural']['Nominativ']) . ' dieser Seite nicht markieren" '
              . 'onClick="$(\'input.markable\').prop(\'checked\', false); $(\'#dialog_markieren\').dialog(\'close\')" '
              . 'onmouseout="ChangeImg(this,MarkierenSeiteNichtGray)" onmousemove="ChangeImg(this,MarkierenSeiteNicht)" >'
            . '</div>'
          . '</div>';
  }

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


    return $fileAry;
  }

  public function getJSDeclarations()
  {
    $result = parent::getJSDeclarations();
    $result .= 'registerNamespace("KuferTools");';
    
    $result .= $this->getSelektorKursFelderToDetails();
    $result .= $this->getJSForIds($this->_data);

    $sprachSettings = SettingsFactory::getSettings('SprachSettingsObject');
    $result .= JsOperations::getDeclarationJson('KuferTools.Spracheinstellungen', $sprachSettings);


    
    
    $result .= 'registerNamespace("KuferTools.Filter");';
    $result .= JsOperations::getDeclarationString('KuferTools.Filter.DatumVon', $this->_datumVon);
    $result .= JsOperations::getDeclarationString('KuferTools.Filter.DatumBis', $this->_datumBis);

    $result .= JsOperations::getDeclarationString('KuferTools.Filter.KnrVon', $this->_knrVon);
    $result .= JsOperations::getDeclarationString('KuferTools.Filter.KnrBis', $this->_knrBis);
    $result .= JsOperations::getDeclarationString('KuferTools.Filter.KursTitel', $this->_kursTitel);

    $result .= JsOperations::getDeclarationString('KuferTools.FilterAction', $this->links['self'] . '&markerAction=loescheMarkierungen');

    return $result;
  }
}