2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2009-09-01 04:03:56 +00:00
|
|
|
* Session Helper provides access to the Session in the Views.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Long description for file
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
|
|
|
* Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.view.helpers
|
|
|
|
* @since CakePHP(tm) v 1.1.7.3328
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2008-11-08 02:54:07 +00:00
|
|
|
if (!class_exists('cakesession')) {
|
2009-07-23 20:07:03 +00:00
|
|
|
require LIBS . 'cake_session.php';
|
2008-11-08 02:54:07 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Session Helper.
|
|
|
|
*
|
|
|
|
* Session reading from the view.
|
|
|
|
*
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.view.helpers
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
class SessionHelper extends CakeSession {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* List of helpers used by this helper
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
var $helpers = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Used to determine if methods implementation is used, or bypassed
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
var $__active = true;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Class constructor
|
|
|
|
*
|
|
|
|
* @param string $base
|
|
|
|
*/
|
|
|
|
function __construct($base = null) {
|
|
|
|
if (Configure::read('Session.start') === true) {
|
|
|
|
parent::__construct($base, false);
|
|
|
|
} else {
|
|
|
|
$this->__active = false;
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Turn sessions on if 'Session.start' is set to false in core.php
|
|
|
|
*
|
|
|
|
* @param string $base
|
|
|
|
*/
|
|
|
|
function activate($base = null) {
|
|
|
|
$this->__active = true;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Used to read a session values set in a controller for a key or return values for all keys.
|
|
|
|
*
|
|
|
|
* In your view: $session->read('Controller.sessKey');
|
|
|
|
* Calling the method without a param will return all session vars
|
|
|
|
*
|
|
|
|
* @param string $name the name of the session key you want to read
|
|
|
|
*
|
|
|
|
* @return values from the session vars
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function read($name = null) {
|
|
|
|
if ($this->__active === true && $this->__start()) {
|
|
|
|
return parent::read($name);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Used to check is a session key has been set
|
|
|
|
*
|
|
|
|
* In your view: $session->check('Controller.sessKey');
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return boolean
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function check($name) {
|
|
|
|
if ($this->__active === true && $this->__start()) {
|
|
|
|
return parent::check($name);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns last error encountered in a session
|
|
|
|
*
|
|
|
|
* In your view: $session->error();
|
|
|
|
*
|
|
|
|
* @return string last error
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function error() {
|
|
|
|
if ($this->__active === true && $this->__start()) {
|
|
|
|
return parent::error();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Used to render the message set in Controller::Session::setFlash()
|
|
|
|
*
|
|
|
|
* In your view: $session->flash('somekey');
|
|
|
|
* Will default to flash if no param is passed
|
|
|
|
*
|
|
|
|
* @param string $key The [Message.]key you are rendering in the view.
|
2009-11-16 23:15:24 +00:00
|
|
|
* @return boolean|string Will return the value if $key is set, or false if not set.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function flash($key = 'flash') {
|
2009-11-16 23:15:24 +00:00
|
|
|
$out = false;
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($this->__active === true && $this->__start()) {
|
|
|
|
if (parent::check('Message.' . $key)) {
|
|
|
|
$flash = parent::read('Message.' . $key);
|
|
|
|
|
2009-09-01 04:03:56 +00:00
|
|
|
if ($flash['element'] == 'default') {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!empty($flash['params']['class'])) {
|
|
|
|
$class = $flash['params']['class'];
|
|
|
|
} else {
|
|
|
|
$class = 'message';
|
|
|
|
}
|
|
|
|
$out = '<div id="' . $key . 'Message" class="' . $class . '">' . $flash['message'] . '</div>';
|
2009-09-01 04:03:56 +00:00
|
|
|
} elseif ($flash['element'] == '' || $flash['element'] == null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$out = $flash['message'];
|
|
|
|
} else {
|
|
|
|
$view =& ClassRegistry::getObject('view');
|
2009-09-01 04:03:56 +00:00
|
|
|
$tmpVars = $flash['params'];
|
|
|
|
$tmpVars['message'] = $flash['message'];
|
|
|
|
$out = $view->element($flash['element'], $tmpVars);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-10-06 12:42:35 +00:00
|
|
|
parent::delete('Message.' . $key);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-11-16 23:15:24 +00:00
|
|
|
return $out;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Used to check is a session is valid in a view
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function valid() {
|
|
|
|
if ($this->__active === true && $this->__start()) {
|
|
|
|
return parent::valid();
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Override CakeSession::write().
|
|
|
|
* This method should not be used in a view
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function write() {
|
|
|
|
trigger_error(__('You can not write to a Session from the view', true), E_USER_WARNING);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Session id
|
|
|
|
*
|
|
|
|
* @return string Session id
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function id() {
|
|
|
|
return parent::id();
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Determine if Session has been started
|
|
|
|
* and attempt to start it if not
|
|
|
|
*
|
|
|
|
* @return boolean true if Session is already started, false if
|
|
|
|
* Session could not be started
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function __start() {
|
2008-10-23 00:10:44 +00:00
|
|
|
if (!parent::started()) {
|
2008-05-30 11:40:08 +00:00
|
|
|
parent::start();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|