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
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2008-05-30 11:40:08 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.View.Helper
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 1.1.7.3328
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-04 18:10:24 +00:00
|
|
|
|
|
|
|
App::uses('AppHelper', 'View/Helper');
|
2010-12-07 04:32:42 +00:00
|
|
|
App::uses('CakeSession', 'Model/Datasource');
|
2010-12-04 18:10:24 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Session Helper.
|
|
|
|
*
|
|
|
|
* Session reading from the view.
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.View.Helper
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-06-23 00:55:06 +00:00
|
|
|
class SessionHelper extends AppHelper {
|
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.
|
|
|
|
*
|
2011-02-21 19:07:33 +00:00
|
|
|
* In your view: `$this->Session->read('Controller.sessKey');`
|
2008-05-30 11:40:08 +00:00
|
|
|
* Calling the method without a param will return all session vars
|
|
|
|
*
|
|
|
|
* @param string $name the name of the session key you want to read
|
2011-08-01 02:57:17 +00:00
|
|
|
* @return mixed values from the session vars
|
2011-11-14 06:08:20 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html#SessionHelper::read
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function read($name = null) {
|
2010-06-23 00:55:06 +00:00
|
|
|
return CakeSession::read($name);
|
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 key has been set
|
|
|
|
*
|
2011-02-21 19:07:33 +00:00
|
|
|
* In your view: `$this->Session->check('Controller.sessKey');`
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2014-05-29 19:01:20 +00:00
|
|
|
* @param string $name Session key to check.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool
|
2011-11-14 06:08:20 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html#SessionHelper::check
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function check($name) {
|
2010-06-23 00:55:06 +00:00
|
|
|
return CakeSession::check($name);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns last error encountered in a session
|
|
|
|
*
|
2011-02-21 19:07:33 +00:00
|
|
|
* In your view: `$this->Session->error();`
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @return string last error
|
2013-05-06 16:00:06 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html#displaying-notifications-or-flash-messages
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function error() {
|
2010-06-23 00:55:06 +00:00
|
|
|
return CakeSession::error();
|
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 render the message set in Controller::Session::setFlash()
|
|
|
|
*
|
2011-02-21 19:07:33 +00:00
|
|
|
* In your view: $this->Session->flash('somekey');
|
2010-01-25 22:59:05 +00:00
|
|
|
* Will default to flash if no param is passed
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2012-12-22 22:48:15 +00:00
|
|
|
* You can pass additional information into the flash message generation. This allows you
|
2011-02-21 19:07:33 +00:00
|
|
|
* to consolidate all the parameters for a given type of flash message into the view.
|
|
|
|
*
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2011-02-21 19:07:33 +00:00
|
|
|
* echo $this->Session->flash('flash', array('params' => array('class' => 'new-flash')));
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2011-02-21 19:07:33 +00:00
|
|
|
*
|
2011-08-16 03:55:08 +00:00
|
|
|
* The above would generate a flash message with a custom class name. Using $attrs['params'] you
|
2011-02-21 19:07:33 +00:00
|
|
|
* can pass additional data into the element rendering that will be made available as local variables
|
|
|
|
* when the element is rendered:
|
|
|
|
*
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2011-02-21 19:07:33 +00:00
|
|
|
* echo $this->Session->flash('flash', array('params' => array('name' => $user['User']['name'])));
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2011-02-21 19:07:33 +00:00
|
|
|
*
|
2011-12-02 05:58:09 +00:00
|
|
|
* This would pass the current user's name into the flash message, so you could create personalized
|
2011-02-21 19:07:33 +00:00
|
|
|
* 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
|
2011-02-21 19:07:33 +00:00
|
|
|
* custom elements allows you to fully customize how flash messages are generated.
|
|
|
|
*
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2011-02-21 19:07:33 +00:00
|
|
|
* echo $this->Session->flash('flash', array('element' => 'my_custom_element'));
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2011-02-21 19:07:33 +00:00
|
|
|
*
|
2012-07-18 01:55:29 +00:00
|
|
|
* If you want to use an element from a plugin for rendering your flash message you can do that using the
|
2011-11-12 16:54:57 +00:00
|
|
|
* plugin param:
|
|
|
|
*
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2011-11-12 16:54:57 +00:00
|
|
|
* echo $this->Session->flash('flash', array(
|
|
|
|
* 'element' => 'my_custom_element',
|
|
|
|
* 'params' => array('plugin' => 'my_plugin')
|
|
|
|
* ));
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2011-11-12 16:54:57 +00:00
|
|
|
*
|
2008-05-30 11:40:08 +00:00
|
|
|
* @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.
|
2011-02-21 19:07:33 +00:00
|
|
|
* Supports the 'params', and 'element' keys that are used in the helper.
|
2011-07-29 02:45:47 +00:00
|
|
|
* @return string
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html#SessionHelper::flash
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-02-21 19:07:33 +00:00
|
|
|
public function flash($key = 'flash', $attrs = array()) {
|
2009-11-16 23:15:24 +00:00
|
|
|
$out = false;
|
2011-08-16 03:55:08 +00:00
|
|
|
|
2010-06-23 00:55:06 +00:00
|
|
|
if (CakeSession::check('Message.' . $key)) {
|
|
|
|
$flash = CakeSession::read('Message.' . $key);
|
2014-08-23 10:17:12 +00:00
|
|
|
CakeSession::delete('Message.' . $key);
|
2011-02-21 19:07:33 +00:00
|
|
|
$message = $flash['message'];
|
|
|
|
unset($flash['message']);
|
|
|
|
|
|
|
|
if (!empty($attrs)) {
|
|
|
|
$flash = array_merge($flash, $attrs);
|
|
|
|
}
|
2009-11-16 23:15:24 +00:00
|
|
|
|
2013-02-12 02:38:08 +00:00
|
|
|
if ($flash['element'] === 'default') {
|
2010-07-28 03:26:24 +00:00
|
|
|
$class = 'message';
|
2010-06-23 00:55:06 +00:00
|
|
|
if (!empty($flash['params']['class'])) {
|
|
|
|
$class = $flash['params']['class'];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-02-21 19:07:33 +00:00
|
|
|
$out = '<div id="' . $key . 'Message" class="' . $class . '">' . $message . '</div>';
|
2012-09-14 17:26:30 +00:00
|
|
|
} elseif (!$flash['element']) {
|
2011-02-21 19:07:33 +00:00
|
|
|
$out = $message;
|
2010-06-23 00:55:06 +00:00
|
|
|
} else {
|
2011-11-12 16:03:17 +00:00
|
|
|
$options = array();
|
2011-11-12 16:54:57 +00:00
|
|
|
if (isset($flash['params']['plugin'])) {
|
|
|
|
$options['plugin'] = $flash['params']['plugin'];
|
2011-11-12 16:03:17 +00:00
|
|
|
}
|
2010-06-23 00:55:06 +00:00
|
|
|
$tmpVars = $flash['params'];
|
2011-02-21 19:07:33 +00:00
|
|
|
$tmpVars['message'] = $message;
|
2011-11-12 16:03:17 +00:00
|
|
|
$out = $this->_View->element($flash['element'], $tmpVars, $options);
|
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
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool
|
2011-11-30 05:23:02 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html#SessionHelper::valid
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function valid() {
|
2010-06-23 00:55:06 +00:00
|
|
|
return CakeSession::valid();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2012-03-03 22:10:12 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|