1: <?php
2: /**
3: * Session Helper provides access to the Session in the Views.
4: *
5: * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
6: * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
7: *
8: * Licensed under The MIT License
9: * For full copyright and license information, please see the LICENSE.txt
10: * Redistributions of files must retain the above copyright notice.
11: *
12: * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
13: * @link https://cakephp.org CakePHP(tm) Project
14: * @package Cake.View.Helper
15: * @since CakePHP(tm) v 1.1.7.3328
16: * @license https://opensource.org/licenses/mit-license.php MIT License
17: */
18:
19: App::uses('AppHelper', 'View/Helper');
20: App::uses('CakeSession', 'Model/Datasource');
21:
22: /**
23: * Session Helper.
24: *
25: * Session reading from the view.
26: *
27: * @package Cake.View.Helper
28: * @link https://book.cakephp.org/2.0/en/core-libraries/helpers/session.html
29: */
30: class SessionHelper extends AppHelper {
31:
32: /**
33: * Reads a session value for a key or returns values for all keys.
34: *
35: * In your view: `$this->Session->read('Controller.sessKey');`
36: * Calling the method without a param will return all session vars
37: *
38: * @param string $name the name of the session key you want to read
39: * @return mixed values from the session vars
40: * @link https://book.cakephp.org/2.0/en/core-libraries/helpers/session.html#SessionHelper::read
41: */
42: public function read($name = null) {
43: return CakeSession::read($name);
44: }
45:
46: /**
47: * Reads and deletes a session value for a key.
48: *
49: * In your view: `$this->Session->consume('Controller.sessKey');`
50: *
51: * @param string $name the name of the session key you want to read
52: * @return mixed values from the session vars
53: */
54: public function consume($name) {
55: return CakeSession::consume($name);
56: }
57:
58: /**
59: * Checks if a session key has been set.
60: *
61: * In your view: `$this->Session->check('Controller.sessKey');`
62: *
63: * @param string $name Session key to check.
64: * @return bool
65: * @link https://book.cakephp.org/2.0/en/core-libraries/helpers/session.html#SessionHelper::check
66: */
67: public function check($name) {
68: return CakeSession::check($name);
69: }
70:
71: /**
72: * Returns last error encountered in a session
73: *
74: * In your view: `$this->Session->error();`
75: *
76: * @return string last error
77: * @link https://book.cakephp.org/2.0/en/core-libraries/helpers/session.html#displaying-notifications-or-flash-messages
78: */
79: public function error() {
80: return CakeSession::error();
81: }
82:
83: /**
84: * Used to render the message set in Controller::Session::setFlash()
85: *
86: * In your view: $this->Session->flash('somekey');
87: * Will default to flash if no param is passed
88: *
89: * You can pass additional information into the flash message generation. This allows you
90: * to consolidate all the parameters for a given type of flash message into the view.
91: *
92: * ```
93: * echo $this->Session->flash('flash', array('params' => array('class' => 'new-flash')));
94: * ```
95: *
96: * The above would generate a flash message with a custom class name. Using $attrs['params'] you
97: * can pass additional data into the element rendering that will be made available as local variables
98: * when the element is rendered:
99: *
100: * ```
101: * echo $this->Session->flash('flash', array('params' => array('name' => $user['User']['name'])));
102: * ```
103: *
104: * This would pass the current user's name into the flash message, so you could create personalized
105: * messages without the controller needing access to that data.
106: *
107: * Lastly you can choose the element that is rendered when creating the flash message. Using
108: * custom elements allows you to fully customize how flash messages are generated.
109: *
110: * ```
111: * echo $this->Session->flash('flash', array('element' => 'my_custom_element'));
112: * ```
113: *
114: * If you want to use an element from a plugin for rendering your flash message you can do that using the
115: * plugin param:
116: *
117: * ```
118: * echo $this->Session->flash('flash', array(
119: * 'element' => 'my_custom_element',
120: * 'params' => array('plugin' => 'my_plugin')
121: * ));
122: * ```
123: *
124: * @param string $key The [Message.]key you are rendering in the view.
125: * @param array $attrs Additional attributes to use for the creation of this flash message.
126: * Supports the 'params', and 'element' keys that are used in the helper.
127: * @return string
128: * @link https://book.cakephp.org/2.0/en/core-libraries/helpers/session.html#SessionHelper::flash
129: * @deprecated 3.0.0 Since 2.7, use FlashHelper::render() instead.
130: */
131: public function flash($key = 'flash', $attrs = array()) {
132: $out = false;
133:
134: if (CakeSession::check('Message.' . $key)) {
135: $flash = CakeSession::read('Message.' . $key);
136: CakeSession::delete('Message.' . $key);
137:
138: $out = '';
139: foreach ($flash as $flashArray) {
140: if (!empty($attrs)) {
141: $flashArray = array_merge($flashArray, $attrs);
142: }
143: $flashArray['key'] = $key;
144: $out .= $this->_render($flashArray);
145: }
146: }
147: return $out;
148: }
149:
150: /**
151: * Used to check is a session is valid in a view
152: *
153: * @return bool
154: * @link https://book.cakephp.org/2.0/en/core-libraries/helpers/session.html#SessionHelper::valid
155: */
156: public function valid() {
157: return CakeSession::valid();
158: }
159:
160: /**
161: * Renders a flash message
162: *
163: * @param array $flash Flash message array
164: * @return string
165: */
166: protected function _render($flash) {
167: $message = $flash['message'];
168: unset($flash['message']);
169:
170: if ($flash['element'] === 'default') {
171: $class = 'message';
172: if (!empty($flash['params']['class'])) {
173: $class = $flash['params']['class'];
174: }
175: $out = '<div id="' . $flash['key'] . 'Message" class="' . $class . '">' . $message . '</div>';
176: } elseif (!$flash['element']) {
177: $out = $message;
178: } else {
179: $options = array();
180: if (isset($flash['params']['plugin'])) {
181: $options['plugin'] = $flash['params']['plugin'];
182: }
183: $tmpVars = $flash['params'];
184: $tmpVars['message'] = $message;
185: $tmpVars['key'] = $flash['key'];
186: $out = $this->_View->element($flash['element'], $tmpVars, $options);
187: }
188: return $out;
189: }
190: }
191: