Fixed bug #14050: CleanUp - CGL format of t3lib files - t3lib_positionmap
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_flashmessagequeue.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009-2010 Rupert Germann <rupi@gmx.li>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28
29 /**
30 * A class which collects and renders flash messages.
31 *
32 * @author Rupert Germann <rupi@gmx.li>
33 * @package TYPO3
34 * @subpackage t3lib
35 */
36 class t3lib_FlashMessageQueue {
37
38 static $messages = array();
39
40 /**
41 * Static class, no instances allowed.
42 */
43 protected function __construct() {
44 }
45
46
47 /**
48 * Adds a message either to the BE_USER session (if the $message has the storeInSession flag set)
49 * or it adds the message to self::$messages.
50 *
51 * @param object instance of t3lib_FlashMessage, representing a message
52 * @return void
53 */
54 public static function addMessage(t3lib_FlashMessage $message) {
55 if ($message->isSessionMessage()) {
56 $queuedFlashMessages = self::getFlashMessagesFromSession();
57 $queuedFlashMessages[] = $message;
58
59 $GLOBALS['BE_USER']->setAndSaveSessionData(
60 'core.template.flashMessages',
61 $queuedFlashMessages
62 );
63 } else {
64 self::$messages[] = $message;
65 }
66 }
67
68 /**
69 * Returns all messages from the current PHP session and from the current request.
70 * After fetching the messages the internal queue and the message queue in the session
71 * will be emptied.
72 *
73 * @return array array of t3lib_FlashMessage objects
74 */
75 public static function getAllMessagesAndFlush() {
76 // get messages from user session
77 $queuedFlashMessagesFromSession = self::getFlashMessagesFromSession();
78 if (!empty($queuedFlashMessagesFromSession)) {
79 // reset messages in user session
80 $GLOBALS['BE_USER']->setAndSaveSessionData(
81 'core.template.flashMessages',
82 null
83 );
84 }
85
86 $queuedFlashMessages = array_merge($queuedFlashMessagesFromSession, self::$messages);
87
88 // reset internal messages
89 self::$messages = array();
90
91 return $queuedFlashMessages;
92 }
93
94 /**
95 * Returns current flash messages from the session, making sure to always
96 * return an array.
97 *
98 * @return array An array of t3lib_FlashMessage flash messages.
99 */
100 protected static function getFlashMessagesFromSession() {
101 $flashMessages = $GLOBALS['BE_USER']->getSessionData('core.template.flashMessages');
102 return is_array($flashMessages) ? $flashMessages : array();
103 }
104
105 /**
106 * Fetches and renders all available flash messages from the queue.
107 *
108 * @return string All flash messages in the queue rendered as HTML.
109 */
110 public static function renderFlashMessages() {
111 $content = '';
112 $flashMessages = self::getAllMessagesAndFlush();
113
114 if (count($flashMessages)) {
115 foreach ($flashMessages as $flashMessage) {
116 $content .= $flashMessage->render();
117 }
118 }
119 return $content;
120 }
121
122
123 }
124
125
126 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_flashmessagequeue.php']) {
127 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_flashmessagequeue.php']);
128 }
129 ?>