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

namespace Kufer\Web\KuferTools\Basis\DozentenInfo\SammelEMail;

abstract class SammelEMailUseCase extends \Kufer\Web\KuferTools\Basis\UseCase\ToolUseCase
{
  protected $kontaktUser = array();

  public function __construct($request)
  {
    parent::__construct($request, new SammelEMailResponse());
  }

  public function hasAccess($pToolSectionId = null)
  {
    
    
    if (!\Kufer\Web\KuferTools\Basis\UseCase\ToolUseCase::hasAccess()) {
      return false;
    }

    return true;
  }

  public function getKontakte()
  {
    
    

    
    if (is_array($this->request->dozNummern) && count($this->request->dozNummern) > 0 && count($this->request->dozNummern) == 1) {
      
      $dbwDoz = new \DBWorkDozenten(1, true);

      return $dbwDoz->getDozentenByIDArray($this->request->dozNummern);
    } else {
      \Logger::logg(' Fehler im EmailSender: DozentenNummern nicht besetzt. ', 41);

      throw new \Exception('Fehler beim Versenden der EMail an die Dozenten');
    }
  }

  
  protected function getKontakteResponsePart()
  {
    $this->response->dozenten = $this->kontaktUser;
  }

  public function action()
  {
    $errorMessage = '';
    try {
      $this->kontaktUser = $this->getKontakte();
    } catch (\Exception $e) {
      $errorMessage = $e->getMessage();
    }

    return $this->getResponse($errorMessage);
  }

  protected function getResponse($pErrorMessage = '')
  {
    $this->handleResponseStrategies();
    $this->response->errorMessage = $pErrorMessage;

    
    $this->getKontakteResponsePart();

    \Kufer\Web\KuferTools\Utils\Referrer::aktualisiereReferrerInSession($this->getToolId(), $this->request->referer, $this->request->refererLabel, 'sammel_email');
    $this->response->backLink = \Kufer\Web\KuferTools\Utils\Referrer::getReferrerFromSession($this->getToolId(), 'index.php?tool_id=' . $this->getToolId(), 'sammel_email');
    $this->response->backButtonLabel = \Kufer\Web\KuferTools\Utils\Referrer::getReferrerLabelFromSession($this->getToolId(), 'Zurück', 'sammel_email');
    $this->response->showEMail = $this->isShowingEMail();

    return $this->response;
  }

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

  
  protected function isShowingEMail(): bool
  {
    return false;
  }
}