source: Dev/branches/cakephp/cake/libs/view/helpers/session.php @ 126

Last change on this file since 126 was 126, checked in by fpvanagthoven, 14 years ago

Cakephp branch.

File size: 4.8 KB
Line 
1<?php
2/**
3 * Session Helper provides access to the Session in the Views.
4 *
5 * PHP versions 4 and 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
16 * @subpackage    cake.cake.libs.view.helpers
17 * @since         CakePHP(tm) v 1.1.7.3328
18 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
19 */
20if (!class_exists('cakesession')) {
21        require LIBS . 'cake_session.php';
22}
23/**
24 * Session Helper.
25 *
26 * Session reading from the view.
27 *
28 * @package       cake
29 * @subpackage    cake.cake.libs.view.helpers
30 * @link http://book.cakephp.org/view/1465/Session
31 */
32class SessionHelper extends CakeSession {
33
34/**
35 * List of helpers used by this helper
36 *
37 * @var array
38 */
39        var $helpers = array();
40
41/**
42 * Used to determine if methods implementation is used, or bypassed
43 *
44 * @var boolean
45 */
46        var $__active = true;
47
48/**
49 * Class constructor
50 *
51 * @param string $base
52 */
53        function __construct($base = null) {
54                if (Configure::read('Session.start') === true) {
55                        parent::__construct($base, false);
56                        $this->start();
57                        $this->__active = true;
58                } else {
59                        $this->__active = false;
60                }
61        }
62
63/**
64 * Turn sessions on if 'Session.start' is set to false in core.php
65 *
66 * @param string $base
67 * @access public
68 */
69        function activate($base = null) {
70                $this->__active = true;
71        }
72
73/**
74 * Used to read a session values set in a controller for a key or return values for all keys.
75 *
76 * In your view: `$session->read('Controller.sessKey');`
77 * Calling the method without a param will return all session vars
78 *
79 * @param string $name the name of the session key you want to read
80 * @return values from the session vars
81 * @access public
82 * @link http://book.cakephp.org/view/1466/Methods
83 */
84        function read($name = null) {
85                if ($this->__active === true && $this->__start()) {
86                        return parent::read($name);
87                }
88                return false;
89        }
90
91/**
92 * Used to check is a session key has been set
93 *
94 * In your view: `$session->check('Controller.sessKey');`
95 *
96 * @param string $name
97 * @return boolean
98 * @access public
99 * @link http://book.cakephp.org/view/1466/Methods
100 */
101        function check($name) {
102                if ($this->__active === true && $this->__start()) {
103                        return parent::check($name);
104                }
105                return false;
106        }
107
108/**
109 * Returns last error encountered in a session
110 *
111 * In your view: `$session->error();`
112 *
113 * @return string last error
114 * @access public
115 * @link http://book.cakephp.org/view/1466/Methods
116 */
117        function error() {
118                if ($this->__active === true && $this->__start()) {
119                        return parent::error();
120                }
121                return false;
122        }
123
124/**
125 * Used to render the message set in Controller::Session::setFlash()
126 *
127 * In your view: $session->flash('somekey');
128 * Will default to flash if no param is passed
129 *
130 * @param string $key The [Message.]key you are rendering in the view.
131 * @return boolean|string Will return the value if $key is set, or false if not set.
132 * @access public
133 * @link http://book.cakephp.org/view/1466/Methods
134 * @link http://book.cakephp.org/view/1467/flash
135 */
136        function flash($key = 'flash') {
137                $out = false;
138
139                if ($this->__active === true && $this->__start()) {
140                        if (parent::check('Message.' . $key)) {
141                                $flash = parent::read('Message.' . $key);
142
143                                if ($flash['element'] == 'default') {
144                                        if (!empty($flash['params']['class'])) {
145                                                $class = $flash['params']['class'];
146                                        } else {
147                                                $class = 'message';
148                                        }
149                                        $out = '<div id="' . $key . 'Message" class="' . $class . '">' . $flash['message'] . '</div>';
150                                } elseif ($flash['element'] == '' || $flash['element'] == null) {
151                                        $out = $flash['message'];
152                                } else {
153                                        $view =& ClassRegistry::getObject('view');
154                                        $tmpVars = $flash['params'];
155                                        $tmpVars['message'] = $flash['message'];
156                                        $out = $view->element($flash['element'], $tmpVars);
157                                }
158                                parent::delete('Message.' . $key);
159                        }
160                }
161                return $out;
162        }
163
164/**
165 * Used to check is a session is valid in a view
166 *
167 * @return boolean
168 * @access public
169 */
170        function valid() {
171                if ($this->__active === true && $this->__start()) {
172                        return parent::valid();
173                }
174        }
175
176/**
177 * Override CakeSession::write().
178 * This method should not be used in a view
179 *
180 * @return boolean
181 * @access public
182 */
183        function write() {
184                trigger_error(__('You can not write to a Session from the view', true), E_USER_WARNING);
185        }
186
187/**
188 * Determine if Session has been started
189 * and attempt to start it if not
190 *
191 * @return boolean true if Session is already started, false if
192 * Session could not be started
193 * @access private
194 */
195        function __start() {
196                if (!$this->started()) {
197                        return $this->start();
198                }
199                return true;
200        }
201}
Note: See TracBrowser for help on using the repository browser.