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


class DataMenuKurseMonatsuebersicht2 extends DataMenuKurseMonatsuebersicht
{
  public const REQUESTNAME_MONAT_JAHR = 'kw_month_monat_jahr';
  public const REQUESTNAME_SUCHBEGRIFF = 'kw_month_suchbegriff';
  public const REQUESTNAME_KATEGORIE = 'kw_month_katid';

  protected function renderMonthList() : string
  {
    
    
    $expGruppiert = new ExpGruppiert();
    $this->exportadapter = $expGruppiert;
    $this->sight = SightsAPI::getSight(414);

    return $this->getnocolumn();
  }

  protected function renderAllKurse() : string
  {
    if (count($this->kurseSorted) > 0) {
      return parent::renderAllKurse();
    }

    return $this->getnocolumn();
  }

  protected function getKurseSorted()
  {
    $this->dbwork = new DBWorkKurse();
    $dbwkurse = $this->initDbwKurse();

    $joinKurseAa = false;
    $q = $this->getSuchbegriff();
    if ($q !== '') {
      $dbwkurse->fromobject->stichworte = $q;
      $joinKurseAa = true;
      $this->resetNonSuchbegriff();
    }

    $fromDate = false;
    $toDate = false;
    if (!$joinKurseAa) {
      $k = $this->getKategorieParam();
      if ($k > 0) {
        $dbwkurse->fromobject->sonderkategorie = $k;
      }

      $m = $this->getMonatJahrParam();
      if ($m !== '') {
        $matches = array();
        $matchResult = preg_match('/^(2\\d{3})_(0[123456789]|1[012])$/', $m, $matches);
        if ($matchResult) {
          $month = $matches[2];
          $year = $matches[1];

          $fromDate = "01.$month.$year";
          $toDate = $this->addfdatetime->getAnzTageImMonat($month, $year) . ".$month.$year";
        }
      }
    }

    if ($fromDate === false) {
      $this->addfdatetime->getValidDateString('1.' . $this->addfdatetime->getMonthByDate($this->addfdatetime->getDateNow()) . '.', true);
      $toDate = '31.12.2999';
    }
    $kurse = $dbwkurse->getKursIdsWithMonthAndYear($fromDate, $toDate, $joinKurseAa);

    $allKurse = array_map(function($pItem) {
      return $pItem['nr'];
    }, $kurse);
    WBKursStore::getKurseByIdArray($allKurse);

    
    $kurseByMonth = $this->prepareKurseByMonth($kurse);

    $this->kurseSorted = $this->fillKurseByMonth($kurseByMonth, $kurse);
  }

  public function getSuchbegriff() : string
  {
    if (isset($this->urlparameter->q)) {
      return $this->urlparameter->q;
    } else {
      return RequestOperations::getVarValue(self::REQUESTNAME_SUCHBEGRIFF, '');
    }
  }

  public function getMonatJahrParam() : string
  {
    if (isset($this->urlparameter->monat_jahr)) {
      return $this->urlparameter->monat_jahr;
    } else {
      return RequestOperations::getVarValue(self::REQUESTNAME_MONAT_JAHR, '');
    }
  }

  public function explodeMonatJahrParam($param) : array
  {
    $tmp = explode('_', $param);

    return array($tmp[0], intval($tmp[1]));
  }

  public function getKategorieParam() : int
  {
    if (isset($this->urlparameter->katid) && $this->urlparameter->katid > 0) {
      return intval($this->urlparameter->katid);
    } elseif (($k = RequestOperations::getVarValue(self::REQUESTNAME_KATEGORIE, 0)) > 0) {
      return intval($k);
    } else {
      return 0;
    }
  }

  private function resetNonSuchbegriff() : void
  {
    RequestOperations::setVarValue(self::REQUESTNAME_KATEGORIE, null);
    RequestOperations::setVarValue(self::REQUESTNAME_MONAT_JAHR, null);
    $this->urlparameter->katid = null;
    $this->urlparameter->monat_jahr = null;
  }

  
  public function getMonthStrings(int $startMonth = 0, int $startYear = 0, int $count = 12) : array
  {
    $curMonth = $startMonth <= 0 ? intval($this->addfdatetime->getMonth($this->addfdatetime->getDateNow())) : $startMonth;
    $curYear = $startYear <= 0 ? intval($this->addfdatetime->getYear($this->addfdatetime->getDateNow())) : $startYear;
    $result = array();
    for ($i = 0; $i < $count; $i++) {
      $result[] = $curYear . '_' . \StringOperations::pad($curMonth, 2, '0', STR_PAD_LEFT);
      $curMonth++;
      if ($curMonth === 13) {
        $curMonth = 1;
        $curYear++;
      }
    }

    return $result;
  }

  public function getStatus() : string
  {
    $resultBegin = 'Veranstaltungen';
    $result = '';
    $q = $this->getSuchbegriff();
    if ($q !== '') {
      return $resultBegin . ' zum Suchbegriff "' . $q . '"';
    }
    $katid = $this->getKategorieParam();
    if ($katid > 0) {
      $dbwcat = DBWorkCategories::getDefaultInstance();
      $cat = $dbwcat->getCategory($katid);
      $result .= ' aus dem Fachbereich ' . $cat->text;
    }
    $m = $this->getMonatJahrParam();
    if ($m !== '') {
      list($year, $month) = $this->explodeMonatJahrParam($m);
      $result .= ' im Monat ' . $this->addfdatetime->getNameofMonth($month) . ' ' . $year;
    }
    if (StringOperations::strlen($result) > 0) {
      return $resultBegin . $result;
    }

    return '';
  }
}