<?php

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

namespace Kufer\Web\Classes\Api\SearchRestriction;


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

  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 !== '') {
      if (!isset($pFromObject->hauptdozent)) {
        $pFromObject->hauptdozent = new \Dozent();
      }
      $pFromObject->hauptdozent->id = $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 $dozid) {
          if (!$this->isReset($dozid)) {
            $filterKriterium[] = \CleanOperations::cleanInt($dozid, '');
          }
        }
      }
    }

    return $filterKriterium;
  }

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

  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 = \DBWorkDozentenWBMinimal::getDefaultInstance();
    }

    return $this->dbw;
  }

  public function getSet()
  {
    $settings = \ToolSettingsAPI::getToolSettings(4);

    return $this->getDbW()->getDozenten4Filter($settings->semlist, false);
  }
}

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

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

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

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

  
}

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