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

  if (!session_id()) {
    session_start();
  }






class Kursfilter
{
  
  public $clearBlaettern = false;

  
  public function setFilter()
  {
    $this->clearAll();
    $this->checkAussenstFilter();
    $this->checkSemesterFilter();
    $this->checkWochentagFilter();
    $this->checkKursbeginnFilter();
    $this->checkKursendeFilter();
    $this->applyAussenstForm();
    $this->applySemesterForm();
    $this->applyWochentagForm();
    $this->applyZeitraumForm();
    $this->checkKursfieldFilter();
    if ($this->clearBlaettern) {
      $this->_removeBlaettern();
    }
  }

  
  public function _removeblaettern()
  {
    $urlparamNavi = new URLParameterNavigation();
    $urlparamNavi->knr = '';
    $urlparamNavi->browse = '';
    $urlparamNavi->implodeSettings();
    $_REQUEST['urlparameter'] = $urlparamNavi->stream;
  }

  
  public function checkAussenstFilter($pURLParamFilter = null)
  {
    if ($pURLParamFilter == null) {
      $urlparameterFilter = new URLParameterFilter();
    } else {
      $urlparameterFilter = $pURLParamFilter;
    }
    
    if (($urlparameterFilter->clearfilteraussenst != '') || ($urlparameterFilter->filteraussenst != '')) { 
      
      $aussenstellen = explode(DELIMITER_FILTER_AUSSENST, $urlparameterFilter->filteraussenst);
      $warenkorb = new DBWorkWarenkorbAussenstFilter(); 
      $warenkorb->clear(); 
      foreach ($aussenstellen as $aussenstelle) {
        if ($aussenstelle != '') {
          $warenkorb->addEntry($aussenstelle);
        }
      }
      SQLOperations::ReBuildAussenstFilterSQL('k'); 
      $this->clearBlaettern = true; 
    }
  }

  
  public function checkSemesterFilter($pURLParamFilter = null)
  {
    if ($pURLParamFilter == null) {
      $urlparameterFilter = new URLParameterFilter();
    } else {
      $urlparameterFilter = $pURLParamFilter;
    }
    
    if (($urlparameterFilter->clearfiltersem != '') || ($urlparameterFilter->filtersem != '') || ($urlparameterFilter->filtersemtext != '')) {
      $semester = array();
      
      if ($urlparameterFilter->filtersemtext != '') {
        
        $semester = explode(DELIMITER_FILTER_SEMESTER, $urlparameterFilter->filtersemtext);
      }

      
      if ($urlparameterFilter->filtersem != '') {
        
        $semids = explode(DELIMITER_FILTER_SEMESTER, $urlparameterFilter->filtersem);

        
        
        
        $dbwhlsemester = new DBWorkHLSemester();
        
        foreach ($semids as $semid) {
          $sem = $dbwhlsemester->getOne($semid);
          array_push($semester, $sem->sem);
        }
      }
      $warenkorb = new DBWorkWarenkorbSemesterFilter();
      $warenkorb->clear(); 
      
      
      foreach ($semester as $oneSemester) {
        $warenkorb->addEntry($oneSemester);
      }

      SQLOperations::ReBuildSemesterFilterSQL('k'); 
      $this->clearBlaettern = true; 
    }
  }

  
  public function applyAussenstForm()
  {
    
    if (PostOperations::isDefined('aussenstids') && '' !== PostOperations::getVarValue('aussenstids')) {
      $ids = explode(FILTER_FORM_DELIMITER, PostOperations::getVarValue('aussenstids'));

      $warenkorb = new DBWorkWarenkorbAussenstFilter();
      $warenkorb->clear();

      foreach ($ids as $id) {
        
        if (PostOperations::isDefined('aussenst' . $id)) {
          $warenkorb->addEntry($id);
        }
      }
      SQLOperations::ReBuildAussenstFilterSQL('k');
      $this->clearBlaettern = true; 
    }
  }

  
  public function applyWochentagForm($pURLParamFilter = null)
  {
    
    if ($pURLParamFilter == null) {
      $param = new URLParameterFilter();
    } else {
      $param = $pURLParamFilter;
    }
    if ($param->clearwochentagfilter != '') {
      $warenkorb = new DBWorkWarenkorbWochentagFilter();
      $warenkorb->clear();

      
      SQLOperations::ReBuildWochentagFilterSQL();
      $this->clearBlaettern = true; 
    }

    if (PostOperations::isDefined('wochentageFilter')) {
      $warenkorb = new DBWorkWarenkorbWochentagFilter();
      $warenkorb->clear();
      for ($wochentag_id = 0;$wochentag_id < 7;$wochentag_id++) {
        if (PostOperations::isDefined('wochentag' . $wochentag_id)) {
          $warenkorb->addEntry('w' . $wochentag_id);
        }
      }
      SQLOperations::ReBuildWochentagFilterSQL();
      $this->clearBlaettern = true; 
    }
  }

  
  public function applyZeitraumForm($pURLParamFilter = null)
  {

    
    if ($pURLParamFilter == null) {
      $param = new URLParameterFilter();
    } else {
      $param = $pURLParamFilter;
    }
    if ($param->clearzeitraumfilter != '') {
      $warenkorb = new DBWorkWarenkorbZeitraumFilter();
      $warenkorb->clear();

      
      SQLOperations::ReBuildZeitraumFilterSQL('k');
      $this->clearBlaettern = true; 
    }

    if (PostOperations::isDefined('noOfAdd')) {
      $warenkorb = new DBWorkWarenkorbZeitraumFilter();
      $warenkorb->clear();
      
      for ($zb = 0;$zb < 3;$zb++) {
        if (PostOperations::isDefined('zeitraum' . $zb)) {
          $warenkorb->addEntry('zb' . $zb);
        }
      }
      
      for ($zb = 0;$zb < StringOperations::substr(PostOperations::getVarValue('noOfAdd'), 2);$zb++) {
        if (PostOperations::isDefined('zeitraum' . $zb . 'Set')
           && PostOperations::isDefined('zeitraum' . $zb . 'Beginn')
           && PostOperations::isDefined('zeitraum' . $zb . 'Ende')) {
          $wkEntry = 'a' . $zb . '|' . str_replace(':', '', PostOperations::getVarValue('zeitraum' . $zb . 'Beginn')) . '|' . str_replace(':', '', PostOperations::getVarValue('zeitraum' . $zb . 'Ende'));
          $warenkorb->addEntry($wkEntry);
        }
      }
      
      if (PostOperations::isDefined('zeitraum' . StringOperations::substr(PostOperations::getVarValue('noOfAdd'), 2) . 'Beginn')
         && PostOperations::getVarValue('zeitraum' . StringOperations::substr(PostOperations::getVarValue('noOfAdd'), 2) . 'Beginn') != ''
         && PostOperations::isDefined('zeitraum' . StringOperations::substr(PostOperations::getVarValue('noOfAdd'), 2) . 'Ende')
         && PostOperations::getVarValue('zeitraum' . StringOperations::substr(PostOperations::getVarValue('noOfAdd'), 2) . 'Ende')) {
        $beginn = PostOperations::getVarValue('zeitraum' . StringOperations::substr(PostOperations::getVarValue('noOfAdd'), 2) . 'Beginn');
        $ende = PostOperations::getVarValue('zeitraum' . StringOperations::substr(PostOperations::getVarValue('noOfAdd'), 2) . 'Ende');
        
        $beginn = str_replace('.', '', str_replace(':', '', $beginn));
        $ende = str_replace('.', '', str_replace(':', '', $ende));
        $wkEntry = 'a' . StringOperations::substr(PostOperations::getVarValue('noOfAdd'), 2) . '|'
                   . $beginn
                   . '|'
                   . $ende;
        $warenkorb->addEntry($wkEntry);
      }

      SQLOperations::ReBuildZeitraumFilterSQL('k.');
      $this->clearBlaettern = true; 
    }
  }

  
  public function applySemesterForm()
  {
    
    if (PostOperations::isDefined('semesterids')) {
      $semids = explode(FILTER_FORM_DELIMITER, PostOperations::getVarValue('semesterids'));

      $warenkorb = new DBWorkWarenkorbSemesterFilter();
      $warenkorb->clear();
      
      foreach ($semids as $semid) {
        
        if (PostOperations::isDefined('semester' . $semid)) {
          $warenkorb->addEntry($semid); 
        }
      }
      SQLOperations::ReBuildSemesterFilterSQL('k');
      $this->clearBlaettern = true; 
    }
  }

  
  public function checkKursfieldFilter($pURLParamFilter = null)
  {
    if ($pURLParamFilter == null) {
      $param = new URLParameterFilter();
    } else {
      $param = $pURLParamFilter;
    }

    if ($param->clearkursfieldfilter != '') { 

      $warenkorb = new DBWorkWarenkorbKursfieldFilter();
      $warenkorb->clear();

      
      SQLOperations::reBuildKursfieldFilterSQL('k');
      $this->clearBlaettern = true; 
    }

    if ($param->kursfield != '') { 
      
      $fields = explode(DELIMITER_FILTER_KURSFIELD, $param->kursfield);
      
      $values = explode(DELIMITER_FILTER_KURSFIELD, $param->kursvalue);

      
      $allowedFieldnames
        = array('aussenst', 'fachb', 'ausfall', 'ort', 'kinderbetr', 'dauer', 'semester',
              'stundenanz', 'kursauswahl1', 'kursauswahl2', 'kursauswahl3', 'kursauswahl4',
              'kursauswahl5', 'kursauswahl6', 'kursauswahl7', 'kursauswahl8', 'kursauswahl9',
              'kursauswahl0', 'webflags', 'typ', 'kart_key');

      
      $dictionary
        = array('as' => 'aussenst', 'kb' => 'kinderbetr', 's' => 'semester',
              'k1' => 'kursauswahl1', 'k2' => 'kursauswahl2', 'k3' => 'kursauswahl3',
              'k4' => 'kursauswahl4', 'k5' => 'kursauswahl5', 'k6' => 'kursauswahl6',
              'k7' => 'kursauswahl7', 'k8' => 'kursauswahl8', 'k9' => 'kursauswahl9',
              'k10' => 'kursauswahl0', 'fb' => 'fachb', 'af' => 'ausfall',
              'sa' => 'stundenanz', 'd' => 'dauer', 'wf' => 'webflags',
              'veranstart' => 'kart_key');

      $warenkorb = new DBWorkWarenkorbKursfieldFilter();
      $warenkorb->clear();

      
      for ($i = 0; $i < count($fields); $i++) {
        $field = $fields[$i];
        if (isset($dictionary[$field])) { 
          $field = $dictionary[$field];
        }
        $value = $values[$i];

        if (in_array($field, $allowedFieldnames)) { 

          $entry = new KursfieldFilterEntry();
          $entry->id = $field;
          $entry->value = $value;

          
          $warenkorb->addEntry(serialize($entry));
        }
      }

      
      SQLOperations::reBuildKursfieldFilterSQL('k');
      $this->clearBlaettern = true; 
    }
  }

  
  public function checkWochentagFilter($pURLParameter = null)
  {
    if ($pURLParameter == null) {
      $param = new URLParameterFilter();
    } else {
      $param = $pURLParameter;
    }
    if ($param->clearwochentagfilter != '' || $param->filterwochentag != '') {
      $warenkorb = new DBWorkWarenkorbWochentagFilter();
      $warenkorb->clear();

      $wochentag_ids = explode(DELIMITER_FILTER_WOCHENTAG, $param->filterwochentag);
      foreach ($wochentag_ids as $id) {
        if ($id >= 0 && $id <= 6) {
          $warenkorb->addEntry('w' . $id);
        }
      }

      
      SQLOperations::ReBuildWochentagFilterSQL();
      $this->clearBlaettern = true; 
    }
  }

  
  public function checkKursbeginnFilter($pURLParameter = null)
  {
    if ($pURLParameter == null) {
      $param = new URLParameterFilter();
    } else {
      $param = $pURLParameter;
    }
    if ($param->clearkursbeginnfilter != '' || $param->filterkursbeginn != '') {
      if (DateTimeOperations::isValidDate($param->filterkursbeginn)) {
        SessionOperations::setVarValueFilter('WKKURSBEGINNFILTER', $param->filterkursbeginn);
      } else {
        SessionOperations::setVarValueFilter('WKKURSBEGINNFILTER', '');
      }
      SQLOperations::ReBuildKursbeginnFilterSQL('k.'); 
      $this->clearBlaettern = true; 
    }
  }

  
  public function checkKursendeFilter($pURLParameter = null)
  {
    if ($pURLParameter == null) {
      $param = new URLParameterFilter();
    } else {
      $param = $pURLParameter;
    }
    if ($param->clearkursendefilter != '' || $param->filterkursende != '') {
      if (DateTimeOperations::isValidDate($param->filterkursende)) {
        SessionOperations::setVarValueFilter('WKKURSENDEFILTER', $param->filterkursende);
      } else {
        SessionOperations::setVarValueFilter('WKKURSENDEFILTER', '');
      }
      SQLOperations::ReBuildKursendeFilterSQL('k.'); 
      $this->clearBlaettern = true; 
    }
  }

  
  public function clearAll($forceClear = false)
  {
    $param = new URLParameterFilter();
    if ((isset($param->clearallfilter) && $param->clearallfilter != '') || $forceClear) {
      $param->clearfilteraussenst = 'true';
      $param->clearkursfieldfilter = 'true';
      $param->clearfiltersem = 'true';
      $param->clearwochentagfilter = 'true';
      $param->clearzeitraumfilter = 'true';
      $param->clearkursendefilter = 'true';
      $param->clearkursbeginnfilter = 'true';
      $this->checkAussenstFilter($param);
      $this->checkSemesterFilter($param);
      $this->checkKursfieldFilter($param);
      $this->applyWochentagForm($param);
      $this->applyZeitraumForm($param);
      $this->checkKursbeginnFilter($param);
      $this->checkKursendeFilter($param);
    }
  }
}