b8e5b5659bd8b1c0cc99e16c4ae6a4b6f9774790
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Messaging / FlashMessage.php
1 <?php
2 namespace TYPO3\CMS\Core\Messaging;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * A class representing flash messages.
19 */
20 class FlashMessage extends AbstractMessage
21 {
22 /**
23 * Defines whether the message should be stored in the session (to survive redirects) or only for one request (default)
24 *
25 * @var bool
26 */
27 protected $storeInSession = false;
28
29 /**
30 * @var string The message severity class names
31 */
32 protected $classes = [
33 self::NOTICE => 'notice',
34 self::INFO => 'info',
35 self::OK => 'success',
36 self::WARNING => 'warning',
37 self::ERROR => 'danger'
38 ];
39
40 /**
41 * @var string The message severity icon names
42 */
43 protected $icons = [
44 self::NOTICE => 'lightbulb-o',
45 self::INFO => 'info',
46 self::OK => 'check',
47 self::WARNING => 'exclamation',
48 self::ERROR => 'times'
49 ];
50
51 /**
52 * Constructor for a flash message
53 *
54 * @param string $message The message.
55 * @param string $title Optional message title.
56 * @param int $severity Optional severity, must be either of one of \TYPO3\CMS\Core\Messaging\FlashMessage constants
57 * @param bool $storeInSession Optional, defines whether the message should be stored in the session or only for one request (default)
58 */
59 public function __construct($message, $title = '', $severity = self::OK, $storeInSession = false)
60 {
61 $this->setMessage($message);
62 $this->setTitle($title);
63 $this->setSeverity($severity);
64 $this->setStoreInSession($storeInSession);
65 }
66
67 /**
68 * Gets the message's storeInSession flag.
69 *
70 * @return bool TRUE if message should be stored in the session, otherwise FALSE.
71 */
72 public function isSessionMessage()
73 {
74 return $this->storeInSession;
75 }
76
77 /**
78 * Sets the message's storeInSession flag
79 *
80 * @param bool $storeInSession The persistence flag
81 * @return void
82 */
83 public function setStoreInSession($storeInSession)
84 {
85 $this->storeInSession = (bool)$storeInSession;
86 }
87
88 /**
89 * Gets the message severity class name
90 *
91 * @return string The message severity class name
92 */
93 public function getClass()
94 {
95 return 'alert-' . $this->classes[$this->severity];
96 }
97
98 /**
99 * Gets the message severity icon name
100 *
101 * @return string The message severity icon name
102 */
103 public function getIconName()
104 {
105 return $this->icons[$this->severity];
106 }
107
108 /**
109 * Gets the message rendered as clean and secure markup
110 *
111 * @return string
112 */
113 public function getMessageAsMarkup()
114 {
115 $messageTitle = $this->getTitle();
116 $markup = [];
117 $markup[] = '<div class="alert ' . htmlspecialchars($this->getClass()) . '">';
118 $markup[] = ' <div class="media">';
119 $markup[] = ' <div class="media-left">';
120 $markup[] = ' <span class="fa-stack fa-lg">';
121 $markup[] = ' <i class="fa fa-circle fa-stack-2x"></i>';
122 $markup[] = ' <i class="fa fa-' . htmlspecialchars($this->getIconName()) . ' fa-stack-1x"></i>';
123 $markup[] = ' </span>';
124 $markup[] = ' </div>';
125 $markup[] = ' <div class="media-body">';
126 if (!empty($messageTitle)) {
127 $markup[] = ' <h4 class="alert-title">' . htmlspecialchars($messageTitle) . '</h4>';
128 }
129 $markup[] = ' <p class="alert-message">' . htmlspecialchars($this->getMessage()) . '</p>';
130 $markup[] = ' </div>';
131 $markup[] = ' </div>';
132 $markup[] = '</div>';
133 return implode('', $markup);
134 }
135 }