TYPO3 CMS  TYPO3_7-6
FlashMessageQueue.php
Go to the documentation of this file.
1 <?php
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 = '<ul class="typo3-messages">';
208  foreach ($flashMessages as $flashMessage) {
209  $severityClass = sprintf('alert %s', $flashMessage->getClass());
210  $messageContent = htmlspecialchars($flashMessage->getMessage());
211  if ($flashMessage->getTitle() !== '') {
212  $messageContent = sprintf('<h4>%s</h4>', htmlspecialchars($flashMessage->getTitle())) . $messageContent;
213  }
214  $content .= sprintf('<li class="%s">%s</li>', htmlspecialchars($severityClass), $messageContent);
215  }
216  $content .= '</ul>';
217  }
218  return $content;
219  }
220 
226  public function toArray()
227  {
228  $array = [];
229  $this->rewind();
230  while ($this->valid()) {
231  $array[] = $this->current();
232  $this->next();
233  }
234  return $array;
235  }
236 
243  public function clear($severity = null)
244  {
245  $this->rewind();
246  if ($severity === null) {
247  while (!$this->isEmpty()) {
248  parent::dequeue();
249  }
250  } else {
251  $keysToRemove = [];
252  while ($cur = $this->current()) {
253  if ($cur->getSeverity() === $severity) {
254  $keysToRemove[] = $this->key();
255  }
256  $this->next();
257  }
258  // keys are renumbered when unsetting elements
259  // so unset them from last to first
260  $keysToRemove = array_reverse($keysToRemove);
261  foreach ($keysToRemove as $key) {
262  $this->offsetUnset($key);
263  }
264  }
265  }
266 }
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
storeFlashMessagesInSession(array $flashMessages=null)