class.t3lib_flashmessagequeue.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 2009-2010 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      * Adds a message either to the BE_USER session (if the $message has the storeInSession flag set)
00048      * or it adds the message to self::$messages.
00049      *
00050      * @param   object  instance of t3lib_FlashMessage, representing a message
00051      * @return  void
00052      */
00053     public static function addMessage(t3lib_FlashMessage $message) {
00054         if ($message->isSessionMessage() === TRUE) {
00055             $queuedFlashMessages = self::getFlashMessagesFromSession();
00056             $queuedFlashMessages[] = $message;
00057 
00058             $GLOBALS['BE_USER']->setAndSaveSessionData(
00059                 'core.template.flashMessages',
00060                 $queuedFlashMessages
00061             );
00062         } else {
00063             self::$messages[] = $message;
00064         }
00065     }
00066 
00067     /**
00068      * Returns all messages from the current PHP session and from the current request.
00069      * After fetching the messages the internal queue and the message queue in the session
00070      * will be emptied.
00071      *
00072      * @return  array   array of t3lib_FlashMessage objects
00073      */
00074     public static function getAllMessagesAndFlush() {
00075             // get messages from user session
00076         $queuedFlashMessagesFromSession = self::getFlashMessagesFromSession();
00077         if (!empty($queuedFlashMessagesFromSession)) {
00078                 // reset messages in user session
00079             $GLOBALS['BE_USER']->setAndSaveSessionData(
00080                 'core.template.flashMessages',
00081                 null
00082             );
00083         }
00084 
00085         $queuedFlashMessages = array_merge($queuedFlashMessagesFromSession, self::$messages);
00086 
00087             // reset internal messages
00088         self::$messages = array();
00089 
00090         return $queuedFlashMessages;
00091     }
00092 
00093     /**
00094      * Returns current flash messages from the session, making sure to always
00095      * return an array.
00096      *
00097      * @return  array   An array of t3lib_FlashMessage flash messages.
00098      */
00099     protected static function getFlashMessagesFromSession() {
00100         $flashMessages = $GLOBALS['BE_USER']->getSessionData('core.template.flashMessages');
00101         return is_array($flashMessages) ? $flashMessages : array();
00102     }
00103 
00104     /**
00105      * Fetches and renders all available flash messages from the queue.
00106      *
00107      * @return  string  All flash messages in the queue rendered as HTML.
00108      */
00109     public static function renderFlashMessages() {
00110         $content = '';
00111         $flashMessages = self::getAllMessagesAndFlush();
00112 
00113         if (count($flashMessages)) {
00114             foreach ($flashMessages as $flashMessage) {
00115                 $content .= $flashMessage->render();
00116             }
00117         }
00118         return $content;
00119     }
00120 
00121 
00122 }
00123 
00124 
00125 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_flashmessagequeue.php']) {
00126     include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_flashmessagequeue.php']);
00127 }
00128 ?>

Generated on Sat Jul 24 04:17:17 2010 for TYPO3 API by  doxygen 1.4.7