SessionHelper.php 4.8 KB

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