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

namespace Kufer\Web\KuferTools\Basis\Ics;

abstract class IcsKalenderUseCase extends \Kufer\Web\KuferTools\Basis\UseCase\ToolUseCase
{
  protected $kursHelper;
  protected $belegungen;

  public function __construct($request)
  {
    parent::__construct($request, new IcsAuswahlResponse());
    $this->belegungen = $this->getBelegungen();
  }

  protected function getBelegungen()
  {
    $user = $this->getUser();
    $dbwBelegung = new \DBWorkBelegung(1, true);

    return $dbwBelegung->getBelegungenbyTeilnehmerNr($user->tnr);
  }

  protected function getKursHelper()
  {
    return \Kufer\Web\KuferTools\Utils\DatamodulHelper\Kurse\KursHelperFactory::getKursHelperByToolID($this->getToolId(), $this->toolSettings);
  }

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

    return $this->areIdsAllowed();
  }

  protected function areIdsAllowed()
  {
    $allowedKnrs = $this->getAllowedKursnummern();
    foreach ($this->request->kursnummern as $knr) {
      if (!in_array($knr, $allowedKnrs)) {
        return false;
      }
    }

    return true;
  }

  abstract protected function getAllowedKursnummern();

  
  public function action()
  {
    return $this->getResponse();
  }

  abstract protected function getKursTage();

  protected function additionalCheckVonBisDat()
  {
    return false;
  }

  
  protected function getResponse()
  {
    $this->response->kurstage = $this->getKursTage();
    $this->response->timestamp = \StringOperations::formatDateForWebTeam(\DateTimeOperations::getDateNow());
    $this->response->vondat = $this->request->vondat;
    $this->response->bisdat = $this->request->bisdat;

    return $this->response;
  }
}

class IcsKalenderRequest
{
  public $terminbereich;
  public $vondat;
  public $bisdat;
  public $kursnummern;
  public $auchTermineAndererDozenten;
  public $terminnummern;
}

class IcsKalenderResponse extends \Kufer\Web\KuferTools\Basis\UseCase\ToolResponse
{
  public $kursnummern;
  public $timestamp;
  public $kurstage;
  public $vondat;
  public $bisdat;
}