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