File indexing completed on 2024-12-29 05:27:31

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Controller
0017  * @subpackage Zend_Controller_Action_Helper
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  */
0021 
0022 /**
0023  * @see Zend_Session
0024  */
0025 // require_once 'Zend/Session.php';
0026 
0027 /**
0028  * @see Zend_Controller_Action_Helper_Abstract
0029  */
0030 // require_once 'Zend/Controller/Action/Helper/Abstract.php';
0031 
0032 /**
0033  * Flash Messenger - implement session-based messages
0034  *
0035  * @uses       Zend_Controller_Action_Helper_Abstract
0036  * @category   Zend
0037  * @package    Zend_Controller
0038  * @subpackage Zend_Controller_Action_Helper
0039  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0040  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0041  * @version    $Id$
0042  */
0043 class Zend_Controller_Action_Helper_FlashMessenger extends Zend_Controller_Action_Helper_Abstract implements IteratorAggregate, Countable
0044 {
0045     /**
0046      * $_messages - Messages from previous request
0047      *
0048      * @var array
0049      */
0050     static protected $_messages = array();
0051 
0052     /**
0053      * $_session - Zend_Session storage object
0054      *
0055      * @var Zend_Session
0056      */
0057     static protected $_session = null;
0058 
0059     /**
0060      * $_messageAdded - Wether a message has been previously added
0061      *
0062      * @var boolean
0063      */
0064     static protected $_messageAdded = false;
0065 
0066     /**
0067      * $_namespace - Instance namespace, default is 'default'
0068      *
0069      * @var string
0070      */
0071     protected $_namespace = 'default';
0072 
0073     /**
0074      * __construct() - Instance constructor, needed to get iterators, etc
0075      *
0076      * @param  string $namespace
0077      * @return void
0078      */
0079     public function __construct()
0080     {
0081         if (!self::$_session instanceof Zend_Session_Namespace) {
0082             self::$_session = new Zend_Session_Namespace($this->getName());
0083             foreach (self::$_session as $namespace => $messages) {
0084                 self::$_messages[$namespace] = $messages;
0085                 unset(self::$_session->{$namespace});
0086             }
0087         }
0088     }
0089 
0090     /**
0091      * postDispatch() - runs after action is dispatched, in this
0092      * case, it is resetting the namespace in case we have forwarded to a different
0093      * action, Flashmessage will be 'clean' (default namespace)
0094      *
0095      * @return Zend_Controller_Action_Helper_FlashMessenger Provides a fluent interface
0096      */
0097     public function postDispatch()
0098     {
0099         $this->resetNamespace();
0100         return $this;
0101     }
0102 
0103     /**
0104      * setNamespace() - change the namespace messages are added to, useful for
0105      * per action controller messaging between requests
0106      *
0107      * @param  string $namespace
0108      * @return Zend_Controller_Action_Helper_FlashMessenger Provides a fluent interface
0109      */
0110     public function setNamespace($namespace = 'default')
0111     {
0112         $this->_namespace = $namespace;
0113         return $this;
0114     }
0115     
0116     /**
0117      * getNamespace() - return the current namepsace
0118      * 
0119      * @return string
0120      */
0121     public function getNamespace()
0122     {
0123         return $this->_namespace;
0124     }
0125 
0126     /**
0127      * resetNamespace() - reset the namespace to the default
0128      *
0129      * @return Zend_Controller_Action_Helper_FlashMessenger Provides a fluent interface
0130      */
0131     public function resetNamespace()
0132     {
0133         $this->setNamespace();
0134         return $this;
0135     }
0136 
0137     /**
0138      * addMessage() - Add a message to flash message
0139      *
0140      * @param  string $message
0141      * @return Zend_Controller_Action_Helper_FlashMessenger Provides a fluent interface
0142      */
0143     public function addMessage($message, $namespace = null)
0144     {
0145         if (!is_string($namespace) || $namespace == '') {
0146             $namespace = $this->getNamespace();
0147         }
0148         
0149         if (self::$_messageAdded === false) {
0150             self::$_session->setExpirationHops(1, null, true);
0151         }
0152 
0153         if (!is_array(self::$_session->{$namespace})) {
0154             self::$_session->{$namespace} = array();
0155         }
0156 
0157         self::$_session->{$namespace}[] = $message;
0158         self::$_messageAdded = true;
0159 
0160         return $this;
0161     }
0162 
0163     /**
0164      * hasMessages() - Wether a specific namespace has messages
0165      *
0166      * @return boolean
0167      */
0168     public function hasMessages($namespace = null)
0169     {
0170         if (!is_string($namespace) || $namespace == '') {
0171             $namespace = $this->getNamespace();
0172         }
0173         
0174         return isset(self::$_messages[$namespace]);
0175     }
0176 
0177     /**
0178      * getMessages() - Get messages from a specific namespace
0179      *
0180      * @return array
0181      */
0182     public function getMessages($namespace = null)
0183     {
0184         if (!is_string($namespace) || $namespace == '') {
0185             $namespace = $this->getNamespace();
0186         }
0187         
0188         if ($this->hasMessages($namespace)) {
0189             return self::$_messages[$namespace];
0190         }
0191 
0192         return array();
0193     }
0194 
0195     /**
0196      * Clear all messages from the previous request & current namespace
0197      *
0198      * @return boolean True if messages were cleared, false if none existed
0199      */
0200     public function clearMessages($namespace = null)
0201     {
0202         if (!is_string($namespace) || $namespace == '') {
0203             $namespace = $this->getNamespace();
0204         }
0205         
0206         if ($this->hasMessages($namespace)) {
0207             unset(self::$_messages[$namespace]);
0208             return true;
0209         }
0210 
0211         return false;
0212     }
0213 
0214     /**
0215      * hasCurrentMessages() - check to see if messages have been added to current
0216      * namespace within this request
0217      *
0218      * @return boolean
0219      */
0220     public function hasCurrentMessages($namespace = null)
0221     {
0222         if (!is_string($namespace) || $namespace == '') {
0223             $namespace = $this->getNamespace();
0224         }
0225         
0226         return isset(self::$_session->{$namespace});
0227     }
0228 
0229     /**
0230      * getCurrentMessages() - get messages that have been added to the current
0231      * namespace within this request
0232      *
0233      * @return array
0234      */
0235     public function getCurrentMessages($namespace = null)
0236     {
0237         if (!is_string($namespace) || $namespace == '') {
0238             $namespace = $this->getNamespace();
0239         }
0240         
0241         if ($this->hasCurrentMessages($namespace)) {
0242             return self::$_session->{$namespace};
0243         }
0244 
0245         return array();
0246     }
0247 
0248     /**
0249      * clear messages from the current request & current namespace
0250      *
0251      * @return boolean
0252      */
0253     public function clearCurrentMessages($namespace = null)
0254     {
0255         if (!is_string($namespace) || $namespace == '') {
0256             $namespace = $this->getNamespace();
0257         }
0258         
0259         if ($this->hasCurrentMessages($namespace)) {
0260             unset(self::$_session->{$namespace});
0261             return true;
0262         }
0263 
0264         return false;
0265     }
0266 
0267     /**
0268      * getIterator() - complete the IteratorAggregate interface, for iterating
0269      *
0270      * @return ArrayObject
0271      */
0272     public function getIterator($namespace = null)
0273     {
0274         if (!is_string($namespace) || $namespace == '') {
0275             $namespace = $this->getNamespace();
0276         }
0277         
0278         if ($this->hasMessages($namespace)) {
0279             return new ArrayObject($this->getMessages($namespace));
0280         }
0281 
0282         return new ArrayObject();
0283     }
0284 
0285     /**
0286      * count() - Complete the countable interface
0287      *
0288      * @return int
0289      */
0290     public function count($namespace = null)
0291     {
0292         if (!is_string($namespace) || $namespace == '') {
0293             $namespace = $this->getNamespace();
0294         }
0295         
0296         if ($this->hasMessages($namespace)) {
0297             return count($this->getMessages($namespace));
0298         }
0299 
0300         return 0;
0301     }
0302 
0303     /**
0304      * Strategy pattern: proxy to addMessage()
0305      *
0306      * @param  string $message
0307      * @return void
0308      */
0309     public function direct($message, $namespace=NULL)
0310     {
0311         return $this->addMessage($message, $namespace);
0312     }
0313 }