Project

General

Profile

Feature #7098 » flashmessages_v3.patch

Axel Böswetter, 2010-08-20 13:24

View differences:

extbase/Classes/MVC/Controller/FlashMessage.php (revision 0)
<?php
/***************************************************************
* Copyright notice
*
* (c) 2010
* All rights reserved
*
* This class is a backport of the corresponding class of FLOW3.
* All credits go to the v5 team.
*
* 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.
*
* 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
*
* @version $Id$
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
* @api
*/
class Tx_Extbase_MVC_Controller_FlashMessage {
const NOTICE = -2;
const INFO = -1;
const OK = 0;
const WARNING = 1;
const ERROR = 2;
/**
* @var string The message
*/
protected $message;
/**
* @var string The message's severity
*/
protected $severity = self::OK;
/**
* @var string The message's severity class names
*/
protected $classes = array(
self::NOTICE => 'notice',
self::INFO => 'information',
self::OK => 'ok',
self::WARNING => 'warning',
self::ERROR => 'error',
);
/**
* @var DateTime The message's creation date
*/
protected $time;
/**
* Default constructor
*
* @param string $message The message
* @param integer $severity Optional severity, must be either of Tx_Extbase_MVC_Controller_FlashMessage::NOTICE, Tx_Extbase_MVC_Controller_FlashMessage::INFO, Tx_Extbase_MVC_Controller_FlashMessage::OK, Tx_Extbase_MVC_Controller_FlashMessage::WARNING or Tx_Extbase_MVC_Controller_FlashMessage::ERROR. Default is Tx_Extbase_MVC_Controller_FlashMessage::OK.
* @return void
*/
public function __construct($message, $severity = self::OK) {
$this->setMessage($message);
$this->setSeverity($severity);
$this->setTime(new DateTime());
}
/**
* Gets the message
*
* @return string The message
*/
public function getMessage() {
return $this->message;
}
/**
* Sets the message
*
* @param string $message The message
* @return void
*/
public function setMessage($message) {
$this->message = (string)$message;
}
/**
* Gets the message' severity
*
* @return integer The message' severity, either of Tx_Extbase_MVC_Controller_FlashMessage::NOTICE, Tx_Extbase_MVC_Controller_FlashMessage::INFO, Tx_Extbase_MVC_Controller_FlashMessage::OK, Tx_Extbase_MVC_Controller_FlashMessage::WARNING or Tx_Extbase_MVC_Controller_FlashMessage::ERROR
*/
public function getSeverity() {
return $this->severity;
}
/**
* Sets the message' severity
*
* @param integer $severity The severity, must be either of Tx_Extbase_MVC_Controller_FlashMessage::NOTICE, Tx_Extbase_MVC_Controller_FlashMessage::INFO, Tx_Extbase_MVC_Controller_FlashMessage::OK, Tx_Extbase_MVC_Controller_FlashMessage::WARNING or Tx_Extbase_MVC_Controller_FlashMessage::ERROR. Default is Tx_Extbase_MVC_Controller_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
);
}
/**
* Gets the message' severity class name
*
* @return string The message' severity class name
*/
public function getClass() {
return 'message-' . $this->classes[$this->severity];
}
/**
* Gets the message's creation date
*
* @return DateTime The DateTime object indicating when this message was created.
*/
public function getTime() {
return $this->time;
}
/**
* Sets the message's creation date
*
* @param DateTime The DateTime object indicating when this message was created.
* @return void
*/
public function setTime(DateTime $time) {
$this->time = $time;
}
public function __toString() {
$severities = array(
self::NOTICE => 'NOTICE',
self::INFO => 'INFO',
self::OK => 'OK',
self::WARNING => 'WARNING',
self::ERROR => 'ERROR',
);
return $severities[$this->severity] . ': ' . $this->message;
}
}
?>
extbase/Classes/MVC/Controller/FlashMessages.php (working copy)
/**
* The array of flash messages
* @var array<string>
* @var array<Tx_Extbase_MVC_Controller_FlashMessage>
*/
protected $flashMessages = array();
......
/**
* Add another flash message.
*
* @param string $message
* @param string $message The message string.
* @param string $severity Use constants definied in Tx_Extbase_MVC_Controller_FlashMessage to set the value. Default: Tx_Extbase_MVC_Controller_FlashMessage::OK
* @return void
* @api
*/
public function add($message) {
public function add($message, $severity = Tx_Extbase_MVC_Controller_FlashMessage::OK) {
if (!is_string($message)) throw new InvalidArgumentException('The flash message must be string, ' . gettype($message) . ' given.', 1243258395);
$this->initialize();
$this->flashMessages[] = $message;
$flashMessage = t3lib_div::makeInstance('Tx_Extbase_MVC_Controller_FlashMessage', $message, $severity);
$this->flashMessages[] = $flashMessage;
}
/**
extbase/ext_autoload.php (working copy)
'tx_extbase_mvc_controller_argumentsvalidator' => $extensionClassesPath . 'MVC/Controller/ArgumentsValidator.php',
'tx_extbase_mvc_controller_controllercontext' => $extensionClassesPath . 'MVC/Controller/ControllerContext.php',
'tx_extbase_mvc_controller_controllerinterface' => $extensionClassesPath . 'MVC/Controller/ControllerInterface.php',
'tx_extbase_mvc_controller_flashmessage' => $extensionClassesPath . 'MVC/Controller/FlashMessage.php',
'tx_extbase_mvc_controller_flashmessages' => $extensionClassesPath . 'MVC/Controller/FlashMessages.php',
'tx_extbase_mvc_exception_infiniteloop' => $extensionClassesPath . 'MVC/Exception/InfiniteLoop.php',
'tx_extbase_mvc_exception_invalidactionname' => $extensionClassesPath . 'MVC/Exception/InvalidActionName.php',
fluid/Classes/ViewHelpers/FlashMessagesViewHelper.php (working copy)
if (count($flashMessages) > 0) {
$tagContent = '';
foreach ($flashMessages as $singleFlashMessage) {
$tagContent .= '<li>' . htmlspecialchars($singleFlashMessage) . '</li>';
$tagContent .= '<li class="'. $singleFlashMessage->getClass() .'">' . $singleFlashMessage->getMessage() . '</li>';
}
$this->tag->setContent($tagContent);
return $this->tag->render();
(6-6/6)