class.t3lib_flashmessage.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 2009-2010 Ingo Renner <ingo@typo3.org>
00006 *  All rights reserved
00007 *
00008 *  This script is part of the TYPO3 project. The TYPO3 project is
00009 *  free software; you can redistribute it and/or modify
00010 *  it under the terms of the GNU General Public License as published by
00011 *  the Free Software Foundation; either version 2 of the License, or
00012 *  (at your option) any later version.
00013 *
00014 *  The GNU General Public License can be found at
00015 *  http://www.gnu.org/copyleft/gpl.html.
00016 *  A copy is found in the textfile GPL.txt and important notices to the license
00017 *  from the author is found in LICENSE.txt distributed with these scripts.
00018 *
00019 *
00020 *  This script is distributed in the hope that it will be useful,
00021 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 *  GNU General Public License for more details.
00024 *
00025 *  This copyright notice MUST APPEAR in all copies of the script!
00026 ***************************************************************/
00027 
00028 
00029 /**
00030  * A class representing flash messages.
00031  *
00032  * @author  Ingo Renner <ingo@typo3.org>
00033  * @package TYPO3
00034  * @subpackage t3lib
00035  */
00036 class t3lib_FlashMessage {
00037 
00038     const NOTICE  = -2;
00039     const INFO    = -1;
00040     const OK      = 0;
00041     const WARNING = 1;
00042     const ERROR   = 2;
00043 
00044     /**
00045      * The message's title
00046      *
00047      * @var string
00048      */
00049     protected $title = '';
00050 
00051     /**
00052      * The message
00053      *
00054      * @var string
00055      */
00056     protected $message = '';
00057 
00058     /**
00059      * The message's severity
00060      *
00061      * @var integer
00062      */
00063     protected $severity = self::OK;
00064 
00065     /**
00066      * defines whether the message should be stored in the session (to survive redirects) or only for one request (default)
00067      *
00068      * @var bool
00069      */
00070     protected $storeInSession = FALSE;
00071 
00072     /**
00073      * Constructor for a flash message
00074      *
00075      * @param   string  The message.
00076      * @param   string  Optional message title.
00077      * @param   integer Optional severity, must be either of t3lib_FlashMessage::INFO, t3lib_FlashMessage::OK,
00078      *                  t3lib_FlashMessage::WARNING or t3lib_FlashMessage::ERROR. Default is t3lib_FlashMessage::OK.
00079      * @param   bool    Optional, defines whether the message should be stored in the session or only for one request (default)
00080      * @return  void
00081      */
00082     public function __construct($message, $title = '', $severity = self::OK, $storeInSession = FALSE) {
00083         $this->setMessage($message);
00084         $this->setTitle($title);
00085         $this->setSeverity($severity);
00086         $this->setStoreInSession($storeInSession);
00087     }
00088 
00089     /**
00090      * Gets the message's title.
00091      *
00092      * @return  string  The message's title.
00093      */
00094     public function getTitle() {
00095         return $this->title;
00096     }
00097 
00098     /**
00099      * Sets the message's title
00100      *
00101      * @param   string  The message's title
00102      * @return  void
00103      */
00104     public function setTitle($title) {
00105         $this->title = (string) $title;
00106     }
00107 
00108 
00109     /**
00110      * Gets the message's storeInSession flag.
00111      *
00112      * @return  bool    true if message should be stored in the session, otherwise false.
00113      */
00114     public function isSessionMessage() {
00115         return $this->storeInSession;
00116     }
00117 
00118     /**
00119      * Sets the message's storeInSession flag
00120      *
00121      * @param   bool    The persistence flag
00122      * @return  void
00123      */
00124     public function setStoreInSession($storeInSession) {
00125         $this->storeInSession = (bool) $storeInSession;
00126     }
00127 
00128 
00129     /**
00130      * Gets the message.
00131      *
00132      * @return  string  The message.
00133      */
00134     public function getMessage() {
00135         return $this->message;
00136     }
00137 
00138     /**
00139      * Sets the message
00140      *
00141      * @param   string  The message
00142      * @return  void
00143      */
00144     public function setMessage($message) {
00145         $this->message = (string) $message;
00146     }
00147 
00148     /**
00149      * Gets the message' severity.
00150      *
00151      * @return  integer The message' severity, either of t3lib_FlashMessage::INFO, t3lib_FlashMessage::OK, t3lib_FlashMessage::WARNING or t3lib_FlashMessage::ERROR
00152      */
00153     public function getSeverity() {
00154         return $this->severity;
00155     }
00156 
00157     /**
00158      * Sets the message' severity
00159      *
00160      * @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.
00161      * @return  void
00162      */
00163     public function setSeverity($severity = self::OK) {
00164         $this->severity = t3lib_div::intInRange(
00165             $severity,
00166             self::NOTICE, // minimum
00167             self::ERROR, // maximum
00168             self::OK // default if out of bounds
00169         );
00170     }
00171 
00172     /**
00173      * Renders the flash message.
00174      *
00175      * @return  string  The flash message as HTML.
00176      */
00177     public function render() {
00178         $classes = array(
00179             t3lib_FlashMessage::NOTICE  => 'notice',
00180             t3lib_FlashMessage::INFO    => 'information',
00181             t3lib_FlashMessage::OK      => 'ok',
00182             t3lib_FlashMessage::WARNING => 'warning',
00183             t3lib_FlashMessage::ERROR   => 'error',
00184         );
00185 
00186         $title = '';
00187         if (!empty($this->title)) {
00188             $title = '<div class="message-header">' . $this->title . '</div>';
00189         }
00190 
00191         $message = '<div class="typo3-message message-' . $classes[$this->severity] . '">'
00192             . $title
00193             . '<div class="message-body">' . $this->message . '</div>'
00194             . '</div>';
00195 
00196         return $message;
00197     }
00198 
00199 
00200     /**
00201      * Creates a string representation of the flash message. Useful for command
00202      * line use.
00203      *
00204      * @return  string  A string representation of the flash message.
00205      */
00206     public function __toString() {
00207         $severities = array(
00208             t3lib_FlashMessage::INFO    => 'INFO',
00209             t3lib_FlashMessage::OK      => 'OK',
00210             t3lib_FlashMessage::WARNING => 'WARNING',
00211             t3lib_FlashMessage::ERROR   => 'ERROR',
00212         );
00213 
00214         $title = '';
00215         if (!empty($this->title)) {
00216             $title = ' - ' . $this->title;
00217         }
00218 
00219         return $severities[$this->severity] . $title . ': ' . $this->message;
00220     }
00221 
00222 }
00223 
00224 
00225 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_flashmessage.php'])  {
00226     include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_flashmessage.php']);
00227 }
00228 
00229 ?>

Generated on Sat Jul 24 04:17:17 2010 for TYPO3 API by  doxygen 1.4.7