TYPO3 API  SVNRelease
class.t3lib_flashmessagequeue.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003  *  Copyright notice
00004  *
00005  *  (c) 2009-2011 Rupert Germann <rupi@gmx.li>
00006  *  All rights reserved
00007  *
00008  *  This script is part of the TYPO3 project. The TYPO3 project is
00009  *  free software; you can redistribute it and/or modify
00010  *  it under the terms of the GNU General Public License as published by
00011  *  the Free Software Foundation; either version 2 of the License, or
00012  *  (at your option) any later version.
00013  *
00014  *  The GNU General Public License can be found at
00015  *  http://www.gnu.org/copyleft/gpl.html.
00016  *  A copy is found in the textfile GPL.txt and important notices to the license
00017  *  from the author is found in LICENSE.txt distributed with these scripts.
00018  *
00019  *
00020  *  This script is distributed in the hope that it will be useful,
00021  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  *  GNU General Public License for more details.
00024  *
00025  *  This copyright notice MUST APPEAR in all copies of the script!
00026  ***************************************************************/
00027 
00028 
00029 /**
00030  *  A class which collects and renders flash messages.
00031  *
00032  * @author  Rupert Germann <rupi@gmx.li>
00033  * @package TYPO3
00034  * @subpackage t3lib
00035  */
00036 class t3lib_FlashMessageQueue {
00037 
00038     static $messages = array();
00039 
00040     /**
00041      * Static class, no instances allowed.
00042      */
00043     protected function __construct() {
00044     }
00045 
00046 
00047     /**
00048      * Adds a message either to the BE_USER session (if the $message has the storeInSession flag set)
00049      * or it adds the message to self::$messages.
00050      *
00051      * @param   object   instance of t3lib_FlashMessage, representing a message
00052      * @return   void
00053      */
00054     public static function addMessage(t3lib_FlashMessage $message) {
00055         if ($message->isSessionMessage()) {
00056             $queuedFlashMessages = self::getFlashMessagesFromSession();
00057             $queuedFlashMessages[] = $message;
00058             self::storeFlashMessagesInSession($queuedFlashMessages);
00059         } else {
00060             self::$messages[] = $message;
00061         }
00062     }
00063 
00064     /**
00065      * Returns all messages from the current PHP session and from the current request.
00066      *
00067      * @return   array   array of t3lib_FlashMessage objects
00068      */
00069     public static function getAllMessages() {
00070             // get messages from user session
00071         $queuedFlashMessagesFromSession = self::getFlashMessagesFromSession();
00072         $queuedFlashMessages = array_merge($queuedFlashMessagesFromSession, self::$messages);
00073 
00074         return $queuedFlashMessages;
00075     }
00076 
00077     /**
00078      * Returns all messages from the current PHP session and from the current request.
00079      * After fetching the messages the internal queue and the message queue in the session
00080      * will be emptied.
00081      *
00082      * @return   array   array of t3lib_FlashMessage objects
00083      */
00084     public static function getAllMessagesAndFlush() {
00085         $queuedFlashMessages = self::getAllMessages();
00086 
00087             // reset messages in user session
00088         self::removeAllFlashMessagesFromSession();
00089             // reset internal messages
00090         self::$messages = array();
00091 
00092         return $queuedFlashMessages;
00093     }
00094 
00095     /**
00096      * Stores given flash messages in the session
00097      *
00098      * @param   array   array of t3lib_FlashMessage
00099      * @return  void
00100      */
00101     protected static function storeFlashMessagesInSession(array $flashMessages) {
00102         self::getUserByContext()->setAndSaveSessionData('core.template.flashMessages', $flashMessages);
00103 
00104     }
00105 
00106     /**
00107      * Removes all flash messages from the session
00108      *
00109      * @return  void
00110      */
00111     protected static function removeAllFlashMessagesFromSession() {
00112         self::getUserByContext()->setAndSaveSessionData('core.template.flashMessages', NULL);
00113     }
00114 
00115     /**
00116      * Returns current flash messages from the session, making sure to always
00117      * return an array.
00118      *
00119      * @return  array   An array of t3lib_FlashMessage flash messages.
00120      */
00121     protected static function getFlashMessagesFromSession() {
00122         $flashMessages = self::getUserByContext()->getSessionData('core.template.flashMessages');
00123 
00124         return is_array($flashMessages) ? $flashMessages : array();
00125     }
00126 
00127     /**
00128      * Gets user object by context
00129      *
00130      * @return object user object
00131      */
00132     protected static function getUserByContext() {
00133         return TYPO3_MODE === 'BE' ? $GLOBALS['BE_USER'] : $GLOBALS['TSFE']->fe_user;
00134     }
00135 
00136     /**
00137      * Fetches and renders all available flash messages from the queue.
00138      *
00139      * @return  string  All flash messages in the queue rendered as HTML.
00140      */
00141     public static function renderFlashMessages() {
00142         $content = '';
00143         $flashMessages = self::getAllMessagesAndFlush();
00144 
00145         if (count($flashMessages)) {
00146             foreach ($flashMessages as $flashMessage) {
00147                 $content .= $flashMessage->render();
00148             }
00149         }
00150         return $content;
00151     }
00152 
00153 
00154 }
00155 
00156 
00157 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_flashmessagequeue.php'])) {
00158     include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_flashmessagequeue.php']);
00159 }
00160 ?>