[+FEATURE] Extbase (MVC): Implemented flash messages.
authorJochen Rau <j.rau@web.de>
Mon, 14 Sep 2009 09:51:58 +0000 (09:51 +0000)
committerJochen Rau <j.rau@web.de>
Mon, 14 Sep 2009 09:51:58 +0000 (09:51 +0000)
typo3/sysext/extbase/Classes/MVC/Controller/AbstractController.php
typo3/sysext/extbase/Classes/MVC/Controller/ActionController.php

index 9295dcb..5645f0d 100755 (executable)
@@ -343,5 +343,62 @@ abstract class Tx_Extbase_MVC_Controller_AbstractController implements Tx_Extbas
                $this->propertyMapper->mapAndValidate($allPropertyNames, $this->request->getArguments(), $this->arguments, $optionalPropertyNames, $validator);
                $this->argumentsMappingResults = $this->propertyMapper->getMappingResults();
        }
+       
+       /**
+        * Add a flash message to the queue. It will live until the next call to
+        * popFlashMessages() in the current session.
+        *
+        * @param mixed $message anything serializable, should be "stringy"
+        * @return void
+        * @api
+        */
+       protected function pushFlashMessage($message) {
+               if (!is_string($message)) throw new InvalidArgumentException('The flash message must be string, ' . gettype($message) . ' given.', 1243258395);
+
+               $queuedFlashMessages = $this->getFlashMessagesFromSession();
+               $queuedFlashMessages[] = $message;
+               if (isset($GLOBALS['TSFE'])) {
+                       $GLOBALS['TSFE']->fe_user->setKey(
+                               'ses',
+                               'Extbase_AbstractController_flashMessages',
+                               $queuedFlashMessages
+                       );
+                       $GLOBALS['TSFE']->fe_user->storeSessionData();
+               }
+               // TODO Support BE
+       }
+
+       /**
+        * Returns queued flash messages and clear queue.
+        *
+        * @return array an array with flash messages or NULL if none available
+        * @api
+        */
+       protected function popFlashMessages() {
+               $queuedFlashMessages = $this->getFlashMessagesFromSession();
+               if (isset($GLOBALS['TSFE'])) {
+                       $GLOBALS['TSFE']->fe_user->setKey(
+                               'ses',
+                               'Extbase_AbstractController_flashMessages',
+                               NULL
+                       );
+                       $GLOBALS['TSFE']->fe_user->storeSessionData();
+               }
+               return $queuedFlashMessages;
+       }
+
+       /**
+        * Returns current flash messages from the seesion, making sure to always
+        * return an array.
+        *
+        * @return array
+        */
+       protected function getFlashMessagesFromSession() {
+               if (is_object($GLOBALS['TSFE']->fe_user)) {
+                       $flashMessages = $GLOBALS['TSFE']->fe_user->getKey('ses', 'Extbase_AbstractController_flashMessages');
+               }
+               return is_array($flashMessages) ? $flashMessages : array();
+       }
+       
 }
-?>
\ No newline at end of file
+?>
index f70e228..21ad9c5 100644 (file)
@@ -272,7 +272,7 @@ class Tx_Extbase_MVC_Controller_ActionController extends Tx_Extbase_MVC_Controll
                        $view->injectSettings($this->settings);
                }
                $view->initializeView(); // In FLOW3, solved through Object Lifecycle methods, we need to call it explicitely.
-               // $view->assign('flashMessages', $this->popFlashMessages());
+               $view->assign('flashMessages', $this->popFlashMessages());
                return $view;
        }
 
@@ -355,6 +355,18 @@ class Tx_Extbase_MVC_Controller_ActionController extends Tx_Extbase_MVC_Controll
                }
                return $message;
        }
+       
+       /**
+        * A template method for displaying custom error flash messages, or to
+        * display no flash message at all on errors. Override this to customize
+        * the flash message in your action controller.
+        *
+        * @return string|boolean The flash message or FALSE if no flash message should be set
+        * @api
+        */
+       protected function getErrorFlashMessage() {
+               return 'An error occurred while trying to call ' . get_class($this) . '->' . $this->actionMethodName . '()';
+       }
 
        /**
         * Clear cache of current page on error. Needed because we want a re-evaluation of the data.
@@ -373,4 +385,4 @@ class Tx_Extbase_MVC_Controller_ActionController extends Tx_Extbase_MVC_Controll
        }
 
 }
-?>
\ No newline at end of file
+?>