Fixed bug #12508: Log usage of XLLfile to deprecation log (thanks to Steffen Gebert)
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_flashmessage.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Ingo Renner <ingo@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28
29 /**
30 * A class representing flash messages.
31 *
32 * @author Ingo Renner <ingo@typo3.org>
33 * @package TYPO3
34 * @subpackage t3lib
35 */
36 class t3lib_FlashMessage {
37
38 const NOTICE = -2;
39 const INFO = -1;
40 const OK = 0;
41 const WARNING = 1;
42 const ERROR = 2;
43
44 /**
45 * The message's title
46 *
47 * @var string
48 */
49 protected $title = '';
50
51 /**
52 * The message
53 *
54 * @var string
55 */
56 protected $message = '';
57
58 /**
59 * The message's severity
60 *
61 * @var integer
62 */
63 protected $severity = self::OK;
64
65 /**
66 * defines whether the message should be stored in the session (to survive redirects) or only for one request (default)
67 *
68 * @var bool
69 */
70 protected $storeInSession = FALSE;
71
72 /**
73 * Constructor for a flash message
74 *
75 * @param string The message.
76 * @param string Optional message title.
77 * @param integer Optional severity, must be either of t3lib_FlashMessage::INFO, t3lib_FlashMessage::OK,
78 * t3lib_FlashMessage::WARNING or t3lib_FlashMessage::ERROR. Default is t3lib_FlashMessage::OK.
79 * @param bool Optional, defines whether the message should be stored in the session or only for one request (default)
80 * @return void
81 */
82 public function __construct($message, $title = '', $severity = self::OK, $storeInSession = FALSE) {
83 $this->setMessage($message);
84 $this->setTitle($title);
85 $this->setSeverity($severity);
86 $this->setStoreInSession($storeInSession);
87 }
88
89 /**
90 * Gets the message's title.
91 *
92 * @return string The message's title.
93 */
94 public function getTitle() {
95 return $this->title;
96 }
97
98 /**
99 * Sets the message's title
100 *
101 * @param string The message's title
102 * @return void
103 */
104 public function setTitle($title) {
105 $this->title = (string) $title;
106 }
107
108
109 /**
110 * Gets the message's storeInSession flag.
111 *
112 * @return bool true if message should be stored in the session, otherwise false.
113 */
114 public function isSessionMessage() {
115 return $this->storeInSession;
116 }
117
118 /**
119 * Sets the message's storeInSession flag
120 *
121 * @param bool The persistence flag
122 * @return void
123 */
124 public function setStoreInSession($storeInSession) {
125 $this->storeInSession = (bool) $storeInSession;
126 }
127
128
129 /**
130 * Gets the message.
131 *
132 * @return string The message.
133 */
134 public function getMessage() {
135 return $this->message;
136 }
137
138 /**
139 * Sets the message
140 *
141 * @param string The message
142 * @return void
143 */
144 public function setMessage($message) {
145 $this->message = (string) $message;
146 }
147
148 /**
149 * Gets the message' severity.
150 *
151 * @return integer The message' severity, either of t3lib_FlashMessage::INFO, t3lib_FlashMessage::OK, t3lib_FlashMessage::WARNING or t3lib_FlashMessage::ERROR
152 */
153 public function getSeverity() {
154 return $this->severity;
155 }
156
157 /**
158 * Sets the message' severity
159 *
160 * @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.
161 * @return void
162 */
163 public function setSeverity($severity = self::OK) {
164 $this->severity = t3lib_div::intInRange(
165 $severity,
166 self::NOTICE, // minimum
167 self::ERROR, // maximum
168 self::OK // default if out of bounds
169 );
170 }
171
172 /**
173 * Renders the flash message.
174 *
175 * @return string The flash message as HTML.
176 */
177 public function render() {
178 $classes = array(
179 t3lib_FlashMessage::NOTICE => 'notice',
180 t3lib_FlashMessage::INFO => 'information',
181 t3lib_FlashMessage::OK => 'ok',
182 t3lib_FlashMessage::WARNING => 'warning',
183 t3lib_FlashMessage::ERROR => 'error',
184 );
185
186 $title = '';
187 if (!empty($this->title)) {
188 $title = '<div class="message-header">' . $this->title . '</div>';
189 }
190
191 $message = '<div class="typo3-message message-' . $classes[$this->severity] . '">'
192 . $title
193 . '<div class="message-body">' . $this->message . '</div>'
194 . '</div>';
195
196 return $message;
197 }
198
199
200 /**
201 * Creates a string representation of the flash message. Useful for command
202 * line use.
203 *
204 * @return string A string representation of the flash message.
205 */
206 public function __toString() {
207 $severities = array(
208 t3lib_FlashMessage::INFO => 'INFO',
209 t3lib_FlashMessage::OK => 'OK',
210 t3lib_FlashMessage::WARNING => 'WARNING',
211 t3lib_FlashMessage::ERROR => 'ERROR',
212 );
213
214 $title = '';
215 if (!empty($this->title)) {
216 $title = ' - ' . $this->title;
217 }
218
219 return $severities[$this->severity] . $title . ': ' . $this->message;
220 }
221
222 }
223
224
225 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_flashmessage.php']) {
226 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_flashmessage.php']);
227 }
228
229 ?>