[TASK] Introduce DeprecationUtility and move methods
[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\DeprecationUtility;
24 use TYPO3\CMS\Core\Utility\GeneralUtility;
25
26 /**
27 * View helper which renders the flash messages (if there are any) as an unsorted list.
28 *
29 * In case you need custom Flash Message HTML output, please write your own ViewHelper for the moment.
30 *
31 *
32 * = Examples =
33 *
34 * <code title="Simple">
35 * <f:flashMessages />
36 * </code>
37 * <output>
38 * An ul-list of flash messages.
39 * </output>
40 *
41 * <code title="Output with custom css class">
42 * <f:flashMessages class="specialClass" />
43 * </code>
44 * <output>
45 * <ul class="specialClass">
46 * ...
47 * </ul>
48 * </output>
49 *
50 * <code title="TYPO3 core style">
51 * <f:flashMessages />
52 * </code>
53 * <output>
54 * <ul class="typo3-messages">
55 * <li class="alert alert-ok">
56 * <h4>Some Message Header</h4>
57 * Some message body
58 * </li>
59 * <li class="alert alert-notice">
60 * Some notice message without header
61 * </li>
62 * </ul>
63 * </output>
64 * <code title="Output flash messages as a description list">
65 * <f:flashMessages as="flashMessages">
66 * <dl class="messages">
67 * <f:for each="{flashMessages}" as="flashMessage">
68 * <dt>{flashMessage.code}</dt>
69 * <dd>{flashMessage.message}</dd>
70 * </f:for>
71 * </dl>
72 * </f:flashMessages>
73 * </code>
74 * <output>
75 * <dl class="messages">
76 * <dt>1013</dt>
77 * <dd>Some Warning Message.</dd>
78 * </dl>
79 * </output>
80 *
81 * <code title="Using a specific queue">
82 * <f:flashMessages queueIdentifier="myQueue" />
83 * </code>
84 *
85 * @api
86 */
87 class FlashMessagesViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper
88 {
89 const RENDER_MODE_UL = 'ul';
90 const RENDER_MODE_DIV = 'div';
91
92 /**
93 * @var string
94 */
95 protected $tagName = 'ul';
96
97 /**
98 * Initialize arguments
99 *
100 * @return void
101 * @api
102 */
103 public function initializeArguments()
104 {
105 $this->registerUniversalTagAttributes();
106 $this->registerArgument('queueIdentifier', 'string', 'Flash-message queue to use', false);
107 }
108
109 /**
110 * Renders FlashMessages and flushes the FlashMessage queue
111 * Note: This disables the current page cache in order to prevent FlashMessage output
112 * from being cached.
113 *
114 * @see \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::no_cache
115 * @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>
116 * @param string $as The name of the current flashMessage variable for rendering inside
117 * @return string rendered Flash Messages, if there are any.
118 * @api
119 */
120 public function render($renderMode = null, $as = null)
121 {
122 $queueIdentifier = isset($this->arguments['queueIdentifier']) ? $this->arguments['queueIdentifier'] : null;
123 $flashMessages = $this->controllerContext->getFlashMessageQueue($queueIdentifier)->getAllMessagesAndFlush();
124 if ($flashMessages === null || count($flashMessages) === 0) {
125 return '';
126 }
127 if ($renderMode !== null) {
128 DeprecationUtility::logMessage('renderMode has been deprecated in TYPO3 CMS 7.3 and will be removed in TYPO3 CMS 8');
129 if ($renderMode === self::RENDER_MODE_DIV) {
130 $content = $this->renderDiv($flashMessages);
131 } else {
132 $content = $this->renderAsList($flashMessages);
133 }
134 } else {
135 if ($as === null) {
136 $content = $this->renderAsList($flashMessages);
137 } else {
138 $content = $this->renderFromTemplate($flashMessages, $as);
139 }
140 }
141
142 return $content;
143 }
144
145 /**
146 * Renders the flash messages as unordered list
147 *
148 * @param array $flashMessages \TYPO3\CMS\Core\Messaging\FlashMessage[]
149 * @return string
150 */
151 protected function renderAsList(array $flashMessages)
152 {
153 $flashMessagesClass = $this->hasArgument('class') ? $this->arguments['class'] : 'typo3-messages';
154 $tagContent = '';
155 $this->tag->addAttribute('class', $flashMessagesClass);
156 /** @var $singleFlashMessage \TYPO3\CMS\Core\Messaging\FlashMessage */
157 foreach ($flashMessages as $singleFlashMessage) {
158 $severityClass = sprintf('alert %s', $singleFlashMessage->getClass());
159 $messageContent = htmlspecialchars($singleFlashMessage->getMessage());
160 if ($singleFlashMessage->getTitle() !== '') {
161 $messageContent = sprintf('<h4>%s</h4>', htmlspecialchars($singleFlashMessage->getTitle())) . $messageContent;
162 }
163 $tagContent .= sprintf('<li class="%s">%s</li>', htmlspecialchars($severityClass), $messageContent);
164 }
165 $this->tag->setContent($tagContent);
166 return $this->tag->render();
167 }
168
169 /**
170 * Renders the flash messages as nested divs
171 * Defer the rendering of Flash Messages to the template. In this case,
172 * the flash messages are stored in the template inside the variable specified
173 * in "as".
174 *
175 * @param array $flashMessages \TYPO3\CMS\Core\Messaging\FlashMessage[]
176 * @param string $as
177 * @return string
178 */
179 protected function renderFromTemplate(array $flashMessages, $as)
180 {
181 $templateVariableContainer = $this->renderingContext->getTemplateVariableContainer();
182 $templateVariableContainer->add($as, $flashMessages);
183 $content = $this->renderChildren();
184 $templateVariableContainer->remove($as);
185
186 return $content;
187 }
188
189 /**
190 * Renders the flash messages as nested divs
191 *
192 * @deprecated in 7.3 will be removed in 8.0
193 * @param array $flashMessages \TYPO3\CMS\Core\Messaging\FlashMessage[]
194 * @return string
195 */
196 protected function renderDiv(array $flashMessages)
197 {
198 $this->tag->setTagName('div');
199 if ($this->hasArgument('class')) {
200 $this->tag->addAttribute('class', $this->arguments['class']);
201 } else {
202 $this->tag->addAttribute('class', 'typo3-messages');
203 }
204 $tagContent = '';
205 foreach ($flashMessages as $singleFlashMessage) {
206 $tagContent .= $singleFlashMessage->render();
207 }
208 $this->tag->setContent($tagContent);
209 return $this->tag->render();
210 }
211 }