<?php

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

namespace Kufer\Web\Classes\Api\SearchRestriction;


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

  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));
      } else {
        $this->reset();
      }
    }
    $krit = \SessionOperations::getVarValueFilter($this->sessionName);
    if ($krit !== '') {
      $pFromObject = $this->setKrit($pFromObject, $krit);
    }
  }

  private function cleanFilterKriterium($pFilterKriterium)
  {
    if (!is_array($pFilterKriterium)) {
      $filterKriterium = \CleanOperations::cleanInt($pFilterKriterium, '');
    } else {
      if (count($pFilterKriterium) === 1 && $this->isReset($pFilterKriterium[0])) {
        $filterKriterium = '';
      } else {
        $filterKriterium = array();
        foreach ($pFilterKriterium as $krit) {
          if (!$this->isReset($krit)) {
            $filterKriterium[] = \CleanOperations::cleanInt($krit, '');
          }
        }
      }
    }

    return $filterKriterium;
  }

  protected function isReset($pString)
  {
    return $pString === '__reset__';
  }

  protected function setKrit($pFromObject, $pKrit)
  {
    $pFromObject->suche_kennzeichen = $pKrit;

    return $pFromObject;
  }

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

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

  public $dbw;

  
  private function getDbW()
  {
    if ($this->dbw == null) {
      $this->dbw = \DBWorkKennz::getDefaultInstance();
    }

    return $this->dbw;
  }

  public function getSet()
  {
    $settings = $this->getSettings();
    $dbw = $this->getDbW();
    $dbw->newSearch();
    $dbw->fromobject->kennz = $settings->katfilterKennzVon;
    $dbw->toobject->kennz = $settings->katfilterKennzBis;

    return $this->getDbW()->getAllKennzeichen();
  }

  
  public $settings;

  private function getSettings()
  {
    if ($this->settings == null) {
      $this->settings = \ToolSettingsAPI::getToolSettings(4);
    }

    return $this->settings;
  }
}

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

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

  protected function getCleanOptionValFromItem($pItem)
  {
    return \CleanOperations::cleanInt($pItem->kennz);
  }

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

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

class SearchRestrictionKatKennzOrRendererCheckboxAsBootstrapRow extends \Kufer\Web\Classes\Api\SearchRestriction\SearchRestrictionKatKennzOrRendererCheckbox
{
  public function __construct($pFilter = null)
  {
    parent::__construct($pFilter);
    $this->itemDecorator = new ItemDecoratorBootstrap();
    $this->decorator = new DecoratorBootstrap();
  }
}