Fixed bug #12154: Improve API of flashmessages
authorRupert Germann <rupi@gmx.li>
Fri, 9 Oct 2009 08:21:59 +0000 (08:21 +0000)
committerRupert Germann <rupi@gmx.li>
Fri, 9 Oct 2009 08:21:59 +0000 (08:21 +0000)
git-svn-id: https://svn.typo3.org/TYPO3v4/Core/trunk@6125 709f56b5-9817-0410-a4d7-c38de5d9e867

ChangeLog
t3lib/class.t3lib_flashmessage.php
t3lib/class.t3lib_messagequeue.php [new file with mode: 0644]
t3lib/core_autoload.php
typo3/alt_doc.php
typo3/mod/web/perm/index.php
typo3/sysext/cms/layout/db_layout.php
typo3/sysext/scheduler/mod1/index.php
typo3/sysext/tstemplate_objbrowser/class.tx_tstemplateobjbrowser.php
typo3/template.php

index d3823f7..13c85cc 100755 (executable)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,16 +1,20 @@
+2009-10-09  Rupert Germann  <rupi@gmx.li>
+
+       * Fixed bug #12154: Improve API of flashmessages
+
 2009-10-09  Francois Suter  <francois@typo3.org>
 
        * Fixed bug #12078: Scheduler: Check if additional fields indeed provide fields
        * Fixed bug #12117: Added use of flash messages to the Extension Manager (thanks to Steffen Gebert)
 
-2009-10-09  Kasper Sk\8crh¿j <kasper@typo3.org>
+2009-10-09  Kasper Skaarhoj  <kasper@typo3.org>
        * Tiny bug with date function in t3lib_befunc. Casted timestamp to integer.
        * Added User TSconfig option "options.checkPageLanguageOverlay" so flexforms only show language fields for languages available on the page.
        * Fixed problem with notification emails to users when a workspace review rejection happens. Now it works
        * Added possibility to reset the diff-information for localizations so translators or admins can click a button that will mark a record so it appears to be in need of translation again.
        * Fixed problem with "Save and view" for new pages: Simply removed the button for new pages because it didn't choose the right PID anyway.
-       * Implemented an override of allowed language per workspace. This is an expert feature available as a USER TSconfig option "options.workspaces.allowed_languages" 
-       * Expanded the feature that disables/enables login in a branch so now you can allow a login but force the usergroups to be "0,-2" (basically, that no usergroups are set for the user). This can help to reduce multiple caching of similar pages in case of many usergroup combinations in a system. 
+       * Implemented an override of allowed language per workspace. This is an expert feature available as a USER TSconfig option "options.workspaces.allowed_languages"
+       * Expanded the feature that disables/enables login in a branch so now you can allow a login but force the usergroups to be "0,-2" (basically, that no usergroups are set for the user). This can help to reduce multiple caching of similar pages in case of many usergroup combinations in a system.
        * Implemented a simple hook after retrieval of a page from cache.
        * Fixed bug in Addwizard: It didn't work properly with wizard for other pages. Took the wrong table name. Thanks to Hassan AIT ABDELLAH, Dassault Systemes, for the fix.
 
index 562d3df..e247ea6 100644 (file)
@@ -41,22 +41,49 @@ class t3lib_FlashMessage {
        const WARNING = 1;
        const ERROR   = 2;
 
-       protected $title    = '';
-       protected $message  = '';
+       /**
+        * The message's title
+        *
+        * @var string
+        */
+       protected $title = '';
+
+       /**
+        * The message
+        *
+        * @var string
+        */
+       protected $message = '';
+
+       /**
+        * The message's severity
+        *
+        * @var integer
+        */
        protected $severity = self::OK;
 
        /**
+        * defines whether the message should be stored in the session (to survive redirects) or only for one request (default)
+        *
+        * @var bool
+        */
+       protected $storeInSession = FALSE;
+
+       /**
         * Constructor for a flash message
         *
         * @param       string  The message.
         * @param       string  Optional message title.
-        * @param       integer Optional severity, must be either of t3lib_FlashMessage::INFO, t3lib_FlashMessage::OK, t3lib_FlashMessage::WARNING or t3lib_FlashMessage::ERROR. Default is t3lib_FlashMessage::OK.
+        * @param       integer Optional severity, must be either of t3lib_FlashMessage::INFO, t3lib_FlashMessage::OK,
+        *                  t3lib_FlashMessage::WARNING or t3lib_FlashMessage::ERROR. Default is t3lib_FlashMessage::OK.
+        * @param       bool    Optional, defines whether the message should be stored in the session or only for one request (default)
         * @return      void
         */
-       public function __construct($message, $title = '', $severity = self::OK) {
+       public function __construct($message, $title = '', $severity = self::OK, $storeInSession = FALSE) {
                $this->setMessage($message);
                $this->setTitle($title);
                $this->setSeverity($severity);
+               $this->setStoreInSession($storeInSession);
        }
 
        /**
@@ -78,6 +105,27 @@ class t3lib_FlashMessage {
                $this->title = (string) $title;
        }
 
+
+       /**
+        * Gets the message's storeInSession flag.
+        *
+        * @return      bool    The persistence flag.
+        */
+       public function getStoreInSession() {
+               return $this->storeInSession;
+       }
+
+       /**
+        * Sets the message's storeInSession flag
+        *
+        * @param       bool    The persistence flag
+        * @return      void
+        */
+       public function setStoreInSession($storeInSession) {
+               $this->storeInSession = (bool) $storeInSession;
+       }
+
+
        /**
         * Gets the message.
         *
diff --git a/t3lib/class.t3lib_messagequeue.php b/t3lib/class.t3lib_messagequeue.php
new file mode 100644 (file)
index 0000000..3555f50
--- /dev/null
@@ -0,0 +1,128 @@
+<?php
+/***************************************************************
+*  Copyright notice
+*
+*  (c) 2009 Rupert Germann <rupi@gmx.li>
+*  All rights reserved
+*
+*  This script is part of the TYPO3 project. The TYPO3 project is
+*  free software; you can redistribute it and/or modify
+*  it under the terms of the GNU General Public License as published by
+*  the Free Software Foundation; either version 2 of the License, or
+*  (at your option) any later version.
+*
+*  The GNU General Public License can be found at
+*  http://www.gnu.org/copyleft/gpl.html.
+*  A copy is found in the textfile GPL.txt and important notices to the license
+*  from the author is found in LICENSE.txt distributed with these scripts.
+*
+*
+*  This script is distributed in the hope that it will be useful,
+*  but WITHOUT ANY WARRANTY; without even the implied warranty of
+*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+*  GNU General Public License for more details.
+*
+*  This copyright notice MUST APPEAR in all copies of the script!
+***************************************************************/
+
+
+/**
+ *  A class which collects and renders flash messages.
+ *
+ * @author     Rupert Germann <rupi@gmx.li>
+ * @package TYPO3
+ * @subpackage t3lib
+ */
+class t3lib_MessageQueue {
+
+       static $messages = array();
+
+       /**
+        * Static class, no instances allowed.
+        */
+       protected function __construct() {}
+
+
+       /**
+        * Adds a message either to the BE_USER session (if the $message has the storeInSession flag set)
+        * or it adds the message to self::$messages.
+        *
+        * @param       object  instance of t3lib_FlashMessage, representing a message
+        * @return      void
+        */
+       public static function addMessage(t3lib_FlashMessage $message) {
+               if ($message->getStoreInSession() === TRUE) {
+                       $queuedFlashMessages = self::getFlashMessagesFromSession();
+                       $queuedFlashMessages[] = $message;
+
+                       $GLOBALS['BE_USER']->setAndSaveSessionData(
+                               'core.template.flashMessages',
+                               $queuedFlashMessages
+                       );
+               } else {
+                       self::$messages[] = $message;
+               }
+       }
+
+       /**
+        * Returns all messages from the current PHP session and from the current request.
+        * After fetching the messages the internal queue and the message queue in the session
+        * will be emptied.
+        *
+        * @return      array   array of t3lib_FlashMessage objects
+        */
+       public static function getMessages() {
+                       // get messages from user session
+               $queuedFlashMessagesFromSession = self::getFlashMessagesFromSession();
+               if (!empty($queuedFlashMessagesFromSession)) {
+                               // reset messages in user session
+                       $GLOBALS['BE_USER']->setAndSaveSessionData(
+                               'core.template.flashMessages',
+                               null
+                       );
+               }
+
+               $queuedFlashMessages = array_merge($queuedFlashMessagesFromSession, self::$messages);
+
+                       // reset internal messages
+               self::$messages = array();
+
+               return $queuedFlashMessages;
+       }
+
+       /**
+        * Returns current flash messages from the session, making sure to always
+        * return an array.
+        *
+        * @return      array   An array of t3lib_FlashMessage flash messages.
+        */
+       protected static function getFlashMessagesFromSession() {
+               $flashMessages = $GLOBALS['BE_USER']->getSessionData('core.template.flashMessages');
+               return is_array($flashMessages) ? $flashMessages : array();
+       }
+
+       /**
+        * Fetches and renders all available flash messages from the queue.
+        *
+        * @return      string  All flash messages in the queue rendered as HTML.
+        */
+       public static function renderFlashMessages() {
+               $content = '';
+               $flashMessages = self::getMessages();
+
+               if (count($flashMessages)) {
+                       foreach ($flashMessages as $flashMessage) {
+                               $content .= $flashMessage->render();
+                       }
+               }
+               return $content;
+       }
+
+
+}
+
+
+if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_messagequeue.php'])     {
+       include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_messagequeue.php']);
+}
+?>
\ No newline at end of file
index 432dfc1..ace0ef0 100644 (file)
@@ -27,6 +27,7 @@ return array(
        't3lib_extmgm' => PATH_t3lib . 'class.t3lib_extmgm.php',
        't3lib_extobjbase' => PATH_t3lib . 'class.t3lib_extobjbase.php',
        't3lib_flashmessage' => PATH_t3lib . 'class.t3lib_flashmessage.php',
+       't3lib_messagequeue' => PATH_t3lib . 'class.t3lib_messagequeue.php',
        't3lib_flexformtools' => PATH_t3lib . 'class.t3lib_flexformtools.php',
        't3lib_foldertree' => PATH_t3lib . 'class.t3lib_foldertree.php',
        't3lib_formmail' => PATH_t3lib . 'class.t3lib_formmail.php',
index 5fa036d..55b5cff 100644 (file)
@@ -777,7 +777,7 @@ class SC_alt_doc {
                                                                                        '',
                                                                                        t3lib_FlashMessage::WARNING
                                                                                );
-                                                                               $this->doc->pushFlashMessage($lockedMessage);
+                                                                               t3lib_messageQueue::addMessage($lockedMessage);
                                                                        }
 
                                                                                // Combine it all:
index e60a559..f0587a6 100755 (executable)
@@ -397,7 +397,7 @@ class SC_mod_web_perm_index {
                                $LANG->getLL('WorkspaceWarning'),
                                t3lib_FlashMessage::WARNING
                        );
-                       $this->doc->pushFlashMessage($lockedMessage);
+                       t3lib_messageQueue::addMessage($lockedMessage);
                }
 
                        // Get usernames and groupnames
index 1106f11..8374365 100755 (executable)
@@ -832,7 +832,7 @@ class SC_db_layout {
                                                '',
                                                t3lib_FlashMessage::WARNING
                                        );
-                                       $this->doc->pushFlashMessage($lockedMessage);
+                                       t3lib_messageQueue::addMessage($lockedMessage);
                                }
 
                                        // Add whole form as a document section:
index 5715a21..19501c0 100755 (executable)
@@ -1280,7 +1280,7 @@ class tx_scheduler_Module extends t3lib_SCbase {
                        $severity
                );
 
-               $this->doc->pushFlashMessage($message);
+               t3lib_messageQueue::addMessage($message);
        }
 
        /**
index 896197d..d1ddd7e 100644 (file)
@@ -249,7 +249,7 @@ class tx_tstemplateobjbrowser extends t3lib_extobjbase {
                                                                $GLOBALS['LANG']->getLL('badProperty'),
                                                                t3lib_FlashMessage::ERROR
                                                        );
-                                                       $this->pObj->doc->pushFlashMessage($badPropertyMessage);
+                                                       t3lib_messageQueue::addMessage($badPropertyMessage);
                                                } else {
                                                        $pline = $name . '.' . $property . ' = '
                                                                . trim($POST['data'][$name]['propertyValue']);
@@ -259,7 +259,7 @@ class tx_tstemplateobjbrowser extends t3lib_extobjbase {
                                                                htmlspecialchars($pline),
                                                                $GLOBALS['LANG']->getLL('propertyAdded')
                                                        );
-                                                       $this->pObj->doc->pushFlashMessage($propertyAddedMessage);
+                                                       t3lib_messageQueue::addMessage($propertyAddedMessage);
 
                                                        $line.=chr(10).$pline;
                                                }
@@ -271,7 +271,7 @@ class tx_tstemplateobjbrowser extends t3lib_extobjbase {
                                                        htmlspecialchars($pline),
                                                        $GLOBALS['LANG']->getLL('valueUpdated')
                                                );
-                                               $this->pObj->doc->pushFlashMessage($updatedMessage);
+                                               t3lib_messageQueue::addMessage($updatedMessage);
 
                                                $line .= chr(10) . $pline;
                                        } elseif ($POST['clear_object']) {
@@ -283,7 +283,7 @@ class tx_tstemplateobjbrowser extends t3lib_extobjbase {
                                                                htmlspecialchars($pline),
                                                                $GLOBALS['LANG']->getLL('objectCleared')
                                                        );
-                                                       $this->pObj->doc->pushFlashMessage($objectClearedMessage);
+                                                       t3lib_messageQueue::addMessage($objectClearedMessage);
 
                                                        $line .= chr(10) . $pline;
                                                }
@@ -405,7 +405,7 @@ class tx_tstemplateobjbrowser extends t3lib_extobjbase {
                                        t3lib_FlashMessage::ERROR
                                );
 
-                               $this->pObj->doc->pushFlashMessage($noTemplateMessage);
+                               t3lib_messageQueue::addMessage($noTemplateMessage);
                        }
                                // Links:
                        $out = '';
index de92be6..547b776 100644 (file)
@@ -1821,77 +1821,6 @@ $str.=$this->docBodyTagBegin().
        }
 
        /**
-        * Adds a flash message to the queue. It will live until the next call to
-        * popFlashMessages() in the current session.
-        *
-        * @param       t3lib_FlashMessage      A flash message object.
-        * @return      void
-        * @author      Karsten Dambekalns <karsten@typo3.org>
-        * @author      Ingo Renner <ingo@typo3.org>
-        */
-       public function pushFlashMessage(t3lib_FlashMessage $message) {
-               $queuedFlashMessages = $this->getFlashMessagesFromSession();
-               $queuedFlashMessages[] = $message;
-
-               $GLOBALS['BE_USER']->setAndSaveSessionData(
-                       'core.template.flashMessages',
-                       $queuedFlashMessages
-               );
-       }
-
-       /**
-        * Returns queued flash messages and clears the queue.
-        *
-        * @return      array   An array with t3lib_FlashMessage flash messages.
-        * @author      Karsten Dambekalns <karsten@typo3.org>
-        * @author      Ingo Renner <ingo@typo3.org>
-        */
-       public function popFlashMessages() {
-               $queuedFlashMessages = $this->getFlashMessagesFromSession();
-
-               if (!empty($queuedFlashMessages)) {
-                       $GLOBALS['BE_USER']->setAndSaveSessionData(
-                               'core.template.flashMessages',
-                               null
-                       );
-               }
-
-               return $queuedFlashMessages;
-       }
-
-       /**
-        * Returns current flash messages from the session, making sure to always
-        * return an array.
-        *
-        * @return      array   An array of t3lib_FlashMessage flash messages.
-        * @author      Karsten Dambekalns <karsten@typo3.org>
-        * @author      Ingo Renner <ingo@typo3.org>
-        */
-       protected function getFlashMessagesFromSession() {
-               $flashMessages = $GLOBALS['BE_USER']->getSessionData('core.template.flashMessages');
-
-               return is_array($flashMessages) ? $flashMessages : array();
-       }
-
-       /**
-        * Renders all available flash messages in the queue.
-        *
-        * @return      string  All flash messages in the queue rendered as HTML.
-        * @author      Ingo Renner <ingo@typo3.org>
-        */
-       public function renderFlashMessages() {
-               $content       = '';
-               $flashMessages = $this->popFlashMessages();
-
-               foreach ($flashMessages as $flashMessage) {
-                       $content .= $flashMessage->render();
-               }
-
-               return $content;
-       }
-
-
-       /**
         * Function to load a HTML template file with markers.
         * When calling from own extension, use  syntax getHtmlTemplate('EXT:extkey/template.html')
         *
@@ -1964,7 +1893,7 @@ $str.=$this->docBodyTagBegin().
                }
 
                        // adding flash messages
-               $flashMessages = $this->renderFlashMessages();
+               $flashMessages = t3lib_messageQueue::renderFlashMessages();
                if (!empty($flashMessages)) {
                        $flashMessages = '<div id="typo3-messages">' . $flashMessages . '</div>';
                }