Follow-up to bugfix #12324: Renamed sanitizeBackEndUrl() to sanitizeLocalUrl() in...
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_flashmessagequeue.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 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 * Adds a message either to the BE_USER session (if the $message has the storeInSession flag set)
48 * or it adds the message to self::$messages.
49 *
50 * @param object instance of t3lib_FlashMessage, representing a message
51 * @return void
52 */
53 public static function addMessage(t3lib_FlashMessage $message) {
54 if ($message->isSessionMessage() === TRUE) {
55 $queuedFlashMessages = self::getFlashMessagesFromSession();
56 $queuedFlashMessages[] = $message;
57
58 $GLOBALS['BE_USER']->setAndSaveSessionData(
59 'core.template.flashMessages',
60 $queuedFlashMessages
61 );
62 } else {
63 self::$messages[] = $message;
64 }
65 }
66
67 /**
68 * Returns all messages from the current PHP session and from the current request.
69 * After fetching the messages the internal queue and the message queue in the session
70 * will be emptied.
71 *
72 * @return array array of t3lib_FlashMessage objects
73 */
74 public static function getAllMessagesAndFlush() {
75 // get messages from user session
76 $queuedFlashMessagesFromSession = self::getFlashMessagesFromSession();
77 if (!empty($queuedFlashMessagesFromSession)) {
78 // reset messages in user session
79 $GLOBALS['BE_USER']->setAndSaveSessionData(
80 'core.template.flashMessages',
81 null
82 );
83 }
84
85 $queuedFlashMessages = array_merge($queuedFlashMessagesFromSession, self::$messages);
86
87 // reset internal messages
88 self::$messages = array();
89
90 return $queuedFlashMessages;
91 }
92
93 /**
94 * Returns current flash messages from the session, making sure to always
95 * return an array.
96 *
97 * @return array An array of t3lib_FlashMessage flash messages.
98 */
99 protected static function getFlashMessagesFromSession() {
100 $flashMessages = $GLOBALS['BE_USER']->getSessionData('core.template.flashMessages');
101 return is_array($flashMessages) ? $flashMessages : array();
102 }
103
104 /**
105 * Fetches and renders all available flash messages from the queue.
106 *
107 * @return string All flash messages in the queue rendered as HTML.
108 */
109 public static function renderFlashMessages() {
110 $content = '';
111 $flashMessages = self::getAllMessagesAndFlush();
112
113 if (count($flashMessages)) {
114 foreach ($flashMessages as $flashMessage) {
115 $content .= $flashMessage->render();
116 }
117 }
118 return $content;
119 }
120
121
122 }
123
124
125 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_flashmessagequeue.php']) {
126 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_flashmessagequeue.php']);
127 }
128 ?>