session.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: view_2helpers_2session_8php-source.html 675 2008-12-26 00:27:14Z 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: 675 $
00025  * @modifiedby      $LastChangedBy: gwoo $
00026  * @lastmodified    $Date: 2008-12-25 16:27:14 -0800 (Thu, 25 Dec 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 class SessionHelper extends CakeSession {
00042 /**
00043  * List of helpers used by this helper
00044  *
00045  * @var array
00046  */
00047     var $helpers = null;
00048 /**
00049  * Used to determine if methods implementation is used, or bypassed
00050  *
00051  * @var boolean
00052  */
00053     var $__active = true;
00054 /**
00055  * Class constructor
00056  *
00057  * @param string $base
00058  */
00059     function __construct($base = null) {
00060         if (!defined('AUTO_SESSION') || AUTO_SESSION === true) {
00061             parent::__construct($base, false);
00062         } else {
00063             $this->__active = false;
00064         }
00065     }
00066 /**
00067  * Turn sessions on if 'Session.start' is set to false in core.php
00068  *
00069  * @param string $base
00070  */
00071     function activate($base = null) {
00072         $this->__active = true;
00073     }
00074 /**
00075  * Used to read a session values set in a controller for a key or return values for all keys.
00076  *
00077  * In your view: $session->read('Controller.sessKey');
00078  * Calling the method without a param will return all session vars
00079  *
00080  * @param string $name the name of the session key you want to read
00081  *
00082  * @return values from the session vars
00083  * @access public
00084  */
00085     function read($name = null) {
00086         if ($this->__active === true && $this->__start()) {
00087             return parent::read($name);
00088         }
00089         return false;
00090     }
00091 /**
00092  * Used to check is a session key has been set
00093  *
00094  * In your view: $session->check('Controller.sessKey');
00095  *
00096  * @param string $name
00097  * @return boolean
00098  * @access public
00099  */
00100     function check($name) {
00101         if ($this->__active === true && $this->__start()) {
00102             return parent::check($name);
00103         }
00104         return false;
00105     }
00106 /**
00107  * Returns last error encountered in a session
00108  *
00109  * In your view: $session->error();
00110  *
00111  * @return string last error
00112  * @access public
00113  */
00114     function error() {
00115         if ($this->__active === true && $this->__start()) {
00116             return parent::error();
00117         }
00118         return false;
00119     }
00120 /**
00121  * Used to render the message set in Controller::Session::setFlash()
00122  *
00123  * In your view: $session->flash('somekey');
00124  *                  Will default to flash if no param is passed
00125  *
00126  * @param string $key The [Message.]key you are rendering in the view.
00127  * @return string Will echo the value if $key is set, or false if not set.
00128  * @access public
00129  */
00130     function flash($key = 'flash') {
00131         if ($this->__active === true && $this->__start()) {
00132             if (parent::check('Message.' . $key)) {
00133                 $flash = parent::read('Message.' . $key);
00134 
00135                 if ($flash['layout'] == 'default') {
00136                     $out = '<div id="' . $key . 'Message" class="message">' . $flash['message'] . '</div>';
00137                 } elseif ($flash['layout'] == '' || $flash['layout'] == null) {
00138                     $out = $flash['message'];
00139                 } else {
00140                     $view =& ClassRegistry::getObject('view');
00141                     list($tmpLayout, $tmpVars, $tmpTitle) = array($view->layout, $view->viewVars, $view->pageTitle);
00142                     list($view->layout, $view->viewVars, $view->pageTitle) = array($flash['layout'], $flash['params'], '');
00143                     $out = $view->renderLayout($flash['message']);
00144                     list($view->layout, $view->viewVars, $view->pageTitle) = array($tmpLayout, $tmpVars, $tmpTitle);
00145                 }
00146                 e($out);
00147                 parent::del('Message.' . $key);
00148                 return true;
00149             }
00150         }
00151         return false;
00152     }
00153 /**
00154  * Used to check is a session is valid in a view
00155  *
00156  * @return boolean
00157  * @access public
00158  */
00159     function valid() {
00160         if ($this->__active === true && $this->__start()) {
00161             return parent::valid();
00162         }
00163     }
00164 /**
00165  * Override CakeSession::write().
00166  * This method should not be used in a view
00167  *
00168  * @return boolean
00169  * @access public
00170  */
00171     function write() {
00172         trigger_error(__('You can not write to a Session from the view', true), E_USER_WARNING);
00173     }
00174 /**
00175  * Session id
00176  *
00177  * @return string Session id
00178  * @access public
00179  */
00180     function id() {
00181         return parent::id();
00182     }
00183 /**
00184  * Determine if Session has been started
00185  * and attempt to start it if not
00186  *
00187  * @return boolean true if Session is already started, false if
00188  * Session could not be started
00189  * @access public
00190  */
00191     function __start() {
00192         if(!parent::started()) {
00193             parent::start();
00194         }
00195         return true;
00196     }
00197 }
00198 ?>