<?php

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

namespace Kufer\Web\Classes\Api\SearchRestriction;

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

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

  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 $dbw;

  
  private function getDbW()
  {
    if ($this->dbw == null) {
      $this->dbw = \DBWorkKursorteWB::getDefaultInstance();
    }
    $this->dbw->newSearch();
    $tool4Settings = \ToolSettingsAPI::getToolSettings(4);
    $this->dbw->fromobject->eigenerraum = $tool4Settings->nurEigeneRaeumeAnzeigen;

    return $this->dbw;
  }

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

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

  public function getSet()
  {
    $set = $this->getDbW()->getOrte();

    return \SortFunctions::sortiereByOrtNichtPlz($set);
  }
}


class SearchRestrictionKatOrtPlzFilter extends SearchRestrictionKatOrtFilter
{
  protected 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 $plz) {
          if (!$this->isReset($plz)) {
            $filterKriterium[] = \CleanOperations::cleanInt($plz, '');
          }
        }
      }
    }

    return $filterKriterium;
  }
}

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

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

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

  protected function getSet($pVarName = '', $pVarParam = '')
  {
    $setWithPlz = $this->filter->getSet();
    $orte = array();
    foreach ($setWithPlz as $item) {
      $orte[] = \StringOperations::getOrt($item);
    }

    return array_unique($orte);
  }

  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()
  {
    if (\DBWorkBASysSettings::isUlmVh()) {
      return 'slctfltr';
    }

    return parent::getClass();
  }

  protected function getId($requestName)
  {
    return 'kw_katortortfilterid';
  }
}

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

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

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

  protected function getSet($pVarName = '', $pVarParam = '')
  {
    $setWithPlz = $this->filter->getSet();
    $orte = array();
    foreach ($setWithPlz as $item) {
      $orte[] = \StringOperations::getOrt($item);
    }

    return array_unique($orte);
  }

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

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

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

  protected function getCleanOptionValFromItem($pItem)
  {
    return \CleanOperations::cleanInt(\StringOperations::getPLZ($pItem));
  }

  protected function getCleanOptionTextFromItem($pItem)
  {
    return \CleanOperations::cleanStringForHtml(\StringOperations::getOrt($pItem));
  }
}

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

  protected function getCleanOptionValFromItem($pItem)
  {
    return \CleanOperations::cleanInt(\StringOperations::getPLZ($pItem));
  }

  protected function getCleanOptionTextFromItem($pItem)
  {
    return \CleanOperations::cleanStringForHtml(\StringOperations::getOrt($pItem));
  }
}

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

class SearchRestrictionKatOrtAsRendererSelect extends SearchRestrictionKatOrtRendererSelect
{
  protected function getSet($pVarName = '', $pVarParam = '')
  {
    $dbwas = \DBWorkHLAussenstellenMinimal::getDefaultInstance();
    $ary = $dbwas->getAussenstellen(false, false, true);
    $as = \ArrayOperations::mapAttribut($ary, 'bezeichner');

    return array_unique($as);
  }
}


class SearchRestrictionKatOrtAsRendererCheckbox extends SearchRestrictionKatOrtRendererCheckbox
{
  protected function getSet($pVarName = '', $pVarParam = '')
  {
    $dbwas = \DBWorkHLAussenstellenMinimal::getDefaultInstance();
    $ary = $dbwas->getAussenstellen(false, false, true);
    $as = \ArrayOperations::mapAttribut($ary, 'bezeichner');

    return array_unique($as);
  }

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

class SearchRestrictionKatOrtAsRendererCheckboxAsList extends SearchRestrictionKatOrtAsRendererCheckbox
{
  public function __construct($pFilter = null)
  {
    parent::__construct($pFilter);
    $this->itemDecorator = new ItemDecoratorList();
    $this->decorator = new DecoratorList('kw-katortaslist');
  }
}

class SearchRestrictionKatOrtRaumRendererCheckbox extends SearchRestrictionKatOrtRendererCheckbox
{
  protected function getSet($pVarName = '', $pVarParam = '')
  {
    $dbwhlkursorte = new \DBWorkKursorteWB();
    $ary = $dbwhlkursorte->getKursorteMitKursen();
    $ort = \ArrayOperations::mapAttribut($ary, 'mktext');

    return array_unique($ort);
  }

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

class SearchRestrictionKatOrtRaumRendererCheckboxAsList extends SearchRestrictionKatOrtRaumRendererCheckbox
{
  public function __construct($pFilter = null)
  {
    parent::__construct($pFilter);
    $this->itemDecorator = new ItemDecoratorList();
    $this->decorator = new DecoratorList('kw-katortraumlist');
  }
}

class SearchRestrictionKatOrtRaumRendererSelect extends SearchRestrictionKatOrtRendererSelect
{
  protected function getSet($pVarName = '', $pVarParam = '')
  {
    $dbwhlkursorte = new \DBWorkKursorteWB();
    $ary = $dbwhlkursorte->getKursorteMitKursen();
    $ort = \ArrayOperations::mapAttribut($ary, 'mktext');

    return array_unique($ort);
  }

  protected function getId($requestName)
  {
    return 'kw_katortraumfilterid';
  }
}