cakephp2-php8/lib/Cake/View/Helper/SessionHelper.php

149 lines
4.5 KiB
PHP
Raw Normal View History

<?php
/**
* Session Helper provides access to the Session in the Views.
*
2010-10-03 16:38:58 +00:00
* PHP 5
*
2009-11-06 06:46:59 +00:00
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
2011-05-29 21:31:39 +00:00
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
2011-05-29 21:31:39 +00:00
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
2009-11-06 06:00:11 +00:00
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.View.Helper
* @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)
*/
2010-12-04 18:10:24 +00:00
App::uses('AppHelper', 'View/Helper');
App::uses('CakeSession', 'Model/Datasource');
2010-12-04 18:10:24 +00:00
/**
* Session Helper.
*
* Session reading from the view.
*
* @package Cake.View.Helper
* @link http://book.cakephp.org/view/1465/Session
*/
class SessionHelper extends AppHelper {
/**
* Used to read a session values set in a controller for a key or return values for all keys.
*
* In your view: `$this->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 mixed values from the session vars
* @link http://book.cakephp.org/view/1466/Methods
*/
public function read($name = null) {
return CakeSession::read($name);
}
/**
* Used to check is a session key has been set
*
* In your view: `$this->Session->check('Controller.sessKey');`
*
* @param string $name
* @return boolean
* @link http://book.cakephp.org/view/1466/Methods
*/
public function check($name) {
return CakeSession::check($name);
}
/**
* Returns last error encountered in a session
*
* In your view: `$this->Session->error();`
*
* @return string last error
* @link http://book.cakephp.org/view/1466/Methods
*/
public function error() {
return CakeSession::error();
}
/**
* Used to render the message set in Controller::Session::setFlash()
*
* In your view: $this->Session->flash('somekey');
* Will default to flash if no param is passed
*
* You can pass additional information into the flash message generation. This allows you
* to consolidate all the parameters for a given type of flash message into the view.
*
* {{{
* echo $this->Session->flash('flash', array('params' => array('class' => 'new-flash')));
* }}}
*
2011-08-16 03:55:08 +00:00
* The above would generate a flash message with a custom class name. Using $attrs['params'] you
* can pass additional data into the element rendering that will be made available as local variables
* when the element is rendered:
*
* {{{
* echo $this->Session->flash('flash', array('params' => array('name' => $user['User']['name'])));
* }}}
*
* This would pass the current user's name into the flash message, so you could create peronsonalized
* messages without the controller needing access to that data.
*
2011-08-16 03:55:08 +00:00
* Lastly you can choose the element that is rendered when creating the flash message. Using
* custom elements allows you to fully customize how flash messages are generated.
*
* {{{
* echo $this->Session->flash('flash', array('element' => 'my_custom_element'));
* }}}
*
* @param string $key The [Message.]key you are rendering in the view.
2011-07-29 02:45:47 +00:00
* @param array $attrs Additional attributes to use for the creation of this flash message.
* Supports the 'params', and 'element' keys that are used in the helper.
2011-07-29 02:45:47 +00:00
* @return string
* @link http://book.cakephp.org/view/1466/Methods
* @link http://book.cakephp.org/view/1467/flash
*/
public function flash($key = 'flash', $attrs = array()) {
$out = false;
2011-08-16 03:55:08 +00:00
if (CakeSession::check('Message.' . $key)) {
$flash = CakeSession::read('Message.' . $key);
$message = $flash['message'];
unset($flash['message']);
if (!empty($attrs)) {
$flash = array_merge($flash, $attrs);
}
if ($flash['element'] == 'default') {
$class = 'message';
if (!empty($flash['params']['class'])) {
$class = $flash['params']['class'];
}
$out = '<div id="' . $key . 'Message" class="' . $class . '">' . $message . '</div>';
} elseif ($flash['element'] == '' || $flash['element'] == null) {
$out = $message;
} else {
$tmpVars = $flash['params'];
$tmpVars['message'] = $message;
$out = $this->_View->element($flash['element'], $tmpVars);
}
CakeSession::delete('Message.' . $key);
}
return $out;
}
/**
* Used to check is a session is valid in a view
*
* @return boolean
*/
public function valid() {
return CakeSession::valid();
}
}