[BUGFIX] stdWrap numRows fails due to wrong SELECT clause
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_flashmessagequeue.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009-2011 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 * A class which collects and renders flash messages.
30 *
31 * @author Rupert Germann <rupi@gmx.li>
32 * @package TYPO3
33 * @subpackage t3lib
34 */
35 class t3lib_FlashMessageQueue {
36
37 static $messages = array();
38
39 /**
40 * Static class, no instances allowed.
41 */
42 protected function __construct() {
43 }
44
45 /**
46 * Adds a message either to the BE_USER session (if the $message has the storeInSession flag set)
47 * or it adds the message to self::$messages.
48 *
49 * @param object $message Instance of t3lib_FlashMessage, representing a message
50 * @return void
51 */
52 public static function addMessage(t3lib_FlashMessage $message) {
53 if ($message->isSessionMessage()) {
54 $queuedFlashMessages = self::getFlashMessagesFromSession();
55 $queuedFlashMessages[] = $message;
56 self::storeFlashMessagesInSession($queuedFlashMessages);
57 } else {
58 self::$messages[] = $message;
59 }
60 }
61
62 /**
63 * Returns all messages from the current PHP session and from the current request.
64 *
65 * @return array Array of t3lib_FlashMessage objects
66 */
67 public static function getAllMessages() {
68 // Get messages from user session
69 $queuedFlashMessagesFromSession = self::getFlashMessagesFromSession();
70 $queuedFlashMessages = array_merge($queuedFlashMessagesFromSession, self::$messages);
71
72 return $queuedFlashMessages;
73 }
74
75 /**
76 * Returns all messages from the current PHP session and from the current request.
77 * After fetching the messages the internal queue and the message queue in the session
78 * will be emptied.
79 *
80 * @return array Array of t3lib_FlashMessage objects
81 */
82 public static function getAllMessagesAndFlush() {
83 $queuedFlashMessages = self::getAllMessages();
84
85 // Reset messages in user session
86 self::removeAllFlashMessagesFromSession();
87 // Reset internal messages
88 self::$messages = array();
89
90 return $queuedFlashMessages;
91 }
92
93 /**
94 * Stores given flash messages in the session
95 *
96 * @param array $flashMessages Array of t3lib_FlashMessage
97 * @return void
98 */
99 protected static function storeFlashMessagesInSession(array $flashMessages) {
100 self::getUserByContext()->setAndSaveSessionData('core.template.flashMessages', $flashMessages);
101 }
102
103 /**
104 * Removes all flash messages from the session
105 *
106 * @return void
107 */
108 protected static function removeAllFlashMessagesFromSession() {
109 self::getUserByContext()->setAndSaveSessionData('core.template.flashMessages', NULL);
110 }
111
112 /**
113 * Returns current flash messages from the session, making sure to always
114 * return an array.
115 *
116 * @return array An array of t3lib_FlashMessage flash messages.
117 */
118 protected static function getFlashMessagesFromSession() {
119 $flashMessages = self::getUserByContext()->getSessionData('core.template.flashMessages');
120
121 return is_array($flashMessages) ? $flashMessages : array();
122 }
123
124 /**
125 * Gets user object by context
126 *
127 * @return object User object
128 */
129 protected static function getUserByContext() {
130 return TYPO3_MODE === 'BE' ? $GLOBALS['BE_USER'] : $GLOBALS['TSFE']->fe_user;
131 }
132
133 /**
134 * Fetches and renders all available flash messages from the queue.
135 *
136 * @return string All flash messages in the queue rendered as HTML.
137 */
138 public static function renderFlashMessages() {
139 $content = '';
140 $flashMessages = self::getAllMessagesAndFlush();
141
142 if (count($flashMessages)) {
143 foreach ($flashMessages as $flashMessage) {
144 $content .= $flashMessage->render();
145 }
146 }
147 return $content;
148 }
149 }
150
151 ?>