<?php

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

namespace Kufer\Web\Classes\Api\SearchRestriction;

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

  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 ($filterKriterium !== '') {
      if ($this->isReset($filterKriterium)) {
        $this->reset();
      } else {
        \SessionOperations::setVarValueFilter($this->sessionName, $this->cleanFilterKriterium($filterKriterium));
      }
    }
    $as = \SessionOperations::getVarValueFilter($this->sessionName);
    if ($as !== '') {
      $pFromObject->aussenst = $as;
    }
  }

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

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

    return $filterKriterium;
  }

  private function isAsValid($pAs)
  {
    $as = $this->getDbW()->getAussenstelleByBezeichner($pAs, false, false, true);

    return $as !== null;
  }

  public $dbw;

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

    return $this->dbw;
  }

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

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

  public function getSet()
  {
    $dbwas = \DBWorkHLAussenstellenMinimal::getDefaultInstance();

    return $dbwas->getAussenstellen(false, false, true);
  }
}

class SearchRestrictionKatAsRendererSelect extends SearchRestrictionRendererSelectBase
{
  public function __construct($pFilter = null)
  {
    parent::__construct(new SearchRestrictionKatAsFilter());
  }

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

  protected function getCleanOptionTextFromItem($pItem)
  {
    return \CleanOperations::cleanStringForHtml($pItem->bezeichner_bf !== '' ? $pItem->bezeichner_bf : $pItem->bezeichner);
  }
}

class SearchRestrictionKatAsRendererCheckbox extends SearchRestrictionRendererCheckboxBase
{
  public function __construct($pFilter = null)
  {
    parent::__construct(new SearchRestrictionKatAsFilter());
  }

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

  protected function getCleanOptionTextFromItem($pItem)
  {
    return \CleanOperations::cleanStringForHtml($pItem->bezeichner_bf !== '' ? $pItem->bezeichner_bf : $pItem->bezeichner);
  }
}

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