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