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

namespace Kufer\KuferTools\Dozent\Lehrplan\GrafikenDruckLehrplan;

class GrafikenDruckLehrplanUseCase extends \Kufer\KuferTools\Basis\UseCase\ToolUseCase
{
  private $dbwKurstage;

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

  public static function getToolId()
  {
    return 7;
  }

  public static function getToolSectionId()
  {
    return 23;
  }

  public function hasAccess($pToolSectionId = null)
  {
    if (!parent::hasAccess()) {
      return false;
    }
    if (!$this->isRechtAllowed('23.0.4')) { 
      return false;
    }

    return true;
  }

  public function action()
  {
    $this->setzeFilterFuerKurstage();

    return $this->getResponse();
  }

  private function setzeFilterFuerKurstage()
  {
    $dtop = new \DateTimeOperations();
    $this->dbwKurstage->newSearch();
    $this->handleKurstagFilter();

    if ($this->dbwKurstage->fromobject->datum == '') {
      $this->dbwKurstage->fromobject->datum = $dtop->getDateByParam(6);
    }
    if ($this->dbwKurstage->toobject->datum == '') {
      $this->dbwKurstage->toobject->datum = $dtop->getDateByParam(7);
    }
    if ($this->dbwKurstage->fromobject->zeit_ende == '') {
      $this->dbwKurstage->fromobject->zeit_ende = $dtop->getFloatasTimeString(STARTZEIT, true);
    }
    if ($this->dbwKurstage->toobject->zeit_beg == '') {
      $this->dbwKurstage->toobject->zeit_beg = $dtop->getFloatasTimeString(ENDEZEIT, true);
    }
  }

  protected function handleKurstagFilter()
  {
    $searchAPI = new \LehrplanSucheAPI();
    $this->dbwKurstage->fromobject = $searchAPI->getFromObjectBySession($this->dbwKurstage->fromobject, 'tool' . $this->getToolId());
    $this->dbwKurstage->toobject = $searchAPI->getToObjectBySession($this->dbwKurstage->toobject, 'tool' . $this->getToolId());
  }

  protected function getResponse()
  {
    $this->response->kurstagFilterVon = $this->dbwKurstage->fromobject;
    $this->response->kurstagFilterBis = $this->dbwKurstage->toobject;

    return $this->response;
  }
}

class GrafikenDruckLehrplanRequest
{
}

class GrafikenDruckLehrplanResponse extends \Kufer\KuferTools\Basis\UseCase\ToolResponse
{
  public $kurstagFilterVon;
  public $kurstagFilterBis;
}