[TASK] Remove trailing whitespace
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / FlashMessagesViewHelper.php
1 <?php
2
3 /* *
4 * This script is backported from the FLOW3 package "TYPO3.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, either version 3 *
8 * of the License, or (at your option) any later version. *
9 * *
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 * @api
63 */
64 class Tx_Fluid_ViewHelpers_FlashMessagesViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractTagBasedViewHelper {
65
66 const RENDER_MODE_UL = 'ul';
67 const RENDER_MODE_DIV = 'div';
68
69 /**
70 * @var tslib_cObj
71 */
72 protected $contentObject;
73
74 /**
75 * @var Tx_Extbase_Configuration_ConfigurationManagerInterface
76 */
77 protected $configurationManager;
78
79 /**
80 * @param Tx_Extbase_Configuration_ConfigurationManagerInterface $configurationManager
81 * @return void
82 */
83 public function injectConfigurationManager(Tx_Extbase_Configuration_ConfigurationManagerInterface $configurationManager) {
84 $this->configurationManager = $configurationManager;
85 $this->contentObject = $this->configurationManager->getContentObject();
86 }
87
88 /**
89 * Initialize arguments
90 *
91 * @return void
92 * @api
93 */
94 public function initializeArguments() {
95 $this->registerUniversalTagAttributes();
96 }
97
98 /**
99 * Renders FlashMessages and flushes the FlashMessage queue
100 * Note: This disables the current page cache in order to prevent FlashMessage output
101 * from being cached.
102 * @see tslib_fe::no_cache
103 *
104 * @param string $renderMode one of the RENDER_MODE_* constants
105 * @return string rendered Flash Messages, if there are any.
106 * @api
107 */
108 public function render($renderMode = self::RENDER_MODE_UL) {
109 $flashMessages = $this->controllerContext->getFlashMessageContainer()->getAllMessagesAndFlush();
110 if ($flashMessages === NULL || count($flashMessages) === 0) {
111 return '';
112 }
113 if (isset($GLOBALS['TSFE']) && $this->contentObject->getUserObjectType() === tslib_cObj::OBJECTTYPE_USER) {
114 $GLOBALS['TSFE']->no_cache = 1;
115 }
116
117 switch ($renderMode) {
118 case self::RENDER_MODE_UL:
119 return $this->renderUl($flashMessages);
120 case self::RENDER_MODE_DIV:
121 return $this->renderDiv($flashMessages);
122 default:
123 throw new Tx_Fluid_Core_ViewHelper_Exception('Invalid render mode "' . $renderMode . '" passed to FlashMessageViewhelper', 1290697924);
124 }
125 }
126
127 /**
128 * Renders the flash messages as unordered list
129 *
130 * @param array $flashMessages array<t3lib_FlashMessage>
131 * @return string
132 */
133 protected function renderUl(array $flashMessages) {
134 $this->tag->setTagName('ul');
135 if ($this->hasArgument('class')) {
136 $this->tag->addAttribute('class', $this->arguments['class']);
137 }
138 $tagContent = '';
139 foreach ($flashMessages as $singleFlashMessage) {
140 $tagContent .= '<li>' . htmlspecialchars($singleFlashMessage->getMessage()) . '</li>';
141 }
142 $this->tag->setContent($tagContent);
143 return $this->tag->render();
144 }
145
146 /*
147 * Renders the flash messages as nested divs
148 *
149 * @param array $flashMessages array<t3lib_FlashMessage>
150 * @return string
151 */
152 protected function renderDiv(array $flashMessages) {
153 $this->tag->setTagName('div');
154 if ($this->hasArgument('class')) {
155 $this->tag->addAttribute('class', $this->arguments['class']);
156 } else {
157 $this->tag->addAttribute('class', 'typo3-messages');
158 }
159 $tagContent = '';
160 foreach ($flashMessages as $singleFlashMessage) {
161 $tagContent .= $singleFlashMessage->render();
162 }
163 $this->tag->setContent($tagContent);
164 return $this->tag->render();
165 }
166 }
167
168 ?>