PageRenderTime 48ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/View/Helper/SessionHelper.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 162 lines | 47 code | 10 blank | 105 comment | 10 complexity | 243f7ef1659dd1fdff761b7fc3656c39 MD5 | raw file
  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#SessionHelper::read
  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#SessionHelper::check
  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 personalized
  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. * If you want to use an element from a plugin for rendering your flash message you can do that using the
  98. * plugin param:
  99. *
  100. * {{{
  101. * echo $this->Session->flash('flash', array(
  102. * 'element' => 'my_custom_element',
  103. * 'params' => array('plugin' => 'my_plugin')
  104. * ));
  105. * }}}
  106. *
  107. * @param string $key The [Message.]key you are rendering in the view.
  108. * @param array $attrs Additional attributes to use for the creation of this flash message.
  109. * Supports the 'params', and 'element' keys that are used in the helper.
  110. * @return string
  111. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html#SessionHelper::flash
  112. */
  113. public function flash($key = 'flash', $attrs = array()) {
  114. $out = false;
  115. if (CakeSession::check('Message.' . $key)) {
  116. $flash = CakeSession::read('Message.' . $key);
  117. $message = $flash['message'];
  118. unset($flash['message']);
  119. if (!empty($attrs)) {
  120. $flash = array_merge($flash, $attrs);
  121. }
  122. if ($flash['element'] == 'default') {
  123. $class = 'message';
  124. if (!empty($flash['params']['class'])) {
  125. $class = $flash['params']['class'];
  126. }
  127. $out = '<div id="' . $key . 'Message" class="' . $class . '">' . $message . '</div>';
  128. } elseif ($flash['element'] == '' || $flash['element'] == null) {
  129. $out = $message;
  130. } else {
  131. $options = array();
  132. if (isset($flash['params']['plugin'])) {
  133. $options['plugin'] = $flash['params']['plugin'];
  134. }
  135. $tmpVars = $flash['params'];
  136. $tmpVars['message'] = $message;
  137. $out = $this->_View->element($flash['element'], $tmpVars, $options);
  138. }
  139. CakeSession::delete('Message.' . $key);
  140. }
  141. return $out;
  142. }
  143. /**
  144. * Used to check is a session is valid in a view
  145. *
  146. * @return boolean
  147. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html#SessionHelper::valid
  148. */
  149. public function valid() {
  150. return CakeSession::valid();
  151. }
  152. }