[BUGFIX] Colorpicker data not set in EM configuration
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Classes / ViewHelpers / Form / TypoScriptConstantsViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Extensionmanager\ViewHelpers\Form;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper;
20
21 /**
22 * View Helper for rendering Extension Manager Configuration Form
23 * @internal
24 */
25 class TypoScriptConstantsViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper
26 {
27 /**
28 * @var array
29 */
30 public $viewHelperMapping = array(
31 'int' => 'renderIntegerField',
32 'int+' => 'renderPositiveIntegerField',
33 'integer' => 'renderIntegerField',
34 'color' => 'renderColorPicker',
35 'wrap' => 'renderWrapField',
36 'offset' => 'renderOffsetField',
37 'options' => 'renderOptionSelect',
38 'boolean' => 'renderCheckbox',
39 'user' => 'renderUserFunction',
40 'small' => 'renderSmallTextField',
41 'string' => 'renderTextField',
42 'input' => 'renderTextField', // only for backwards compatibility, many extensions depend on that
43 'default' => 'renderTextField' // only for backwards compatibility, many extensions depend on that
44 );
45
46 /**
47 * @var string
48 */
49 public $tagName = 'input';
50
51 /**
52 * Initialize arguments of this view helper
53 *
54 * @return void
55 */
56 public function initializeArguments()
57 {
58 parent::initializeArguments();
59 $this->registerArgument('name', 'string', 'Name of input tag');
60 $this->registerArgument('value', 'mixed', 'Value of input tag');
61 $this->registerUniversalTagAttributes();
62 }
63
64 /**
65 * Render
66 *
67 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
68 * @return string the rendered tag
69 */
70 public function render(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration)
71 {
72 $input = '';
73 if (isset($this->viewHelperMapping[$configuration->getType()]) && method_exists($this, $this->viewHelperMapping[$configuration->getType()])) {
74 $input = $this->{$this->viewHelperMapping[$configuration->getType()]}($configuration);
75 } else {
76 $input = $this->{$this->viewHelperMapping['default']}($configuration);
77 }
78
79 return $input;
80 }
81
82 /**
83 * Render field of type color picker
84 *
85 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
86 * @return string
87 */
88 protected function renderColorPicker(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration)
89 {
90 $elementId = 'em-' . $configuration->getName();
91 $elementName = $this->getName($configuration);
92
93 // configure the field
94 $this->tag->setTagName('input');
95 $this->tag->addAttribute('type', 'text');
96 $this->tag->addAttribute('id', $elementId);
97 $this->tag->addAttribute('name', $elementName);
98 $this->tag->addAttribute('data-formengine-input-name', $elementName);
99 $this->tag->addAttribute('class', 'form-control');
100 if ($configuration->getValue() !== null) {
101 $this->tag->addAttribute('value', $configuration->getValue());
102 }
103
104 // configure colorpicker wizard
105 $params = array(
106 'formName' => 'configurationform',
107 'itemName' => $elementName,
108 );
109 $onClick =
110 'this.blur();' .
111 'vHWin=window.open(' .
112 GeneralUtility::quoteJSvalue(BackendUtility::getModuleUrl('wizard_colorpicker', array('P' => $params))) . ' + \'&P[currentValue]=\' + encodeURIComponent(document.getElementById(' . GeneralUtility::quoteJSvalue($elementId) . ').value),' .
113 '\'popUpem-' . GeneralUtility::shortmd5($elementName) . '\',' .
114 '\'height=400,width=400,status=0,menubar=0,scrollbars=1\'' .
115 ');' .
116 'vHWin.focus();' .
117 'return false;';
118
119 // wrap the field
120 $output = '<div class="form-wizards-wrap form-wizards-aside">'
121 . '<div class="form-wizards-element">' . $this->tag->render() . '</div>'
122 . '<div class="form-wizards-items"><a href="#" onClick="' . htmlspecialchars($onClick) . '" class="btn btn-default"><span class="t3-icon fa fa-eyedropper"></span></a></div>'
123 . '</div>';
124
125 return $output;
126 }
127
128 /**
129 * Render field of type "offset"
130 *
131 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
132 * @return string
133 */
134 protected function renderOffsetField(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration)
135 {
136 $this->tag->setTagName('input');
137 $this->tag->addAttribute('type', 'text');
138 $this->tag->addAttribute('id', 'em-' . $configuration->getName());
139 $this->tag->addAttribute('name', $this->getName($configuration));
140 $this->tag->addAttribute('class', 'form-control t3js-emconf-offset');
141 if ($configuration->getValue() !== null) {
142 $this->tag->addAttribute('value', $configuration->getValue());
143 }
144 return $this->tag->render();
145 }
146
147 /**
148 * Render field of type "wrap"
149 *
150 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
151 * @return string
152 */
153 protected function renderWrapField(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration)
154 {
155 $this->tag->setTagName('input');
156 $this->tag->addAttribute('type', 'text');
157 $this->tag->addAttribute('id', 'em-' . $configuration->getName());
158 $this->tag->addAttribute('name', $this->getName($configuration));
159 $this->tag->addAttribute('class', 'form-control t3js-emconf-wrap');
160 if ($configuration->getValue() !== null) {
161 $this->tag->addAttribute('value', $configuration->getValue());
162 }
163 return $this->tag->render();
164 }
165
166 /**
167 * Render field of type "option"
168 *
169 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
170 * @return string
171 */
172 protected function renderOptionSelect(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration)
173 {
174 $this->tag->setTagName('select');
175 $this->tag->addAttribute('id', 'em-' . $configuration->getName());
176 $this->tag->addAttribute('name', $this->getName($configuration));
177 $this->tag->addAttribute('class', 'form-control');
178 $optionValueArray = $configuration->getGeneric();
179 $output = '';
180 foreach ($optionValueArray as $label => $value) {
181 $output .= '<option value="' . htmlspecialchars($value) . '"';
182 if ($configuration->getValue() == $value) {
183 $output .= ' selected="selected"';
184 }
185 $output .= '>' . $GLOBALS['LANG']->sL($label, true) . '</option>';
186 }
187 $this->tag->setContent($output);
188 return $this->tag->render();
189 }
190
191 /**
192 * Render field of type "int+"
193 *
194 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
195 * @return string
196 */
197 protected function renderPositiveIntegerField(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration)
198 {
199 $this->tag->setTagName('input');
200 $this->tag->addAttribute('type', 'number');
201 $this->tag->addAttribute('id', 'em-' . $configuration->getName());
202 $this->tag->addAttribute('name', $this->getName($configuration));
203 $this->tag->addAttribute('class', 'form-control');
204 $this->tag->addAttribute('min', '0');
205 if ($configuration->getValue() !== null) {
206 $this->tag->addAttribute('value', $configuration->getValue());
207 }
208 return $this->tag->render();
209 }
210
211 /**
212 * Render field of type "integer"
213 *
214 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
215 * @return string
216 */
217 protected function renderIntegerField(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration)
218 {
219 $this->tag->setTagName('input');
220 $this->tag->addAttribute('type', 'number');
221 $this->tag->addAttribute('id', 'em-' . $configuration->getName());
222 $this->tag->addAttribute('name', $this->getName($configuration));
223 $this->tag->addAttribute('class', 'form-control');
224 if ($configuration->getValue() !== null) {
225 $this->tag->addAttribute('value', $configuration->getValue());
226 }
227 return $this->tag->render();
228 }
229
230 /**
231 * Render field of type "text"
232 *
233 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
234 * @return string
235 */
236 protected function renderTextField(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration)
237 {
238 $this->tag->setTagName('input');
239 $this->tag->addAttribute('type', 'text');
240 $this->tag->addAttribute('id', 'em-' . $configuration->getName());
241 $this->tag->addAttribute('name', $this->getName($configuration));
242 $this->tag->addAttribute('class', 'form-control');
243 if ($configuration->getValue() !== null) {
244 $this->tag->addAttribute('value', $configuration->getValue());
245 }
246 return $this->tag->render();
247 }
248
249 /**
250 * Render field of type "small text"
251 *
252 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
253 * @return string
254 */
255 protected function renderSmallTextField(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration)
256 {
257 return $this->renderTextField($configuration);
258 }
259
260 /**
261 * Render field of type "checkbox"
262 *
263 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
264 * @return string
265 */
266 public function renderCheckbox(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration)
267 {
268 $this->tag->addAttribute('type', 'checkbox');
269 $this->tag->addAttribute('name', $this->getName($configuration));
270 $this->tag->addAttribute('value', 1);
271 $this->tag->addAttribute('id', 'em-' . $configuration->getName());
272 if ($configuration->getValue() == 1) {
273 $this->tag->addAttribute('checked', 'checked');
274 }
275 $hiddenField = $this->renderHiddenFieldForEmptyValue($configuration);
276 return '<div class="checkbox">' . $hiddenField . '<label>' . $this->tag->render() . '</label></div>';
277 }
278
279 /**
280 * Render field of type "userFunc"
281 *
282 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
283 * @return string
284 */
285 protected function renderUserFunction(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration)
286 {
287 $userFunction = $configuration->getGeneric();
288 $userFunctionParams = array(
289 'fieldName' => $this->getName($configuration),
290 'fieldValue' => $configuration->getValue(),
291 'propertyName' => $configuration->getName()
292 );
293 return \TYPO3\CMS\Core\Utility\GeneralUtility::callUserFunction($userFunction, $userFunctionParams, $this, '');
294 }
295
296 /**
297 * Get Field Name
298 *
299 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
300 * @return string
301 */
302 protected function getName(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration)
303 {
304 return 'tx_extensionmanager_tools_extensionmanagerextensionmanager[config][' . $configuration->getName() . '][value]';
305 }
306
307 /**
308 * Render a hidden field for empty values
309 *
310 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
311 * @return string
312 */
313 protected function renderHiddenFieldForEmptyValue($configuration)
314 {
315 $hiddenFieldNames = array();
316 if ($this->viewHelperVariableContainer->exists(FormViewHelper::class, 'renderedHiddenFields')) {
317 $hiddenFieldNames = $this->viewHelperVariableContainer->get(FormViewHelper::class, 'renderedHiddenFields');
318 }
319 $fieldName = $this->getName($configuration);
320 if (substr($fieldName, -2) === '[]') {
321 $fieldName = substr($fieldName, 0, -2);
322 }
323 if (!in_array($fieldName, $hiddenFieldNames)) {
324 $hiddenFieldNames[] = $fieldName;
325 $this->viewHelperVariableContainer->addOrUpdate(FormViewHelper::class, 'renderedHiddenFields', $hiddenFieldNames);
326 return '<input type="hidden" name="' . htmlspecialchars($fieldName) . '" value="0" />';
327 }
328 return '';
329 }
330 }