f0299f548296d0a8f2995b4657980d0d4abaf8dc
[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 * Add another flash message.
62 *
63 * @param string $message
64 * @return void
65 * @api
66 */
67 public function add($message) {
68 if (!is_string($message)) throw new InvalidArgumentException('The flash message must be string, ' . gettype($message) . ' given.', 1243258395);
69 $this->initialize();
70 $this->flashMessages[] = $message;
71 }
72
73 /**
74 * Get all flash messages currently available.
75 *
76 * @return array<string> An array of flash messages
77 * @api
78 */
79 public function getAll() {
80 $this->initialize();
81 return $this->flashMessages;
82 }
83
84 /**
85 * Reset all flash messages.
86 *
87 * @return void
88 * @api
89 */
90 public function flush() {
91 $this->initialize();
92 $this->flashMessages = array();
93 }
94
95 /**
96 * Get all flash messages currently available and delete them afterwards.
97 *
98 * @return array<string>
99 * @api
100 */
101 public function getAllAndFlush() {
102 $this->initialize();
103 $flashMessages = $this->flashMessages;
104 $this->flashMessages = array();
105 return $flashMessages;
106 }
107
108 /**
109 * Initialize the flash message
110 */
111 protected function initialize() {
112 if ($this->initialized) return;
113
114 $frameworkConfiguration = Tx_Extbase_Dispatcher::getExtbaseFrameworkConfiguration();
115 $this->flashMessageStorageKey = 'Tx_Extbase_MVC_Controller_FlashMessages_messages_' . $frameworkConfiguration['extensionName'] . $frameworkConfiguration['pluginName'];
116
117 $flashMessages = $this->loadFlashMessagesFromSession();
118 if (is_array($flashMessages)) {
119 $this->flashMessages = $flashMessages;
120 }
121
122 $this->initialized = TRUE;
123 }
124
125 /**
126 * Loads the flash messages from the current user session.
127 */
128 protected function loadFlashMessagesFromSession() {
129 $flashMessages = NULL;
130 if (TYPO3_MODE === 'FE') {
131 $flashMessages = $GLOBALS['TSFE']->fe_user->getKey('ses', $this->flashMessageStorageKey);
132 } else {
133 $flashMessages = $GLOBALS['BE_USER']->uc[$this->flashMessageStorageKey];
134 $GLOBALS['BE_USER']->writeUC();
135 }
136 return $flashMessages;
137 }
138
139 /**
140 * Reset the flash messages. Needs to be called at the beginning of a new rendering,
141 * to account when multiple plugins appear on the same page.
142 */
143 public function reset() {
144 $this->flashMessages = array();
145 $this->initialized = FALSE;
146 $this->flashMessageStorageKey = NULL;
147 }
148
149 /**
150 * Persist the flash messages in the session.
151 */
152 public function persist() {
153 if (!$this->initialized) {
154 return;
155 }
156 if (TYPO3_MODE === 'FE') {
157 $GLOBALS['TSFE']->fe_user->setKey(
158 'ses',
159 $this->flashMessageStorageKey,
160 $this->flashMessages
161 );
162 $GLOBALS['TSFE']->fe_user->storeSessionData();
163 } else {
164 $GLOBALS['BE_USER']->uc[$this->flashMessageStorageKey] = $this->flashMessages;
165 $GLOBALS['BE_USER']->writeUc();
166 }
167 }
168 }
169
170 ?>