<?php

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

namespace Kufer\Web\Classes\Api\SearchRestriction;


class SearchRestrictionKatKeineBegonnenFilter extends SearchRestriction
{
  public function __construct()
  {
    $this->sessionName = $this->requestName = 'katkeinebegonnenenfilter';
  }

  public function applyRestriction($pFromObject, $pToObject)
  {
    if ($pFromObject == null || $pToObject == null || !($pFromObject instanceof \Kurs) || !($pToObject instanceof \Kurs)) {
      throw new \InvalidArgumentException('pFromObject oder pToObject ist kein Kurs');
    }


    $filterKriterium = \RequestOperations::getVarValue($this->requestName, '');
    if (!empty($filterKriterium)) {
      if (is_array($filterKriterium) && count($filterKriterium) > 1) {
        \SessionOperations::setVarValueFilter($this->sessionName, $this->cleanFilterKriterium($filterKriterium[0]));
      } else {
        $this->reset();
      }
    }
    $krit = \SessionOperations::getVarValueFilter($this->sessionName);
    if ($krit !== '') {
      $pFromObject->beginn_dat = $krit;

      
      if (\DateTimeOperations::isValidDate($pToObject->beginn_dat) && \DateTimeOperations::isDateBefore($pToObject->beginn_dat, $krit)) {
        $pToObject->beginn_dat = '';
      }
    }
  }

  public static function cleanFilterKriterium($pFilterKriterium)
  {
    if ($pFilterKriterium != 'F') {
      $sett = \ToolSettingsAPI::getToolSettings(4);
      if ($sett->katfilterBegonneneOffset != 0) {
        
        return \DateTimeOperations::addDays(\DateTimeOperations::getDateNow(), -1 * $sett->katfilterBegonneneOffset);
      } else {
        return \DateTimeOperations::getDateNow();
      }
    } else {
      return '';
    }
  }

  public function getFilterValue()
  {
    $this->applyRestriction(new \Kurs(), new \Kurs());

    return \SessionOperations::getVarValueFilter($this->sessionName);
  }
}

class SearchRestrictionKatKeineBegonnenRendererCheckbox extends SearchRestrictionRendererCheckboxBase
{
  protected static $mySeparator = '|';

  public function __construct($pFilter = null)
  {
    parent::__construct(new SearchRestrictionKatKeineBegonnenFilter());
  }

  protected function getSet($pVarName = '', $pVarParam = '')
  {
    $default = $pVarParam !== '' ? $pVarParam : 'Keine begonnenen Kurse';
    $result = '1' . static::$mySeparator . $default;

    return array($result);
  }

  protected function getCleanOptionValFromItem($pItem)
  {
    $parts = explode(static::$mySeparator, $pItem);

    return \CleanOperations::cleanStringForHtml($parts[0]);
  }

  protected function getCleanOptionTextFromItem($pItem)
  {
    $parts = explode(static::$mySeparator, $pItem);

    return \CleanOperations::cleanStringForHtml($parts[1]);
  }

  protected function isSelected($itemVal, $filterVal)
  {
    return \DateTimeOperations::isValidDate($filterVal);
  }
}

class SearchRestrictionKatKeineBegonnenRendererCheckboxAsList extends \Kufer\Web\Classes\Api\SearchRestriction\SearchRestrictionKatKeineBegonnenRendererCheckbox
{
  public function __construct($pFilter = null)
  {
    parent::__construct($pFilter);
    $this->itemDecorator = new ItemDecoratorList();
    $this->decorator = new DecoratorList('kw-katkeinebegonnenenlist');
  }
}