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