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

namespace Kufer\KuferTools\Dozent\KursUebersicht\Detailsreiter\Termine\SmsKursort;

class SmsKursortUseCase extends \Kufer\KuferTools\Dozent\KursUebersicht\EinzelKursUseCase
{
  public $dbwKurseTage;

  public function __construct($request)
  {
    parent::__construct($request, new SmsKursortResponse());
    $this->dbwKurseTage = new \DBWorkKurseTageWT(1, true);
  }

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

  public static function getToolSectionId()
  {
    return 4;
  }

  public function hasAccess($pToolSectionId = null)
  {
    if (!parent::hasAccess()) {
      return false;
    } elseif (!$this->isKursTagFromKurs()) {
      return false;
    } elseif (!$this->columnSmsGrafikExists()) {
      return false;
    } elseif (!$this->isKurstagAllowed()) {
      return false;
    }

    return true;
  }

  public function action()
  {
    $kursTag = $this->dbwKurseTage->getKurstag($this->request->kursTagId);

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

  protected function getResponse($pKursTag)
  {
    $this->handleResponseStrategies();
    $zentralSettings = \ToolSettingsAPI::getToolSettings(0);
    $this->response->kursTag = $pKursTag;
    $this->response->showHinweis = $zentralSettings->showSmsBetreff;

    return $this->response;
  }

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

  private function columnSmsGrafikExists()
  {
    $columns = self::getColumns();
    foreach ($columns as $column) {
      if ($column->fieldname === 'smsgrafik') {
        return true;
      }
    }

    return false;
  }

  private function isKursTagFromKurs()
  {
    $kursTag = $this->dbwKurseTage->getKurstag($this->request->kursTagId);
    if ($kursTag->kurs->id == $this->request->knr) {
      return true;
    }

    return false;
  }

  private function isKurstagAllowed()
  {
    $kursTag = $this->dbwKurseTage->getKurstag($this->request->kursTagId);
    if ($kursTag->nodoztool) {
      return false;
    }

    return true;
  }
}

class SmsKursortRequest
{
  public $knr;
  public $kursTagId;
}

class SmsKursortResponse extends \Kufer\KuferTools\Basis\UseCase\ToolResponse
{
  public $kursTag;
  public $showHinweis;
  public $user;
  public $menuPunkte;
}