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