75ef50d2d342fc301f62fb691ae4edb6c6d64608
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / FlashMessagesViewHelper.php
1 <?php
2
3 /* *
4 * This script belongs to the FLOW3 package "Fluid". *
5 * *
6 * It is free software; you can redistribute it and/or modify it under *
7 * the terms of the GNU Lesser General Public License as published by the *
8 * Free Software Foundation, either version 3 of the License, or (at your *
9 * option) any later version. *
10 * *
11 * This script is distributed in the hope that it will be useful, but *
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
13 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
14 * General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with the script. *
18 * If not, see http://www.gnu.org/licenses/lgpl.html *
19 * *
20 * The TYPO3 project - inspiring people to share! *
21 * */
22
23 /**
24 * View helper which renders the flash messages (if there are any) as an unsorted list.
25 *
26 * In case you need custom Flash Message HTML output, please write your own ViewHelper for the moment.
27 *
28 *
29 * = Examples =
30 *
31 * <code title="Simple">
32 * <f:flashMessages />
33 * </code>
34 * <output>
35 * An ul-list of flash messages.
36 * </output>
37 *
38 * <code title="Output with custom css class">
39 * <f:flashMessages class="specialClass" />
40 * </code>
41 * <output>
42 * <ul class="specialClass">
43 * ...
44 * </ul>
45 * </output>
46 *
47 * <code title="TYPO3 core style">
48 * <f:flashMessages renderMode="div" />
49 * </code>
50 * <output>
51 * <div class="typo3-messages">
52 * <div class="typo3-message message-ok">
53 * <div class="message-header">Some Message Header</div>
54 * <div class="message-body">Some message body</div>
55 * </div>
56 * <div class="typo3-message message-notice">
57 * <div class="message-body">Some notice message without header</div>
58 * </div>
59 * </div>
60 * </output>
61 *
62 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
63 * @api
64 */
65 class Tx_Fluid_ViewHelpers_FlashMessagesViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractTagBasedViewHelper {
66
67 const RENDER_MODE_UL = 'ul';
68 const RENDER_MODE_DIV = 'div';
69
70 /**
71 * Initialize arguments
72 *
73 * @return void
74 * @author Sebastian Kurfürst <sebastian@typo3.org>
75 * @api
76 */
77 public function initializeArguments() {
78 $this->registerUniversalTagAttributes();
79 }
80
81 /**
82 * Render method.
83 *
84 * @param string $renderMode one of the RENDER_MODE_* constants
85 * @return string rendered Flash Messages, if there are any.
86 * @author Sebastian Kurfürst <sebastian@typo3.org>
87 * @api
88 */
89 public function render($renderMode = self::RENDER_MODE_UL) {
90 $flashMessages = $this->controllerContext->getFlashMessageContainer()->getAllMessagesAndFlush();
91 if ($flashMessages === NULL || count($flashMessages) === 0) {
92 return '';
93 }
94 switch ($renderMode) {
95 case self::RENDER_MODE_UL:
96 return $this->renderUl($flashMessages);
97 case self::RENDER_MODE_DIV:
98 return $this->renderDiv($flashMessages);
99 default:
100 throw new Tx_Fluid_Core_ViewHelper_Exception('Invalid render mode "' . $renderMode . '" passed to FlashMessageViewhelper', 1290697924);
101 }
102 }
103
104 /**
105 * Renders the flash messages as unordered list
106 *
107 * @param array $flashMessages array<t3lib_FlashMessage>
108 * @return string
109 */
110 protected function renderUl(array $flashMessages) {
111 $this->tag->setTagName('ul');
112 if ($this->hasArgument('class')) {
113 $this->tag->addAttribute('class', $this->arguments['class']);
114 }
115 $tagContent = '';
116 foreach ($flashMessages as $singleFlashMessage) {
117 $tagContent .= '<li>' . htmlspecialchars($singleFlashMessage->getMessage()) . '</li>';
118 }
119 $this->tag->setContent($tagContent);
120 return $this->tag->render();
121 }
122
123 /*
124 * Renders the flash messages as nested divs
125 *
126 * @param array $flashMessages array<t3lib_FlashMessage>
127 * @return string
128 */
129 protected function renderDiv(array $flashMessages) {
130 $this->tag->setTagName('div');
131 if ($this->hasArgument('class')) {
132 $this->tag->addAttribute('class', $this->arguments['class']);
133 } else {
134 $this->tag->addAttribute('class', 'typo3-messages');
135 }
136 $tagContent = '';
137 foreach ($flashMessages as $singleFlashMessage) {
138 $tagContent .= $singleFlashMessage->render();
139 }
140 $this->tag->setContent($tagContent);
141 return $this->tag->render();
142 }
143 }
144
145 ?>