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


class DruckListen
{
  
  public $url;
  
  public $listen;
  
  public $selectedList;
  
  public $toolID;

  
  public $changeHL;

  
  public $htmlOperations;
  
  public $toolSectionID;

  
  public $userMayOverrideHL = false;

  public $selektierteIds = '';
  
  protected $dozId = -1;

  
  public function __construct($url, $toolId, $toolSectionID, $changeHL = false, $pUserMayOverrideHL = false, $pDozId = -1)
  {
    $this->url = $url;
    $this->toolID = $toolId;
    $this->toolSectionID = $toolSectionID;
    $this->changeHL = $changeHL;
    $this->userMayOverrideHL = $pUserMayOverrideHL;
    $this->listen = $this->getListAry();
    $this->dozId = $pDozId;


    
    
    
    $selected = GetOperations::getVarValue('liste');
    if (isset($this->listen[$selected])) {
      $this->selectedList = $selected;
    }

    $this->htmlOperations = new HTMLOperations();
  }

  
  public function getListAry()
  {
    
    $sprachsettings = new SprachSettingsObject();

    $kursdatenlisteText = 'Liste Ihrer ' . $sprachsettings->Kurs['Plural']['Genitiv'];
    $kurstagelisteText = 'Liste Ihrer ' . $sprachsettings->Kurstag['Plural']['Genitiv'];
    if ($this->toolID == \ToolsAPI::TOOL_DOZENTENINFO_MA) {
      $kursdatenlisteText = 'Liste der ' . $sprachsettings->Kurs['Plural']['Genitiv'];
      $kurstagelisteText = 'Liste der ' . $sprachsettings->Kurstag['Plural']['Genitiv'];
    }


    return array(
              'Kursdatenliste' => $kursdatenlisteText,
              'Kurstageliste' => $kurstagelisteText,
              'Einsatzplanliste' => 'Einsatzplan',
              'Kursstammblattliste' => $sprachsettings->Kurs['Singular']['Nominativ'] . 'stammblätter',
              'Lehrplanliste' => 'Lehrplanliste');
  }

  
  public function show()
  {
    echo $this->getContent();
  }

  public function getContent($pSuppressBackButtons = false)
  {
    $result = '';

    
    
    if ($this->selectedList) {
      
      $action = $this->changeHL ? 'saveDruckListe.php' : 'printDruckListe.php';
      if ($this->changeHL) {
        $result .= "<form name=\"form_liste\" method=\"POST\" action=\"$action\">";
      } else {
        $result .= "<form name=\"form_liste\" method=\"POST\" action=\"$action\" target=\"_blank\">";
      }

      
      if (GetOperations::getVarValue('fehler') == 'keineEinstellungenInSql') {
        $result .= '<p class="fehlermeldung" style="margin-bottom:1em;">Die Einstellungen konnten nicht kopiert werden, weil sie in ' . KUFERSQL . ' nicht vorhanden sind.</p>';
      }

      
      
      if (!$this->changeHL) {
        $result .= $this->htmlOperations->CreateInputTag('selnr', 'hidden', '', '', ($this->selektierteIds != '' ? $this->selektierteIds : GetOperations::getVarValue('selnr', SessionOperations::getVarValueWTIntern('selnrstr_'))));

        
        
        
        
        

        if ($this->dozId != -1) {
          $result .= $this->htmlOperations->CreateInputTag('dnr', 'hidden', '', '', $this->dozId);
        }

        $result .= $this->_getHiddenInput4Zahlung();
      }
      $result .= $this->htmlOperations->CreateInputTag('toolid', 'hidden', '', '', $this->toolID);


      
      if ($this->changeHL) {
        $result .= $this->htmlOperations->CreateInputTag('sightid', 'hidden', '', '', GetOperations::getVarValue('sightid'));
        $result .= $this->htmlOperations->CreateInputTag('section', 'hidden', '', '', GetOperations::getVarValue('section'));
      }
      $result .= $this->_getListen();
      $result .= $this->_getOptions();

      $result .= '<div style="clear: both;">';
      

      if ($this->changeHL) {
        $result .= $this->htmlOperations->CreateInputTag('', 'image', 'speichern', '', '', 'src="layout/button_save.gif"');
      } else {
        $result .= $this->htmlOperations->CreateInputTag('', 'image', 'drucken', '', '', 'src="layout/button_druck.gif"');

        
        $result .= "<a href='javascript:history.back()'>";
        $result .= "<img border='0' class='zurueck' src='layout/button_close_small.gif' alt='Zur&uuml;ck' title='Zur&uuml;ck' />";
        $result .= "</a>\n";
      }

      
      if ($this->changeHL) {
        $sight = GetOperations::getVarValue('sightid');
        $section = GetOperations::getVarValue('section');
        
        
        if ($this->selectedList == 'KursvertragSonstiges' || $this->selectedList == 'KurssammelvertragSonstiges'
            
          || ($this->selectedList == 'FinanzamtVorlage')) {
          $result .= "<a href=\"saveDruckListe.php?copy=$this->selectedList&toolid=" . CleanOperations::cleanInt($this->toolID) . '&sightid=' . CleanOperations::cleanInt($sight) . '">';
          $result .= HTMLOperations::createButtonSpan('Von ' . KUFERSQL . ' kopieren', 'Von ' . KUFERSQL . ' kopieren', '', 'copysql', 'images/copy_pdf_settings.gif');
          
          $result .= '</a>';
        } 
        elseif ($this->selectedList == 'TNWochenplangrafik') {
          $result .= "<a href=\"saveDruckListe.php?standardliste=$this->selectedList&toolid=" . CleanOperations::cleanInt($this->toolID) . '&sightid=' . CleanOperations::cleanInt($sight) . '">';
          $result .= HTMLOperations::CreateImageTag('layout/button_standard.gif', 'Standard herstellen', 'standard', 'buttons');
          $result .= '</a>';
        }
      } 

      $result .= '</div>';
      $result .= '</form>';
    } else {
      
      if ($this->changeHL) {
        
        
        
        $result .= $this->_getListen();
      } else {
        $result .= $this->_getListenFast();
        if (!$pSuppressBackButtons) {
          
          $seitenr = (SessionOperations::getVarValueWTIntern('aktseite') != 1) ? '&seitenr=' . SessionOperations::getVarValueWTIntern('aktseite') : '';
          $result .= Menu::getOverviewLink(GetOperations::getVarValue('tool_id'), $this->getUrlSearchString() . $seitenr, '');
          $result .= Menu::getOverviewLink(GetOperations::getVarValue('tool_id'), '', 'zurueck_startseite');
        }
      }
    }

    return $result;
  }

  
  public function getUrlSearchString()
  {
    return '&searchkurs=1';
  }

  
  protected function _getHiddenInput4Zahlung()
  {
    return '';
  }

  
  protected function getUrlString4ZahlungsKnr()
  {
    return '';
  }

  
  protected function getUrlString4AllNrAry()
  {
    return '';
  }

  
  public function _getFastHeader()
  {
    $sprachsettings = new SprachSettingsObject();
    
    
    $headertext = 'Listen für Ihre ' . $sprachsettings->Kurs['Plural']['Nominativ'];
    if ($this->toolID == \ToolsAPI::TOOL_DOZENTENINFO_MA) {
      $headertext = 'Listen der ' . $sprachsettings->Kurs['Plural']['Nominativ']
                    . ' ' . $sprachsettings->Dozent['Singular_mit_Artikel']['Genitiv'];
    }
    
    return '<h1>' . CleanOperations::cleanStringForHtml($headertext) . ':</h1>';
  }

  
  public function _getListenFast()
  {
    $result = $this->_getFastHeader();
    $result .= '<ul style="list-style-type: none;">';
    $nolist = true;

    
    
    $urlPart4Dnr = '';
    if ($this->dozId != -1) {
      $urlPart4Dnr = '&dnr=' . CleanOperations::cleanInt($this->dozId);
    }
    
    $urlPart4ZahlungsKnr = $this->getUrlString4ZahlungsKnr();
    
    $url4AllNrAry = $this->getUrlString4AllNrAry();

    $selektierteIds = ($this->selektierteIds != '' ? $this->selektierteIds : SessionOperations::getVarValueWTIntern('selnrstr_'));

    foreach ($this->listen as $liste => $name) {
      $listallowed = $this->getListenrechte($liste);

      if ($listallowed['druck']) {
        $nolist = false;
        $result .= '<li class="listfast">';
        $result .= '<div class="listfast ' . CleanOperations::cleanStringForHtml($this->_getListFastClassName()) . '">' . CleanOperations::cleanStringForHtml($name) . '</div>';
        $result .= '<a href="printDruckListe.php?fastprint=' . CleanOperations::cleanStringForHtml($liste) . '&toolid=' . CleanOperations::cleanInt($this->toolID) . $urlPart4Dnr . '&selnr=' . CleanOperations::cleanStringForHtml($selektierteIds)
                     . $urlPart4ZahlungsKnr . "\" target='_blank'>"
               . $this->htmlOperations->CreateImageTag('layout/button_druck_small.gif', 'Liste drucken', '', 'buttons drucklisten small')
               . '</a> ';

        
        $toolSettings = ToolSettingsAPI::getToolSettings(PostOperations::getVarValue('tool_id'));
        if (isset($toolSettings->enableDozHatListeGedrucktButton) && $toolSettings->enableDozHatListeGedrucktButton && $listallowed['info']) {
          $paramAry = StringOperations::getUrlParams($this->url);
          $params = '';
          foreach ($paramAry as $param => $val) {
            $params .= CleanOperations::cleanStringForHtml($param) . '=' . CleanOperations::cleanStringForHtml($val) . '&';
          }


          $url = 'usertools.php?' . $params . 'infoAnVHS=1&liste=' . CleanOperations::cleanStringForHtml($liste) . '&selnr=' . CleanOperations::cleanStringForHtml($this->selektierteIds != '' ? $this->selektierteIds : SessionOperations::getVarValueWTIntern('selnrstr_'));
          $result .= '<span class="druckliste_vhs_info"><input type="hidden" class="druckliste_vhs_info_link" value="' . CleanOperations::cleanStringForHtml($url) . '">';
          $result .= HTMLOperations::createButtonSpan(
            CleanOperations::cleanStringForHtml($toolSettings->dozHatListeGeduruckt_beschriftung),
            CleanOperations::cleanStringForHtml($toolSettings->dozHatListeGeduruckt_beschreibung)
          );
          $result .= '</span>';
        }

        
        
        if ($this->userMayOverrideHL && $listallowed['einstell'] && $listallowed['druck']) {
          
          if (CodingOperations::areInternalAndExternalEncodingEqual()) {
            
            $urlObj = new URL('www.dummy.de/' . $this->url);
            $selnr = $urlObj->getParameterValue('selnr');
            $encode = new EncodeCharsetFromTo(CodingOperations::getInternalEncoding(), UTF_8);
            $urlObj->setParameterValue('selnr', urlencode($encode->encode($selnr)));
            $url = $urlObj->getUrlRelative();
          } else {
            $url = $this->url;
          }
          $result .= '<a href="' . CleanOperations::cleanStringForHtml($url . $url4AllNrAry) . '&liste=' . CleanOperations::cleanStringForHtml($liste) . '">'
                 . $this->htmlOperations->CreateImageTag('layout/button_einstellungen.gif', 'Einstellungen', '', 'buttons drucklisten')
                 . '</a> ';
        }
        $result .= '</li>';
      }
    } 
    if ($nolist) {
      $sprachsettings = new SprachSettingsObject();
      $result .= '<li class="nolist">Keine Liste zur Auswahl verfügbar. Bitte wenden Sie sich an Ihren Administrator, um die Rechte für das Ausdrucken Ihrer ' . CleanOperations::cleanStringForHtml($sprachsettings->Teilnehmer['Singular']['Nominativ']) . 'listen zu setzen.</li>';
    }
    $result .= '</ul>';

    return $result;
  }

  
  public function getListenrechte($pListe)
  {
    $druckallowed = 0;
    $einstellallowed = 0;
    $infoallowed = 0; 
    
    $sectID = $this->toolSectionID;
    switch ($pListe) {
      case 'Kursdatenliste':
        $druckallowed = \SecurityOperations::privilegAllowedCurrentUser($this->toolID, $sectID . '.0.2') > 1;
        $einstellallowed = \SecurityOperations::privilegAllowedCurrentUser($this->toolID, $sectID . '.0.3') > 1;
        $infoallowed = \SecurityOperations::privilegAllowedCurrentUser($this->toolID, $sectID . '.2.2') > 1;
        break;
      case 'Kurstageliste':
        $druckallowed = \SecurityOperations::privilegAllowedCurrentUser($this->toolID, $sectID . '.0.4') > 1;
        $einstellallowed = \SecurityOperations::privilegAllowedCurrentUser($this->toolID, $sectID . '.0.5') > 1;
        $infoallowed = \SecurityOperations::privilegAllowedCurrentUser($this->toolID, $sectID . '.2.3') > 1;
        break;
      case 'Einsatzplanliste':
        $druckallowed = \SecurityOperations::privilegAllowedCurrentUser($this->toolID, $sectID . '.0.6') > 1;
        $einstellallowed = \SecurityOperations::privilegAllowedCurrentUser($this->toolID, $sectID . '.0.7') > 1;
        $infoallowed = \SecurityOperations::privilegAllowedCurrentUser($this->toolID, $sectID . '.2.4') > 1;
        break;
      case 'Kursstammblattliste':
        $druckallowed = \SecurityOperations::privilegAllowedCurrentUser($this->toolID, $sectID . '.0.8') > 1;
        $einstellallowed = \SecurityOperations::privilegAllowedCurrentUser($this->toolID, $sectID . '.0.9') > 1;
        $infoallowed = \SecurityOperations::privilegAllowedCurrentUser($this->toolID, $sectID . '.2.5') > 1;
        break;
      case 'Lehrplanliste':
        $druckallowed = \SecurityOperations::privilegAllowedCurrentUser($this->toolID, $sectID . '.0.10') > 1;
        $einstellallowed = \SecurityOperations::privilegAllowedCurrentUser($this->toolID, $sectID . '.0.11') > 1;
        $infoallowed = \SecurityOperations::privilegAllowedCurrentUser($this->toolID, $sectID . '.2.6') > 1;
    }
    if (\SecurityOperations::privilegAllowedCurrentUser($this->toolID, $sectID . '.2.1') <= 1) {
      $infoallowed = 0;
    }

    return array('druck' => $druckallowed, 'einstell' => $einstellallowed, 'info' => $infoallowed);
  }

  

  
  public function _getListen()
  {
    
    $onchange = "'" . CleanOperations::cleanStringForHtml($this->url) . "&liste=' + this.options[this.options.selectedIndex].value";

    $result = "<select id=\"drucklistenart\" name=\"art\" onchange=\"document.location.href = $onchange\">";

    $result .= '<option value="">Bitte eine Druck-Liste auswählen...</option>';

    foreach ($this->listen as $liste => $name) {
      $listallowed = $this->getListenrechte($liste);
      if ($listallowed['druck'] && $listallowed['einstell']) {
        if ($liste == $this->selectedList) {
          $selected = $this->htmlOperations->getEmptyAttribute('selected');
        } else {
          $selected = '';
        }

        $result .= '<option value="' . CleanOperations::cleanStringForHtml($liste) . "\" $selected>" . CleanOperations::cleanStringForHtml($name) . '</option>';
      }
    }

    $result .= '</select>';

    
    if (!$this->changeHL) {
      $result .= $this->htmlOperations->CreateInputTag('', 'image', 'drucken_float', '', '', 'src="layout/button_druck.gif"');


      $result .= "<a href='javascript:history.back()'>";
      $result .= "<img border='0' class='zurueck' src='layout/button_close_small.gif' alt='Zur&uuml;ck' title='Zur&uuml;ck' />";
      $result .= "</a>\n";
    }

    return $result;
  }

  
  public function _getOptions()
  {
    $selnr = GetOperations::getVarValue('selnr');
    if (!$this->changeHL && !$selnr) { 
      die();
    }

    $obj = DruckListeFactory::create($this->selectedList, $this->toolID, $this->changeHL ? false : $selnr);
    if (!$obj) {
      $fehlerText = 'Schwerer Fehler: Liste ' . CleanOperations::cleanStringForHtml($this->selectedList) . ' konnte nicht erzeugt werden!';
      Logger::logg($fehlerText, 110);
      die('<div class="warnung">' . $fehlerText . '</div>');
    }

    
    
    
    $obj->loadSettings();

    $einstellung = $obj->_getEinstellDarstellung();
    $ueberschrift = $this->_getUeberschrift($obj);
    

    $subtitle = $obj->_getSubtitle();
    $endbemerkung = $obj->_getEndbemerkung();
    
    $druckbereich = $this->_getDruckbereich($obj);

    
    $unterschriftSettings = $obj->getVertragListeDarstellung($this->toolID, $this->changeHL);

    $kurstage = $obj->_getKurstage($this->changeHL);
    
    $zeitbereich = $obj->_getZeitbereich($this->changeHL);
    $hausmeisterOptionen = $obj->_getHausmeisterOptionen($this->changeHL);
    $kalenderwochenplan = $obj->_getKalenderWochenPlan($this->changeHL);
    $kalendermonatsplan = $obj->_getKalenderMonatsPlan($this->changeHL);

    
    $zusatzspalten = $obj->_getZusatzspalten();

    $ausfallEinstellungen = $obj->_getAusfallEinstellungen();

    $retour = '';

    if ($einstellung != '') {
      $retour .= "<div>$einstellung</div>";
    }
    $retour .= "<div>$ueberschrift</div>"
                . "<div>$subtitle</div>";
    if ($endbemerkung != '') {
      $retour .= "<div>$endbemerkung</div>";
    }
    if ($zusatzspalten != '') {
      $retour .= "<div>$zusatzspalten</div>";
    }
    $retour .= '<div>';
    if ($kurstage != '') {
      $retour .= $kurstage;
    }
    $retour .= $druckbereich . '</div>';

    $retour .= $unterschriftSettings;
    
    
    if ($zeitbereich != '') {
      $retour .= "<div>$zeitbereich</div>";
    }
    if ($ausfallEinstellungen != '') {
      $retour .= "<div>$ausfallEinstellungen</div>";
    }
    if ($hausmeisterOptionen != '') {
      $retour .= "<div>$hausmeisterOptionen</div>";
    }
    if ($kalenderwochenplan != '') {
      $retour .= "<div>$kalenderwochenplan</div>";
    }
    if ($kalendermonatsplan != '') {
      $retour .= "<div>$kalendermonatsplan</div>";
    }

    return $retour;
  }

  
  public function _getDruckbereich($liste)
  {
    $labelHochformat = $liste->getLabel4Hochformat();
    $indivHochformat = $liste->getIndiv4InputHochformat();
    $indivQuerformat = $liste->getIndiv4InputQuerformat();

    $result = '<fieldset id="druckausgabe"><legend>Druckausgabe</legend>';
    $disabled = !$liste->getDruckbereichEnabled('hochformat');
    $result .= $this->htmlOperations->CreateInputTag('format', 'radio', 'hochformat', '', 'H', $indivHochformat, $liste->settings->druckbereich->hochformat, $disabled);
    $result .= '<label for="hochformat">' . $labelHochformat . '</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
    $disabled = !$liste->getDruckbereichEnabled('querformat');
    $result .= $this->htmlOperations->CreateInputTag('format', 'radio', 'querformat', '', 'Q', $indivQuerformat, !$liste->settings->druckbereich->hochformat, $disabled);
    $result .= '<label for="querformat">Querformat</label>';
    $result .= BR_WT;
    $result .= '</fieldset>';

    return $result;
  }

  
  public function _getUeberschrift($liste)
  {
    $result = '<fieldset id="f_ueberschrift"><legend>Überschrift</legend>';
    $result .= '<label for="ueberschrift">Überschrift:</label>';
    $result .= $this->htmlOperations->CreateInputTag('ueberschrift', 'text', 'ueberschrift', '', $liste->settings->title1);
    $result .= '</fieldset>';

    return $result;
  }

  
  public static function ausgabeNoDruckliste($p_Listen, $pMitBackButton = true)
  {
    $result = "<center>\n"
          . "<p style='font-family: arial; color: #FF0000;'>Die Druckausgabe für die " . CleanOperations::cleanStringForHtml($p_Listen) . " kann nicht erstellt werden. Bitte treffen Sie eine Auswahl!</p>\n";
    if ($pMitBackButton) {
      $result .= "<p><a href='javascript:window.history.back();'><img class='zurueck' border='0' title='Zurück' alt='Zurück'  src='layout/button_close_small.gif' /></a> <p>\n";
    }
    $result .= '</center>';

    return $result;
  }

  
  public static function ausgabeNoSingleDruckliste($p_Liste, $pMitCloseButton = true)
  {
    $result = "<center>\n"
        . "<p style='font-family: arial; color: #FF0000;'>Für die Liste " . CleanOperations::cleanStringForHtml($p_Liste) . " wurden für diese Auswahl keine Daten gefunden!</p>\n";
    if ($pMitCloseButton) {
      $button = self::getCloseButton();
      $result .= "<p><a href='javascript:window.close();'>$button</a> <p>\n";
    }
    $result .= '</center>';

    return StringOperations::utf8_encode($result);
  }

  public static function getCloseButton()
  {
    return "<img class='zurueck' border='0' title='Schließen' alt='Schließen' src='layout/button_close.gif' />";
  }

  protected function _getListFastClassName()
  {
    return 'listen';
  }
}