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

namespace Kufer\KuferTools\Firma\KursUebersicht\TermineUebersicht;


class TermineUebersichtUseCase extends \Kufer\Web\KuferTools\Basis\KursUebersicht\TermineUebersicht\TermineUebersichtUseCase
{
  private $dbwKurstage;

  public function __construct($request)
  {
    parent::__construct($request, new TermineUebersichtResponse());
    $this->dbwKurstage = new \DBWorkKurseTageWT();
  }

  public static function getToolSectionId()
  {
    return 313;
  }

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

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

  private function initDbwKurstage()
  {
    $searchAPI = new \KursplanungKurstagSucheAPI();
    if (isset($this->request->suchParameter)) {
      $searchAPI->setSearchParamsToSession($this->request->suchParameter);
    }
    $this->dbwKurstage->newSearch();
    $this->dbwKurstage->fromobject = $searchAPI->getFromObjectBySession($this->dbwKurstage->fromobject, 'tool' . $this->getToolId());
    $this->dbwKurstage->toobject = $searchAPI->getToObjectBySession($this->dbwKurstage->toobject, 'tool' . $this->getToolId());
    $belegungshelper = new \Kufer\KuferTools\Utils\DatamodulHelper\BelegungsHelper\FirmaBelegungen($this->toolSettings, $this->getUser()->getTnrIdentifier());
    $dbwBelegungen = $belegungshelper->getFilter($this->getToolId());
    $this->dbwKurstage->fromobject->kurs = $dbwBelegungen->fromobject->kurs;
    $this->dbwKurstage->toobject->kurs = $dbwBelegungen->toobject->kurs;

    $this->dbwKurstage->orderby = 'datum';
    $this->dbwKurstage->setOrderAktuellesSemester($this->toolSettings);

    if ($this->toolSettings->versteckeKurstageWieImRaumanzeigeprogramm) {
      $this->dbwKurstage->fromobject->ranzprognoanz = true;
    }

    $this->dbwKurstage->fromobject->ausfall = $this->getKtStatusErlaubt();
  }

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

    if ($this->isRechtAllowed('313.0.5') && $this->datensaetzeVorhanden()) {
      $buttons[] = array($buttonFactory->getTerminPlanButton('Terminplan', 'terminplan_kt'));
    }
    if ($this->isRechtAllowed('313.0.1') && $this->datensaetzeVorhanden()) {
      $user = $this->getUser();
      
      $kursNrAry = $this->dbwKurstage->getKurstageNrByTn($user->tnr);
      
      $joinedKnrAry = implode(':', $kursNrAry);
      $count = count($this->response->kurstage);
      $vondat = $this->response->kurstage[0]->datum;
      $bisdat = $this->response->kurstage[$count - 1]->datum;
      $buttons[] = array($buttonFactory->getTermineIcsButton(
        'Termine nach Outlook',
        'alleMeineTermine_terminenachoutlooklink',
        'index.php?tool_id=21&action=icsKalender&von=' . $vondat . '&bis=' . $bisdat . '&terminnr=' . $joinedKnrAry
      ));
    }
    if ($this->isRechtAllowed('313.0.2')) {
      $buttons[] = array($buttonFactory->getFilterButton('Filter', 'filter_alletermine'));
    }

    return $buttons;
  }

  protected function checkboxNeeded()
  {
    return ($this->isRechtAllowed('313.0.5')); 
  }

  protected function verlinkePanelHeaderAllowed()
  {
    return ($this->isRechtAllowed('313.0.4')
            && \ToolsAPI::rechteOR(array('336.1.1', '336.1.2', '336.1.3', '336.1.4', '336.1.5', '336.1.6', '336.1.7'), $this->getToolId()));
  }

  protected function getUeberschrift()
  {
    return 'meine Termine';
  }

  
  protected function getDatamoduleProSeite()
  {
    return $this->toolSettings->kurstageproseite;
  }

  
  protected function getAlleIds()
  {
    $user = $this->getUser();
    $searchAPI = new \KurssuchAPIwSearch(false);
    if (isset($this->request->suchParameter)) {
      $searchAPI->getSearchParamsFromReqToSession('tool' . $this->getToolId());
    }
    $belegungshelper = new \Kufer\KuferTools\Utils\DatamodulHelper\BelegungsHelper\FirmaTermineBelegungen($this->toolSettings, $user->tnr);
    $belegungen = $belegungshelper->getDatamoduleMitFilter($this->getToolId());
    $kursNrAry = \ArrayOperations::mapAttribut($belegungen, 'knr');
    $kursNrAry = array_unique($kursNrAry);

    if (count($kursNrAry) == 0) {
      $alleIds = array();
    } else {
      
      $alleIds = $this->dbwKurstage->getKurstageIdsByTNOnlyDifferent($user->tnr, $kursNrAry);
    }

    return $alleIds;
  }

  private function getKtStatusErlaubt()
  {
    $ktStatus = $this->toolSettings->ktstatus;
    if (\trim($ktStatus) === '') {
      $alleStatus = \ConstantOperations::getKurstagStatus4EinschraenkungAufKTStatus();
      $kuerzel = \array_keys($alleStatus);
      $ktStatus = \implode('', $kuerzel);
    }

    return $ktStatus;
  }

  
  protected function getResponse()
  {
    parent::getResponse();
    
    $this->response->datamodule = $this->response->kurstage;
    $this->response->vonDatum = $this->request->vonDatum;
    $this->response->bisDatum = $this->request->bisDatum;
    $this->response->uebersichtAlsTabelle = isset($this->toolSettings->kurstageUebersichtAlsTabelle) && $this->toolSettings->kurstageUebersichtAlsTabelle;


    return $this->response;
  }

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

  protected function getSeiteSessionPrefix()
  {
    return 'seiteTermine_tool';
  }
}

class TermineUebersichtRequest extends \Kufer\KuferTools\Basis\Uebersicht\UebersichtMitWssRequest
{
  public $vonDatum = '';
  public $bisDatum = '';
}

class TermineUebersichtResponse extends \Kufer\KuferTools\Basis\Uebersicht\UebersichtMitWssResponse
{
  public $kurstage = array();
  public $vonDatum;
  public $bisDatum;
  public $uebersichtAlsTabelle;
}