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

namespace Kufer\KuferTools\Basis\PdfListen\PrintPdfListen;


abstract class PrintPdfListenUseCase extends \Kufer\KuferTools\Basis\UseCase\ToolUseCase
{
  protected $listObj;
  protected $_printAllowed4User;
  protected $dnr;

  public function __construct($pRequest)
  {
    
    parent::__construct($pRequest, $this->getPrintListenResponse());
    
    $this->listObj = $this->_getPdfListe();

    
    $this->_printAllowed4User = $this->_isPrintAllowed();
    
    $this->dnr = $pRequest->dnr; 
  }

  

  
  protected function _getPdfListe()
  {
    $result = \DruckListeFactory::create($this->request->listName, $this->getToolId(), $this->request->id, $this->request->id);
    if ($result === false) {
      die('Fehler: Liste ' . $this->request->listName . ' existiert nicht');
    }

    return $result;
  }

  
  protected function _isPrintAllowed()
  {
    return $this->listObj !== false && $this->listObj->isAllowedToPrint($this->getToolId());
  }

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

    return new \Kufer\KuferTools\Basis\PdfListen\DruckPdfListen($this->getToolId(), $this->getToolSectionId(), $userId);
  }

  protected function getPrintListenResponse()
  {
    return new PrintPdfListenResponse();
  }

  
  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 false;
    }

    
    if (!$this->listObj) {
      $fehlerText = 'Schwerer Fehler: Liste ' . $this->request->listName . ' konnte nicht erzeugt werden.';
      \Logger::logg($fehlerText, 110);

      return false;
    }

    return true;
  }

  

  public function action()
  {

    
    if (!$this->_printAllowed4User) {
      $fehlerText = ' Keine Zugriffsberechtigung auf einen Kurs oder mehrere Kurse für Liste ' . $this->request->listName . '.';
      \Logger::logg($fehlerText, 120);
    } else {
      $this->listObj->loadSettings();
    }

    return $this->getResponse();
  }

  protected function getResponseStrategies()
  {
    return array(
        new \Kufer\KuferTools\Utils\ResponseStrategies\ResponseMitRahmen($this)
    );
  }

  
  protected function getResponse()
  {
    $this->handleResponseStrategies();
    
    $this->response->listObj = $this->listObj;
    $this->response->printAllowed4User = $this->_printAllowed4User;
    $this->response->dnr = $this->dnr;

    return $this->response;
  }
} 

class PrintPdfListenRequest
{
  public $id;
  public $listName;
  public $dnr;
}


class PrintPdfListenResponse extends \Kufer\KuferTools\Basis\UseCase\ToolResponse
{
  public $listObj;
  public $printAllowed4User;
  public $dnr;
  public $postError = false;
  public $sendInfoSuccess;
  public $menuPunkte;
}