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

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

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

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

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

  protected function getDownloads($kurs)
  {
    $dbwkursdokumente = new \DBWorkDokumenteWeb();
    $dbwkursdokumente->orderby = $this->toolSettings->sortdownloads ?? 'reihenfolge';
    $dbwkursdokumente->orderbyasc = $this->toolSettings->sortdownloadsasc ?? true;
    $dbwkursdokumente->orderby2 = $this->toolSettings->sortdownloads2 ?? 'bezeichnung';
    $dbwkursdokumente->orderbyasc2 = $this->toolSettings->sortdownloadsasc2 ?? true;

    return $dbwkursdokumente->getDokumente4Kurs($kurs->id);
  }

  protected function setButtonRechteInResponse($pDownloads)
  {
    
    

    
    
    $this->response->showUploadButton = $this->isRechtAllowed($this->response->toolSectionId . '.0.1');
    
    $this->response->downloadZuWeitereKurse = $this->isAllowedDownloadZuWeitereKurse($pDownloads);
    $this->response->bearbeitenAllowed = $this->isAllowedBearbeiten();
    
    $this->response->deleteAllowed = $this->isAllowedDelete($pDownloads);
  }

  protected function getResponse($kurs, $downloads)
  {
    $this->handleResponseStrategies();
    $this->response->kurs = $kurs;
    $this->response->downloads = $downloads;
    $this->setButtonRechteInResponse($this->response->downloads);
    $this->response->statusMessageContainer = $this->request->statusMessageContainer;

    return $this->response;
  }

  
  protected function isAllowedDownloadZuWeitereKurse($pDokumente)
  {
    $kursDownloadAllowed4Tool = $this->isRechtAllowed($this->response->toolSectionId . '.0.2');
    $kursDownloadAllowedAry = array();
    foreach ($pDokumente as $dokument) {
      $kursDownloadAllowedAry[$dokument->id] = $kursDownloadAllowed4Tool;
    }

    return $kursDownloadAllowedAry;
  }

  protected function isAllowedBearbeiten()
  {
    return false;
  }

  protected function isAllowedDelete($pDokumente)
  {
    return false;
  }

  protected function areButtonsAllowed4DbAccess()
  {
    return !\SessionOperations::isReplikat() || \SessionOperations::isDmzBetrieb();
  }

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

class DownloadsRequest
{
  public $knr;
  public $statusMessageContainer;
}

class DownloadsResponse extends \Kufer\KuferTools\Basis\UseCase\ToolResponse
{
  public $kurs;
  public $downloads;
  public $columns;
  public $showUploadButton;
  public $downloadZuWeitereKurse;
  public $statusMessageContainer;
  public $bearbeitenAllowed;
  public $deleteAllowed;
}