[FEATURE] Added support for multiple FlashMessage queues
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / FlashMessagesViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers;
3
4 /* *
5 * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License, either version 3 *
9 * of the License, or (at your option) any later version. *
10 * *
11 * *
12 * This script is distributed in the hope that it will be useful, but *
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
15 * General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with the script. *
19 * If not, see http://www.gnu.org/licenses/lgpl.html *
20 * *
21 * The TYPO3 project - inspiring people to share! *
22 * */
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24
25 /**
26 * View helper which renders the flash messages (if there are any) as an unsorted list.
27 *
28 * In case you need custom Flash Message HTML output, please write your own ViewHelper for the moment.
29 *
30 *
31 * = Examples =
32 *
33 * <code title="Simple">
34 * <f:flashMessages />
35 * </code>
36 * <output>
37 * An ul-list of flash messages.
38 * </output>
39 *
40 * <code title="Output with custom css class">
41 * <f:flashMessages class="specialClass" />
42 * </code>
43 * <output>
44 * <ul class="specialClass">
45 * ...
46 * </ul>
47 * </output>
48 *
49 * <code title="TYPO3 core style">
50 * <f:flashMessages />
51 * </code>
52 * <output>
53 * <ul class="typo3-messages">
54 * <li class="alert alert-ok">
55 * <h4>Some Message Header</h4>
56 * Some message body
57 * </li>
58 * <li class="alert alert-notice">
59 * Some notice message without header
60 * </li>
61 * </ul>
62 * </output>
63 * <code title="Output flash messages as a description list">
64 * <f:flashMessages as="flashMessages">
65 * <dl class="messages">
66 * <f:for each="{flashMessages}" as="flashMessage">
67 * <dt>{flashMessage.code}</dt>
68 * <dd>{flashMessage.message}</dd>
69 * </f:for>
70 * </dl>
71 * </f:flashMessages>
72 * </code>
73 * <output>
74 * <dl class="messages">
75 * <dt>1013</dt>
76 * <dd>Some Warning Message.</dd>
77 * </dl>
78 * </output>
79 *
80 * <code title="Using a specific queue">
81 * <f:flashMessages queueIdentifier="myQueue" />
82 * </code>
83 *
84 * @api
85 */
86 class FlashMessagesViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper {
87
88 const RENDER_MODE_UL = 'ul';
89 const RENDER_MODE_DIV = 'div';
90
91 /**
92 * @var string
93 */
94 protected $tagName = 'ul';
95
96 /**
97 * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
98 */
99 protected $configurationManager;
100
101 /**
102 * @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager
103 * @return void
104 */
105 public function injectConfigurationManager(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager) {
106 $this->configurationManager = $configurationManager;
107 }
108
109 /**
110 * Initialize arguments
111 *
112 * @return void
113 * @api
114 */
115 public function initializeArguments() {
116 $this->registerUniversalTagAttributes();
117 $this->registerArgument('queueIdentifier', 'string', 'Flash-message queue to use', FALSE);
118 }
119
120 /**
121 * Renders FlashMessages and flushes the FlashMessage queue
122 * Note: This disables the current page cache in order to prevent FlashMessage output
123 * from being cached.
124 *
125 * @see \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::no_cache
126 * @param string $renderMode @deprecated since TYPO3 CMS 7.3. If you need custom output, use <f:flashMessages as="messages"><f:for each="messages" as="message">...</f:for></f:flashMessages>
127 * @param string $as The name of the current flashMessage variable for rendering inside
128 * @return string rendered Flash Messages, if there are any.
129 * @api
130 */
131 public function render($renderMode = NULL, $as = NULL) {
132 $queueIdentifier = isset($this->arguments['queueIdentifier']) ? $this->arguments['queueIdentifier'] : NULL;
133 $flashMessages = $this->controllerContext->getFlashMessageQueue($queueIdentifier)->getAllMessagesAndFlush();
134 if ($flashMessages === NULL || count($flashMessages) === 0) {
135 return '';
136 }
137 if ($renderMode !== NULL) {
138 GeneralUtility::deprecationLog('renderMode has been deprecated in TYPO3 CMS 7.3 and will be removed in TYPO3 CMS 8');
139 if ($renderMode === self::RENDER_MODE_DIV) {
140 $content = $this->renderDiv($flashMessages);
141 } else {
142 $content = $this->renderAsList($flashMessages);
143 }
144 } else {
145 if ($as === NULL) {
146 $content = $this->renderAsList($flashMessages);
147 } else {
148 $content = $this->renderFromTemplate($flashMessages, $as);
149 }
150 }
151
152 return $content;
153 }
154
155 /**
156 * Renders the flash messages as unordered list
157 *
158 * @param array $flashMessages \TYPO3\CMS\Core\Messaging\FlashMessage[]
159 * @return string
160 */
161 protected function renderAsList(array $flashMessages) {
162 $flashMessagesClass = $this->hasArgument('class') ? $this->arguments['class'] : 'typo3-messages';
163 $tagContent = '';
164 $this->tag->addAttribute('class', $flashMessagesClass);
165 /** @var $singleFlashMessage \TYPO3\CMS\Core\Messaging\FlashMessage */
166 foreach ($flashMessages as $singleFlashMessage) {
167 $severityClass = sprintf('alert %s', $singleFlashMessage->getClass());
168 $messageContent = htmlspecialchars($singleFlashMessage->getMessage());
169 if ($singleFlashMessage->getTitle() !== '') {
170 $messageContent = sprintf('<h4>%s</h4>', htmlspecialchars($singleFlashMessage->getTitle())) . $messageContent;
171 }
172 $tagContent .= sprintf('<li class="%s">%s</li>', htmlspecialchars($severityClass), $messageContent);
173 }
174 $this->tag->setContent($tagContent);
175 return $this->tag->render();
176 }
177
178 /**
179 * Renders the flash messages as nested divs
180 * Defer the rendering of Flash Messages to the template. In this case,
181 * the flash messages are stored in the template inside the variable specified
182 * in "as".
183 *
184 * @param array $flashMessages \TYPO3\CMS\Core\Messaging\FlashMessage[]
185 * @param string $as
186 * @return string
187 */
188 protected function renderFromTemplate(array $flashMessages, $as) {
189 $templateVariableContainer = $this->renderingContext->getTemplateVariableContainer();
190 $templateVariableContainer->add($as, $flashMessages);
191 $content = $this->renderChildren();
192 $templateVariableContainer->remove($as);
193
194 return $content;
195 }
196
197 /**
198 * Renders the flash messages as nested divs
199 *
200 * @deprecated in 7.3 will be removed in 8.0
201 * @param array $flashMessages \TYPO3\CMS\Core\Messaging\FlashMessage[]
202 * @return string
203 */
204 protected function renderDiv(array $flashMessages) {
205 $this->tag->setTagName('div');
206 if ($this->hasArgument('class')) {
207 $this->tag->addAttribute('class', $this->arguments['class']);
208 } else {
209 $this->tag->addAttribute('class', 'typo3-messages');
210 }
211 $tagContent = '';
212 foreach ($flashMessages as $singleFlashMessage) {
213 $tagContent .= $singleFlashMessage->render();
214 }
215 $this->tag->setContent($tagContent);
216 return $this->tag->render();
217 }
218
219 }