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

namespace Kufer\KuferTools\Mitarbeiter\DozentenInfo\Detailsreiter\Kurse\PdfListen\DruckListen\ShowSettingsDruckListen;


class ShowSettingsDruckListenUseCase extends \Kufer\KuferTools\Basis\UseCase\ToolUseCase
{
  public function __construct($pRequest)
  {
    
    parent::__construct($pRequest, $this->_getListSettingsResponseObj());
  }

  

  
  protected function _getListSettingsResponseObj()
  {
    return new ShowSettingsDruckListenResponse();
  }

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

  public static function getToolSectionId()
  {
    return 404;
  }

  
  public function hasAccess($pToolSectionId = null)
  {
    $accessZurToolSection = parent::hasAccess();
    if (!$accessZurToolSection) {
      return false;
    }

    
    
    $selected = $this->request->listName;
    $pdfListen = $this->_getPdfDrucklistenObj();
    $allowedLists = $pdfListen->getListAry();

    if (isset($allowedLists[$selected])) {
      return true;
    }

    return false;
  }

  

  
  protected function _getPdfDrucklistenObj()
  {
    $userId = $this->getUser()->getIdentifier();

    return new \Kufer\KuferTools\Basis\PdfListen\DruckPdfListen(self::getToolId(), self::getToolSectionId(), $userId);
  }

  
  public function action()
  {
    $kursNrStr = implode(':', $this->request->kursIds);

    
    $generateList = true;
    $listObj = \DruckListeFactory::create($this->request->listName, self::getToolId(), $kursNrStr);
    if (!$listObj) {
      $generateList = false;
      $fehlerText = 'Schwerer Fehler: Liste ' . $this->request->listName . ' konnte nicht erzeugt werden.';
      \Logger::logg($fehlerText, 110);
    }

    
    
    $dataModulList = new \Datamodul();
    $dataModulList->name = $this->request->listName; 

    return $this->getResponse($dataModulList, $generateList, $kursNrStr, $listObj);
  }

  

  
  protected function getResponse($pDataModulList, $pGenerateList, $pKursNrStr, $pListObj)
  {
    
    $this->response->name = $pDataModulList->name; 
    $this->response->generateList = $pGenerateList;
    
    if (!empty($pKursNrStr) && is_object($pListObj)) {

       
      $pdfListen = $this->_getPdfDrucklistenObj();
      $listen = $pdfListen->getListAry();

      
      
      $pDataModulList->bezeichnung = $listen[$pDataModulList->name]; 
      $pDataModulList->selNr = $pKursNrStr;  
      $pDataModulList->toolId = $this->getToolId();
      $pDataModulList->dnr = $this->request->dnr;

      
      $pListObj->loadSettings();

      

      
      
      $this->getKursTag4Response($pListObj);

      
      
      
      $isHochformat = $pListObj->settings->druckbereich->hochformat;
      $enabledHoch = $pListObj->getDruckbereichEnabled('hochformat');
      $enabledQuer = $pListObj->getDruckbereichEnabled('querformat');
      $this->response->druckBereichFormatDisabled = array(!$enabledHoch, !$enabledQuer);
      $this->response->druckBereichFormatChecked = array($isHochformat, !$isHochformat);

      
      $this->response->ueberschrift = trim($pListObj->settings->title1);
      
      $this->response->subtitle = trim($pListObj->settings->title2);
      
      $this->response->endbemerkung = trim($pListObj->settings->endbem);

      
      $this->getVertraegeSettings4response($pListObj);

      
      $this->getStammblattSettings4Response($pListObj);

      
      $this->getLehrplanSettings4Response($pListObj);
    } 

    
    $this->response->datamodulListObj = $pDataModulList;

    return $this->response;
  }

  

  
  private function getVertraegeSettings4response($pListObj)
  {
    $this->response->useUnterschrift = false;

    if ($pListObj->showVertragUnterschrift()) {
      $this->response->useUnterschrift = true;
      $this->response->vertraglisteName = $pListObj->settings->vertraglisteName;
      $this->response->vertraglisteBezeichnung = $pListObj->settings->vertraglisteBezeichnung;
      $this->response->vertraglisteTitel = $pListObj->settings->vertraglisteTitel;
    }
  }

  
  protected function getStammblattSettings4Response($pListObj)
  {
    $this->response->useStammblatt = false;

    if ($this->canShowKursStammblatt()) {
      $this->response->useStammblatt = true;

      
      $vars = get_object_vars($pListObj->settings->einstellDarstellung);
      $this->response->einstellOptionChecked = $pListObj->settings->einstellDarstellung;

      
      $enabledAry = array();
      $checkedAry = array();
      $i = 0;
      
      foreach ($vars as $name => $value) {
        
        $enabledAry[$i] = true;
        $checkedAry[$i] = $pListObj->settings->einstellDarstellung->{$name};
        $i++;
      }
      
      $enabledAry[1] = $checkedAry[0];
      $this->response->einstellOptionChecked = $checkedAry;
      $this->response->einstellOptionEnabled = $enabledAry;
    }
  }

  

  
  protected function getLehrplanSettings4Response($pListObj)
  {
    $this->response->useLehrplan = false;
    
    if ($this->canShowLehrplan()) {
      $this->response->useLehrplan = true;

      
      $vars = get_object_vars($pListObj->settings->einstellDarstellung);
      $this->response->einstellOptionChecked = $pListObj->settings->einstellDarstellung;

      
      $enabledAry = array();
      $checkedAry = array();
      $i = 0;
      
      foreach ($vars as $name => $value) {
        
        $enabledAry[$i] = true;
        $checkedAry[$i] = $pListObj->settings->einstellDarstellung->{$name};
        $i++;
      }

      
      
      if (!$checkedAry[2] && $pListObj->settings->druckbereich->hochformat) {
        $enabledAry[5] = false;
      }

      $this->response->einstellOptionChecked = $checkedAry;
      $this->response->einstellOptionEnabled = $enabledAry;
    } 
  }

  

  
  protected function getKursTag4Response($pListObj)
  {
    $this->response->useKurstage = false;
    if ($this->canShowKurstageFields()) {
      
      
      $this->response->kursTageVon = $pListObj->kurstage->von;
      $this->response->kursTageBis = $pListObj->kurstage->bis;
      
      $this->response->kursTageDisabled = !$pListObj->getKurstageEnabled();
      
      $this->response->useKurstage = true;
    }
  }

  

  
  protected function canShowKurstageFields()
  {
    return true;
  }

  protected function canShowKursStammblatt()
  {
    return $this->response->name === 'Kursstammblattliste';
  }

  protected function canShowLehrplan()
  {
    return $this->response->name === 'Lehrplanliste';
  }
} 


class ShowSettingsDruckListenRequest
{
  public $kursIds;
  public $listName;
  public $dnr;
}


class ShowSettingsDruckListenResponse extends \Kufer\KuferTools\Basis\UseCase\ToolResponse
{
  
  public $generateList;
  
  public $datamodulListObj;

  
  public $ueberschrift;
  public $subtitle;
  public $endbemerkung;

  
  public $druckBereichFormatChecked;
  public $druckBereichFormatDisabled;

  
  public $kursTageVon;
  public $kursTageBis;
  public $kursTageDisabled;
  public $useKurstage;

  
  public $einstellOptionEnabled;
  public $einstellOptionChecked;

  
  public $vertraglisteName;
  public $vertraglisteBezeichnung;
  public $vertraglisteTitel;
  public $useUnterschrift;
}