d6c70af601502323ba12a57d1b46b166232e389b
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / Element / InputColorPickerElement.php
1 <?php
2 namespace TYPO3\CMS\Backend\Form\Element;
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\Core\Localization\LanguageService;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Core\Utility\MathUtility;
20 use TYPO3\CMS\Core\Utility\StringUtility;
21
22 /**
23 * Render an input field with a color picker
24 */
25 class InputColorPickerElement extends AbstractFormElement
26 {
27 /**
28 * Default field wizards enabled for this element.
29 *
30 * @var array
31 */
32 protected $defaultFieldWizard = [
33 'localizationStateSelector' => [
34 'renderType' => 'localizationStateSelector',
35 ],
36 'otherLanguageContent' => [
37 'renderType' => 'otherLanguageContent',
38 'after' => [
39 'localizationStateSelector'
40 ],
41 ],
42 'defaultLanguageDifferences' => [
43 'renderType' => 'defaultLanguageDifferences',
44 'after' => [
45 'otherLanguageContent',
46 ],
47 ],
48 ];
49
50 /**
51 * This will render a single-line input form field, possibly with various control/validation features
52 *
53 * @return array As defined in initializeResultArray() of AbstractNode
54 */
55 public function render()
56 {
57 $languageService = $this->getLanguageService();
58
59 $table = $this->data['tableName'];
60 $fieldName = $this->data['fieldName'];
61 $row = $this->data['databaseRow'];
62 $parameterArray = $this->data['parameterArray'];
63 $resultArray = $this->initializeResultArray();
64
65 $itemValue = $parameterArray['itemFormElValue'];
66 $config = $parameterArray['fieldConf']['config'];
67 $size = MathUtility::forceIntegerInRange($config['size'] ?: $this->defaultInputWidth, $this->minimumInputWidth, $this->maxInputWidth);
68 $evalList = GeneralUtility::trimExplode(',', $config['eval'], true);
69 $width = (int)$this->formMaxWidth($size);
70 $nullControlNameEscaped = htmlspecialchars('control[active][' . $table . '][' . $row['uid'] . '][' . $fieldName . ']');
71
72 if ($config['readOnly']) {
73 $html = [];
74 $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
75 $html[] = '<div class="form-wizards-wrap">';
76 $html[] = '<div class="form-wizards-element">';
77 $html[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
78 $html[] = '<input class="form-control" value="' . htmlspecialchars($itemValue) . '" type="text" disabled>';
79 $html[] = '</div>';
80 $html[] = '</div>';
81 $html[] = '</div>';
82 $html[] = '</div>';
83 $resultArray['html'] = implode(LF, $html);
84 return $resultArray;
85 }
86
87 // @todo: The whole eval handling is a mess and needs refactoring
88 foreach ($evalList as $func) {
89 // @todo: This is ugly: The code should find out on it's own whether a eval definition is a
90 // @todo: keyword like "date", or a class reference. The global registration could be dropped then
91 // Pair hook to the one in \TYPO3\CMS\Core\DataHandling\DataHandler::checkValue_input_Eval()
92 if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$func])) {
93 if (class_exists($func)) {
94 $evalObj = GeneralUtility::makeInstance($func);
95 if (method_exists($evalObj, 'deevaluateFieldValue')) {
96 $_params = [
97 'value' => $itemValue
98 ];
99 $itemValue = $evalObj->deevaluateFieldValue($_params);
100 }
101 if (method_exists($evalObj, 'returnFieldJS')) {
102 $resultArray['additionalJavaScriptPost'][] = 'TBE_EDITOR.customEvalFunctions[' . GeneralUtility::quoteJSvalue($evalData) . '] = function(value) {' . $evalObj->returnFieldJS() . '};';
103 }
104 }
105 }
106 }
107
108 // Load needed js library
109 $resultArray['requireJsModules'][] = [
110 'TYPO3/CMS/Backend/ColorPicker' => 'function(ColorPicker){ColorPicker.initialize()}'
111 ];
112
113 $attributes = [
114 'value' => $itemValue,
115 'id' => StringUtility::getUniqueId('formengine-input-'),
116 'class' => implode(' ', [
117 'form-control',
118 'hasDefaultValue',
119 't3js-clearable',
120 't3js-color-picker',
121 'formengine-colorpickerelement',
122 ]),
123 'data-formengine-validation-rules' => $this->getValidationDataAsJsonString($config),
124 'data-formengine-input-params' => json_encode([
125 'field' => $parameterArray['itemFormElName'],
126 'evalList' => implode(',', $evalList),
127 'is_in' => trim($config['is_in']),
128 ]),
129 'data-formengine-input-name' => $parameterArray['itemFormElName'],
130 ];
131
132 if (isset($config['max']) && (int)$config['max'] > 0) {
133 $attributes['maxlength'] = (int)$config['max'];
134 }
135 if (!empty($config['placeholder'])) {
136 $attributes['placeholder'] = trim($config['placeholder']);
137 }
138 if (isset($config['autocomplete'])) {
139 $attributes['autocomplete'] = empty($config['autocomplete']) ? 'new-' . $fieldName : 'on';
140 }
141
142 $valuePickerHtml = [];
143 if (isset($config['valuePicker']['items']) && is_array($config['valuePicker']['items'])) {
144 $valuePickerHtml[] = '<select class="t3js-colorpicker-value-trigger form-control tceforms-select tceforms-wizardselect">';
145 $valuePickerHtml[] = '<option></option>';
146 foreach ($config['valuePicker']['items'] as $item) {
147 $valuePickerHtml[] = '<option value="' . htmlspecialchars($item[1]) . '">' . htmlspecialchars($languageService->sL($item[0])) . '</option>';
148 }
149 $valuePickerHtml[] = '</select>';
150 }
151
152 $fieldInformationResult = $this->renderFieldInformation();
153 $fieldInformationHtml = $fieldInformationResult['html'];
154 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
155
156 $fieldWizardResult = $this->renderFieldWizard();
157 $fieldWizardHtml = $fieldWizardResult['html'];
158 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
159
160 $fieldControlResult = $this->renderFieldControl();
161 $fieldControlHtml = $fieldControlResult['html'];
162 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldControlResult, false);
163
164 $mainFieldHtml = [];
165 $mainFieldHtml[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
166 $mainFieldHtml[] = '<div class="form-wizards-wrap">';
167 $mainFieldHtml[] = '<div class="form-wizards-element">';
168 $mainFieldHtml[] = '<input type="text"' . GeneralUtility::implodeAttributes($attributes, true) . ' />';
169 $mainFieldHtml[] = '<input type="hidden" name="' . $parameterArray['itemFormElName'] . '" value="' . htmlspecialchars($itemValue) . '" />';
170 $mainFieldHtml[] = '</div>';
171 $mainFieldHtml[] = '<div class="form-wizards-items-aside">';
172 $mainFieldHtml[] = '<div class="btn-group">';
173 $mainFieldHtml[] = $fieldControlHtml;
174 $mainFieldHtml[] = implode(LF, $valuePickerHtml);
175 $mainFieldHtml[] = '</div>';
176 $mainFieldHtml[] = '</div>';
177 $mainFieldHtml[] = '<div class="form-wizards-items-bottom">';
178 $mainFieldHtml[] = $fieldWizardHtml;
179 $mainFieldHtml[] = '</div>';
180 $mainFieldHtml[] = '</div>';
181 $mainFieldHtml[] = '</div>';
182 $mainFieldHtml = implode(LF, $mainFieldHtml);
183
184 $fullElement = $mainFieldHtml;
185 if ($this->hasNullCheckboxButNoPlaceholder()) {
186 $checked = $itemValue !== null ? ' checked="checked"' : '';
187 $fullElement = [];
188 $fullElement[] = '<div class="t3-form-field-disable"></div>';
189 $fullElement[] = '<div class="checkbox t3-form-field-eval-null-checkbox">';
190 $fullElement[] = '<label for="' . $nullControlNameEscaped . '">';
191 $fullElement[] = '<input type="hidden" name="' . $nullControlNameEscaped . '" value="0" />';
192 $fullElement[] = '<input type="checkbox" name="' . $nullControlNameEscaped . '" id="' . $nullControlNameEscaped . '" value="1"' . $checked . ' />';
193 $fullElement[] = $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.nullCheckbox');
194 $fullElement[] = '</label>';
195 $fullElement[] = '</div>';
196 $fullElement[] = $mainFieldHtml;
197 $fullElement = implode(LF, $fullElement);
198 } elseif ($this->hasNullCheckboxWithPlaceholder()) {
199 $checked = $itemValue !== null ? ' checked="checked"' : '';
200 $placeholder = $shortenedPlaceholder = $config['placeholder'] ?? '';
201 $disabled = '';
202 $fallbackValue = 0;
203 if (strlen($placeholder) > 0) {
204 $shortenedPlaceholder = GeneralUtility::fixed_lgd_cs($placeholder, 20);
205 if ($placeholder !== $shortenedPlaceholder) {
206 $overrideLabel = sprintf(
207 $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
208 '<span title="' . htmlspecialchars($placeholder) . '">' . htmlspecialchars($shortenedPlaceholder) . '</span>'
209 );
210 } else {
211 $overrideLabel = sprintf(
212 $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
213 htmlspecialchars($placeholder)
214 );
215 }
216 } else {
217 $fallbackValue = 1;
218 $checked = ' checked="checked"';
219 $disabled = ' disabled="disabled"';
220 $overrideLabel = $languageService->sL(
221 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override_not_available'
222 );
223 }
224 $fullElement = [];
225 $fullElement[] = '<div class="checkbox t3js-form-field-eval-null-placeholder-checkbox">';
226 $fullElement[] = '<label for="' . $nullControlNameEscaped . '">';
227 $fullElement[] = '<input type="hidden" name="' . $nullControlNameEscaped . '" value="' . $fallbackValue . '" />';
228 $fullElement[] = '<input type="checkbox" name="' . $nullControlNameEscaped . '" id="' . $nullControlNameEscaped . '" value="1"' . $checked . $disabled . ' />';
229 $fullElement[] = $overrideLabel;
230 $fullElement[] = '</label>';
231 $fullElement[] = '</div>';
232 $fullElement[] = '<div class="t3js-formengine-placeholder-placeholder">';
233 $fullElement[] = '<div class="form-control-wrap" style="max-width:' . $width . 'px">';
234 $fullElement[] = '<input type="text" class="form-control" disabled="disabled" value="' . $shortenedPlaceholder . '" />';
235 $fullElement[] = '</div>';
236 $fullElement[] = '</div>';
237 $fullElement[] = '<div class="t3js-formengine-placeholder-formfield">';
238 $fullElement[] = $mainFieldHtml;
239 $fullElement[] = '</div>';
240 $fullElement = implode(LF, $fullElement);
241 }
242
243 $resultArray['html'] = '<div class="formengine-field-item t3js-formengine-field-item">' . $fieldInformationHtml . $fullElement . '</div>';
244 return $resultArray;
245 }
246
247 /**
248 * @return LanguageService
249 */
250 protected function getLanguageService()
251 {
252 return $GLOBALS['LANG'];
253 }
254 }