2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2009-09-01 04:03:56 +00:00
|
|
|
* Session Helper provides access to the Session in the Views.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 16:38:58 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2011-05-29 21:31:39 +00:00
|
|
|
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2011-05-29 21:31:39 +00:00
|
|
|
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.View.Helper
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 1.1.7.3328
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-04 18:10:24 +00:00
|
|
|
|
|
|
|
App::uses('AppHelper', 'View/Helper');
|
2010-12-07 04:32:42 +00:00
|
|
|
App::uses('CakeSession', 'Model/Datasource');
|
2010-12-04 18:10:24 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Session Helper.
|
|
|
|
*
|
|
|
|
* Session reading from the view.
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.View.Helper
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-06-23 00:55:06 +00:00
|
|
|
class SessionHelper extends AppHelper {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Used to read a session values set in a controller for a key or return values for all keys.
|
|
|
|
*
|
2011-02-21 19:07:33 +00:00
|
|
|
* In your view: `$this->Session->read('Controller.sessKey');`
|
2008-05-30 11:40:08 +00:00
|
|
|
* Calling the method without a param will return all session vars
|
|
|
|
*
|
|
|
|
* @param string $name the name of the session key you want to read
|
2011-08-01 02:57:17 +00:00
|
|
|
* @return mixed values from the session vars
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html#displaying-notifcations-or-flash-messages
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function read($name = null) {
|
2010-06-23 00:55:06 +00:00
|
|
|
return CakeSession::read($name);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Used to check is a session key has been set
|
|
|
|
*
|
2011-02-21 19:07:33 +00:00
|
|
|
* In your view: `$this->Session->check('Controller.sessKey');`
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return boolean
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html#displaying-notifcations-or-flash-messages
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function check($name) {
|
2010-06-23 00:55:06 +00:00
|
|
|
return CakeSession::check($name);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns last error encountered in a session
|
|
|
|
*
|
2011-02-21 19:07:33 +00:00
|
|
|
* In your view: `$this->Session->error();`
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @return string last error
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html#displaying-notifcations-or-flash-messages
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function error() {
|
2010-06-23 00:55:06 +00:00
|
|
|
return CakeSession::error();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Used to render the message set in Controller::Session::setFlash()
|
|
|
|
*
|
2011-02-21 19:07:33 +00:00
|
|
|
* In your view: $this->Session->flash('somekey');
|
2010-01-25 22:59:05 +00:00
|
|
|
* Will default to flash if no param is passed
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-02-21 19:07:33 +00:00
|
|
|
* You can pass additional information into the flash message generation. This allows you
|
|
|
|
* to consolidate all the parameters for a given type of flash message into the view.
|
|
|
|
*
|
|
|
|
* {{{
|
|
|
|
* echo $this->Session->flash('flash', array('params' => array('class' => 'new-flash')));
|
|
|
|
* }}}
|
|
|
|
*
|
2011-08-16 03:55:08 +00:00
|
|
|
* The above would generate a flash message with a custom class name. Using $attrs['params'] you
|
2011-02-21 19:07:33 +00:00
|
|
|
* can pass additional data into the element rendering that will be made available as local variables
|
|
|
|
* when the element is rendered:
|
|
|
|
*
|
|
|
|
* {{{
|
|
|
|
* echo $this->Session->flash('flash', array('params' => array('name' => $user['User']['name'])));
|
|
|
|
* }}}
|
|
|
|
*
|
|
|
|
* This would pass the current user's name into the flash message, so you could create peronsonalized
|
|
|
|
* messages without the controller needing access to that data.
|
|
|
|
*
|
2011-08-16 03:55:08 +00:00
|
|
|
* Lastly you can choose the element that is rendered when creating the flash message. Using
|
2011-02-21 19:07:33 +00:00
|
|
|
* custom elements allows you to fully customize how flash messages are generated.
|
|
|
|
*
|
|
|
|
* {{{
|
|
|
|
* echo $this->Session->flash('flash', array('element' => 'my_custom_element'));
|
|
|
|
* }}}
|
|
|
|
*
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $key The [Message.]key you are rendering in the view.
|
2011-07-29 02:45:47 +00:00
|
|
|
* @param array $attrs Additional attributes to use for the creation of this flash message.
|
2011-02-21 19:07:33 +00:00
|
|
|
* Supports the 'params', and 'element' keys that are used in the helper.
|
2011-07-29 02:45:47 +00:00
|
|
|
* @return string
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html#displaying-notifcations-or-flash-messages
|
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html#SessionHelper::flash
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-02-21 19:07:33 +00:00
|
|
|
public function flash($key = 'flash', $attrs = array()) {
|
2009-11-16 23:15:24 +00:00
|
|
|
$out = false;
|
2011-08-16 03:55:08 +00:00
|
|
|
|
2010-06-23 00:55:06 +00:00
|
|
|
if (CakeSession::check('Message.' . $key)) {
|
|
|
|
$flash = CakeSession::read('Message.' . $key);
|
2011-02-21 19:07:33 +00:00
|
|
|
$message = $flash['message'];
|
|
|
|
unset($flash['message']);
|
|
|
|
|
|
|
|
if (!empty($attrs)) {
|
|
|
|
$flash = array_merge($flash, $attrs);
|
|
|
|
}
|
2009-11-16 23:15:24 +00:00
|
|
|
|
2010-06-23 00:55:06 +00:00
|
|
|
if ($flash['element'] == 'default') {
|
2010-07-28 03:26:24 +00:00
|
|
|
$class = 'message';
|
2010-06-23 00:55:06 +00:00
|
|
|
if (!empty($flash['params']['class'])) {
|
|
|
|
$class = $flash['params']['class'];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-02-21 19:07:33 +00:00
|
|
|
$out = '<div id="' . $key . 'Message" class="' . $class . '">' . $message . '</div>';
|
2010-06-23 00:55:06 +00:00
|
|
|
} elseif ($flash['element'] == '' || $flash['element'] == null) {
|
2011-02-21 19:07:33 +00:00
|
|
|
$out = $message;
|
2010-06-23 00:55:06 +00:00
|
|
|
} else {
|
2011-11-12 16:03:17 +00:00
|
|
|
$options = array();
|
|
|
|
if (isset($flash['plugin'])) {
|
|
|
|
$options['plugin'] = $flash['plugin'];
|
|
|
|
}
|
2010-06-23 00:55:06 +00:00
|
|
|
$tmpVars = $flash['params'];
|
2011-02-21 19:07:33 +00:00
|
|
|
$tmpVars['message'] = $message;
|
2011-11-12 16:03:17 +00:00
|
|
|
$out = $this->_View->element($flash['element'], $tmpVars, $options);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-06-23 00:55:06 +00:00
|
|
|
CakeSession::delete('Message.' . $key);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-11-16 23:15:24 +00:00
|
|
|
return $out;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Used to check is a session is valid in a view
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function valid() {
|
2010-06-23 00:55:06 +00:00
|
|
|
return CakeSession::valid();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|