PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/libs/view/helpers/session.php

https://github.com/hardsshah/bookmarks
PHP | 202 lines | 81 code | 2 blank | 119 comment | 23 complexity | c9a1ea39c72dbc73925d876b53e2c693 MD5 | raw file
  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://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$
  23. * @modifiedby $LastChangedBy$
  24. * @lastmodified $Date$
  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. * 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. } else {
  61. $this->__active = false;
  62. }
  63. }
  64. /**
  65. * Turn sessions on if 'Session.start' is set to false in core.php
  66. *
  67. * @param string $base
  68. */
  69. function activate($base = null) {
  70. $this->__active = true;
  71. }
  72. /**
  73. * Used to read a session values set in a controller for a key or return values for all keys.
  74. *
  75. * In your view: $session->read('Controller.sessKey');
  76. * Calling the method without a param will return all session vars
  77. *
  78. * @param string $name the name of the session key you want to read
  79. *
  80. * @return values from the session vars
  81. * @access public
  82. */
  83. function read($name = null) {
  84. if ($this->__active === true && $this->__start()) {
  85. return parent::read($name);
  86. }
  87. return false;
  88. }
  89. /**
  90. * Used to check is a session key has been set
  91. *
  92. * In your view: $session->check('Controller.sessKey');
  93. *
  94. * @param string $name
  95. * @return boolean
  96. * @access public
  97. */
  98. function check($name) {
  99. if ($this->__active === true && $this->__start()) {
  100. return parent::check($name);
  101. }
  102. return false;
  103. }
  104. /**
  105. * Returns last error encountered in a session
  106. *
  107. * In your view: $session->error();
  108. *
  109. * @return string last error
  110. * @access public
  111. */
  112. function error() {
  113. if ($this->__active === true && $this->__start()) {
  114. return parent::error();
  115. }
  116. return false;
  117. }
  118. /**
  119. * Used to render the message set in Controller::Session::setFlash()
  120. *
  121. * In your view: $session->flash('somekey');
  122. * Will default to flash if no param is passed
  123. *
  124. * @param string $key The [Message.]key you are rendering in the view.
  125. * @return string Will echo the value if $key is set, or false if not set.
  126. * @access public
  127. */
  128. function flash($key = 'flash') {
  129. if ($this->__active === true && $this->__start()) {
  130. if (parent::check('Message.' . $key)) {
  131. $flash = parent::read('Message.' . $key);
  132. if ($flash['layout'] == 'default') {
  133. if (!empty($flash['params']['class'])) {
  134. $class = $flash['params']['class'];
  135. } else {
  136. $class = 'message';
  137. }
  138. $out = '<div id="' . $key . 'Message" class="' . $class . '">' . $flash['message'] . '</div>';
  139. } elseif ($flash['layout'] == '' || $flash['layout'] == null) {
  140. $out = $flash['message'];
  141. } else {
  142. $view =& ClassRegistry::getObject('view');
  143. list($tmpLayout, $tmpVars, $tmpTitle) = array($view->layout, $view->viewVars, $view->pageTitle);
  144. list($view->layout, $view->viewVars, $view->pageTitle) = array($flash['layout'], $flash['params'], '');
  145. $out = $view->renderLayout($flash['message']);
  146. list($view->layout, $view->viewVars, $view->pageTitle) = array($tmpLayout, $tmpVars, $tmpTitle);
  147. }
  148. echo($out);
  149. parent::del('Message.' . $key);
  150. return true;
  151. }
  152. }
  153. return false;
  154. }
  155. /**
  156. * Used to check is a session is valid in a view
  157. *
  158. * @return boolean
  159. * @access public
  160. */
  161. function valid() {
  162. if ($this->__active === true && $this->__start()) {
  163. return parent::valid();
  164. }
  165. }
  166. /**
  167. * Override CakeSession::write().
  168. * This method should not be used in a view
  169. *
  170. * @return boolean
  171. * @access public
  172. */
  173. function write() {
  174. trigger_error(__('You can not write to a Session from the view', true), E_USER_WARNING);
  175. }
  176. /**
  177. * Session id
  178. *
  179. * @return string Session id
  180. * @access public
  181. */
  182. function id() {
  183. return parent::id();
  184. }
  185. /**
  186. * Determine if Session has been started
  187. * and attempt to start it if not
  188. *
  189. * @return boolean true if Session is already started, false if
  190. * Session could not be started
  191. * @access public
  192. */
  193. function __start() {
  194. if (!parent::started()) {
  195. parent::start();
  196. }
  197. return true;
  198. }
  199. }
  200. ?>