<?php


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

namespace Kufer\Web\Webbasys\Includee\Ics;

use DBWorkKurseTageWB;
use IcsAPI;
use StringOperations;
use ToolSettingsAPI;

class IcsExportKurstage extends IcsExport {

  protected function getData()
  {
    $dbwkurstage = new DBWorkKurseTageWB();
    $dbwkurstage->newSearch();
    $settings = ToolSettingsAPI::getToolSettings(4);
    if ($settings->versteckeKurstageWieImRaumanzeigeprogramm) {
      $dbwkurstage->fromobject->ranzprognoanz = true;
    }
    if ($settings->versteckeKurstageOhneMinuten) {
      $dbwkurstage->fromobject->minuten = 1;
    }
    $kurstage = $dbwkurstage->getKurstagebyKursNROnlyDifferentLimit(StringOperations::getfilledStr($this->urlparameter->knr, '-1'), null);
    if ($kurstage == null || count($kurstage) == 0) {
      return '';
    }
    return IcsAPI::getIcsContentKurstage($kurstage);
  }

  public function getFilename(): string
  {
    $dbwkurstage = new DBWorkKurseTageWB();
    $kurstage = $dbwkurstage->getKurstagebyKursNROnlyDifferentLimit(StringOperations::getfilledStr($this->urlparameter->knr, '-1'), null);
    $key = key($kurstage);
    $dangerousCharacters = array(" ", '"', "'", "&", "/", "\\", "?", "#", '.');
    return str_replace($dangerousCharacters, '_', $kurstage[$key]->knr);
  }
}