<?php

function getRequestAndSessionSave($requestName, $sessionName, $smartyName=false, $type="txt", $default=null, $restrictList=false, $restrictListByKey=false) {
    $val = $default;
    
    // LOAD REQUEST VALUE
    if(isset($_REQUEST[$requestName])) {
        $v = convertValTo( $_REQUEST[$requestName], $type);
        if(is_array($restrictList)) {
            if($restrictListByKey && array_key_exists($v, $restrictList)) $val = $v;
            else if(in_array($v, $restrictList)) $val = $v;
        }
        else $val = $v;
    }
    
    // LOAD SESSION VAL
    else if(isset($_SESSION[$sessionName])) {
        $v = convertValTo( $_SESSION[$sessionName], $type);
        if(is_array($restrictList)) {
            if($restrictListByKey && array_key_exists($v, $restrictList)) $val = $v;
            else if(in_array($v, $restrictList)) $val = $v;
        }
        else $val = $v;
    }
    
    // SAVE SESSION VAL
    $_SESSION[$sessionName] = $val;
    
    // ASSIGN TO SMARTY
    if($smartyName) $GLOBALS['smarty'] -> assign(strval($smartyName),$val);
    
    return $val;
}

function convertValTo($val, $type) {
    switch(type) {
        case "int":     $val = intval($val);            break;
        case "float":   $val = floatval($val);          break;
        case "bool":    $val = ($val || (int)$val==1);  break;
        case "string":  $val = strval($val);            break;
    }
    return $val;
}

function getSessionListFiltre($filtre, $list, $default=0, $intVal=true) {
    $val = $default;
    
    if(isset($_SESSION[$filtre])) {
        // INT VAL
        if($intVal) {
            if((int)$_SESSION[$filtre]>0 && array_key_exists($_SESSION[$filtre],$list)) $val = intval($_SESSION[$filtre]);
            else if((int)$_SESSION[$filtre] == $default) $val = $default;
        }
        // TEXT VAL
        else {
            if(array_key_exists($_SESSION[$filtre],$list)) $val = $_SESSION[$filtre];
            else if($_SESSION[$filtre] == $default) $val = $default;
        }
    }
    
    return $val;
}

function getListFiltre($filtre, $list, $default=0, $intVal=true) {
    // SESSION VAL
    $val = getSessionListFiltre($filtre, $list, $default, $intVal);
    
    // REQUEST VAL
    if(isset($_REQUEST[$filtre])) {
        // INT VAL
        if($intVal) {
            if((int)$_REQUEST[$filtre]>0 && array_key_exists($_REQUEST[$filtre],$list)) $val = intval($_REQUEST[$filtre]);
            else if((int)$_REQUEST[$filtre] == $default) $val = $default;
        }
        // TEXT VAL
        else {
            if(array_key_exists($_REQUEST[$filtre],$list)) $val = $_REQUEST[$filtre];
            else if($_REQUEST[$filtre] == $default) $val = $default;
        }
    }
    
    // ASSIGN & SAVE
    $_SESSION[$filtre] = $val;
    $GLOBALS['smarty'] -> assign($filtre, $val);
    
    return $val;
}

// SEARCH

function getSearch($session_search) {
    $search = false;
    
    // SESSION VAL
    if(isset($_SESSION[$session_search])) $search = $_SESSION[$session_search];
    
    // REQUEST VAL
    if(isset($_REQUEST["search"]) && $_REQUEST["search"]!="") $search = $_REQUEST["search"];
    
    // CLEAR VAL
    if(isset($_REQUEST["clearSearch"])) $search = false;
    
    // ASSIGN & SAVE
    $_SESSION[$session_search] = $search;
    $GLOBALS['smarty'] -> assign("search", $search);
    
    return $search;
}

// ORDER

function getListOrder($session_order_var, $session_sens_var, $allowOrder, $defaultOrder = 'ref', $defaultSens = "ASC") {
    /*** ORDER ***/
    $order = $defaultOrder;
    
    // SESSION VAL
    if(isset($_SESSION[$session_order_var]) && array_key_exists($_SESSION[$session_order_var],$allowOrder)) $order = $_SESSION[$session_order_var];
    
    // REQUEST VAL
    if(isset($_REQUEST['order']) && array_key_exists($_REQUEST['order'],$allowOrder)) $order = $_REQUEST['order'];
    
    // SAVE & ASSIGN VAL
    $_SESSION[$session_order_var] = $order;
    $GLOBALS['smarty'] -> assign('order',$order);
    $GLOBALS['smarty'] -> assign('allowOrder',$allowOrder);
    
    /*** SENS ***/
    $sens = $defaultSens;
    
    // SESSION VAL
    if(isset($_SESSION[$session_sens_var]) && ($_SESSION[$session_sens_var]=='ASC' || $_SESSION[$session_sens_var]=='DESC')) $sens = $_SESSION[$session_sens_var];
    
    // REQUEST VAL
    if(isset($_REQUEST['sens']) && ($_REQUEST['sens']=='ASC' || $_REQUEST['sens']=='DESC')) $sens = $_REQUEST['sens'];
    
    // SAVE & ASSIGN VAL
    $_SESSION[$session_sens_var] = $sens;
    $GLOBALS['smarty'] -> assign('sens',$sens);
    
    return array("order" => $order, "sens" => $sens);
}