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