<?php

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

namespace Kufer\Web\Classes\Api\SearchRestriction;


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

  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_noKennzeichen = true;
    }
  }

  private function cleanFilterKriterium($pFilterKriterium)
  {
    if ($pFilterKriterium != 'F') {
      return true;
    } else {
      return false;
    }
  }

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

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

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

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

  protected function getSet($pVarName = '', $pVarParam = '')
  {
    $default = $pVarParam !== '' ? $pVarParam : 'Kurse ohne Kennzeichen';
    $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 $itemVal == $filterVal;
  }
}

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