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

namespace Kufer\KuferTools\Basis\Kursdetails\DetailsReiter\Info;

abstract class InfoUseCase extends \Kufer\KuferTools\Basis\UseCase\EinzelKursUseCase
{
  public function __construct($request)
  {
    parent::__construct($request, new InfoResponse());
  }

  
  abstract protected function getMainReiterId();

  public function hasAccess($pToolSectionId = null)
  {
    
    if (!parent::hasAccess($pToolSectionId)) {
      return false;
    }
    
    if (!$this->isRechtAllowed($this->getMainReiterId() . '.1.' . $this->getSubRechteNumber())) {
      return false;
    }

    return true;
  }

  protected function getSubRechteNumber(): int
  {
    return 2;
  }

  public function action()
  {
    $kurs = $this->getKurs($this->request->knr);

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

  protected function getResponse($kurs)
  {
    $this->response->info = $this->getInfo($kurs);
    $this->response->kurs = $kurs;

    return $this->response;
  }

  
  protected function getInfo($kurs)
  {
    switch ($this->toolSettings->benutzteinfo) {
      default:
      case 0:
        if (\System::isKuferSql5()) {
          if (trim(strip_tags($kurs->webinfohtml)) !== '') {
            return $kurs->webinfohtml;
          } elseif (trim(strip_tags($kurs->kursinfohtml)) !== '') {
            return $kurs->kursinfohtml;
          }
        }

        return \StringOperations::convertLinebreaks2HTML($kurs->getVarValue('webinfo'));
      case 1:
        if (\System::isKuferSql5() && trim(strip_tags($kurs->webinfohtml)) !== '') {
          return $kurs->webinfohtml;
        }

        return \StringOperations::convertLinebreaks2HTML($kurs->getVarValue('nur_webinfo'));
      case 2:
        if (\System::isKuferSql5() && trim(strip_tags($kurs->kursinfohtml)) !== '') {
          return $kurs->kursinfohtml;
        }

        return \StringOperations::convertLinebreaks2HTML($kurs->getVarValue('info'));
    }
  }
}

class InfoRequest
{
  public $knr;
}

class InfoResponse extends \Kufer\KuferTools\Basis\UseCase\ToolResponse
{
  public $info;
  public $kurs;
}