SessionHelper.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * Session Helper provides access to the Session in the Views.
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://cakephp.org CakePHP(tm) Project
  16. * @package Cake.View.Helper
  17. * @since CakePHP(tm) v 1.1.7.3328
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. App::uses('AppHelper', 'View/Helper');
  21. App::uses('CakeSession', 'Model/Datasource');
  22. /**
  23. * Session Helper.
  24. *
  25. * Session reading from the view.
  26. *
  27. * @package Cake.View.Helper
  28. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html
  29. */
  30. class SessionHelper extends AppHelper {
  31. /**
  32. * Used to read a session values set in a controller for a key or return values for all keys.
  33. *
  34. * In your view: `$this->Session->read('Controller.sessKey');`
  35. * Calling the method without a param will return all session vars
  36. *
  37. * @param string $name the name of the session key you want to read
  38. * @return mixed values from the session vars
  39. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html#SessionHelper::read
  40. */
  41. public function read($name = null) {
  42. return CakeSession::read($name);
  43. }
  44. /**
  45. * Used to check is a session key has been set
  46. *
  47. * In your view: `$this->Session->check('Controller.sessKey');`
  48. *
  49. * @param string $name
  50. * @return boolean
  51. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html#SessionHelper::check
  52. */
  53. public function check($name) {
  54. return CakeSession::check($name);
  55. }
  56. /**
  57. * Returns last error encountered in a session
  58. *
  59. * In your view: `$this->Session->error();`
  60. *
  61. * @return string last error
  62. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html#displaying-notifications-or-flash-messages
  63. */
  64. public function error() {
  65. return CakeSession::error();
  66. }
  67. /**
  68. * Used to render the message set in Controller::Session::setFlash()
  69. *
  70. * In your view: $this->Session->flash('somekey');
  71. * Will default to flash if no param is passed
  72. *
  73. * You can pass additional information into the flash message generation. This allows you
  74. * to consolidate all the parameters for a given type of flash message into the view.
  75. *
  76. * {{{
  77. * echo $this->Session->flash('flash', array('params' => array('class' => 'new-flash')));
  78. * }}}
  79. *
  80. * The above would generate a flash message with a custom class name. Using $attrs['params'] you
  81. * can pass additional data into the element rendering that will be made available as local variables
  82. * when the element is rendered:
  83. *
  84. * {{{
  85. * echo $this->Session->flash('flash', array('params' => array('name' => $user['User']['name'])));
  86. * }}}
  87. *
  88. * This would pass the current user's name into the flash message, so you could create personalized
  89. * messages without the controller needing access to that data.
  90. *
  91. * Lastly you can choose the element that is rendered when creating the flash message. Using
  92. * custom elements allows you to fully customize how flash messages are generated.
  93. *
  94. * {{{
  95. * echo $this->Session->flash('flash', array('element' => 'my_custom_element'));
  96. * }}}
  97. *
  98. * If you want to use an element from a plugin for rendering your flash message you can do that using the
  99. * plugin param:
  100. *
  101. * {{{
  102. * echo $this->Session->flash('flash', array(
  103. * 'element' => 'my_custom_element',
  104. * 'params' => array('plugin' => 'my_plugin')
  105. * ));
  106. * }}}
  107. *
  108. * @param string $key The [Message.]key you are rendering in the view.
  109. * @param array $attrs Additional attributes to use for the creation of this flash message.
  110. * Supports the 'params', and 'element' keys that are used in the helper.
  111. * @return string
  112. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html#SessionHelper::flash
  113. */
  114. public function flash($key = 'flash', $attrs = array()) {
  115. $out = false;
  116. if (CakeSession::check('Message.' . $key)) {
  117. $flash = CakeSession::read('Message.' . $key);
  118. $message = $flash['message'];
  119. unset($flash['message']);
  120. if (!empty($attrs)) {
  121. $flash = array_merge($flash, $attrs);
  122. }
  123. if ($flash['element'] === 'default') {
  124. $class = 'message';
  125. if (!empty($flash['params']['class'])) {
  126. $class = $flash['params']['class'];
  127. }
  128. $out = '<div id="' . $key . 'Message" class="' . $class . '">' . $message . '</div>';
  129. } elseif (!$flash['element']) {
  130. $out = $message;
  131. } else {
  132. $options = array();
  133. if (isset($flash['params']['plugin'])) {
  134. $options['plugin'] = $flash['params']['plugin'];
  135. }
  136. $tmpVars = $flash['params'];
  137. $tmpVars['message'] = $message;
  138. $out = $this->_View->element($flash['element'], $tmpVars, $options);
  139. }
  140. CakeSession::delete('Message.' . $key);
  141. }
  142. return $out;
  143. }
  144. /**
  145. * Used to check is a session is valid in a view
  146. *
  147. * @return boolean
  148. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html#SessionHelper::valid
  149. */
  150. public function valid() {
  151. return CakeSession::valid();
  152. }
  153. }