session.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: view_2helpers_2session_8php-source.html 580 2008-07-01 14:45:49Z gwoo $ */
00003 /**
00004  * Short description for file.
00005  *
00006  * Long description for file
00007  *
00008  * PHP versions 4 and 5
00009  *
00010  * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
00011  * Copyright 2005-2008, Cake Software Foundation, Inc.
00012  *                              1785 E. Sahara Avenue, Suite 490-204
00013  *                              Las Vegas, Nevada 89104
00014  *
00015  * Licensed under The MIT License
00016  * Redistributions of files must retain the above copyright notice.
00017  *
00018  * @filesource
00019  * @copyright       Copyright 2005-2008, Cake Software Foundation, Inc.
00020  * @link                http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00021  * @package         cake
00022  * @subpackage      cake.cake.libs.view.helpers
00023  * @since           CakePHP(tm) v 1.1.7.3328
00024  * @version         $Revision: 580 $
00025  * @modifiedby      $LastChangedBy: gwoo $
00026  * @lastmodified    $Date: 2008-07-01 09:45:49 -0500 (Tue, 01 Jul 2008) $
00027  * @license         http://www.opensource.org/licenses/mit-license.php The MIT License
00028  */
00029 /**
00030  * Session Helper.
00031  *
00032  * Session reading from the view.
00033  *
00034  * @package     cake
00035  * @subpackage  cake.cake.libs.view.helpers
00036  *
00037  */
00038 if (!class_exists('cakesession')) {
00039     uses('session');
00040 }
00041 
00042 class SessionHelper extends CakeSession {
00043 /**
00044  * List of helpers used by this helper
00045  *
00046  * @var array
00047  */
00048     var $helpers = null;
00049 /**
00050  * Used to determine if methods implementation is used, or bypassed
00051  *
00052  * @var boolean
00053  */
00054     var $__active = true;
00055 /**
00056  * Class constructor
00057  *
00058  * @param string $base
00059  */
00060     function __construct($base = null) {
00061         if (Configure::read('Session.start') === true) {
00062             parent::__construct($base, false);
00063         } else {
00064             $this->__active = false;
00065         }
00066     }
00067 /**
00068  * Turn sessions on if 'Session.start' is set to false in core.php
00069  *
00070  * @param string $base
00071  */
00072     function activate($base = null) {
00073         $this->__active = true;
00074     }
00075 /**
00076  * Used to read a session values set in a controller for a key or return values for all keys.
00077  *
00078  * In your view: $session->read('Controller.sessKey');
00079  * Calling the method without a param will return all session vars
00080  *
00081  * @param string $name the name of the session key you want to read
00082  *
00083  * @return values from the session vars
00084  * @access public
00085  */
00086     function read($name = null) {
00087         if ($this->__active === true && $this->__start()) {
00088             return parent::read($name);
00089         }
00090         return false;
00091     }
00092 /**
00093  * Used to check is a session key has been set
00094  *
00095  * In your view: $session->check('Controller.sessKey');
00096  *
00097  * @param string $name
00098  * @return boolean
00099  * @access public
00100  */
00101     function check($name) {
00102         if ($this->__active === true && $this->__start()) {
00103             return parent::check($name);
00104         }
00105         return false;
00106     }
00107 /**
00108  * Returns last error encountered in a session
00109  *
00110  * In your view: $session->error();
00111  *
00112  * @return string last error
00113  * @access public
00114  */
00115     function error() {
00116         if ($this->__active === true && $this->__start()) {
00117             return parent::error();
00118         }
00119         return false;
00120     }
00121 /**
00122  * Used to render the message set in Controller::Session::setFlash()
00123  *
00124  * In your view: $session->flash('somekey');
00125  *                  Will default to flash if no param is passed
00126  *
00127  * @param string $key The [Message.]key you are rendering in the view.
00128  * @return string Will echo the value if $key is set, or false if not set.
00129  * @access public
00130  */
00131     function flash($key = 'flash') {
00132         if ($this->__active === true && $this->__start()) {
00133             if (parent::check('Message.' . $key)) {
00134                 $flash = parent::read('Message.' . $key);
00135 
00136                 if ($flash['layout'] == 'default') {
00137                     if (!empty($flash['params']['class'])) {
00138                         $class = $flash['params']['class'];
00139                     } else {
00140                         $class = 'message';
00141                     }
00142                     $out = '<div id="' . $key . 'Message" class="' . $class . '">' . $flash['message'] . '</div>';
00143                 } elseif ($flash['layout'] == '' || $flash['layout'] == null) {
00144                     $out = $flash['message'];
00145                 } else {
00146                     $view =& ClassRegistry::getObject('view');
00147                     list($tmpLayout, $tmpVars, $tmpTitle) = array($view->layout, $view->viewVars, $view->pageTitle);
00148                     list($view->layout, $view->viewVars, $view->pageTitle) = array($flash['layout'], $flash['params'], '');
00149                     $out = $view->renderLayout($flash['message']);
00150                     list($view->layout, $view->viewVars, $view->pageTitle) = array($tmpLayout, $tmpVars, $tmpTitle);
00151                 }
00152                 echo($out);
00153                 parent::del('Message.' . $key);
00154                 return true;
00155             }
00156         }
00157         return false;
00158     }
00159 /**
00160  * Used to check is a session is valid in a view
00161  *
00162  * @return boolean
00163  * @access public
00164  */
00165     function valid() {
00166         if ($this->__active === true && $this->__start()) {
00167             return parent::valid();
00168         }
00169     }
00170 /**
00171  * Override CakeSession::write().
00172  * This method should not be used in a view
00173  *
00174  * @return boolean
00175  * @access public
00176  */
00177     function write() {
00178         trigger_error(__('You can not write to a Session from the view', true), E_USER_WARNING);
00179     }
00180 /**
00181  * Session id
00182  *
00183  * @return string Session id
00184  * @access public
00185  */
00186     function id() {
00187         return parent::id();
00188     }
00189 /**
00190  * Determine if Session has been started
00191  * and attempt to start it if not
00192  *
00193  * @return boolean true if Session is already started, false if
00194  * Session could not be started
00195  * @access public
00196  */
00197     function __start() {
00198         if(!parent::started()) {
00199             parent::start();
00200         }
00201         return true;
00202     }
00203 }
00204 ?>