[CLEANUP] Adjust code to CGL and fix small namespace bugs
[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 /**
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 FlashMessagesViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper {
65
66 const RENDER_MODE_UL = 'ul';
67 const RENDER_MODE_DIV = 'div';
68
69 /**
70 * @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
71 */
72 protected $contentObject;
73
74 /**
75 * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
76 */
77 protected $configurationManager;
78
79 /**
80 * @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager
81 * @return void
82 */
83 public function injectConfigurationManager(\TYPO3\CMS\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 *
103 * @see tslib_fe::no_cache
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() === \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::OBJECTTYPE_USER) {
114 $GLOBALS['TSFE']->no_cache = 1;
115 }
116 switch ($renderMode) {
117 case self::RENDER_MODE_UL:
118 return $this->renderUl($flashMessages);
119 case self::RENDER_MODE_DIV:
120 return $this->renderDiv($flashMessages);
121 default:
122 throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('Invalid render mode "' . $renderMode . '" passed to FlashMessageViewhelper', 1290697924);
123 }
124 }
125
126 /**
127 * Renders the flash messages as unordered list
128 *
129 * @param array $flashMessages array<t3lib_FlashMessage>
130 * @return string
131 */
132 protected function renderUl(array $flashMessages) {
133 $this->tag->setTagName('ul');
134 if ($this->hasArgument('class')) {
135 $this->tag->addAttribute('class', $this->arguments['class']);
136 }
137 $tagContent = '';
138 foreach ($flashMessages as $singleFlashMessage) {
139 $tagContent .= '<li>' . htmlspecialchars($singleFlashMessage->getMessage()) . '</li>';
140 }
141 $this->tag->setContent($tagContent);
142 return $this->tag->render();
143 }
144
145 /*
146 * Renders the flash messages as nested divs
147 *
148 * @param array $flashMessages array<t3lib_FlashMessage>
149 * @return string
150 */
151 protected function renderDiv(array $flashMessages) {
152 $this->tag->setTagName('div');
153 if ($this->hasArgument('class')) {
154 $this->tag->addAttribute('class', $this->arguments['class']);
155 } else {
156 $this->tag->addAttribute('class', 'typo3-messages');
157 }
158 $tagContent = '';
159 foreach ($flashMessages as $singleFlashMessage) {
160 $tagContent .= $singleFlashMessage->render();
161 }
162 $this->tag->setContent($tagContent);
163 return $this->tag->render();
164 }
165 }
166
167 ?>