[CLEANUP] Rework/simplify copyright header and remove @package
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / FlashMessagesViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is backported from the TYPO3 Flow package "TYPO3.Fluid".
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 /**
20 * View helper which renders the flash messages (if there are any) as an unsorted list.
21 *
22 * In case you need custom Flash Message HTML output, please write your own ViewHelper for the moment.
23 *
24 *
25 * = Examples =
26 *
27 * <code title="Simple">
28 * <f:flashMessages />
29 * </code>
30 * <output>
31 * An ul-list of flash messages.
32 * </output>
33 *
34 * <code title="Output with custom css class">
35 * <f:flashMessages class="specialClass" />
36 * </code>
37 * <output>
38 * <ul class="specialClass">
39 * ...
40 * </ul>
41 * </output>
42 *
43 * <code title="TYPO3 core style">
44 * <f:flashMessages renderMode="div" />
45 * </code>
46 * <output>
47 * <div class="typo3-messages">
48 * <div class="alert alert-ok">
49 * <h4>Some Message Header</h4>
50 * <div class="alert-body">Some message body</div>
51 * </div>
52 * <div class="alert alert-notice">
53 * <div class="alert-body">Some notice message without header</div>
54 * </div>
55 * </div>
56 * </output>
57 *
58 * @api
59 */
60 class FlashMessagesViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper {
61
62 const RENDER_MODE_UL = 'ul';
63 const RENDER_MODE_DIV = 'div';
64
65 /**
66 * @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
67 */
68 protected $contentObject;
69
70 /**
71 * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
72 */
73 protected $configurationManager;
74
75 /**
76 * @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager
77 * @return void
78 */
79 public function injectConfigurationManager(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager) {
80 $this->configurationManager = $configurationManager;
81 $this->contentObject = $this->configurationManager->getContentObject();
82 }
83
84 /**
85 * Initialize arguments
86 *
87 * @return void
88 * @api
89 */
90 public function initializeArguments() {
91 $this->registerUniversalTagAttributes();
92 }
93
94 /**
95 * Renders FlashMessages and flushes the FlashMessage queue
96 * Note: This disables the current page cache in order to prevent FlashMessage output
97 * from being cached.
98 *
99 * @see \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::no_cache
100 * @param string $renderMode one of the RENDER_MODE_* constants
101 * @return string rendered Flash Messages, if there are any.
102 * @api
103 */
104 public function render($renderMode = self::RENDER_MODE_UL) {
105 $flashMessages = $this->controllerContext->getFlashMessageQueue()->getAllMessagesAndFlush();
106 if ($flashMessages === NULL || count($flashMessages) === 0) {
107 return '';
108 }
109 if (isset($GLOBALS['TSFE']) && $this->contentObject->getUserObjectType() === \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::OBJECTTYPE_USER) {
110 $GLOBALS['TSFE']->no_cache = 1;
111 }
112 switch ($renderMode) {
113 case self::RENDER_MODE_UL:
114 return $this->renderUl($flashMessages);
115 case self::RENDER_MODE_DIV:
116 return $this->renderDiv($flashMessages);
117 default:
118 throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('Invalid render mode "' . $renderMode . '" passed to FlashMessageViewhelper', 1290697924);
119 }
120 }
121
122 /**
123 * Renders the flash messages as unordered list
124 *
125 * @param array $flashMessages array<\TYPO3\CMS\Core\Messaging\FlashMessage>
126 * @return string
127 */
128 protected function renderUl(array $flashMessages) {
129 $this->tag->setTagName('ul');
130 if ($this->hasArgument('class')) {
131 $this->tag->addAttribute('class', $this->arguments['class']);
132 }
133 $tagContent = '';
134 foreach ($flashMessages as $singleFlashMessage) {
135 $tagContent .= '<li>' . htmlspecialchars($singleFlashMessage->getMessage()) . '</li>';
136 }
137 $this->tag->setContent($tagContent);
138 return $this->tag->render();
139 }
140
141 /**
142 * Renders the flash messages as nested divs
143 *
144 * @param array $flashMessages array<\TYPO3\CMS\Core\Messaging\FlashMessage>
145 * @return string
146 */
147 protected function renderDiv(array $flashMessages) {
148 $this->tag->setTagName('div');
149 if ($this->hasArgument('class')) {
150 $this->tag->addAttribute('class', $this->arguments['class']);
151 } else {
152 $this->tag->addAttribute('class', 'typo3-messages');
153 }
154 $tagContent = '';
155 foreach ($flashMessages as $singleFlashMessage) {
156 $tagContent .= $singleFlashMessage->render();
157 }
158 $this->tag->setContent($tagContent);
159 return $this->tag->render();
160 }
161
162 }