FlashComponent.php 2.8 KB

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