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

namespace Kufer\KuferTools\Dozent\Kursplanung\TerminSammelaenderung;

use Kufer\Web\KuferTools\Utils\Dozent\KursBearbeitenPlanungUtils;


class TerminSammelaenderungFormularUseCase extends \Kufer\KuferTools\Dozent\Kursplanung\EinzelKursUseCase
{
  public function __construct($request)
  {
    parent::__construct($request, new TerminSammelaenderungFormularResponse());
  }

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

  public static function getToolSectionId()
  {
    return 281;
  }

  
  
  public function hasAccess($pToolSectionId = null)
  {
    
    
    if (!\Kufer\KuferTools\Basis\UseCase\ToolUseCase::hasAccess()) {
      return false;
    }
    if (!$this->isRechtAllowed('10.0.3')) {
      return false;
    }

    
    $alleKursnummern = $this->getAllKursnrOhneFilter();
    $user = $this->getUser();
    $dbwkt = new \DBWorkKurseTageWT();
    $kurstage = $dbwkt->getKurstageFromIDs($this->request->ktIds, false);

    
    foreach ($kurstage as $kurstag) {
      if ($kurstag->nodoztool) {
        return false;
      }
      if (!in_array($kurstag->knr, $alleKursnummern)) {
        return false;
      }
      if ($kurstag->dnr != $user->dnr) {
        return false;
      }

      if (!KursBearbeitenPlanungUtils::isTerminBearbeitbar(
        $this->toolSettings->kursstatusKursIstBearbeitbar,
        $this->toolSettings->kursstatusKursIstNichtBearbeitbar,
        $this->toolSettings->kursstatusKursInPlanung,
        $kurstag->getKurs()
      )) {
        return false;
      }
    } 

    return true;
  }

  

  
  public function action()
  {
    $this->response->kurstag = $this->getKurstag();
    $this->getRechte4TerminSammelBearbeiten();

    return $this->getResponse();
  }

  private function getKurstag()
  {
    $kurstag = null;
    if (is_null($kurstag)) {
      $dbw = new \DBWorkKurseTageWT(1, true);
      $kurstag = $dbw->getKurstag($this->request->ktIds[0], true);
    }

    return $kurstag;
  }

  private function getRechte4TerminSammelBearbeiten()
  {
    $toolID = $this->getToolId();
    $felder = array(
      'datum' => array('281.117', '281.118'),
      'ausfall' => array('281.123'),
      'bemerkung' => array('281.124'),
      'beginn' => array('281.119'),
      'ue' => array('281.328'),
      'ende' => array('281.120'),
      'minuten' => array('281.121'),
      'pause' => array('281.122'),
      'kursort' => array('281.116')
    );

    $this->response->showDatum = \ToolsAPI::rechteOR($felder['datum'], $toolID, true);
    $this->response->showUhrzeitBeginn = \ToolsAPI::rechteOR($felder['beginn'], $toolID, true);
    $this->response->showUhrzeitEnde = \ToolsAPI::rechteOR($felder['ende'], $toolID, true);
    $this->response->showUE = \ToolsAPI::rechteOR($felder['ue'], $toolID, true);
    $this->response->showMinuten = \ToolsAPI::rechteOR($felder['minuten'], $toolID, true);
    $this->response->showPause = \ToolsAPI::rechteOR($felder['pause'], $toolID, true);
    $this->response->showAusfall = \ToolsAPI::rechteOR($felder['ausfall'], $toolID, true);
    $this->response->showKursort = \ToolsAPI::rechteOR($felder['kursort'], $toolID, true);
    $this->response->showBemerkung = \ToolsAPI::rechteOR($felder['bemerkung'], $toolID, true);
  }

  
  protected function getResponse()
  {
    $this->response->ktIds = $this->request->ktIds;
    $this->response->knr = $this->request->knr;

    return $this->response;
  }
}

class TerminSammelaenderungFormularRequest
{
  public $knr;
  public $ktIds;
}

class TerminSammelaenderungFormularResponse extends \Kufer\KuferTools\Basis\UseCase\ToolResponse
{
  public $ktIds = array();
  public $knr;
  public $showDatum;
  public $showUhrzeitBeginn;
  public $showUhrzeitEnde;
  public $showUE;
  public $showMinuten;
  public $showPause;
  public $showAusfall;
  public $showKursort;
  public $showBemerkung;
}