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

namespace Kufer\KuferTools\Basis\DozentenInfo\SammelEMail;

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

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

  public function hasAccess($pToolSectionId = null)
  {
    
    
    if (!\Kufer\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\KuferTools\Utils\Referrer::aktualisiereReferrerInSession($this->getToolId(), $this->request->referer, $this->request->refererLabel, 'sammel_email');
    $this->response->backLink = \Kufer\KuferTools\Utils\Referrer::getReferrerFromSession($this->getToolId(), 'index.php?tool_id=' . $this->getToolId(), 'sammel_email');
    $this->response->backButtonLabel = \Kufer\KuferTools\Utils\Referrer::getReferrerLabelFromSession($this->getToolId(), 'Zurück', 'sammel_email');

    return $this->response;
  }

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

  
  protected function isSetAbsenderToolsettings()
  {
    return false;
    
    
  }
}

class SammelEMailRequest
{
  public $dozNummern = array();
  public $emailVorbelegen;
}

class SammelEMailResponse extends \Kufer\KuferTools\Ausgaben\SammelEMail\SammelEMailResponse
{
  public $dozenten;

  public $user;
  public $menuBar;
  public $errorMessage;
  public $emailInhalt;

  public $backLink;
  public $backButtonLabel;
}