[TASK] Make TYPO3 Core PSR-2 standard compliant
[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 {
107 $this->configurationManager = $configurationManager;
108 }
109
110 /**
111 * Initialize arguments
112 *
113 * @return void
114 * @api
115 */
116 public function initializeArguments()
117 {
118 $this->registerUniversalTagAttributes();
119 $this->registerArgument('queueIdentifier', 'string', 'Flash-message queue to use', false);
120 }
121
122 /**
123 * Renders FlashMessages and flushes the FlashMessage queue
124 * Note: This disables the current page cache in order to prevent FlashMessage output
125 * from being cached.
126 *
127 * @see \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::no_cache
128 * @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>
129 * @param string $as The name of the current flashMessage variable for rendering inside
130 * @return string rendered Flash Messages, if there are any.
131 * @api
132 */
133 public function render($renderMode = null, $as = null)
134 {
135 $queueIdentifier = isset($this->arguments['queueIdentifier']) ? $this->arguments['queueIdentifier'] : null;
136 $flashMessages = $this->controllerContext->getFlashMessageQueue($queueIdentifier)->getAllMessagesAndFlush();
137 if ($flashMessages === null || count($flashMessages) === 0) {
138 return '';
139 }
140 if ($renderMode !== null) {
141 GeneralUtility::deprecationLog('renderMode has been deprecated in TYPO3 CMS 7.3 and will be removed in TYPO3 CMS 8');
142 if ($renderMode === self::RENDER_MODE_DIV) {
143 $content = $this->renderDiv($flashMessages);
144 } else {
145 $content = $this->renderAsList($flashMessages);
146 }
147 } else {
148 if ($as === null) {
149 $content = $this->renderAsList($flashMessages);
150 } else {
151 $content = $this->renderFromTemplate($flashMessages, $as);
152 }
153 }
154
155 return $content;
156 }
157
158 /**
159 * Renders the flash messages as unordered list
160 *
161 * @param array $flashMessages \TYPO3\CMS\Core\Messaging\FlashMessage[]
162 * @return string
163 */
164 protected function renderAsList(array $flashMessages)
165 {
166 $flashMessagesClass = $this->hasArgument('class') ? $this->arguments['class'] : 'typo3-messages';
167 $tagContent = '';
168 $this->tag->addAttribute('class', $flashMessagesClass);
169 /** @var $singleFlashMessage \TYPO3\CMS\Core\Messaging\FlashMessage */
170 foreach ($flashMessages as $singleFlashMessage) {
171 $severityClass = sprintf('alert %s', $singleFlashMessage->getClass());
172 $messageContent = htmlspecialchars($singleFlashMessage->getMessage());
173 if ($singleFlashMessage->getTitle() !== '') {
174 $messageContent = sprintf('<h4>%s</h4>', htmlspecialchars($singleFlashMessage->getTitle())) . $messageContent;
175 }
176 $tagContent .= sprintf('<li class="%s">%s</li>', htmlspecialchars($severityClass), $messageContent);
177 }
178 $this->tag->setContent($tagContent);
179 return $this->tag->render();
180 }
181
182 /**
183 * Renders the flash messages as nested divs
184 * Defer the rendering of Flash Messages to the template. In this case,
185 * the flash messages are stored in the template inside the variable specified
186 * in "as".
187 *
188 * @param array $flashMessages \TYPO3\CMS\Core\Messaging\FlashMessage[]
189 * @param string $as
190 * @return string
191 */
192 protected function renderFromTemplate(array $flashMessages, $as)
193 {
194 $templateVariableContainer = $this->renderingContext->getTemplateVariableContainer();
195 $templateVariableContainer->add($as, $flashMessages);
196 $content = $this->renderChildren();
197 $templateVariableContainer->remove($as);
198
199 return $content;
200 }
201
202 /**
203 * Renders the flash messages as nested divs
204 *
205 * @deprecated in 7.3 will be removed in 8.0
206 * @param array $flashMessages \TYPO3\CMS\Core\Messaging\FlashMessage[]
207 * @return string
208 */
209 protected function renderDiv(array $flashMessages)
210 {
211 $this->tag->setTagName('div');
212 if ($this->hasArgument('class')) {
213 $this->tag->addAttribute('class', $this->arguments['class']);
214 } else {
215 $this->tag->addAttribute('class', 'typo3-messages');
216 }
217 $tagContent = '';
218 foreach ($flashMessages as $singleFlashMessage) {
219 $tagContent .= $singleFlashMessage->render();
220 }
221 $this->tag->setContent($tagContent);
222 return $this->tag->render();
223 }
224 }