<?php

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

namespace Kufer\Web\Classes\Api\SearchRestriction;

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

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

  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 $ort) {
          if (!$this->isReset($ort)) {
            $filterKriterium[] = \FilterBasis::getOnlyAlphaNumeric($ort, ' -');
          }
        }
      }
    }

    return $filterKriterium;
  }

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

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

  public function getSet()
  {
    return \Kurs2::getAllWebinarTypen();
  }
}

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

  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 'webinarfilterclass';
  }
}

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

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

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

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

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