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

namespace Kufer\Web\Classes\Api\SearchRestriction;

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

  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->suche_exceptKennzeichen = $krit;
    }
  }

  private function cleanFilterKriterium($pFilterKriterium)
  {
    $ex = explode('-', $pFilterKriterium);

    return array_filter($ex, 'is_numeric');
  }

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

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

class SearchRestrictionKatExceptKennzRendererCheckbox extends SearchRestrictionRendererCheckboxBase
{
  public function __construct($pFilter = null)
  {
    parent::__construct(new SearchRestrictionKatExceptKennzFilter());
    $this->withReset = true;
  }

  protected function getSet($pVarName = '', $pVarParam = '')
  {
    return array($pVarParam);
  }

  protected function getCleanOptionValFromItem($pItem)
  {
    return \CleanOperations::cleanStringForHtml($pItem);
  }

  protected function getCleanOptionTextFromItem($pItem)
  {
    return \CleanOperations::cleanStringForHtml($this->varParamZwei);
  }

  protected function isSelected($itemVal, $filterVal)
  {
    return !empty($filterVal);
  }
}

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