Added feature #11684: Add support for flash messages in the backend
authorIngo Renner <ingo.renner@typo3.org>
Tue, 25 Aug 2009 08:26:34 +0000 (08:26 +0000)
committerIngo Renner <ingo.renner@typo3.org>
Tue, 25 Aug 2009 08:26:34 +0000 (08:26 +0000)
git-svn-id: https://svn.typo3.org/TYPO3v4/Core/trunk@5816 709f56b5-9817-0410-a4d7-c38de5d9e867

ChangeLog
t3lib/class.t3lib_flashmessage.php [new file with mode: 0644]
t3lib/core_autoload.php
typo3/gfx/error.png [new file with mode: 0644]
typo3/gfx/information.png [new file with mode: 0644]
typo3/gfx/ok.png [new file with mode: 0644]
typo3/gfx/warning.png [new file with mode: 0644]
typo3/stylesheet.css
typo3/template.php

index e7a0810..b153760 100755 (executable)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2009-08-14  Ingo Renner  <ingo@typo3.org>
+
+       * Added feature #11684: Add support for flash messages in the backend
+
 2009-08-24  Rupert Germann  <rupi@gmx.li>
 
        * Fixed bug #11764: Performance improvement: speed up list module by improving internal handling of localisations
diff --git a/t3lib/class.t3lib_flashmessage.php b/t3lib/class.t3lib_flashmessage.php
new file mode 100644 (file)
index 0000000..562d3df
--- /dev/null
@@ -0,0 +1,181 @@
+<?php
+/***************************************************************
+*  Copyright notice
+*
+*  (c) 2009 Ingo Renner <ingo@typo3.org>
+*  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 representing flash messages.
+ *
+ * @author     Ingo Renner <ingo@typo3.org>
+ * @package TYPO3
+ * @subpackage t3lib
+ */
+class t3lib_FlashMessage {
+
+       const NOTICE  = -2;
+       const INFO    = -1;
+       const OK      = 0;
+       const WARNING = 1;
+       const ERROR   = 2;
+
+       protected $title    = '';
+       protected $message  = '';
+       protected $severity = self::OK;
+
+       /**
+        * 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.
+        * @return      void
+        */
+       public function __construct($message, $title = '', $severity = self::OK) {
+               $this->setMessage($message);
+               $this->setTitle($title);
+               $this->setSeverity($severity);
+       }
+
+       /**
+        * Gets the message's title.
+        *
+        * @return      string  The message's title.
+        */
+       public function getTitle() {
+               return $this->title;
+       }
+
+       /**
+        * Sets the message's title
+        *
+        * @param       string  The message's title
+        * @return      void
+        */
+       public function setTitle($title) {
+               $this->title = (string) $title;
+       }
+
+       /**
+        * Gets the message.
+        *
+        * @return      string  The message.
+        */
+       public function getMessage() {
+               return $this->message;
+       }
+
+       /**
+        * Sets the message
+        *
+        * @param       string  The message
+        * @return      void
+        */
+       public function setMessage($message) {
+               $this->message = (string) $message;
+       }
+
+       /**
+        * Gets the message' severity.
+        *
+        * @return      integer The message' severity, either of t3lib_FlashMessage::INFO, t3lib_FlashMessage::OK, t3lib_FlashMessage::WARNING or t3lib_FlashMessage::ERROR
+        */
+       public function getSeverity() {
+               return $this->severity;
+       }
+
+       /**
+        * Sets the message' severity
+        *
+        * @param       string  The severity, must be either of t3lib_FlashMessage::INFO, t3lib_FlashMessage::OK, t3lib_FlashMessage::WARNING or t3lib_FlashMessage::ERROR. Default is t3lib_FlashMessage::OK.
+        * @return      void
+        */
+       public function setSeverity($severity = self::OK) {
+               $this->severity = t3lib_div::intInRange(
+                       $severity,
+                       self::NOTICE, // minimum
+                       self::ERROR, // maximum
+                       self::OK // default if out of bounds
+               );
+       }
+
+       /**
+        * Renders the flash message.
+        *
+        * @return      string  The flash message as HTML.
+        */
+       public function render() {
+               $classes = array(
+                       t3lib_FlashMessage::NOTICE  => 'notice',
+                       t3lib_FlashMessage::INFO    => 'information',
+                       t3lib_FlashMessage::OK      => 'ok',
+                       t3lib_FlashMessage::WARNING => 'warning',
+                       t3lib_FlashMessage::ERROR   => 'error',
+               );
+
+               $title = '';
+               if (!empty($this->title)) {
+                       $title = '<div class="message-header">' . $this->title . '</div>';
+               }
+
+               $message = '<div class="typo3-message message-' . $classes[$this->severity] . '">'
+                       . $title
+                       . '<div class="message-body">' . $this->message . '</div>'
+                       . '</div>';
+
+               return $message;
+       }
+
+
+       /**
+        * Creates a string representation of the flash message. Useful for command
+        * line use.
+        *
+        * @return      string  A string representation of the flash message.
+        */
+       public function __toString() {
+               $severities = array(
+                       t3lib_FlashMessage::INFO    => 'INFO',
+                       t3lib_FlashMessage::OK      => 'OK',
+                       t3lib_FlashMessage::WARNING => 'WARNING',
+                       t3lib_FlashMessage::ERROR   => 'ERROR',
+               );
+
+               $title = '';
+               if (!empty($this->title)) {
+                       $title = ' - ' . $this->title;
+               }
+
+               return $severities[$this->severity] . $title . ': ' . $this->message;
+       }
+
+}
+
+
+if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_flashmessage.php'])     {
+       include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_flashmessage.php']);
+}
+
+?>
\ No newline at end of file
index a6bcba2..80dde04 100644 (file)
@@ -25,6 +25,7 @@ return array(
        't3lib_extfilefunctions' => PATH_t3lib . 'class.t3lib_extfilefunc.php',
        '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_flexformtools' => PATH_t3lib . 'class.t3lib_flexformtools.php',
        't3lib_foldertree' => PATH_t3lib . 'class.t3lib_foldertree.php',
        't3lib_formmail' => PATH_t3lib . 'class.t3lib_formmail.php',
diff --git a/typo3/gfx/error.png b/typo3/gfx/error.png
new file mode 100644 (file)
index 0000000..c37bd06
Binary files /dev/null and b/typo3/gfx/error.png differ
diff --git a/typo3/gfx/information.png b/typo3/gfx/information.png
new file mode 100644 (file)
index 0000000..12cd1ae
Binary files /dev/null and b/typo3/gfx/information.png differ
diff --git a/typo3/gfx/ok.png b/typo3/gfx/ok.png
new file mode 100644 (file)
index 0000000..89c8129
Binary files /dev/null and b/typo3/gfx/ok.png differ
diff --git a/typo3/gfx/warning.png b/typo3/gfx/warning.png
new file mode 100644 (file)
index 0000000..628cf2d
Binary files /dev/null and b/typo3/gfx/warning.png differ
index eba3a37..0a24fa8 100644 (file)
@@ -597,6 +597,63 @@ div.typo3-dyntabmenu-divs-foldout div.tabact_over a {
 
 /* - - - - - - - - - - - - - - - - - - - - -
 
+Flash Messages (template.php)
+
+- - - - - - - - - - - - - - - - - - - - - */
+
+#typo3-messages {
+       margin-bottom: 10px;
+}
+
+.typo3-message {
+       padding: 6px;
+       padding-left: 26px;
+       margin-bottom: 4px;
+       background-repeat: no-repeat;
+       background-position: 4px 4px;
+       border: 1px solid;
+}
+
+.typo3-message .message-header {
+       display: block;
+       margin-bottom: 5px;
+       margin-top: -1px;
+       font-size: 11px;
+       font-weight: bold;
+}
+
+.message-notice {
+       background-color: #fff;
+       border-color: #dee0e1;
+}
+
+.message-information {
+       background-image: url(gfx/information.png);
+       background-color: #dfedf8;
+       border-color: #bccfe4;
+}
+
+.message-ok {
+       background-image: url(gfx/ok.png);
+       background-color: #dfd;
+       border-color: #beb;
+}
+
+.message-warning {
+       background-image: url(gfx/warning.png);
+       background-color: #ffd;
+       border-color: #f6ea68;
+}
+
+.message-error {
+       background-image: url(gfx/error.png);
+       background-color: #fcc;
+       border-color: #ebb;
+}
+
+
+/* - - - - - - - - - - - - - - - - - - - - -
+
 User settings
 
 - - - - - - - - - - - - - - - - - - - - - */
index 1e1997b..d5b8e0d 100644 (file)
@@ -1812,6 +1812,76 @@ $str.=$this->docBodyTagBegin().
                }
        }
 
+       /**
+        * Add 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>
+        */
+       protected function popFlashMessages() {
+               $queuedFlashMessages = $this->getFlashMessagesFromSession();
+
+               if (!empty($queuedFlashMessages)) {
+                       $GLOBALS['BE_USER']->setAndSaveSessionData(
+                               'core.template.flashMessages',
+                               null
+                       );
+               }
+
+               return $queuedFlashMessages;
+       }
+
+       /**
+        * Returns current flash messages from the seesion, 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.
@@ -1884,6 +1954,29 @@ $str.=$this->docBodyTagBegin().
                foreach ($subpartArray as $marker => $content) {
                        $moduleBody = t3lib_parsehtml::substituteSubpart($moduleBody, $marker, $content);
                }
+
+                       // adding flash messages
+               $flashMessages = $this->renderFlashMessages();
+               if (!empty($flashMessages)) {
+                       $flashMessages = '<div id="typo3-messages">' . $flashMessages . '</div>';
+               }
+
+               if (strstr($moduleBody, '###FLASHMESSAGES###')) {
+                               // either replace a dedicated marker for the messages if present
+                       $moduleBody = str_replace(
+                               '###FLASHMESSAGES###',
+                               $flashMessages,
+                               $moduleBody
+                       );
+               } else {
+                               // or force them to appear before the content
+                       $moduleBody = str_replace(
+                               '###CONTENT###',
+                               $flashMessages . '###CONTENT###',
+                               $moduleBody
+                       );
+               }
+
                        // replacing all markers with the finished markers and return the HTML content
                return t3lib_parsehtml::substituteMarkerArray($moduleBody, $markerArray, '###|###');