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


class DataMenuKurseMonatsuebersicht2 extends DataMenuKurseMonatsuebersicht
{
  const REQUESTNAME_MONAT_JAHR = 'kw_month_monat_jahr';
  const REQUESTNAME_SUCHBEGRIFF = 'kw_month_suchbegriff';
  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;
    if (($q = $this->getSuchbegriff()) !== '') {
      $dbwkurse->fromobject->stichworte = $q;
      $joinKurseAa = true;
      $this->resetNonSuchbegriff();
    }

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

      if (($m = $this->getMonatJahrParam()) !== '') {
        $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;
    } elseif (($q = RequestOperations::getVarValue(self::REQUESTNAME_SUCHBEGRIFF)) !== '') {
      return $q;
    } else {
      return '';
    }
  }

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

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

    return '';
  }
}