*prechod na novsiu verziu ZF
[sport-group.git] / library / Zend / Controller / Action / Helper / FlashMessenger.php
blobefced8aa00152bae13774235221188c6ccfc7e2f
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
15 * @category Zend
16 * @package Zend_Controller
17 * @subpackage Zend_Controller_Action_Helper
18 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
22 /**
23 * @see Zend_Session
25 require_once 'Zend/Session.php';
27 /**
28 * @see Zend_Controller_Action_Helper_Abstract
30 require_once 'Zend/Controller/Action/Helper/Abstract.php';
32 /**
33 * Flash Messenger - implement session-based messages
35 * @uses Zend_Controller_Action_Helper_Abstract
36 * @category Zend
37 * @package Zend_Controller
38 * @subpackage Zend_Controller_Action_Helper
39 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
40 * @license http://framework.zend.com/license/new-bsd New BSD License
41 * @version $Id: FlashMessenger.php 16971 2009-07-22 18:05:45Z mikaelkael $
43 class Zend_Controller_Action_Helper_FlashMessenger extends Zend_Controller_Action_Helper_Abstract implements IteratorAggregate, Countable
45 /**
46 * $_messages - Messages from previous request
48 * @var array
50 static protected $_messages = array();
52 /**
53 * $_session - Zend_Session storage object
55 * @var Zend_Session
57 static protected $_session = null;
59 /**
60 * $_messageAdded - Wether a message has been previously added
62 * @var boolean
64 static protected $_messageAdded = false;
66 /**
67 * $_namespace - Instance namespace, default is 'default'
69 * @var string
71 protected $_namespace = 'default';
73 /**
74 * __construct() - Instance constructor, needed to get iterators, etc
76 * @param string $namespace
77 * @return void
79 public function __construct()
81 if (!self::$_session instanceof Zend_Session_Namespace) {
82 self::$_session = new Zend_Session_Namespace($this->getName());
83 foreach (self::$_session as $namespace => $messages) {
84 self::$_messages[$namespace] = $messages;
85 unset(self::$_session->{$namespace});
90 /**
91 * postDispatch() - runs after action is dispatched, in this
92 * case, it is resetting the namespace in case we have forwarded to a different
93 * action, Flashmessage will be 'clean' (default namespace)
95 * @return Zend_Controller_Action_Helper_FlashMessenger Provides a fluent interface
97 public function postDispatch()
99 $this->resetNamespace();
100 return $this;
104 * setNamespace() - change the namespace messages are added to, useful for
105 * per action controller messaging between requests
107 * @param string $namespace
108 * @return Zend_Controller_Action_Helper_FlashMessenger Provides a fluent interface
110 public function setNamespace($namespace = 'default')
112 $this->_namespace = $namespace;
113 return $this;
117 * resetNamespace() - reset the namespace to the default
119 * @return Zend_Controller_Action_Helper_FlashMessenger Provides a fluent interface
121 public function resetNamespace()
123 $this->setNamespace();
124 return $this;
128 * addMessage() - Add a message to flash message
130 * @param string $message
131 * @return Zend_Controller_Action_Helper_FlashMessenger Provides a fluent interface
133 public function addMessage($message)
135 if (self::$_messageAdded === false) {
136 self::$_session->setExpirationHops(1, null, true);
139 if (!is_array(self::$_session->{$this->_namespace})) {
140 self::$_session->{$this->_namespace} = array();
143 self::$_session->{$this->_namespace}[] = $message;
145 return $this;
149 * hasMessages() - Wether a specific namespace has messages
151 * @return boolean
153 public function hasMessages()
155 return isset(self::$_messages[$this->_namespace]);
159 * getMessages() - Get messages from a specific namespace
161 * @return array
163 public function getMessages()
165 if ($this->hasMessages()) {
166 return self::$_messages[$this->_namespace];
169 return array();
173 * Clear all messages from the previous request & current namespace
175 * @return boolean True if messages were cleared, false if none existed
177 public function clearMessages()
179 if ($this->hasMessages()) {
180 unset(self::$_messages[$this->_namespace]);
181 return true;
184 return false;
188 * hasCurrentMessages() - check to see if messages have been added to current
189 * namespace within this request
191 * @return boolean
193 public function hasCurrentMessages()
195 return isset(self::$_session->{$this->_namespace});
199 * getCurrentMessages() - get messages that have been added to the current
200 * namespace within this request
202 * @return array
204 public function getCurrentMessages()
206 if ($this->hasCurrentMessages()) {
207 return self::$_session->{$this->_namespace};
210 return array();
214 * clear messages from the current request & current namespace
216 * @return boolean
218 public function clearCurrentMessages()
220 if ($this->hasCurrentMessages()) {
221 unset(self::$_session->{$this->_namespace});
222 return true;
225 return false;
229 * getIterator() - complete the IteratorAggregate interface, for iterating
231 * @return ArrayObject
233 public function getIterator()
235 if ($this->hasMessages()) {
236 return new ArrayObject($this->getMessages());
239 return new ArrayObject();
243 * count() - Complete the countable interface
245 * @return int
247 public function count()
249 if ($this->hasMessages()) {
250 return count($this->getMessages());
253 return 0;
257 * Strategy pattern: proxy to addMessage()
259 * @param string $message
260 * @return void
262 public function direct($message)
264 return $this->addMessage($message);