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

class DBWorkAktuelleTermine extends DBWorkKurseTageWB
{
  public function __construct($pDBTyp = 1, $newInstance = false, $pAddKurstagWithSelectedStatus = true)
  {
    parent::__construct($pDBTyp, $newInstance, $pAddKurstagWithSelectedStatus);

    $this->toolsettings = ToolSettingsAPI::getToolSettings(4);

    $this->newSearch();
    $this->setDefaultZeitraum();
    $this->setStatus($this->toolsettings->aktTermineStatis);
    $this->fromobject->kurs->semester = $this->toolsettings->semlist;
  }

  public function getTableName(): string
  {
    return 'kurstage';
  }

  public function getAktuelleTermine($pLimit = false)
  {
    $this->saveRequestSettings();
    if (StringOperations::strpos($this->fromobject->ausfall, 'A') !== false && isset($this->toolsettings->colorkurstageausf) && $this->toolsettings->colorkurstageausf == '#blind') {
      unset($this->toolsettings->colorkurstageausf);
    }

    
    $this->fromobject->kurs->kursausfall = '';

    $sql = 'SELECT ' . $this->select . PHP_EOL
            . 'FROM kurstage kt ' . PHP_EOL
            . 'LEFT JOIN kurse k ' . PHP_EOL
            . 'ON k.nr = kt.knr ' . PHP_EOL
            . 'WHERE 1=1 ' . $this->getSearchSQL('kt.', true) . PHP_EOL
            . 'ORDER BY ' . $this->getSQLOrderBy();

    if ($pLimit && intval($this->toolsettings->aktTermineLimit) >= 1) {
      
      
      
    }

    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getAllRows();
  }

  public function getAktuelleTermineLimit()
  {
    return $this->getAktuelleTermine(true);
  }

  protected function getSQLOrderBy()
  {
    switch ($this->orderby) {
      default:
      case 'beginn_dat':
      case 'datum':
        return 'kt.datum ' . ($this->orderbyasc ? 'ASC' : 'DESC') . ', kt.zeit_beg ' . ($this->orderbyasc ? 'ASC' : 'DESC')
                  . ', kt.zeit_ende ' . ($this->orderbyasc ? 'ASC' : 'DESC');
      case 'beginn':
        return 'kt.zeit_beg ' . ($this->orderbyasc ? 'ASC' : 'DESC');
      case 'id':
      case 'nr':
        return 'kt.id ' . ($this->orderbyasc ? 'ASC' : 'DESC');
      case 'ende':
        return 'kt.zeit_ende ' . ($this->orderbyasc ? 'ASC' : 'DESC') . ', kt.zeit_beg ' . ($this->orderbyasc ? 'ASC' : 'DESC');
      case 'kurs_nr':
        return 'k.nr ' . ($this->orderbyasc ? 'ASC' : 'DESC') . ', kt.zeit_beg ' . ($this->orderbyasc ? 'ASC' : 'DESC');
      case 'kurs':
        return 'k.nr ' . ($this->orderbyasc ? 'ASC' : 'DESC') . ', kt.zeit_beg ' . ($this->orderbyasc ? 'ASC' : 'DESC');
      case 'ort':
        return 'kt.ort ' . ($this->orderbyasc ? 'ASC' : 'DESC') . ', kt.zeit_beg ' . ($this->orderbyasc ? 'ASC' : 'DESC');
    }
  }

  
  public function setBeginn($pFrom)
  {
    $from = strtotime($pFrom);
    if ($from === false) {
      return false;
    }

    $this->fromobject->datum = date('d.m.Y', $from);
    $this->fromobject->zeit_beg = date('H:i', $from);

    return true;
  }

  public function setDefaultSucheZeitraum()
  {
    if ($this->toolsettings->aktTermineKeineUhrzeit) {
      $this->setBeginn('today');
      $this->setEnde('tomorrow');
    } else {
      $zeitraum = intval($this->toolsettings->kfsUhrzeitSelectZeitraum);
      $zeitraum = in_array($zeitraum, array(1, 2, 4, 6, 8, 12)) ? $zeitraum : 2;

      $beginn = array();
      $ende = array();
      $c = 24 / $zeitraum;
      for ($i = 0; $i < $c; $i++) {
        array_push($beginn, sprintf('%02s', $i * $zeitraum));
        array_push($ende, sprintf('%02s', ($i * $zeitraum) + $zeitraum - 1));
      }
      $jetzt = floor((date('H')) / $zeitraum);

      $this->setBeginn('today ' . $beginn[$jetzt] . ':00');
      $this->setEnde('today ' . $ende[$jetzt] . ':59 +1 min');
    }
  }

  public function setDefaultZeitraum()
  {
    $zeitraum = intval($this->toolsettings->aktTermineAnzKomMin);
    $min = -intval($this->toolsettings->aktTermineAnzVerMin);

    $beginn = 'now ' . ($min >= 0 ? '+' : '') . $min . ' min';
    $ende = 'now +' . $zeitraum . ' min';

    $this->setBeginn($beginn);
    if ($this->toolsettings->aktTermineKeineUhrzeit) {
      $this->setEnde('tomorrow');
    } else {
      $this->setEnde($ende);
    }
  }

  
  public function setEnde($pEnde)
  {
    $ende = strtotime($pEnde);
    if ($ende === false) {
      return false;
    }

    $this->toobject->datum = date('d.m.Y', $ende);
    $this->toobject->zeit_beg = date('H:i', $ende);

    return true;
  }

  public function setOrderBy(URLParameterNavigation $pUrlparameter)
  {
    $orderby = SecurityOperations::filterSQL($pUrlparameter->orderby);
    if ((StringOperations::substr($orderby, -5, 5) == '_desc')) {
      $this->orderby = StringOperations::substr($orderby, 0, -5);
      $this->orderbyasc = false;
    } else {
      $this->orderby = $orderby;
      $this->orderbyasc = true;
    }
  }

  
  public function setStatus($pStatusstr)
  {
    if ($pStatusstr == '-1' && $this->toolsettings->colorkurstageausf == '#blind') {
      unset($this->toolsettings->colorkurstageausf);
    }

    
    elseif ($pStatusstr == '_not_A_' || $pStatusstr == '') {
      $this->toolsettings->colorkurstageausf = '#blind';
    } elseif ($pStatusstr != '-1') {
      $this->fromobject->ausfall = preg_replace('/[^-[:alnum:]]/', '', $pStatusstr);
    }
  }

  public function setSuche($pUebersicht = true)
  {
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
      $addfsession = new SessionOperations();
      $addfsession->session_destroy_suchvariablen();
    }

    SessionOperations::setKursSearch();

    $dat = SessionOperations::getVarValueSuche('kfs_beginn_dat1');
    $dat2 = SessionOperations::getVarValueSuche('kfs_beginn_dat2');

    $zeit = SessionOperations::getVarValueSuche('kfs_beginn_time1');
    $zeit2 = SessionOperations::getVarValueSuche('kfs_beginn_time2');

    if ($dat != '') {
      $this->setBeginn($dat . ' ' . $zeit);
    } elseif ($zeit != '') {
      $this->setBeginn('today ' . $zeit);
    } elseif ($pUebersicht) {
      $this->setDefaultZeitraum();
    } else {
      $this->setDefaultSucheZeitraum();
    }

    if ($dat2 != '') {
      $this->setEnde($dat2 . ' ' . $zeit2);
    } elseif ($zeit2 != '') {
      $this->setEnde('today ' . $zeit2);
    } elseif ($pUebersicht) {
      $this->setDefaultZeitraum();
    }


    $this->setStatus(SessionOperations::getVarValueSuche('kfs_ausfall'));

    if (!$pUebersicht) {
      $this->fromobject->kurs = SessionOperations::getKurssearchFromObject(true);
      $this->fromobject->kurs->beginn_dat = '';
      $this->fromobject->kurs->beginn = '';
    }

    
    
    
  }

  protected function saveRequestSettings()
  {
    SessionOperations::setVarValueSuche('kfs_beginn_time1', $this->fromobject->zeit_beg);
    SessionOperations::setVarValueSuche('kfs_beginn_time2', $this->toobject->zeit_beg);
    SessionOperations::setVarValueSuche('kfs_beginn_dat1', $this->fromobject->datum);
    SessionOperations::setVarValueSuche('kfs_beginn_dat2', $this->toobject->datum);
  }
}