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

class IndexOperations
{
  
  public static function setFontSize()
  {
    
    $urlparam = new UrlParameterFontSize();
    if ($urlparam->size != 0) {
      SessionOperations::setVarValueIntern(FONTSIZE_SESSION_VAR, $urlparam->size);
    } elseif (SessionOperations::getVarValueIntern(FONTSIZE_SESSION_VAR, '###') == '###') {
      SessionOperations::setVarValueIntern(FONTSIZE_SESSION_VAR, FONTSIZE_NORMAL);
    }
  }

  
  public static function setAnmeldungSessionVars()
  {
    $tmpParam = new URLParameterHauptmenue();

    
    
    if (($tmpParam->kathaupt == 4) || ($tmpParam->kathaupt == 5)) {
      $param = new URLParameterAnmeldungArt();
      
      $oldAnmeldeArt = SessionOperations::getVarValueAnm(ANMELDEPFAD_ART, 0);

      
      
      if (($oldAnmeldeArt != 0) && ($oldAnmeldeArt != $param->anmart)) {
        IndexOperations::unsetAnmeldungSessionVars();
      }

      
      $oldSchritt = SessionOperations::getVarValueAnm(ANMELDEPFAD_SCHRITTMAX, 0);
      
      SessionOperations::setVarValueAnm(ANMELDEPFAD_SCHRITTMAX, $param->schritt);

      
      SessionOperations::setVarValueAnm(ANMELDEPFAD_ART, $param->anmart);

      
      SessionOperations::setVarValueAnm(ANMELDEPFAD_SCHRITT, $param->schritt);

      
      
      



    }
  }

  
  public static function unsetAnmeldungSessionVars()
  {
    SessionOperations::setVarValueAnm(ANMELDEPFAD_ART, null);
    SessionOperations::setVarValueAnm(ANMELDEPFAD_SCHRITTMAX, null);
    SessionOperations::setVarValueAnm(ANMELDEPFAD_SCHRITT, null);
  }

  
  public static function interpretWK($pURLParameter)
  {

    
    if (isset($pURLParameter->warenkorbclear) && $pURLParameter->warenkorbclear == 'true') {
      WarenkorbAPI::clear();
    }
    
    if (isset($pURLParameter->knradd) && $pURLParameter->knradd != '') {
      WarenkorbAPI::add($pURLParameter->knradd);
    }
    
    if (isset($pURLParameter->knrdel) && $pURLParameter->knrdel != '') {
      WarenkorbAPI::del($pURLParameter->knrdel);
    }
    if (isset($pURLParameter->payOnline) && $pURLParameter->payOnline == true) {
      WarenkorbAPI::returnEPayment($pURLParameter->message);
    }

    self::interpretWKBeratungstage($pURLParameter);
    self::interpretWKGrillhuetten($pURLParameter);
    self::interpretWKArtikel($pURLParameter);
  }

  
  public static function interpretML($pURLParameter)
  {
    
    if (isset($pURLParameter->mladd) && $pURLParameter->mladd != '' && Xsrf::checkAndUseXsrf4KuferWEB('WbMerklisteAdd', $pURLParameter->mladd) == Xsrf::KEY_OK) {
      MerklisteAPI::add($pURLParameter->mladd);
    }
    
    if (isset($pURLParameter->mldel) && $pURLParameter->mldel != '' && Xsrf::checkAndUseXsrf4KuferWEB('WbMerklisteDel', $pURLParameter->mldel) == Xsrf::KEY_OK) {
      MerklisteAPI::delete($pURLParameter->mldel);
    }
  }

  
  private static function interpretWKBeratungstage($pURLParameter)
  {
    if (WbPluginBasis::pluginExists(PLUGIN_BERATUNGSTAGE)) {
      
      if (isset($pURLParameter->knrbtadd) && $pURLParameter->knrbtadd != '') {
        WarenkorbAPIBeratungstage::add($pURLParameter->knrbtadd);
      }
      
      if (isset($pURLParameter->knrbtdel) && $pURLParameter->knrbtdel != '') {
        WarenkorbAPIBeratungstage::del($pURLParameter->knrbtdel);
      }
      
      if (isset($pURLParameter->warenkorbbtclear) && $pURLParameter->warenkorbbtclear == 'true') {
        WarenkorbAPIBeratungstage::clear();
      }
    }
  }

  
  private static function interpretWKGrillhuetten($pURLParameter)
  {
    if (WbPluginBasis::pluginExists(PLUGIN_GRILLHUETTENVERWALTUNG)) {
      
      if (isset($pURLParameter->knrghadd) && $pURLParameter->knrghadd != '') {
        WarenkorbAPIGrillhuetten::clear();
        WarenkorbAPIGrillhuetten::add($pURLParameter->knrghadd);
      }
      
      if (isset($pURLParameter->knrghdel) && $pURLParameter->knrghdel != '') {
        WarenkorbAPIGrillhuetten::del($pURLParameter->knrghdel);
      }
      
      if (isset($pURLParameter->warenkorbghclear) && $pURLParameter->warenkorbghclear == 'true') {
        WarenkorbAPIGrillhuetten::clear();
      }
    }
  }

  
  private static function interpretWKArtikel($pURLParameter)
  {
    if (isset($pURLParameter->articleadd) && $pURLParameter->articleadd != '') {
      WarenkorbArtikelApi::add($pURLParameter->articleadd, RequestOperations::getVarValue('articlequantity', 1));
    }
    if (isset($pURLParameter->articledel) && $pURLParameter->articledel != '') {
      WarenkorbArtikelApi::del($pURLParameter->articledel);
    }
  }

  
  public static function setHistory()
  {
    
    $urlparams = new URLParameterNavigation();
    $urlparams->explodeSettingStream();

    
    if ($urlparams->isstatic == true) {
      return;
    }

    if ($urlparams->suchesetzen) {
      unset($urlparams->suchesetzen);
    }
    $urlparams->implodeSettings();
    if ($urlparams->stream == '') {
      return;
    }
    $act_stream = $urlparams->stream . '&history=back';

    
    
    $history = SessionOperations::getVarValueIntern('history', array());
    $history_referer = SessionOperations::getVarValueIntern('history_referer', '');
    SessionOperations::setVarValueIntern('history_referer', $act_stream);

    
    if (isset($_GET['history']) && $_GET['history'] == 'reset') {
      $history = array();
      SessionOperations::setVarValueIntern('history', $history);
    }

    
    
    if ($history_referer == $act_stream) {
      return;
    }

    
    
    
    if (isset($_GET['history']) && $_GET['history'] == 'back') {
      array_pop($history);

    
    
    } elseif ($history_referer != '') {
      $filterFactory = new FilterFactory();
      $filter = $filterFactory->getFilterClass();
      $urlparams->stream = urldecode($history_referer);
      $urlparams->stream = $filter->filterURLParameter($urlparams->stream);
      $urlparams->explodeSettingStream();
      if (!in_array($urlparams->kathaupt, array(5, 202, 203, 206, 207))) {
        $history[] = $history_referer;
      }
    }

    
    $count = count($history);
    while (count($history) > 100) {
      array_shift($history);
    }

    SessionOperations::setVarValueIntern('history', $history);
  }

  public static function decodeToUrlParameter($stream)
  {
    $filterFactory = new FilterFactory();
    $filter = $filterFactory->getFilterClass();
    $urlparam = new URLParameterNavigation(false);
    $urlparam->stream = urldecode($stream);
    $urlparam->stream = $filter->filterURLParameter($urlparam->stream);
    $urlparam->explodeSettingStream();

    return $urlparam;
  }

  
  public static function setUserSessionVars()
  {
    for ($i = 1; $i <= 10; $i++) {
      $varname = 'kw_session_put_' . $i;
      if (RequestOperations::isDefined($varname)) {
        if (RequestOperations::getVarValue($varname, false) != '') {
          $wert = RequestOperations::getVarValue($varname, false);
          SessionOperations::setVarvalueIntern($varname, $wert);
        }
        
        else {
          SessionOperations::setVarvalueIntern($varname, null);
        }
      }
    }
  }

  
}