FlashMessenger.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ 33tNb6gPF5z1q1WGuwD/267TbrBfalBhsX5eDc9csp0=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2014 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.0
  11. */
  12. /**
  13. * flash messenger action helper
  14. */
  15. namespace Cube\Controller\Action\Helper;
  16. use Cube\Controller\Front,
  17. Cube\Session;
  18. class FlashMessenger extends AbstractHelper
  19. {
  20. const SESSION_NAMESPACE = 'FlashMessenger';
  21. const MESSAGES_NAMESPACE = 'Messages';
  22. /**
  23. *
  24. * name of session variable
  25. *
  26. * @var string
  27. */
  28. protected $_name = null;
  29. /**
  30. *
  31. * array of messages held by the flash messenger
  32. *
  33. * @var array
  34. */
  35. protected $_messages = array();
  36. /**
  37. *
  38. * session object
  39. *
  40. * @var \Cube\Session
  41. */
  42. protected $_session;
  43. /**
  44. *
  45. * class constructor
  46. *
  47. * @param string $name
  48. */
  49. public function __construct($name = null)
  50. {
  51. $this->setName($name);
  52. $this->setSession();
  53. }
  54. /**
  55. *
  56. * get session variable name
  57. *
  58. * @return string
  59. */
  60. public function getName()
  61. {
  62. return $this->_name;
  63. }
  64. /**
  65. *
  66. * set session variable name
  67. *
  68. * @param string $name
  69. * @return \Cube\Controller\Action\Helper\FlashMessenger
  70. */
  71. public function setName($name = null)
  72. {
  73. if ($name === null) {
  74. $name = self::MESSAGES_NAMESPACE;
  75. }
  76. $this->_name = $name;
  77. return $this;
  78. }
  79. /**
  80. *
  81. * get session object
  82. *
  83. * @return \Cube\Session
  84. */
  85. public function getSession()
  86. {
  87. if (!($this->_session instanceof Session)) {
  88. $this->setSession();
  89. }
  90. return $this->_session;
  91. }
  92. /**
  93. *
  94. * set session object
  95. *
  96. * @param \Cube\Session $session
  97. * @return \Cube\Controller\Action\Helper\FlashMessenger
  98. */
  99. public function setSession(Session $session = null)
  100. {
  101. if ($session === null) {
  102. $session = Front::getInstance()->getBootstrap()->getResource('session');
  103. }
  104. if (!$session instanceof Session) {
  105. $session = new Session();
  106. $session->setNamespace(self::SESSION_NAMESPACE);
  107. }
  108. $this->_session = $session;
  109. return $this;
  110. }
  111. /**
  112. *
  113. * add a new message to the flash messenger action helper
  114. *
  115. * @param mixed $message
  116. * @return \Cube\Controller\Action\Helper\FlashMessenger
  117. */
  118. public function setMessage($message)
  119. {
  120. $this->_messages = $this->getMessages();
  121. array_push($this->_messages, $message);
  122. $this->_session->set($this->_name, $this->_messages);
  123. return $this;
  124. }
  125. /**
  126. *
  127. * get all messages saved in the current flash messenger helper
  128. *
  129. * @return array
  130. */
  131. public function getMessages()
  132. {
  133. $this->_messages = (array) $this->_session->get($this->_name);
  134. $this->_session->unregister($this->_name);
  135. return $this->_messages;
  136. }
  137. }