<?php

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

namespace Kufer\Web\Classes\Api\SearchRestriction;

abstract class SearchRestrictionKatDatumFilter extends SearchRestriction
{
  abstract protected function initSessionAndRequestName();

  abstract protected function setFilterKrit($val, $pFromObject, $pToObject);

  public function __construct()
  {
    $this->initSessionAndRequestName();
  }

  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 !== '' && $this->isValid($filterKriterium)) {
      \SessionOperations::setVarValueFilter($this->sessionName, $this->cleanFilterKriterium($filterKriterium));
    } else {
      if (\RequestOperations::isDefined($this->requestName)) { 
        
        $this->reset();
      }
    }
    $f = \SessionOperations::getVarValueFilter($this->sessionName);
    if ($f !== '') {
      $this->setFilterKrit($f, $pFromObject, $pToObject);
    }
  }

  protected function isValid($pFilterKriterium)
  {
    return \DateTimeOperations::isValidDate($pFilterKriterium);
  }

  protected function cleanFilterKriterium($pFilterKriterium)
  {
    return \DateTimeOperations::getValidDateString($pFilterKriterium);
  }

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

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