[FEATURE] Severity-filtering for FlashMessageQueue 41/32941/8
authorStefan Neufeind <typo3.neufeind@speedpartner.de>
Tue, 23 Sep 2014 13:38:05 +0000 (15:38 +0200)
committerAndreas Wolf <andreas.wolf@typo3.org>
Wed, 5 Aug 2015 21:25:00 +0000 (23:25 +0200)
This allows to fetch/clear only messages of a given
severity. FlashMessageContainer in TYPO3.Flow
offers similar functionality.

This is the basis for later extending the
FlashMessageViewHelper as well to only show messages
of a certain level.

Change-Id: Ibb84b5aa051be3a350cd97ae5c5058290748d4fe
Resolves: #7098
Releases: master
Reviewed-on: http://review.typo3.org/32941
Reviewed-by: Cedric Ziel <cedric@cedric-ziel.com>
Reviewed-by: Markus Klein <markus.klein@typo3.org>
Reviewed-by: Frederic Gaus <frederic.gaus@flagbit.de>
Reviewed-by: Andreas Wolf <andreas.wolf@typo3.org>
Tested-by: Daniel Maier <dani-maier@gmx.de>
Tested-by: Andreas Wolf <andreas.wolf@typo3.org>
typo3/sysext/core/Classes/Messaging/FlashMessageQueue.php
typo3/sysext/core/Documentation/Changelog/master/Feature-7098-SeverityFilteringForFlashMessageQueue.rst [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Messaging/FlashMessageQueueTest.php

index a7a855e..468c814 100644 (file)
@@ -88,12 +88,23 @@ class FlashMessageQueue extends \SplQueue {
        /**
         * Returns all messages from the current PHP session and from the current request.
         *
+        * @param int $severity Optional severity, must be one of \TYPO3\CMS\Core\Messaging\AbstractMessage constants
         * @return FlashMessage[]
         */
-       public function getAllMessages() {
+       public function getAllMessages($severity = NULL) {
                // Get messages from user session
                $queuedFlashMessagesFromSession = $this->getFlashMessagesFromSession();
                $queuedFlashMessages = array_merge($queuedFlashMessagesFromSession, $this->toArray());
+               if ($severity !== NULL) {
+                       $filteredFlashMessages = array();
+                       foreach ($queuedFlashMessages as $message) {
+                               if ($message->getSeverity() === $severity) {
+                                       $filteredFlashMessages[] = $message;
+                               }
+                       }
+                       return $filteredFlashMessages;
+               }
+
                return $queuedFlashMessages;
        }
 
@@ -102,14 +113,15 @@ class FlashMessageQueue extends \SplQueue {
         * After fetching the messages the internal queue and the message queue in the session
         * will be emptied.
         *
+        * @param int $severity Optional severity, must be one of \TYPO3\CMS\Core\Messaging\AbstractMessage constants
         * @return FlashMessage[]
         */
-       public function getAllMessagesAndFlush() {
-               $queuedFlashMessages = $this->getAllMessages();
+       public function getAllMessagesAndFlush($severity = NULL) {
+               $queuedFlashMessages = $this->getAllMessages($severity);
                // Reset messages in user session
-               $this->removeAllFlashMessagesFromSession();
+               $this->removeAllFlashMessagesFromSession($severity);
                // Reset internal messages
-               $this->clear();
+               $this->clear($severity);
                return $queuedFlashMessages;
        }
 
@@ -119,17 +131,28 @@ class FlashMessageQueue extends \SplQueue {
         * @param FlashMessage[] $flashMessages
         * @return void
         */
-       protected function storeFlashMessagesInSession(array $flashMessages) {
+       protected function storeFlashMessagesInSession(array $flashMessages = NULL) {
                $this->getUserByContext()->setAndSaveSessionData($this->identifier, $flashMessages);
        }
 
        /**
         * Removes all flash messages from the session
         *
+        * @param int $severity Optional severity, must be one of \TYPO3\CMS\Core\Messaging\AbstractMessage constants
         * @return void
         */
-       protected function removeAllFlashMessagesFromSession() {
-               $this->getUserByContext()->setAndSaveSessionData($this->identifier, NULL);
+       protected function removeAllFlashMessagesFromSession($severity = NULL) {
+               if ($severity === NULL) {
+                       $this->storeFlashMessagesInSession(NULL);
+               } else {
+                       $messages = $this->getFlashMessagesFromSession();
+                       foreach ($messages as $index => $message) {
+                               if ($message->getSeverity() === $severity) {
+                                       unset($messages[$index]);
+                               }
+                       }
+                       $this->storeFlashMessagesInSession($messages);
+               }
        }
 
        /**
@@ -193,12 +216,29 @@ class FlashMessageQueue extends \SplQueue {
        /**
         * Removes all items from the queue
         *
+        * @param int $severity Optional severity, must be one of \TYPO3\CMS\Core\Messaging\AbstractMessage constants
         * @return void
         */
-       public function clear() {
+       public function clear($severity = NULL) {
                $this->rewind();
-               while (!$this->isEmpty()) {
-                       parent::dequeue();
+               if ($severity === NULL) {
+                       while (!$this->isEmpty()) {
+                               parent::dequeue();
+                       }
+               } else {
+                       $keysToRemove = array();
+                       while ($cur = $this->current()) {
+                               if ($cur->getSeverity() === $severity) {
+                                       $keysToRemove[] = $this->key();
+                               }
+                               $this->next();
+                       }
+                       // keys are renumbered when unsetting elements
+                       // so unset them from last to first
+                       $keysToRemove = array_reverse($keysToRemove);
+                       foreach ($keysToRemove as $key) {
+                               $this->offsetUnset($key);
+                       }
                }
        }
 
diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-7098-SeverityFilteringForFlashMessageQueue.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-7098-SeverityFilteringForFlashMessageQueue.rst
new file mode 100644 (file)
index 0000000..2d37512
--- /dev/null
@@ -0,0 +1,19 @@
+=========================================================
+Feature: #7098 - Severity-filtering for FlashMessageQueue
+=========================================================
+
+Description
+===========
+
+So far only all messages of the FlashMessageQueue could be fetched and/or
+removed. With this addition it's possible to do that for a given
+severity only. The existing functions get an optional $severity-parameter.
+
+Usage:
+
+.. code-block:: php
+
+       FlashMessageQueue::getAllMessages($severity);
+       FlashMessageQueue::getAllMessagesAndFlush($severity);
+       FlashMessageQueue::removeAllFlashMessagesFromSession($severity);
+       FlashMessageQueue::clear($severity);
index ed3ce5f..c1eff03 100644 (file)
@@ -124,4 +124,47 @@ class FlashMessageQueueTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                $this->assertNull($frontendUser->getSessionData('core.template.flashMessages'));
        }
 
+
+       /**
+        * @test
+        */
+       public function messagesCanBeFilteredBySeverity() {
+               $messages = array (
+                       0 => new \TYPO3\CMS\Core\Messaging\FlashMessage('This is a test message', 1, \TYPO3\CMS\Core\Messaging\FlashMessage::NOTICE),
+                       1 => new \TYPO3\CMS\Core\Messaging\FlashMessage('This is another test message', 2, \TYPO3\CMS\Core\Messaging\FlashMessage::WARNING)
+               );
+               $this->flashMessageQueue->enqueue($messages[0]);
+               $this->flashMessageQueue->enqueue($messages[1]);
+
+               $filteredFlashMessages = $this->flashMessageQueue->getAllMessages(\TYPO3\CMS\Core\Messaging\FlashMessage::NOTICE);
+
+               $this->assertEquals(count($filteredFlashMessages), 1);
+
+               reset($filteredFlashMessages);
+               $flashMessage = current($filteredFlashMessages);
+               $this->assertEquals($messages[0], $flashMessage);
+       }
+
+       /**
+        * @test
+        */
+       public function getMessagesAndFlushCanAlsoFilterBySeverity() {
+               $messages = array (
+                       0 => new \TYPO3\CMS\Core\Messaging\FlashMessage('This is a test message', 1, \TYPO3\CMS\Core\Messaging\FlashMessage::NOTICE),
+                       1 => new \TYPO3\CMS\Core\Messaging\FlashMessage('This is another test message', 2, \TYPO3\CMS\Core\Messaging\FlashMessage::WARNING)
+               );
+               $this->flashMessageQueue->enqueue($messages[0]);
+               $this->flashMessageQueue->enqueue($messages[1]);
+
+               $filteredFlashMessages = $this->flashMessageQueue->getAllMessagesAndFlush(\TYPO3\CMS\Core\Messaging\FlashMessage::NOTICE);
+
+               $this->assertEquals(count($filteredFlashMessages), 1);
+
+               reset($filteredFlashMessages);
+               $flashMessage = current($filteredFlashMessages);
+               $this->assertEquals($messages[0], $flashMessage);
+
+               $this->assertEquals(array(), $this->flashMessageQueue->getAllMessages(\TYPO3\CMS\Core\Messaging\FlashMessage::NOTICE));
+               $this->assertEquals(array($messages[1]), array_values($this->flashMessageQueue->getAllMessages()));
+       }
 }