mirror of
https://github.com/FH-Complete/FHC-Core.git
synced 2026-07-19 16:02:15 +00:00
- Added helper session_helper to add utility functions to manage the php session
- Adapted the library FiltersLib to use the session_helper
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* FH-Complete
|
||||
*
|
||||
* @package FHC-Helper
|
||||
* @author FHC-Team
|
||||
* @copyright Copyright (c) 2016 fhcomplete.org
|
||||
* @license GPLv3
|
||||
* @since Version 1.0.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Message Helper
|
||||
*
|
||||
* @subpackage Helpers
|
||||
* @category Helpers
|
||||
*/
|
||||
|
||||
if (! defined('BASEPATH')) exit('No direct script access allowed');
|
||||
|
||||
// -------------------------------------------------------------------------------------------------------
|
||||
// Collection of functions to handle comfortably the php session.
|
||||
// It works keeping a different session name for each functionality (ex. FilterWidget and NavigationWidget)
|
||||
// -------------------------------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Returns the whole session by its name given as parameter
|
||||
* If it's not present the a null value is returned
|
||||
*/
|
||||
function getSession($sessionName)
|
||||
{
|
||||
$session = null;
|
||||
|
||||
// If it is present a session for this filter
|
||||
if (isset($_SESSION[$sessionName]))
|
||||
{
|
||||
$session = $_SESSION[$sessionName];
|
||||
}
|
||||
|
||||
return $session;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns one element specified by the paraemter name, from the session specified by the parameters sessionName
|
||||
* If it's not present the a null value is returned
|
||||
*/
|
||||
function getElementSession($sessionName, $name)
|
||||
{
|
||||
$session = getSession($sessionName); // get the whole session for this filter
|
||||
|
||||
if (isset($session[$name]))
|
||||
{
|
||||
return $session[$name];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the whole session specified by the parameters sessionName
|
||||
*/
|
||||
function setSession($sessionName, $data)
|
||||
{
|
||||
// If is NOT already present into the session
|
||||
if (!isset($_SESSION[$sessionName])
|
||||
|| (isset($_SESSION[$sessionName]) && !is_array($_SESSION[$sessionName])))
|
||||
{
|
||||
$_SESSION[$sessionName] = array(); // then create it
|
||||
}
|
||||
|
||||
$_SESSION[$sessionName] = $data; // stores data
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets one element of the session specified by the parameters sessionName
|
||||
*/
|
||||
function setElementSession($sessionName, $name, $value)
|
||||
{
|
||||
// If is NOT already present into the session
|
||||
if (!isset($_SESSION[$sessionName])
|
||||
|| (isset($_SESSION[$sessionName]) && !is_array($_SESSION[$sessionName])))
|
||||
{
|
||||
$_SESSION[$sessionName] = array(); // then create it
|
||||
}
|
||||
|
||||
$_SESSION[$sessionName][$name] = $value; // stores the single value
|
||||
}
|
||||
@@ -68,7 +68,7 @@ class FiltersLib
|
||||
|
||||
const FILTER_PHRASES_CATEGORY = 'FilterWidget'; // The category used to store phrases for the FilterWidget
|
||||
|
||||
const FILTER_PAGE_PARAM = 'filter_page'; // Filter page parameter used to overwrite the page URI
|
||||
const FILTER_PAGE_PARAM = 'filter_page'; // Filter page parameter name
|
||||
|
||||
private $_ci; // Code igniter instance
|
||||
private $_filterUniqueId; // unique id for this filter widget
|
||||
@@ -78,10 +78,12 @@ class FiltersLib
|
||||
*/
|
||||
public function __construct($params = null)
|
||||
{
|
||||
$this->_ci =& get_instance();
|
||||
$this->_ci =& get_instance(); // get code igniter instance
|
||||
|
||||
// Loads helper message to manage returning messages
|
||||
$this->_ci->load->helper('message');
|
||||
// Loads helper session to manage the php session
|
||||
$this->_ci->load->helper('session');
|
||||
|
||||
$this->_filterUniqueId = $this->_getFilterUniqueId($params); // sets the id for the related filter widget
|
||||
}
|
||||
@@ -90,27 +92,19 @@ class FiltersLib
|
||||
// Public methods
|
||||
|
||||
/**
|
||||
* Returns the whole session for this filter widget if found, otherwise null
|
||||
* Wrapper method to the session helper funtions to retrive the whole session for this filter
|
||||
*/
|
||||
public function getSession()
|
||||
{
|
||||
$session = null;
|
||||
|
||||
// If it is present a session for this filter
|
||||
if (isset($_SESSION[self::SESSION_NAME]) && isset($_SESSION[self::SESSION_NAME][$this->_filterUniqueId]))
|
||||
{
|
||||
$session = $_SESSION[self::SESSION_NAME][$this->_filterUniqueId];
|
||||
}
|
||||
|
||||
return $session;
|
||||
return getElementSession(self::SESSION_NAME, $this->_filterUniqueId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns one element from the session of this filter widget, otherwise null
|
||||
* Wrapper method to the session helper funtions to retrive one element from the session of this filter
|
||||
*/
|
||||
public function getElementSession($name)
|
||||
{
|
||||
$session = $this->getSession(); // get the whole session for this filter
|
||||
$session = getElementSession(self::SESSION_NAME, $this->_filterUniqueId);
|
||||
|
||||
if (isset($session[$name]))
|
||||
{
|
||||
@@ -121,41 +115,23 @@ class FiltersLib
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the whole session for this filter widget
|
||||
* Wrapper method to the session helper funtions to set the whole session for this filter
|
||||
*/
|
||||
public function setSession($data)
|
||||
{
|
||||
// If is NOT already present into the session
|
||||
if (!isset($_SESSION[self::SESSION_NAME])
|
||||
|| (isset($_SESSION[self::SESSION_NAME]) && !is_array($_SESSION[self::SESSION_NAME])))
|
||||
{
|
||||
$_SESSION[self::SESSION_NAME] = array(); // then create it
|
||||
}
|
||||
|
||||
$_SESSION[self::SESSION_NAME][$this->_filterUniqueId] = $data; // stores data
|
||||
setElementSession(self::SESSION_NAME, $this->_filterUniqueId, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets one element of the session of this filter widget
|
||||
* Wrapper method to the session helper funtions to set one element in the session for this filter
|
||||
*/
|
||||
public function setElementSession($name, $value)
|
||||
{
|
||||
// If is NOT already present into the session
|
||||
if (!isset($_SESSION[self::SESSION_NAME])
|
||||
|| (isset($_SESSION[self::SESSION_NAME]) && !is_array($_SESSION[self::SESSION_NAME])))
|
||||
{
|
||||
$_SESSION[self::SESSION_NAME] = array(); // then create it
|
||||
}
|
||||
$session = getElementSession(self::SESSION_NAME, $this->_filterUniqueId);
|
||||
|
||||
// If the session for this filter is NOT already present into the session
|
||||
if (!isset($_SESSION[self::SESSION_NAME][$this->_filterUniqueId])
|
||||
|| (isset($_SESSION[self::SESSION_NAME][$this->_filterUniqueId])
|
||||
&& !is_array($_SESSION[self::SESSION_NAME][$this->_filterUniqueId])))
|
||||
{
|
||||
$_SESSION[self::SESSION_NAME][$this->_filterUniqueId] = array(); // then create it
|
||||
}
|
||||
$session[$name] = $value;
|
||||
|
||||
$_SESSION[self::SESSION_NAME][$this->_filterUniqueId][$name] = $value; // stores the single value
|
||||
setElementSession(self::SESSION_NAME, $this->_filterUniqueId, $session); // stores the single value
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user