FlashComponent.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. App::uses('Component', 'Controller');
  3. /**
  4. * A flash component to enhance flash message support with stackable messages, both
  5. * persistent and transient.
  6. *
  7. * @author Mark Scherer
  8. * @copyright 2012 Mark Scherer
  9. * @license MIT
  10. */
  11. class FlashComponent extends Component {
  12. public $components = array('Session');
  13. /**
  14. * For automatic startup
  15. * for this helper the controller has to be passed as reference
  16. *
  17. * @return void
  18. */
  19. public function initialize(Controller $Controller) {
  20. parent::initialize($Controller);
  21. $this->Controller = $Controller;
  22. }
  23. /**
  24. * Called after the Controller::beforeRender(), after the view class is loaded, and before the
  25. * Controller::render()
  26. *
  27. * @param object $Controller Controller with components to beforeRender
  28. * @return void
  29. */
  30. public function beforeRender(Controller $Controller) {
  31. if (Configure::read('Common.messages') !== false && $messages = $this->Session->read('Message')) {
  32. foreach ($messages as $message) {
  33. $this->message($message['message'], 'error');
  34. }
  35. $this->Session->delete('Message');
  36. }
  37. if ($this->Controller->request->is('ajax')) {
  38. $ajaxMessages = array_merge(
  39. (array)$this->Session->read('messages'),
  40. (array)Configure::read('messages')
  41. );
  42. // The header can be read with JavaScript and a custom Message can be displayed
  43. $this->Controller->response->header('X-Ajax-Flashmessage', json_encode($ajaxMessages));
  44. $this->Session->delete('messages');
  45. }
  46. }
  47. /**
  48. * Adds a flash message.
  49. * Updates "messages" session content (to enable multiple messages of one type).
  50. *
  51. * @param string $message Message to output.
  52. * @param string $type Type ('error', 'warning', 'success', 'info' or custom class).
  53. * @return void
  54. */
  55. public function message($message, $type = null) {
  56. if (!$type) {
  57. $type = 'info';
  58. }
  59. $old = (array)$this->Session->read('messages');
  60. if (isset($old[$type]) && count($old[$type]) > 99) {
  61. array_shift($old[$type]);
  62. }
  63. $old[$type][] = $message;
  64. $this->Session->write('messages', $old);
  65. }
  66. /**
  67. * Adds a transient flash message.
  68. * These flash messages that are not saved (only available for current view),
  69. * will be merged into the session flash ones prior to output.
  70. *
  71. * @param string $message Message to output.
  72. * @param string $type Type ('error', 'warning', 'success', 'info' or custom class).
  73. * @return void
  74. */
  75. public static function transientMessage($message, $type = null) {
  76. if (!$type) {
  77. $type = 'info';
  78. }
  79. $old = (array)Configure::read('messages');
  80. if (isset($old[$type]) && count($old[$type]) > 99) {
  81. array_shift($old[$type]);
  82. }
  83. $old[$type][] = $message;
  84. Configure::write('messages', $old);
  85. }
  86. /**
  87. * Magic method for verbose flash methods based on types.
  88. *
  89. * For example: $this->Flash->success('My message')
  90. *
  91. * @param string $name Element name to use.
  92. * @param array $args Parameters to pass when calling `FlashComponent::set()`.
  93. * @return void
  94. * @throws InternalErrorException If missing the flash message.
  95. */
  96. public function __call($name, $args) {
  97. if (count($args) < 1) {
  98. throw new InternalErrorException('Flash message missing.');
  99. }
  100. $this->message($args[0], $name);
  101. }
  102. }