FlashHelper.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. App::uses('AppHelper', 'View/Helper');
  3. App::uses('FlashComponent', 'Tools.Controller/Component');
  4. App::uses('Hash', 'Utility');
  5. /**
  6. * Flash helper
  7. *
  8. * Partial backport from the 3.x one to ease migration.
  9. */
  10. class FlashHelper extends AppHelper {
  11. public $helpers = array('Session');
  12. protected $_defaultConfig = array(
  13. 'useElements' => false, //Set to true to use 3.x flash message rendering via Elements
  14. );
  15. public function __construct(View $View, $settings = array()) {
  16. $defaults = (array)Configure::read('Flash') + $this->_defaultConfig;
  17. $settings += $defaults;
  18. parent::__construct($View, $settings);
  19. }
  20. /**
  21. * Displays all flash messages.
  22. *
  23. * TODO: export div wrapping method (for static messaging on a page)
  24. *
  25. * @param array $types Types to output. Defaults to all if none are specified.
  26. * @return string HTML
  27. */
  28. public function flash(array $types = array()) {
  29. // Get the messages from the session
  30. $messages = (array)$this->Session->read('messages');
  31. $cMessages = (array)Configure::read('messages');
  32. if (!empty($cMessages)) {
  33. $messages = (array)Hash::merge($messages, $cMessages);
  34. }
  35. $html = '';
  36. if (!empty($messages)) {
  37. $html = '<div class="flash-messages flashMessages">';
  38. if ($types) {
  39. foreach ($types as $type) {
  40. // Add a div for each message using the type as the class.
  41. foreach ($messages as $messageType => $msgs) {
  42. if ($messageType !== $type) {
  43. continue;
  44. }
  45. foreach ((array)$msgs as $msg) {
  46. $html .= $this->_message($msg, $messageType);
  47. }
  48. }
  49. }
  50. } else {
  51. foreach ($messages as $messageType => $msgs) {
  52. foreach ((array)$msgs as $msg) {
  53. $html .= $this->_message($msg, $messageType);
  54. }
  55. }
  56. }
  57. $html .= '</div>';
  58. if ($types) {
  59. foreach ($types as $type) {
  60. CakeSession::delete('messages.' . $type);
  61. Configure::delete('messages.' . $type);
  62. }
  63. } else {
  64. CakeSession::delete('messages');
  65. Configure::delete('messages');
  66. }
  67. }
  68. return $html;
  69. }
  70. /**
  71. * Outputs a single flash message directly.
  72. * Note that this does not use the Session.
  73. *
  74. * $escape is deprecated as it is already part of the message
  75. *
  76. * @param array|string $message String to output.
  77. * @param string $type Type (success, warning, error, info)
  78. * @param bool|null $escape Set to false to disable escaping.
  79. * @return string HTML
  80. */
  81. public function message($msg, $type = 'info', $escape = null) {
  82. if ($escape === null && is_array($msg) && !isset($msg['escape'])) {
  83. $msg['escape'] = true;
  84. }
  85. $escape = is_array($msg) && isset($msg['escape']) ? $msg['escape'] : true;
  86. $html = '<div class="flash-messages flashMessages">';
  87. $html .= $this->_message($msg, $type);
  88. $html .= '</div>';
  89. return $html;
  90. }
  91. /**
  92. * Formats a message
  93. *
  94. * @param string $msg Message to output.
  95. * @param string $type Type that will be formatted to a class tag.
  96. * @return string
  97. */
  98. protected function _message($msg, $type) {
  99. if (!is_array($msg)) {
  100. if (!empty($msg)) {
  101. return '<div class="message' . (!empty($type) ? ' ' . $type : '') . '">' . $msg . '</div>';
  102. }
  103. return '';
  104. }
  105. $msg['type'] = $type;
  106. return $this->_View->element($msg['element'], $msg);
  107. }
  108. /**
  109. * Adds a message on the fly.
  110. *
  111. * Only works with static Configure configuration.
  112. *
  113. * This method might not be in 3.x branch anymore, since the overhead of maintaining
  114. * this static method is not worth it. Try switching to addMessage instead().
  115. *
  116. * @param string $msg
  117. * @param string $class
  118. * @return void
  119. */
  120. public function addTransientMessage($msg, $options = array()) {
  121. FlashComponent::transientMessage($msg, $options);
  122. }
  123. /**
  124. * CommonHelper::transientFlashMessage()
  125. *
  126. * @param mixed $msg
  127. * @param mixed $class
  128. * @return void
  129. * @deprecated Use addFlashMessage() instead
  130. */
  131. public function transientFlashMessage($msg, $options = array()) {
  132. $this->addFlashMessage($msg, $options);
  133. }
  134. }