SessionHelper.php 5.2 KB

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