FlashMessenger.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Controller
  17. * @subpackage Zend_Controller_Action_Helper
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @see Zend_Session
  23. */
  24. require_once 'Zend/Session.php';
  25. /**
  26. * @see Zend_Controller_Action_Helper_Abstract
  27. */
  28. require_once 'Zend/Controller/Action/Helper/Abstract.php';
  29. /**
  30. * Flash Messenger - implement session-based messages
  31. *
  32. * @uses Zend_Controller_Action_Helper_Abstract
  33. * @category Zend
  34. * @package Zend_Controller
  35. * @subpackage Zend_Controller_Action_Helper
  36. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. * @version $Id$
  39. */
  40. class Zend_Controller_Action_Helper_FlashMessenger extends Zend_Controller_Action_Helper_Abstract implements IteratorAggregate, Countable
  41. {
  42. /**
  43. * $_messages - Messages from previous request
  44. *
  45. * @var array
  46. */
  47. static protected $_messages = array();
  48. /**
  49. * $_session - Zend_Session storage object
  50. *
  51. * @var Zend_Session
  52. */
  53. static protected $_session = null;
  54. /**
  55. * $_messageAdded - Wether a message has been previously added
  56. *
  57. * @var boolean
  58. */
  59. static protected $_messageAdded = false;
  60. /**
  61. * $_namespace - Instance namespace, default is 'default'
  62. *
  63. * @var string
  64. */
  65. protected $_namespace = 'default';
  66. /**
  67. * __construct() - Instance constructor, needed to get iterators, etc
  68. *
  69. * @param string $namespace
  70. * @return void
  71. */
  72. public function __construct()
  73. {
  74. if (!self::$_session instanceof Zend_Session_Namespace) {
  75. self::$_session = new Zend_Session_Namespace($this->getName());
  76. foreach (self::$_session as $namespace => $messages) {
  77. self::$_messages[$namespace] = $messages;
  78. unset(self::$_session->{$namespace});
  79. }
  80. }
  81. }
  82. /**
  83. * postDispatch() - runs after action is dispatched, in this
  84. * case, it is resetting the namespace in case we have forwarded to a different
  85. * action, Flashmessage will be 'clean' (default namespace)
  86. *
  87. * @return Zend_Controller_Action_Helper_FlashMessenger Provides a fluent interface
  88. */
  89. public function postDispatch()
  90. {
  91. $this->resetNamespace();
  92. return $this;
  93. }
  94. /**
  95. * setNamespace() - change the namespace messages are added to, useful for
  96. * per action controller messaging between requests
  97. *
  98. * @param string $namespace
  99. * @return Zend_Controller_Action_Helper_FlashMessenger Provides a fluent interface
  100. */
  101. public function setNamespace($namespace = 'default')
  102. {
  103. $this->_namespace = $namespace;
  104. return $this;
  105. }
  106. /**
  107. * resetNamespace() - reset the namespace to the default
  108. *
  109. * @return Zend_Controller_Action_Helper_FlashMessenger Provides a fluent interface
  110. */
  111. public function resetNamespace()
  112. {
  113. $this->setNamespace();
  114. return $this;
  115. }
  116. /**
  117. * addMessage() - Add a message to flash message
  118. *
  119. * @param string $message
  120. * @return Zend_Controller_Action_Helper_FlashMessenger Provides a fluent interface
  121. */
  122. public function addMessage($message)
  123. {
  124. if (self::$_messageAdded === false) {
  125. self::$_session->setExpirationHops(1, null, true);
  126. }
  127. if (!is_array(self::$_session->{$this->_namespace})) {
  128. self::$_session->{$this->_namespace} = array();
  129. }
  130. self::$_session->{$this->_namespace}[] = $message;
  131. return $this;
  132. }
  133. /**
  134. * hasMessages() - Wether a specific namespace has messages
  135. *
  136. * @return boolean
  137. */
  138. public function hasMessages()
  139. {
  140. return isset(self::$_messages[$this->_namespace]);
  141. }
  142. /**
  143. * getMessages() - Get messages from a specific namespace
  144. *
  145. * @return array
  146. */
  147. public function getMessages()
  148. {
  149. if ($this->hasMessages()) {
  150. return self::$_messages[$this->_namespace];
  151. }
  152. return array();
  153. }
  154. /**
  155. * Clear all messages from the previous request & current namespace
  156. *
  157. * @return boolean True if messages were cleared, false if none existed
  158. */
  159. public function clearMessages()
  160. {
  161. if ($this->hasMessages()) {
  162. unset(self::$_messages[$this->_namespace]);
  163. return true;
  164. }
  165. return false;
  166. }
  167. /**
  168. * hasCurrentMessages() - check to see if messages have been added to current
  169. * namespace within this request
  170. *
  171. * @return boolean
  172. */
  173. public function hasCurrentMessages()
  174. {
  175. return isset(self::$_session->{$this->_namespace});
  176. }
  177. /**
  178. * getCurrentMessages() - get messages that have been added to the current
  179. * namespace within this request
  180. *
  181. * @return array
  182. */
  183. public function getCurrentMessages()
  184. {
  185. if ($this->hasCurrentMessages()) {
  186. return self::$_session->{$this->_namespace};
  187. }
  188. return array();
  189. }
  190. /**
  191. * clear messages from the current request & current namespace
  192. *
  193. * @return boolean
  194. */
  195. public function clearCurrentMessages()
  196. {
  197. if ($this->hasCurrentMessages()) {
  198. unset(self::$_session->{$this->_namespace});
  199. return true;
  200. }
  201. return false;
  202. }
  203. /**
  204. * getIterator() - complete the IteratorAggregate interface, for iterating
  205. *
  206. * @return ArrayObject
  207. */
  208. public function getIterator()
  209. {
  210. if ($this->hasMessages()) {
  211. return new ArrayObject($this->getMessages());
  212. }
  213. return new ArrayObject();
  214. }
  215. /**
  216. * count() - Complete the countable interface
  217. *
  218. * @return int
  219. */
  220. public function count()
  221. {
  222. if ($this->hasMessages()) {
  223. return count($this->getMessages());
  224. }
  225. return 0;
  226. }
  227. /**
  228. * Strategy pattern: proxy to addMessage()
  229. *
  230. * @param string $message
  231. * @return void
  232. */
  233. public function direct($message)
  234. {
  235. return $this->addMessage($message);
  236. }
  237. }