[!!!] Extbase: Reintegrating branch "dispatcher" to trunk. Resolves: #10605
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / MVC / Controller / FlashMessages.php
1 <?php
2
3 /***************************************************************
4 * Copyright notice
5 *
6 * (c) 2009 Sebastian Kurf├╝rst <sebastian@typo3.org>
7 * All rights reserved
8 *
9 * This class is a backport of the corresponding class of FLOW3.
10 * All credits go to the v5 team.
11 *
12 * This script is part of the TYPO3 project. The TYPO3 project is
13 * free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * The GNU General Public License can be found at
19 * http://www.gnu.org/copyleft/gpl.html.
20 *
21 * This script is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * This copyright notice MUST APPEAR in all copies of the script!
27 ***************************************************************/
28
29 /**
30 * This is a container for all Flash Messages. It is of scope session, but as Extbase
31 * has no session scope, we need to save it manually.
32 *
33 * @version $Id$
34 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
35 * @scope session
36 * @api
37 */
38 class Tx_Extbase_MVC_Controller_FlashMessages implements t3lib_Singleton {
39
40 /**
41 * The array of flash messages
42 * @var array<string>
43 */
44 protected $flashMessages = array();
45
46 /**
47 * If FALSE, flash message container still needs to be initialized.
48 * @var boolean
49 */
50 protected $initialized = FALSE;
51
52 /**
53 * The key from which the flash messages should be retrieved.
54 * We have to incorporate the PluginKey and the Extension Key in here, to make
55 * it working when multiple plugins are on the same page.
56 * @var string
57 */
58 protected $flashMessageStorageKey = NULL;
59
60 /**
61 * @var Tx_Extbase_Configuration_ConfigurationManager
62 */
63 protected $configurationManager;
64
65 /**
66 * @param Tx_Extbase_Configuration_ConfigurationManagerInterface $configurationManager
67 * @return void
68 */
69 public function injectConfigurationManager(Tx_Extbase_Configuration_ConfigurationManagerInterface $configurationManager) {
70 $this->configurationManager = $configurationManager;
71 }
72
73 /**
74 * Add another flash message.
75 *
76 * @param string $message
77 * @return void
78 * @api
79 */
80 public function add($message) {
81 if (!is_string($message)) throw new InvalidArgumentException('The flash message must be string, ' . gettype($message) . ' given.', 1243258395);
82 $this->initialize();
83 $this->flashMessages[] = $message;
84 }
85
86 /**
87 * Get all flash messages currently available.
88 *
89 * @return array<string> An array of flash messages
90 * @api
91 */
92 public function getAll() {
93 $this->initialize();
94 return $this->flashMessages;
95 }
96
97 /**
98 * Reset all flash messages.
99 *
100 * @return void
101 * @api
102 */
103 public function flush() {
104 $this->initialize();
105 $this->flashMessages = array();
106 }
107
108 /**
109 * Get all flash messages currently available and delete them afterwards.
110 *
111 * @return array<string>
112 * @api
113 */
114 public function getAllAndFlush() {
115 $this->initialize();
116 $flashMessages = $this->flashMessages;
117 $this->flashMessages = array();
118 return $flashMessages;
119 }
120
121 /**
122 * Initialize the flash message
123 */
124 protected function initialize() {
125 if ($this->initialized) return;
126
127 $frameworkConfiguration = $this->configurationManager->getConfiguration(Tx_Extbase_Configuration_ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
128 $this->flashMessageStorageKey = 'Tx_Extbase_MVC_Controller_FlashMessages_messages_' . $frameworkConfiguration['extensionName'] . $frameworkConfiguration['pluginName'];
129
130 $flashMessages = $this->loadFlashMessagesFromSession();
131 if (is_array($flashMessages)) {
132 $this->flashMessages = $flashMessages;
133 }
134
135 $this->initialized = TRUE;
136 }
137
138 /**
139 * Loads the flash messages from the current user session.
140 */
141 protected function loadFlashMessagesFromSession() {
142 $flashMessages = NULL;
143 if (TYPO3_MODE === 'FE') {
144 $flashMessages = $GLOBALS['TSFE']->fe_user->getKey('ses', $this->flashMessageStorageKey);
145 } else {
146 $flashMessages = $GLOBALS['BE_USER']->uc[$this->flashMessageStorageKey];
147 $GLOBALS['BE_USER']->writeUC();
148 }
149 return $flashMessages;
150 }
151
152 /**
153 * Reset the flash messages. Needs to be called at the beginning of a new rendering,
154 * to account when multiple plugins appear on the same page.
155 */
156 public function reset() {
157 $this->flashMessages = array();
158 $this->initialized = FALSE;
159 $this->flashMessageStorageKey = NULL;
160 }
161
162 /**
163 * Persist the flash messages in the session.
164 */
165 public function persist() {
166 if (!$this->initialized) {
167 return;
168 }
169 if (TYPO3_MODE === 'FE') {
170 $GLOBALS['TSFE']->fe_user->setKey(
171 'ses',
172 $this->flashMessageStorageKey,
173 $this->flashMessages
174 );
175 $GLOBALS['TSFE']->fe_user->storeSessionData();
176 } else {
177 $GLOBALS['BE_USER']->uc[$this->flashMessageStorageKey] = $this->flashMessages;
178 $GLOBALS['BE_USER']->writeUc();
179 }
180 }
181 }
182
183 ?>