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

namespace Kufer\Web\KuferTools\Basis\Kursdetails\TnZertifikat;

abstract class HandleTnZertifikatController extends \Kufer\KuferTools\Basis\Controller\Controller
{
  protected function getNewRequestObj(): HandleTnZertifikatRequest
  {
    return new HandleTnZertifikatRequest();
  }

  protected function getRequestObj()
  {
    $result = $this->getNewRequestObj();
    $result->knr = \GetOperations::getVarValue('knr');
    $result->kursinfo = \PostOperations::getVarValue('lehrinhalt');
    $belids = \PostOperations::getVarValue('belids', array());
    if (is_array($belids)) {
      $result->values = array();
      foreach ($belids as $belid) {
        $result->values[$belid] = array(
          'eintritt' => trim(\PostOperations::getVarValue('b' . $belid . '_eintritt')),
          'austritt' => trim(\PostOperations::getVarValue('b' . $belid . '_austritt')),
          'lehrinhalt' => trim(\PostOperations::getVarValue('b' . $belid . '_lehrinhalt')),
        );
      }
    }

    return $this->handleXsrf($result, \GetOperations::getVarValue('action'), $result->knr);
  }

  protected function getPresenter($pUseCaseResponse)
  {
    if ($pUseCaseResponse->status !== HandleTnZertifikatUseCase::STATUS_SUCCESS) {
      return new TnZertifikatPresenter($pUseCaseResponse);
    } else {
      return new TnZertifikatPrintPresenter($pUseCaseResponse);
    }
  }
}