2014-04-23 10:51:50 +08:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Session Helper provides access to the Session in the Views.
|
|
|
|
*
|
2018-03-18 09:24:15 +08:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
|
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2014-04-23 10:51:50 +08:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2018-03-18 09:24:15 +08:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
|
|
|
* @link https://cakephp.org CakePHP(tm) Project
|
2014-04-23 10:51:50 +08:00
|
|
|
* @package Cake.View.Helper
|
|
|
|
* @since CakePHP(tm) v 1.1.7.3328
|
2018-03-18 09:24:15 +08:00
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License
|
2014-04-23 10:51:50 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
App::uses('AppHelper', 'View/Helper');
|
|
|
|
App::uses('CakeSession', 'Model/Datasource');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Session Helper.
|
|
|
|
*
|
|
|
|
* Session reading from the view.
|
|
|
|
*
|
|
|
|
* @package Cake.View.Helper
|
2018-03-18 09:24:15 +08:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-libraries/helpers/session.html
|
2014-04-23 10:51:50 +08:00
|
|
|
*/
|
|
|
|
class SessionHelper extends AppHelper {
|
|
|
|
|
|
|
|
/**
|
2016-02-25 20:25:24 +08:00
|
|
|
* Reads a session value for a key or returns values for all keys.
|
2014-04-23 10:51:50 +08:00
|
|
|
*
|
|
|
|
* 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
|
2018-03-18 09:24:15 +08:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-libraries/helpers/session.html#SessionHelper::read
|
2014-04-23 10:51:50 +08:00
|
|
|
*/
|
|
|
|
public function read($name = null) {
|
|
|
|
return CakeSession::read($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-02-25 20:25:24 +08:00
|
|
|
* Reads and deletes a session value for a key.
|
|
|
|
*
|
|
|
|
* In your view: `$this->Session->consume('Controller.sessKey');`
|
|
|
|
*
|
|
|
|
* @param string $name the name of the session key you want to read
|
|
|
|
* @return mixed values from the session vars
|
|
|
|
*/
|
|
|
|
public function consume($name) {
|
|
|
|
return CakeSession::consume($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if a session key has been set.
|
2014-04-23 10:51:50 +08:00
|
|
|
*
|
|
|
|
* In your view: `$this->Session->check('Controller.sessKey');`
|
|
|
|
*
|
2016-02-25 20:25:24 +08:00
|
|
|
* @param string $name Session key to check.
|
|
|
|
* @return bool
|
2018-03-18 09:24:15 +08:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-libraries/helpers/session.html#SessionHelper::check
|
2014-04-23 10:51:50 +08:00
|
|
|
*/
|
|
|
|
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
|
2018-03-18 09:24:15 +08:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-libraries/helpers/session.html#displaying-notifications-or-flash-messages
|
2014-04-23 10:51:50 +08:00
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*
|
2016-02-25 20:25:24 +08:00
|
|
|
* ```
|
2014-04-23 10:51:50 +08:00
|
|
|
* echo $this->Session->flash('flash', array('params' => array('class' => 'new-flash')));
|
2016-02-25 20:25:24 +08:00
|
|
|
* ```
|
2014-04-23 10:51:50 +08: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:
|
|
|
|
*
|
2016-02-25 20:25:24 +08:00
|
|
|
* ```
|
2014-04-23 10:51:50 +08:00
|
|
|
* echo $this->Session->flash('flash', array('params' => array('name' => $user['User']['name'])));
|
2016-02-25 20:25:24 +08:00
|
|
|
* ```
|
2014-04-23 10:51:50 +08:00
|
|
|
*
|
|
|
|
* This would pass the current user's name into the flash message, so you could create personalized
|
|
|
|
* messages without the controller needing access to that data.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2016-02-25 20:25:24 +08:00
|
|
|
* ```
|
2014-04-23 10:51:50 +08:00
|
|
|
* echo $this->Session->flash('flash', array('element' => 'my_custom_element'));
|
2016-02-25 20:25:24 +08:00
|
|
|
* ```
|
2014-04-23 10:51:50 +08:00
|
|
|
*
|
|
|
|
* If you want to use an element from a plugin for rendering your flash message you can do that using the
|
|
|
|
* plugin param:
|
|
|
|
*
|
2016-02-25 20:25:24 +08:00
|
|
|
* ```
|
2014-04-23 10:51:50 +08:00
|
|
|
* echo $this->Session->flash('flash', array(
|
|
|
|
* 'element' => 'my_custom_element',
|
|
|
|
* 'params' => array('plugin' => 'my_plugin')
|
|
|
|
* ));
|
2016-02-25 20:25:24 +08:00
|
|
|
* ```
|
2014-04-23 10:51:50 +08:00
|
|
|
*
|
|
|
|
* @param string $key The [Message.]key you are rendering in the view.
|
|
|
|
* @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.
|
|
|
|
* @return string
|
2018-03-18 09:24:15 +08:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-libraries/helpers/session.html#SessionHelper::flash
|
2016-02-25 20:25:24 +08:00
|
|
|
* @deprecated 3.0.0 Since 2.7, use FlashHelper::render() instead.
|
2014-04-23 10:51:50 +08:00
|
|
|
*/
|
|
|
|
public function flash($key = 'flash', $attrs = array()) {
|
|
|
|
$out = false;
|
|
|
|
|
|
|
|
if (CakeSession::check('Message.' . $key)) {
|
|
|
|
$flash = CakeSession::read('Message.' . $key);
|
2016-02-25 20:25:24 +08:00
|
|
|
CakeSession::delete('Message.' . $key);
|
2014-04-23 10:51:50 +08:00
|
|
|
|
2018-03-18 09:24:15 +08:00
|
|
|
$out = '';
|
|
|
|
foreach ($flash as $flashArray) {
|
|
|
|
if (!empty($attrs)) {
|
|
|
|
$flashArray = array_merge($flashArray, $attrs);
|
2014-04-23 10:51:50 +08:00
|
|
|
}
|
2018-03-18 09:24:15 +08:00
|
|
|
$flashArray['key'] = $key;
|
|
|
|
$out .= $this->_render($flashArray);
|
2014-04-23 10:51:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to check is a session is valid in a view
|
|
|
|
*
|
2016-02-25 20:25:24 +08:00
|
|
|
* @return bool
|
2018-03-18 09:24:15 +08:00
|
|
|
* @link https://book.cakephp.org/2.0/en/core-libraries/helpers/session.html#SessionHelper::valid
|
2014-04-23 10:51:50 +08:00
|
|
|
*/
|
|
|
|
public function valid() {
|
|
|
|
return CakeSession::valid();
|
|
|
|
}
|
|
|
|
|
2018-03-18 09:24:15 +08:00
|
|
|
/**
|
|
|
|
* Renders a flash message
|
|
|
|
*
|
|
|
|
* @param array $flash Flash message array
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function _render($flash) {
|
|
|
|
$message = $flash['message'];
|
|
|
|
unset($flash['message']);
|
|
|
|
|
|
|
|
if ($flash['element'] === 'default') {
|
|
|
|
$class = 'message';
|
|
|
|
if (!empty($flash['params']['class'])) {
|
|
|
|
$class = $flash['params']['class'];
|
|
|
|
}
|
|
|
|
$out = '<div id="' . $flash['key'] . 'Message" class="' . $class . '">' . $message . '</div>';
|
|
|
|
} elseif (!$flash['element']) {
|
|
|
|
$out = $message;
|
|
|
|
} else {
|
|
|
|
$options = array();
|
|
|
|
if (isset($flash['params']['plugin'])) {
|
|
|
|
$options['plugin'] = $flash['params']['plugin'];
|
|
|
|
}
|
|
|
|
$tmpVars = $flash['params'];
|
|
|
|
$tmpVars['message'] = $message;
|
|
|
|
$tmpVars['key'] = $flash['key'];
|
|
|
|
$out = $this->_View->element($flash['element'], $tmpVars, $options);
|
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
}
|
2014-04-23 10:51:50 +08:00
|
|
|
}
|