session.php

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

Generated on Sun Nov 22 00:30:53 2009 for CakePHP 1.2.x.x (v1.2.4.8284) by doxygen 1.4.7