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

namespace Kufer\KuferTools\Firma\KursUebersicht\KursUebersicht\Detailsreiter\InfoAnDoz;

class InfoAnDozUseCase extends \Kufer\KuferTools\Basis\UseCase\EinzelKursUseCase
{
  public function __construct($request)
  {
    parent::__construct($request, new InfoAnDozResponse());
  }

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

  public static function getToolSectionId()
  {
    return 336;
  }

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

    return true;
  }

  protected function getAllowedIds()
  {
    $user = $this->getUser();
    $belegungshelper = new \Kufer\KuferTools\Utils\DatamodulHelper\BelegungsHelper\TeilnehmerBelegungen($this->toolSettings, $user->tnr);

    return $belegungshelper->getIdsOhneFilter();
  }

  protected function getRequestedId()
  {
    return $this->request->belegId;
  }

  public function action()
  {
    $user = $this->getUser();
    $belegungshelper = new \Kufer\KuferTools\Utils\DatamodulHelper\BelegungsHelper\TeilnehmerBelegungen($this->toolSettings, $user->tnr);
    $belegung = $belegungshelper->getDatamodulById($this->request->belegId);
    $knr = $belegung->knr;
    $kurs = $this->getKurs($knr);

    $dbwKurseBase = new \DBWorkKurseBase(1, true);
    $dozentenTmp = $dbwKurseBase->getDozentenForKurse(array($knr));

    $dozenten = array();
    
    array_walk_recursive($dozentenTmp, function($dozentObj) use (&$dozenten) { $dozenten[] = $dozentObj; });

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

  protected function getResponse($kurs, $dozenten)
  {
    $this->handleResponseStrategies();
    $this->response->kurs = $kurs;
    $this->response->dozenten = $dozenten;
    $this->response->countDozenten = count($dozenten);
    $this->response->belegId = $this->request->belegId;

    return $this->response;
  }

  protected function getResponseStrategies()
  {
    return array(
        new \Kufer\KuferTools\Utils\ResponseStrategies\ResponseMitRahmen($this)
    );
  }
}

class InfoAnDozRequest
{
  public $knr;
  public $belegId;
}

class InfoAnDozResponse extends \Kufer\KuferTools\Basis\UseCase\ToolResponse
{
  public $kurs;
  public $belegId;
  public $dozenten;
  public $countDozenten;
  public $user;
  public $menuBar;
}