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

namespace Kufer\KuferTools\Utils\DatamodulHelper\KursTageHelper;

class WasIstWoKurstage extends KursTageHelper
{
  public function getDBWork()
  {
    if ($this->user->isAdmin() || $this->user->dnr > -1
      || $this->user->anr > -1 || $this->user->ben > -1) {
      $dbwKurseTage = new \DBWorkKurseTage();
    } else {
      $dbwKurseTage = new \DBWorkKurseTageWTINetAktivFilter(1, true);
    }
    $dbwKurseTage->newSearch();

    return $dbwKurseTage;
  }

  protected function getKurstageIds($dbwKurseTage)
  {
    return $dbwKurseTage->getKurstageIds();
  }

  protected function setzeEinstellungen($dbwKurseTage)
  {
    
    if ($this->toolSettings->abweichendeKtStatus) {
      $ktstatus = $this->toolSettings->ktstatus;
    } else {
      $ktstatus = '-XZNPVEI';
    }
    $dbwKurseTage->fromobject->ausfall = $ktstatus;
    $dbwKurseTage->toobject->ausfall = $ktstatus;

    if ($this->user->dnr > 0) {
      $dbwKurseTage->toobject->nodoztool = true;
    }
    if ($this->user->tnr > 0) {
      $dbwKurseTage->toobject->notntool = true;
    }

    
    if ($this->toolSettings->weiterekurstage > 0) {
      $dbwKurseTage->toobject->datum = \DateTimeOperations::getNextDate($dbwKurseTage->toobject->datum, $this->toolSettings->weiterekurstage);
    }

    return $dbwKurseTage;
  }

  protected function setzeSortierung($dbwKurseTage)
  {
    $dbwKurseTage->orderby = 'datum';
    $dbwKurseTage->orderby2 = 'kt.zeit_beg';
    $dbwKurseTage->orderby3 = 'k.kurzbez';
    $dbwKurseTage->orderby4 = 'k.titel';

    return $dbwKurseTage;
  }

  protected function setzeFilter($dbwKurseTage, $pToolId)
  {
    $searchAPI = new \KurstagSucheAPI();
    $dbwKurseTage->fromobject = $searchAPI->getFromObjectBySession($dbwKurseTage->fromobject, 'tool' . $pToolId);
    $dbwKurseTage->toobject = $searchAPI->getToObjectBySession($dbwKurseTage->toobject, 'tool' . $pToolId);

    if (!\DateTimeOperations::isValidDate($dbwKurseTage->fromobject->datum)) {
      $dbwKurseTage->fromobject->datum = \DateTimeOperations::getDateNow();
    }
    if (!\DateTimeOperations::isValidDate($dbwKurseTage->toobject->datum)) {
      $dbwKurseTage->toobject->datum = \DateTimeOperations::getDateNow();
    }

    return $dbwKurseTage;
  }
}