web/lib/Zend/Controller/Action/Helper/FlashMessenger.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     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-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  */
       
    21 
       
    22 /**
       
    23  * @see Zend_Session
       
    24  */
       
    25 require_once 'Zend/Session.php';
       
    26 
       
    27 /**
       
    28  * @see Zend_Controller_Action_Helper_Abstract
       
    29  */
       
    30 require_once 'Zend/Controller/Action/Helper/Abstract.php';
       
    31 
       
    32 /**
       
    33  * Flash Messenger - implement session-based messages
       
    34  *
       
    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-2010 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 20096 2010-01-06 02:05:09Z bkarwin $
       
    42  */
       
    43 class Zend_Controller_Action_Helper_FlashMessenger extends Zend_Controller_Action_Helper_Abstract implements IteratorAggregate, Countable
       
    44 {
       
    45     /**
       
    46      * $_messages - Messages from previous request
       
    47      *
       
    48      * @var array
       
    49      */
       
    50     static protected $_messages = array();
       
    51 
       
    52     /**
       
    53      * $_session - Zend_Session storage object
       
    54      *
       
    55      * @var Zend_Session
       
    56      */
       
    57     static protected $_session = null;
       
    58 
       
    59     /**
       
    60      * $_messageAdded - Wether a message has been previously added
       
    61      *
       
    62      * @var boolean
       
    63      */
       
    64     static protected $_messageAdded = false;
       
    65 
       
    66     /**
       
    67      * $_namespace - Instance namespace, default is 'default'
       
    68      *
       
    69      * @var string
       
    70      */
       
    71     protected $_namespace = 'default';
       
    72 
       
    73     /**
       
    74      * __construct() - Instance constructor, needed to get iterators, etc
       
    75      *
       
    76      * @param  string $namespace
       
    77      * @return void
       
    78      */
       
    79     public function __construct()
       
    80     {
       
    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});
       
    86             }
       
    87         }
       
    88     }
       
    89 
       
    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)
       
    94      *
       
    95      * @return Zend_Controller_Action_Helper_FlashMessenger Provides a fluent interface
       
    96      */
       
    97     public function postDispatch()
       
    98     {
       
    99         $this->resetNamespace();
       
   100         return $this;
       
   101     }
       
   102 
       
   103     /**
       
   104      * setNamespace() - change the namespace messages are added to, useful for
       
   105      * per action controller messaging between requests
       
   106      *
       
   107      * @param  string $namespace
       
   108      * @return Zend_Controller_Action_Helper_FlashMessenger Provides a fluent interface
       
   109      */
       
   110     public function setNamespace($namespace = 'default')
       
   111     {
       
   112         $this->_namespace = $namespace;
       
   113         return $this;
       
   114     }
       
   115 
       
   116     /**
       
   117      * resetNamespace() - reset the namespace to the default
       
   118      *
       
   119      * @return Zend_Controller_Action_Helper_FlashMessenger Provides a fluent interface
       
   120      */
       
   121     public function resetNamespace()
       
   122     {
       
   123         $this->setNamespace();
       
   124         return $this;
       
   125     }
       
   126 
       
   127     /**
       
   128      * addMessage() - Add a message to flash message
       
   129      *
       
   130      * @param  string $message
       
   131      * @return Zend_Controller_Action_Helper_FlashMessenger Provides a fluent interface
       
   132      */
       
   133     public function addMessage($message)
       
   134     {
       
   135         if (self::$_messageAdded === false) {
       
   136             self::$_session->setExpirationHops(1, null, true);
       
   137         }
       
   138 
       
   139         if (!is_array(self::$_session->{$this->_namespace})) {
       
   140             self::$_session->{$this->_namespace} = array();
       
   141         }
       
   142 
       
   143         self::$_session->{$this->_namespace}[] = $message;
       
   144 
       
   145         return $this;
       
   146     }
       
   147 
       
   148     /**
       
   149      * hasMessages() - Wether a specific namespace has messages
       
   150      *
       
   151      * @return boolean
       
   152      */
       
   153     public function hasMessages()
       
   154     {
       
   155         return isset(self::$_messages[$this->_namespace]);
       
   156     }
       
   157 
       
   158     /**
       
   159      * getMessages() - Get messages from a specific namespace
       
   160      *
       
   161      * @return array
       
   162      */
       
   163     public function getMessages()
       
   164     {
       
   165         if ($this->hasMessages()) {
       
   166             return self::$_messages[$this->_namespace];
       
   167         }
       
   168 
       
   169         return array();
       
   170     }
       
   171 
       
   172     /**
       
   173      * Clear all messages from the previous request & current namespace
       
   174      *
       
   175      * @return boolean True if messages were cleared, false if none existed
       
   176      */
       
   177     public function clearMessages()
       
   178     {
       
   179         if ($this->hasMessages()) {
       
   180             unset(self::$_messages[$this->_namespace]);
       
   181             return true;
       
   182         }
       
   183 
       
   184         return false;
       
   185     }
       
   186 
       
   187     /**
       
   188      * hasCurrentMessages() - check to see if messages have been added to current
       
   189      * namespace within this request
       
   190      *
       
   191      * @return boolean
       
   192      */
       
   193     public function hasCurrentMessages()
       
   194     {
       
   195         return isset(self::$_session->{$this->_namespace});
       
   196     }
       
   197 
       
   198     /**
       
   199      * getCurrentMessages() - get messages that have been added to the current
       
   200      * namespace within this request
       
   201      *
       
   202      * @return array
       
   203      */
       
   204     public function getCurrentMessages()
       
   205     {
       
   206         if ($this->hasCurrentMessages()) {
       
   207             return self::$_session->{$this->_namespace};
       
   208         }
       
   209 
       
   210         return array();
       
   211     }
       
   212 
       
   213     /**
       
   214      * clear messages from the current request & current namespace
       
   215      *
       
   216      * @return boolean
       
   217      */
       
   218     public function clearCurrentMessages()
       
   219     {
       
   220         if ($this->hasCurrentMessages()) {
       
   221             unset(self::$_session->{$this->_namespace});
       
   222             return true;
       
   223         }
       
   224 
       
   225         return false;
       
   226     }
       
   227 
       
   228     /**
       
   229      * getIterator() - complete the IteratorAggregate interface, for iterating
       
   230      *
       
   231      * @return ArrayObject
       
   232      */
       
   233     public function getIterator()
       
   234     {
       
   235         if ($this->hasMessages()) {
       
   236             return new ArrayObject($this->getMessages());
       
   237         }
       
   238 
       
   239         return new ArrayObject();
       
   240     }
       
   241 
       
   242     /**
       
   243      * count() - Complete the countable interface
       
   244      *
       
   245      * @return int
       
   246      */
       
   247     public function count()
       
   248     {
       
   249         if ($this->hasMessages()) {
       
   250             return count($this->getMessages());
       
   251         }
       
   252 
       
   253         return 0;
       
   254     }
       
   255 
       
   256     /**
       
   257      * Strategy pattern: proxy to addMessage()
       
   258      *
       
   259      * @param  string $message
       
   260      * @return void
       
   261      */
       
   262     public function direct($message)
       
   263     {
       
   264         return $this->addMessage($message);
       
   265     }
       
   266 }