1: <?php
2: /**
3: * Session Helper provides access to the Session in the Views.
4: *
5: * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
6: * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
13: * @link http://cakephp.org CakePHP(tm) Project
14: * @package Cake.View.Helper
15: * @since CakePHP(tm) v 1.1.7.3328
16: * @license http://www.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 http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html
29: */
30: class SessionHelper extends AppHelper {
31:
32: /**
33: * Used to read a session values set in a controller for a key or return 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 http://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: * Used to check is a session key has been set
48: *
49: * In your view: `$this->Session->check('Controller.sessKey');`
50: *
51: * @param string $name Session key to check.
52: * @return bool
53: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html#SessionHelper::check
54: */
55: public function check($name) {
56: return CakeSession::check($name);
57: }
58:
59: /**
60: * Returns last error encountered in a session
61: *
62: * In your view: `$this->Session->error();`
63: *
64: * @return string last error
65: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html#displaying-notifications-or-flash-messages
66: */
67: public function error() {
68: return CakeSession::error();
69: }
70:
71: /**
72: * Used to render the message set in Controller::Session::setFlash()
73: *
74: * In your view: $this->Session->flash('somekey');
75: * Will default to flash if no param is passed
76: *
77: * You can pass additional information into the flash message generation. This allows you
78: * to consolidate all the parameters for a given type of flash message into the view.
79: *
80: * ```
81: * echo $this->Session->flash('flash', array('params' => array('class' => 'new-flash')));
82: * ```
83: *
84: * The above would generate a flash message with a custom class name. Using $attrs['params'] you
85: * can pass additional data into the element rendering that will be made available as local variables
86: * when the element is rendered:
87: *
88: * ```
89: * echo $this->Session->flash('flash', array('params' => array('name' => $user['User']['name'])));
90: * ```
91: *
92: * This would pass the current user's name into the flash message, so you could create personalized
93: * messages without the controller needing access to that data.
94: *
95: * Lastly you can choose the element that is rendered when creating the flash message. Using
96: * custom elements allows you to fully customize how flash messages are generated.
97: *
98: * ```
99: * echo $this->Session->flash('flash', array('element' => 'my_custom_element'));
100: * ```
101: *
102: * If you want to use an element from a plugin for rendering your flash message you can do that using the
103: * plugin param:
104: *
105: * ```
106: * echo $this->Session->flash('flash', array(
107: * 'element' => 'my_custom_element',
108: * 'params' => array('plugin' => 'my_plugin')
109: * ));
110: * ```
111: *
112: * @param string $key The [Message.]key you are rendering in the view.
113: * @param array $attrs Additional attributes to use for the creation of this flash message.
114: * Supports the 'params', and 'element' keys that are used in the helper.
115: * @return string
116: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html#SessionHelper::flash
117: */
118: public function flash($key = 'flash', $attrs = array()) {
119: $out = false;
120:
121: if (CakeSession::check('Message.' . $key)) {
122: $flash = CakeSession::read('Message.' . $key);
123: CakeSession::delete('Message.' . $key);
124: $message = $flash['message'];
125: unset($flash['message']);
126:
127: if (!empty($attrs)) {
128: $flash = array_merge($flash, $attrs);
129: }
130:
131: if ($flash['element'] === 'default') {
132: $class = 'message';
133: if (!empty($flash['params']['class'])) {
134: $class = $flash['params']['class'];
135: }
136: $out = '<div id="' . $key . 'Message" class="' . $class . '">' . $message . '</div>';
137: } elseif (!$flash['element']) {
138: $out = $message;
139: } else {
140: $options = array();
141: if (isset($flash['params']['plugin'])) {
142: $options['plugin'] = $flash['params']['plugin'];
143: }
144: $tmpVars = $flash['params'];
145: $tmpVars['message'] = $message;
146: $out = $this->_View->element($flash['element'], $tmpVars, $options);
147: }
148: }
149: return $out;
150: }
151:
152: /**
153: * Used to check is a session is valid in a view
154: *
155: * @return bool
156: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html#SessionHelper::valid
157: */
158: public function valid() {
159: return CakeSession::valid();
160: }
161:
162: }
163: