FlashHelper.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace Tools\View\Helper;
  3. use Cake\Core\Configure;
  4. use Cake\View\Helper;
  5. use Cake\Utility\Hash;
  6. use Tools\Controller\Component\FlashComponent;
  7. /**
  8. * Flash helper
  9. *
  10. * @author Mark Scherer
  11. */
  12. class FlashHelper extends Helper {
  13. public $helpers = array('Session');
  14. /**
  15. * Display all flash messages.
  16. *
  17. * TODO: export div wrapping method (for static messaging on a page)
  18. *
  19. * @param array $types Types to output. Defaults to all if none are specified.
  20. * @return string HTML
  21. */
  22. public function flash(array $types = array()) {
  23. // Get the messages from the session
  24. $messages = (array)$this->Session->read('messages');
  25. $cMessages = (array)Configure::read('messages');
  26. if (!empty($cMessages)) {
  27. $messages = (array)Hash::merge($messages, $cMessages);
  28. }
  29. $html = '';
  30. if (!empty($messages)) {
  31. $html = '<div class="flash-messages flashMessages">';
  32. if ($types) {
  33. foreach ($types as $type) {
  34. // Add a div for each message using the type as the class.
  35. foreach ($messages as $messageType => $msgs) {
  36. if ($messageType !== $type) {
  37. continue;
  38. }
  39. foreach ((array)$msgs as $msg) {
  40. $html .= $this->_message($msg, $messageType);
  41. }
  42. }
  43. }
  44. } else {
  45. foreach ($messages as $messageType => $msgs) {
  46. foreach ((array)$msgs as $msg) {
  47. $html .= $this->_message($msg, $messageType);
  48. }
  49. }
  50. }
  51. $html .= '</div>';
  52. if ($types) {
  53. foreach ($types as $type) {
  54. $this->request->session()->delete('messages.' . $type);
  55. Configure::delete('messages.' . $type);
  56. }
  57. } else {
  58. $this->request->session()->delete('messages');
  59. Configure::delete('messages');
  60. }
  61. }
  62. return $html;
  63. }
  64. /**
  65. * Outputs a single flash message directly.
  66. * Note that this does not use the Session.
  67. *
  68. * @param string $message String to output.
  69. * @param string $type Type (success, warning, error, info)
  70. * @param bool $escape Set to false to disable escaping.
  71. * @return string HTML
  72. */
  73. public function message($msg, $type = 'info', $escape = true) {
  74. $html = '<div class="flash-messages flashMessages">';
  75. if ($escape) {
  76. $msg = h($msg);
  77. }
  78. $html .= $this->_message($msg, $type);
  79. $html .= '</div>';
  80. return $html;
  81. }
  82. /**
  83. * Formats a message
  84. *
  85. * @param string $msg Message to output.
  86. * @param string $type Type that will be formatted to a class tag.
  87. * @return string
  88. */
  89. protected function _message($msg, $type) {
  90. if (!empty($msg)) {
  91. return '<div class="message' . (!empty($type) ? ' ' . $type : '') . '">' . $msg . '</div>';
  92. }
  93. return '';
  94. }
  95. /**
  96. * Add a message on the fly
  97. *
  98. * @param string $msg
  99. * @param string $class
  100. * @return void
  101. */
  102. public function addTransientMessage($msg, $class = null) {
  103. FlashComponent::transientMessage($msg, $class);
  104. }
  105. }