<?php

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

namespace Kufer\Web\Classes\Api\SearchRestriction;

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

  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));
      }
    }
    $kurstyp = \SessionOperations::getVarValueFilter($this->sessionName);
    if ($kurstyp !== '') {
      $pFromObject->setValue2Var('typ', $kurstyp);
    }
  }

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

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

    return $filterKriterium;
  }

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

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

  public function getSet()
  {
    $dbwkurse = new \DBWorkKurse();
    foreach($dbwkurse->getKursTypenInDB() as $kurstyp){
      $bezeichnung = $dbwkurse->getKurstypBezeichnung4Kurstyp($kurstyp);
      $obj = new \StdClass();
      $kurstypnormal = $kurstyp == '' ? ' ' : $kurstyp;
      $obj->id = $kurstypnormal;
      $obj->text = $bezeichnung;
      $arraykurstypen[] = $obj;
    }
    return $arraykurstypen;
  }
}

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

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

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

  protected function isSelected($itemVal, $filterVal)
  {
    $val = \StringOperations::htmlUmlautEntityToUmlaut($itemVal);
    if (!is_array($filterVal)) {
      return $val === $filterVal;
    } else {
      return in_array($val, $filterVal);
    }
  }

  protected function getClass()
  {
    return 'kurstypfilterclass';
  }
}

class SearchRestrictionKurstypRendererCheckbox extends SearchRestrictionRendererCheckboxBase
{
  public function __construct($pFilter = null)
  {
    parent::__construct(new SearchRestrictionKurstypFilter());
    $this->itemDecorator = new ItemDecoratorDivClass();
  }

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

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

  protected function getClass()
  {
    return 'kurstypfilterclass';
  }
}

class SearchRestrictionKurstypRendererCheckboxAsList extends \Kufer\Web\Classes\Api\SearchRestriction\SearchRestrictionKurstypRendererCheckbox
{
  public function __construct($pFilter = null)
  {
    parent::__construct($pFilter);
    $this->itemDecorator = new ItemDecoratorListDivClass();
    $this->decorator = new DecoratorList('kw-kurstyplist');
  }
}