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

namespace Kufer\Web\KuferTools\Hausmeister\Raumbelegung\KursDetails\Detailsreiter\Dozenten;

class DozentenUseCase extends \Kufer\Web\KuferTools\Basis\UseCase\ToolUseCase
{
  public function __construct($request)
  {
    parent::__construct($request, new DozentenResponse());
  }

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

  public static function getToolSectionId()
  {
    return 135;
  }

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

    return true;
  }

  public function action()
  {
    $dbwKurse = new \DBWorkKurseWT(1, true);
    $dbwDozhon = new \DBWorkDozentenHonorar();
    $kurs = $dbwKurse->getKurs($this->request->knr);
    $honorare = $dbwDozhon->getHonorarebyKNR($this->request->knr);
    $this->sortDozenten($honorare);

    return $this->getResponse($kurs, $honorare);
  }

  private function sortDozenten(&$honorare)
  {
    if (preg_match('/^(d\\.)?name$/', $this->toolSettings->sortdoz)) {
      $honorare = \SortFunctions::sortiereByDozentenName($honorare, $this->toolSettings->sortdozasc);
    }
  }

  protected function getResponse($kurs, $honorare)
  {
    $this->handleResponseStrategies();
    $this->response->kurs = $kurs;
    $this->response->honorare = $honorare;

    return $this->response;
  }

  protected function getResponseStrategies()
  {
    return array(
      new \Kufer\Web\KuferTools\Utils\ResponseStrategies\ResponseWss($this),
      new \Kufer\Web\KuferTools\Utils\ResponseStrategies\ResponseWithToolSettings($this)
    );
  }
}

class DozentenRequest
{
  public $knr;
}

class DozentenResponse extends \Kufer\Web\KuferTools\Basis\UseCase\ToolResponse
{
  public $kurs;
  public $honorare;
  public $columns;
}