<?php

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

namespace Kufer\Web\Classes\Api\SearchRestriction;


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

  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[0]));
      } else {
        $this->reset();
      }
    }
    $krit = \SessionOperations::getVarValueFilter($this->sessionName);
    if ($krit !== '') {
      $pFromObject->neuerkurs = true; 
    }
  }

  private function cleanFilterKriterium($pFilterKriterium)
  {
    if ($pFilterKriterium != 'F') {
      return true;
    } else {
      return false;
    }
  }

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

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

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

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

  protected function getSet($pVarName = '', $pVarParam = '')
  {
    $default = $pVarParam !== '' ? $pVarParam : 'Neu im Programm';
    $result = '1' . static::$mySeparator . $default;

    return array($result);
  }

  protected function getCleanOptionValFromItem($pItem)
  {
    $parts = explode(static::$mySeparator, $pItem);

    return \CleanOperations::cleanStringForHtml($parts[0]);
  }

  protected function getCleanOptionTextFromItem($pItem)
  {
    $parts = explode(static::$mySeparator, $pItem);

    return \CleanOperations::cleanStringForHtml($parts[1]);
  }

  protected function isSelected($itemVal, $filterVal)
  {
    return $itemVal == $filterVal;
  }
}

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