935f05501dd8d0be2bd6a227069166402577f7ff
[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('class', 'form-control');
99 if ($configuration->getValue() !== null) {
100 $this->tag->addAttribute('value', $configuration->getValue());
101 }
102
103 // configure colorpicker wizard
104 $params = array(
105 'formName' => 'configurationform',
106 'itemName' => $elementName,
107 );
108 $onClick =
109 'this.blur();' .
110 'vHWin=window.open(' .
111 GeneralUtility::quoteJSvalue(BackendUtility::getModuleUrl('wizard_colorpicker', array('P' => $params))) . ' + \'&P[currentValue]=\' + encodeURIComponent(document.getElementById(' . GeneralUtility::quoteJSvalue($elementId) . ').value),' .
112 '\'popUpem-' . GeneralUtility::shortmd5($elementName) . '\',' .
113 '\'height=400,width=400,status=0,menubar=0,scrollbars=1\'' .
114 ');' .
115 'vHWin.focus();' .
116 'return false;';
117
118 // wrap the field
119 $output = '<div class="form-wizards-wrap form-wizards-aside">'
120 . '<div class="form-wizards-element">' . $this->tag->render() . '</div>'
121 . '<div class="form-wizards-items"><a href="#" onClick="' . htmlspecialchars($onClick) . '" class="btn btn-default"><span class="t3-icon fa fa-eyedropper"></span></a></div>'
122 . '</div>';
123
124 return $output;
125 }
126
127 /**
128 * Render field of type "offset"
129 *
130 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
131 * @return string
132 */
133 protected function renderOffsetField(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration)
134 {
135 $this->tag->setTagName('input');
136 $this->tag->addAttribute('type', 'text');
137 $this->tag->addAttribute('id', 'em-' . $configuration->getName());
138 $this->tag->addAttribute('name', $this->getName($configuration));
139 $this->tag->addAttribute('class', 'form-control t3js-emconf-offset');
140 if ($configuration->getValue() !== null) {
141 $this->tag->addAttribute('value', $configuration->getValue());
142 }
143 return $this->tag->render();
144 }
145
146 /**
147 * Render field of type "wrap"
148 *
149 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
150 * @return string
151 */
152 protected function renderWrapField(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration)
153 {
154 $this->tag->setTagName('input');
155 $this->tag->addAttribute('type', 'text');
156 $this->tag->addAttribute('id', 'em-' . $configuration->getName());
157 $this->tag->addAttribute('name', $this->getName($configuration));
158 $this->tag->addAttribute('class', 'form-control t3js-emconf-wrap');
159 if ($configuration->getValue() !== null) {
160 $this->tag->addAttribute('value', $configuration->getValue());
161 }
162 return $this->tag->render();
163 }
164
165 /**
166 * Render field of type "option"
167 *
168 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
169 * @return string
170 */
171 protected function renderOptionSelect(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration)
172 {
173 $this->tag->setTagName('select');
174 $this->tag->addAttribute('id', 'em-' . $configuration->getName());
175 $this->tag->addAttribute('name', $this->getName($configuration));
176 $this->tag->addAttribute('class', 'form-control');
177 $optionValueArray = $configuration->getGeneric();
178 $output = '';
179 foreach ($optionValueArray as $label => $value) {
180 $output .= '<option value="' . htmlspecialchars($value) . '"';
181 if ($configuration->getValue() == $value) {
182 $output .= ' selected="selected"';
183 }
184 $output .= '>' . $GLOBALS['LANG']->sL($label, true) . '</option>';
185 }
186 $this->tag->setContent($output);
187 return $this->tag->render();
188 }
189
190 /**
191 * Render field of type "int+"
192 *
193 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
194 * @return string
195 */
196 protected function renderPositiveIntegerField(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration)
197 {
198 $this->tag->setTagName('input');
199 $this->tag->addAttribute('type', 'number');
200 $this->tag->addAttribute('id', 'em-' . $configuration->getName());
201 $this->tag->addAttribute('name', $this->getName($configuration));
202 $this->tag->addAttribute('class', 'form-control');
203 $this->tag->addAttribute('min', '0');
204 if ($configuration->getValue() !== null) {
205 $this->tag->addAttribute('value', $configuration->getValue());
206 }
207 return $this->tag->render();
208 }
209
210 /**
211 * Render field of type "integer"
212 *
213 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
214 * @return string
215 */
216 protected function renderIntegerField(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration)
217 {
218 $this->tag->setTagName('input');
219 $this->tag->addAttribute('type', 'number');
220 $this->tag->addAttribute('id', 'em-' . $configuration->getName());
221 $this->tag->addAttribute('name', $this->getName($configuration));
222 $this->tag->addAttribute('class', 'form-control');
223 if ($configuration->getValue() !== null) {
224 $this->tag->addAttribute('value', $configuration->getValue());
225 }
226 return $this->tag->render();
227 }
228
229 /**
230 * Render field of type "text"
231 *
232 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
233 * @return string
234 */
235 protected function renderTextField(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration)
236 {
237 $this->tag->setTagName('input');
238 $this->tag->addAttribute('type', 'text');
239 $this->tag->addAttribute('id', 'em-' . $configuration->getName());
240 $this->tag->addAttribute('name', $this->getName($configuration));
241 $this->tag->addAttribute('class', 'form-control');
242 if ($configuration->getValue() !== null) {
243 $this->tag->addAttribute('value', $configuration->getValue());
244 }
245 return $this->tag->render();
246 }
247
248 /**
249 * Render field of type "small text"
250 *
251 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
252 * @return string
253 */
254 protected function renderSmallTextField(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration)
255 {
256 return $this->renderTextField($configuration);
257 }
258
259 /**
260 * Render field of type "checkbox"
261 *
262 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
263 * @return string
264 */
265 public function renderCheckbox(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration)
266 {
267 $this->tag->addAttribute('type', 'checkbox');
268 $this->tag->addAttribute('name', $this->getName($configuration));
269 $this->tag->addAttribute('value', 1);
270 $this->tag->addAttribute('id', 'em-' . $configuration->getName());
271 if ($configuration->getValue() == 1) {
272 $this->tag->addAttribute('checked', 'checked');
273 }
274 $hiddenField = $this->renderHiddenFieldForEmptyValue($configuration);
275 return '<div class="checkbox">' . $hiddenField . '<label>' . $this->tag->render() . '</label></div>';
276 }
277
278 /**
279 * Render field of type "userFunc"
280 *
281 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
282 * @return string
283 */
284 protected function renderUserFunction(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration)
285 {
286 $userFunction = $configuration->getGeneric();
287 $userFunctionParams = array(
288 'fieldName' => $this->getName($configuration),
289 'fieldValue' => $configuration->getValue(),
290 'propertyName' => $configuration->getName()
291 );
292 return \TYPO3\CMS\Core\Utility\GeneralUtility::callUserFunction($userFunction, $userFunctionParams, $this, '');
293 }
294
295 /**
296 * Get Field Name
297 *
298 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
299 * @return string
300 */
301 protected function getName(\TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration)
302 {
303 return 'tx_extensionmanager_tools_extensionmanagerextensionmanager[config][' . $configuration->getName() . '][value]';
304 }
305
306 /**
307 * Render a hidden field for empty values
308 *
309 * @param \TYPO3\CMS\Extensionmanager\Domain\Model\ConfigurationItem $configuration
310 * @return string
311 */
312 protected function renderHiddenFieldForEmptyValue($configuration)
313 {
314 $hiddenFieldNames = array();
315 if ($this->viewHelperVariableContainer->exists(FormViewHelper::class, 'renderedHiddenFields')) {
316 $hiddenFieldNames = $this->viewHelperVariableContainer->get(FormViewHelper::class, 'renderedHiddenFields');
317 }
318 $fieldName = $this->getName($configuration);
319 if (substr($fieldName, -2) === '[]') {
320 $fieldName = substr($fieldName, 0, -2);
321 }
322 if (!in_array($fieldName, $hiddenFieldNames)) {
323 $hiddenFieldNames[] = $fieldName;
324 $this->viewHelperVariableContainer->addOrUpdate(FormViewHelper::class, 'renderedHiddenFields', $hiddenFieldNames);
325 return '<input type="hidden" name="' . htmlspecialchars($fieldName) . '" value="0" />';
326 }
327 return '';
328 }
329 }