TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
FlashMessageQueue.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Messaging;
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
18 
22 class FlashMessageQueue extends \SplQueue
23 {
29  protected $identifier;
30 
34  public function __construct($identifier)
35  {
36  $this->identifier = $identifier;
37  }
38 
42  public function getIdentifier()
43  {
44  return $this->identifier;
45  }
46 
55  public function enqueue($message)
56  {
57  if (!($message instanceof FlashMessage)) {
58  throw new \TYPO3\CMS\Core\Exception(
59  'FlashMessageQueue::enqueue() expects an object of type \TYPO3\CMS\Core\Messaging\FlashMessage but got type "' . (is_object($message) ? get_class($message) : gettype($message)) . '"',
60  1376833554
61  );
62  }
63  if ($message->isSessionMessage()) {
64  $this->addFlashMessageToSession($message);
65  } else {
66  parent::enqueue($message);
67  }
68  }
69 
74  public function addMessage(FlashMessage $message)
75  {
76  $this->enqueue($message);
77  }
78 
82  public function dequeue()
83  {
84  // deliberately empty
85  }
86 
94  protected function addFlashMessageToSession(FlashMessage $message)
95  {
96  $queuedFlashMessages = $this->getFlashMessagesFromSession();
97  $queuedFlashMessages[] = $message;
98  $this->storeFlashMessagesInSession($queuedFlashMessages);
99  }
100 
107  public function getAllMessages($severity = null)
108  {
109  // Get messages from user session
110  $queuedFlashMessagesFromSession = $this->getFlashMessagesFromSession();
111  $queuedFlashMessages = array_merge($queuedFlashMessagesFromSession, $this->toArray());
112  if ($severity !== null) {
113  $filteredFlashMessages = [];
114  foreach ($queuedFlashMessages as $message) {
115  if ($message->getSeverity() === $severity) {
116  $filteredFlashMessages[] = $message;
117  }
118  }
119  return $filteredFlashMessages;
120  }
121 
122  return $queuedFlashMessages;
123  }
124 
133  public function getAllMessagesAndFlush($severity = null)
134  {
135  $queuedFlashMessages = $this->getAllMessages($severity);
136  // Reset messages in user session
137  $this->removeAllFlashMessagesFromSession($severity);
138  // Reset internal messages
139  $this->clear($severity);
140  return $queuedFlashMessages;
141  }
142 
149  protected function storeFlashMessagesInSession(array $flashMessages = null)
150  {
151  $this->getUserByContext()->setAndSaveSessionData($this->identifier, $flashMessages);
152  }
153 
160  protected function removeAllFlashMessagesFromSession($severity = null)
161  {
162  if ($severity === null) {
163  $this->storeFlashMessagesInSession(null);
164  } else {
165  $messages = $this->getFlashMessagesFromSession();
166  foreach ($messages as $index => $message) {
167  if ($message->getSeverity() === $severity) {
168  unset($messages[$index]);
169  }
170  }
171  $this->storeFlashMessagesInSession($messages);
172  }
173  }
174 
181  protected function getFlashMessagesFromSession()
182  {
183  $flashMessages = $this->getUserByContext()->getSessionData($this->identifier);
184  return is_array($flashMessages) ? $flashMessages : [];
185  }
186 
192  protected function getUserByContext()
193  {
194  return TYPO3_MODE === 'BE' ? $GLOBALS['BE_USER'] : $GLOBALS['TSFE']->fe_user;
195  }
196 
202  public function renderFlashMessages()
203  {
204  $content = '';
205  $flashMessages = $this->getAllMessagesAndFlush();
206  if (!empty($flashMessages)) {
207  $content .= '<div class="typo3-messages">';
208  foreach ($flashMessages as $flashMessage) {
209  $content .= $flashMessage->getMessageAsMarkup();
210  }
211  $content .= '</div>';
212  }
213  return $content;
214  }
215 
221  public function toArray()
222  {
223  $array = [];
224  $this->rewind();
225  while ($this->valid()) {
226  $array[] = $this->current();
227  $this->next();
228  }
229  return $array;
230  }
231 
238  public function clear($severity = null)
239  {
240  $this->rewind();
241  if ($severity === null) {
242  while (!$this->isEmpty()) {
243  parent::dequeue();
244  }
245  } else {
246  $keysToRemove = [];
247  while ($cur = $this->current()) {
248  if ($cur->getSeverity() === $severity) {
249  $keysToRemove[] = $this->key();
250  }
251  $this->next();
252  }
253  // keys are renumbered when unsetting elements
254  // so unset them from last to first
255  $keysToRemove = array_reverse($keysToRemove);
256  foreach ($keysToRemove as $key) {
257  $this->offsetUnset($key);
258  }
259  }
260  }
261 }
if(TYPO3_MODE=== 'BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
storeFlashMessagesInSession(array $flashMessages=null)