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

namespace Kufer\KuferTools\Mitarbeiter\Anmeldung\KinderDialog;

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

  public function hasAccess($pToolSectionId = null)
  {
    if (!parent::hasAccess($pToolSectionId)) {
      return false;
    }
    $knr = \SessionOperations::getVarValueWTIntern('anmeldung_knr', -1);
    if ($knr != -1) {
      
      
      $kurshelper = new \Kufer\KuferTools\Utils\DatamodulHelper\KursHelper\MitarbeiterAnmeldungKurse($this->toolSettings, $this->getUser(), array());
      $allowedKnrs = $kurshelper->getIdsMitFilter($this->getToolId());
      if (in_array($knr, $allowedKnrs) == false) {
        return false;
      }
    }

    return true;
  }

  public static function getToolSectionId()
  {
    return 21;
  }

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

  public function action()
  {
    return $this->getResponse();
  }

  private function getKurs()
  {
    $knr = \SessionOperations::getVarValueWTIntern('anmeldung_knr', -1);
    $dbwKurse = new \DBWorkKurse(1, true);
    if ($knr != -1) {
      return $dbwKurse->getKurs($knr, true);
    } else {
      return $dbwKurse->getNewObject();
    }
  }

  private function getKinderfrei()
  {
    $kurs = $this->getKurs();
    $kinder_frei = $kurs->getKinderMax() - $kurs->getVarValue('kinderanzahl');
    if ($kinder_frei < 0) {
      $kinder_frei = 0;
    }

    return $kinder_frei;
  }

  private function isFormReadonly()
  {
    return \SessionOperations::getVarValueWTIntern('AnmeldungReadonly_' . $this->getToolId()) == 1;
  }

  protected function getResponse()
  {
    $this->response->kinderfrei = $this->getKinderfrei();
    $this->response->readonly = $this->isFormReadonly();

    return $this->response;
  }
}

class KinderDialogRequest
{
}

class KinderDialogResponse extends \Kufer\KuferTools\Basis\UseCase\ToolResponse
{
  public $kinderfrei;
}