<?php
/**
 * @copyright (c) Kufer Software GmbH, Oderstraße 7, D-84453 Mühldorf
 */

namespace Kufer\Web\KuferTools\Teilnehmer\KursUebersicht\TermineUebersicht;


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

  public function __construct($request)
  {
    parent::__construct($request, new TermineUebersichtResponse());
    $this->ktForTn = new \Kufer\Web\KuferTools\Teilnehmer\KursUebersicht\TermineUebersicht\Utils\KurstageForTeilnehmer(
      $this->request->suchParameter,
      $this->toolSettings->kurstageAufFaecherEinschraenken
    );
  }

  public static function getToolSectionId()
  {
    return 300;
  }

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

  protected function vorAction()
  {
  }

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

    if ($this->isRechtAllowed('300.0.5') && $this->datensaetzeVorhanden()) {
      $buttons[] = array($buttonFactory->getTerminPlanButton('Terminplan', 'terminplan_kt'));
    }
    if ($this->isRechtAllowed('300.0.1') && $this->datensaetzeVorhanden()) {
      

      $kursNrAry = $this->getAlleIds();
      
      $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=20&action=icsKalender&von=' . $vondat . '&bis=' . $bisdat . '&terminnr=' . $joinedKnrAry
      ));
    }
    if ($this->isRechtAllowed('300.0.2')) {
      $buttons[] = array($buttonFactory->getFilterButton('Filter', 'filter_alletermine'));
    }

    return $buttons;
  }

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

  protected function verlinkePanelHeaderAllowed()
  {
    return (\SecurityOperations::privilegAllowedCurrentUser($this->getToolId(), '300.0.4') == 2
        && \ToolsAPI::rechteOR(array('335.1.1', '335.1.2', '335.1.3', '335.1.4', '335.1.5', '335.1.6', '335.1.7', '335.1.8'), $this->getToolId()));
  }

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

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

  
  protected function getAlleIds()
  {
    return $this->ktForTn->getAlleIds();
  }

  
  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\Web\KuferTools\Utils\ResponseStrategies\ResponseKurseTageOnCurrentPage($this);
  }

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

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

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