Subversion-Projekte lars-tiefland.cakephp

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/* SVN FILE: $Id: session.php 7945 2008-12-19 02:16:01Z gwoo $ */
3
/**
4
 * Short description for file.
5
 *
6
 * Long description for file
7
 *
8
 * PHP versions 4 and 5
9
 *
10
 * CakePHP(tm) :  Rapid Development Framework (http://www.cakephp.org)
11
 * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
12
 *
13
 * Licensed under The MIT License
14
 * Redistributions of files must retain the above copyright notice.
15
 *
16
 * @filesource
17
 * @copyright     Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
18
 * @link          http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
19
 * @package       cake
20
 * @subpackage    cake.cake.libs.view.helpers
21
 * @since         CakePHP(tm) v 1.1.7.3328
22
 * @version       $Revision: 7945 $
23
 * @modifiedby    $LastChangedBy: gwoo $
24
 * @lastmodified  $Date: 2008-12-18 18:16:01 -0800 (Thu, 18 Dec 2008) $
25
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
26
 */
27
if (!class_exists('cakesession')) {
28
	uses('session');
29
}
30
 
31
/**
32
 * Session Helper.
33
 *
34
 * Session reading from the view.
35
 *
36
 * @package       cake
37
 * @subpackage    cake.cake.libs.view.helpers
38
 *
39
 */
40
class SessionHelper extends CakeSession {
41
/**
42
 * List of helpers used by this helper
43
 *
44
 * @var array
45
 */
46
	var $helpers = null;
47
/**
48
 * Used to determine if methods implementation is used, or bypassed
49
 *
50
 * @var boolean
51
 */
52
	var $__active = true;
53
/**
54
 * Class constructor
55
 *
56
 * @param string $base
57
 */
58
	function __construct($base = null) {
59
		if (Configure::read('Session.start') === true) {
60
			parent::__construct($base, false);
61
		} else {
62
			$this->__active = false;
63
		}
64
	}
65
/**
66
 * Turn sessions on if 'Session.start' is set to false in core.php
67
 *
68
 * @param string $base
69
 */
70
	function activate($base = null) {
71
		$this->__active = true;
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
 *
81
 * @return values from the session vars
82
 * @access public
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
 * Used to check is a session key has been set
92
 *
93
 * In your view: $session->check('Controller.sessKey');
94
 *
95
 * @param string $name
96
 * @return boolean
97
 * @access public
98
 */
99
	function check($name) {
100
		if ($this->__active === true && $this->__start()) {
101
			return parent::check($name);
102
		}
103
		return false;
104
	}
105
/**
106
 * Returns last error encountered in a session
107
 *
108
 * In your view: $session->error();
109
 *
110
 * @return string last error
111
 * @access public
112
 */
113
	function error() {
114
		if ($this->__active === true && $this->__start()) {
115
			return parent::error();
116
		}
117
		return false;
118
	}
119
/**
120
 * Used to render the message set in Controller::Session::setFlash()
121
 *
122
 * In your view: $session->flash('somekey');
123
 * 					Will default to flash if no param is passed
124
 *
125
 * @param string $key The [Message.]key you are rendering in the view.
126
 * @return string Will echo the value if $key is set, or false if not set.
127
 * @access public
128
 */
129
	function flash($key = 'flash') {
130
		if ($this->__active === true && $this->__start()) {
131
			if (parent::check('Message.' . $key)) {
132
				$flash = parent::read('Message.' . $key);
133
 
134
				if ($flash['layout'] == 'default') {
135
					if (!empty($flash['params']['class'])) {
136
						$class = $flash['params']['class'];
137
					} else {
138
						$class = 'message';
139
					}
140
					$out = '<div id="' . $key . 'Message" class="' . $class . '">' . $flash['message'] . '</div>';
141
				} elseif ($flash['layout'] == '' || $flash['layout'] == null) {
142
					$out = $flash['message'];
143
				} else {
144
					$view =& ClassRegistry::getObject('view');
145
					list($tmpLayout, $tmpVars, $tmpTitle) = array($view->layout, $view->viewVars, $view->pageTitle);
146
					list($view->layout, $view->viewVars, $view->pageTitle) = array($flash['layout'], $flash['params'], '');
147
					$out = $view->renderLayout($flash['message']);
148
					list($view->layout, $view->viewVars, $view->pageTitle) = array($tmpLayout, $tmpVars, $tmpTitle);
149
				}
150
				echo($out);
151
				parent::del('Message.' . $key);
152
				return true;
153
			}
154
		}
155
		return false;
156
	}
157
/**
158
 * Used to check is a session is valid in a view
159
 *
160
 * @return boolean
161
 * @access public
162
 */
163
	function valid() {
164
		if ($this->__active === true && $this->__start()) {
165
			return parent::valid();
166
		}
167
	}
168
/**
169
 * Override CakeSession::write().
170
 * This method should not be used in a view
171
 *
172
 * @return boolean
173
 * @access public
174
 */
175
	function write() {
176
		trigger_error(__('You can not write to a Session from the view', true), E_USER_WARNING);
177
	}
178
/**
179
 * Session id
180
 *
181
 * @return string Session id
182
 * @access public
183
 */
184
	function id() {
185
		return parent::id();
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 public
194
 */
195
	function __start() {
196
		if (!parent::started()) {
197
			parent::start();
198
		}
199
		return true;
200
	}
201
}
202
?>