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

namespace Kufer\KuferTools\Basis\DozentenInfo\DozentenUebersicht;


abstract class DozentenUebersichtUseCase extends \Kufer\KuferTools\Basis\Uebersicht\UebersichtMitWssUseCase
{
  public function __construct($request)
  {
    parent::__construct($request, new DozentenUebersichtResponse());

    if (isset($this->request->suchParameter)) {
      $searchAPI = new \Kufer\Web\KuferTools\Utils\FilterSearch\DozentenSearchFilter();
      $searchAPI->setSearchMapToSession($this->request->suchParameter, 'tool' . $this->getToolId());
    }
  }

  
  abstract protected function getDozentenHelper();

  protected function vorAction()
  {
    $this->resetSessionVars();
  }

  private function resetSessionVars()
  {
    \SessionOperations::setVarValueWTIntern('newDozStatusCode', '');
    \SessionOperations::setVarValueWTIntern('newDozWrongFields', '');
  }

  protected function getDatamoduleProSeite()
  {
    return $this->toolSettings->datensaetzeproseite > 0 ? $this->toolSettings->datensaetzeproseite : 6;
  }

  
  protected function getAlleIds()
  {
    return $this->getDozentenHelper()->getIdsMitFilter($this->getToolId());
  }

  protected function getUeberschrift()
  {
    return 'Übersicht der ' . $this->response->sprachSettings->Dozent['Plural']['Genitiv'];
  }

  protected function getButtons()
  {
    $buttonFactory = new \Kufer\KuferTools\Basis\Button\ButtonFactory();
    $buttons = array();
    $allgSettings = \AllgSettingsAPI::getAllgSettings();

    
    if ($this->isNeuerDozentAllowed() && ((!\SessionOperations::isReplikat()) || $allgSettings->dmzEnabled)) {
      $buttons[] = array($buttonFactory->getNeuerPersonenButton('neuer Dozent', 'newDozent', 'neuer Dozent'));
    }

    $anzahlEintraege = count($this->response->dozenten);
    
    $this->addFilterButton($buttons, $buttonFactory, $anzahlEintraege, 'Filter', 'suchen_button_doz', 'reset_filter_button_doz');

    return $buttons;
  }

  
  abstract protected function isNeuerDozentAllowed();

  protected function isFilterGesetzt()
  {
    $suchApi = new \Kufer\Web\KuferTools\Utils\FilterSearch\DozentenSearchFilter();

    return !$suchApi->isSearchMapFromSessionEmpty('tool' . $this->getToolId());
  }

  protected function verlinkePanelHeaderAllowed()
  {
    return $this->isRechtAllowed('54.0.1') == 2
                                     && \ToolsAPI::rechteOR(array('397.1.1', '397.1.3', '397.1.4'), $this->getToolId());
  }

  protected function getResponse()
  {
    parent::getResponse();

    $url = \URL::loadCurrent();
    $persErweiterungStrategy = new \Kufer\KuferTools\Utils\PersonenErweiternStrategies\ErweitereDozentStrategy();
    $dozenten = $persErweiterungStrategy->fillAll($this->response->dozenten, $this->getToolId(), '', $url->getUrlRelative(), 'Zurück');
    $this->response->dozenten = $dozenten;

    $this->response->datamodule = $this->response->dozenten;
    $this->response->newDozStatusCode = $this->request->newDozStatusCode ?? '';
    $this->response->newDozWrongFields = $this->request->newDozWrongFields ?? array();

    $this->response->uebersichtAlsTabelle = isset($this->toolSettings->dozentenUebersichtAlsTabelle) && $this->toolSettings->dozentenUebersichtAlsTabelle;

    return $this->response;
  }

  protected function getDatamodulOnCurrentPage()
  {
    return new \Kufer\KuferTools\Utils\ResponseStrategies\ResponseDozentenOnCurrentPage($this);
  }
}

class DozentenUebersichtRequest extends \Kufer\KuferTools\Basis\Uebersicht\UebersichtMitWssRequest
{
  public $newDozStatusCode;
  public $newDozWrongFields;
}

class DozentenUebersichtResponse extends \Kufer\KuferTools\Basis\Uebersicht\UebersichtMitWssResponse
{
  public $dozenten = array();
  public $newDozStatusCode;
  public $newDozWrongFields;
  public $uebersichtAlsTabelle;
}