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