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

namespace Kufer\KuferTools\Mitarbeiter\DozentenInfo\Detailsreiter\Fotos;

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

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

  public static function getToolSectionId()
  {
    return 397;
  }

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

    return true;
  }

  public function action()
  {
    $dozent = $this->getDozent($this->request->dnr);
    $fotos = $this->getFotos($this->request->dnr);

    return $this->getResponse($dozent, $fotos);
  }

  protected function getDozent($dnr)
  {
    $dbwDozenten = new \DBWorkDozenten();

    return $dbwDozenten->getDozent($dnr, true);
  }

  private function getFotos($dnr)
  {
    $dbwFoto = new \DBWorkFoto();
    $fotoNummern = $dbwFoto->getFotoNummern('D', $dnr);
    $result = array();
    if (is_array($fotoNummern)) {
      foreach ($fotoNummern as $fotoNr) {
        $result[] = $dbwFoto->getFoto('D', $dnr, $fotoNr);
      }
    }

    return $result;
  }

  protected function getResponse($dozent, $fotos)
  {
    $this->response->dozent = $dozent;
    $this->response->fotos = $fotos;

    return $this->response;
  }
}

class FotosRequest
{
  public $dnr;
}

class FotosResponse extends \Kufer\KuferTools\Basis\UseCase\ToolResponse
{
  public $fotos;
  public $dozent;
}