[TASK] Render wizards of FormEngine elements only if needed
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / Element / InputDateTimeElement.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\Imaging\Icon;
18 use TYPO3\CMS\Core\Localization\LanguageService;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Core\Utility\MathUtility;
21 use TYPO3\CMS\Core\Utility\StringUtility;
22
23 /**
24 * Generation of TCEform elements of the type "input type=text"
25 */
26 class InputDateTimeElement extends AbstractFormElement
27 {
28 /**
29 * Default field information enabled for this element.
30 *
31 * @var array
32 */
33 protected $defaultFieldInformation = [
34 'tcaDescription' => [
35 'renderType' => 'tcaDescription',
36 ],
37 ];
38
39 /**
40 * Default field wizards enabled for this element.
41 *
42 * @var array
43 */
44 protected $defaultFieldWizard = [
45 'localizationStateSelector' => [
46 'renderType' => 'localizationStateSelector',
47 ],
48 'otherLanguageContent' => [
49 'renderType' => 'otherLanguageContent',
50 'after' => [
51 'localizationStateSelector'
52 ],
53 ],
54 'defaultLanguageDifferences' => [
55 'renderType' => 'defaultLanguageDifferences',
56 'after' => [
57 'otherLanguageContent',
58 ],
59 ],
60 ];
61
62 /**
63 * This will render a single-line input form field, possibly with various control/validation features
64 *
65 * @return array As defined in initializeResultArray() of AbstractNode
66 * @throws \RuntimeException with invalid configuration
67 */
68 public function render()
69 {
70 $languageService = $this->getLanguageService();
71
72 $table = $this->data['tableName'];
73 $fieldName = $this->data['fieldName'];
74 $row = $this->data['databaseRow'];
75 $parameterArray = $this->data['parameterArray'];
76 $resultArray = $this->initializeResultArray();
77 $config = $parameterArray['fieldConf']['config'];
78
79 $itemValue = $parameterArray['itemFormElValue'];
80 $defaultInputWidth = 10;
81 $evalList = GeneralUtility::trimExplode(',', $config['eval'], true);
82 $nullControlNameEscaped = htmlspecialchars('control[active][' . $table . '][' . $row['uid'] . '][' . $fieldName . ']');
83
84 if (in_array('date', $evalList, true)) {
85 $format = 'date';
86 $defaultInputWidth = 13;
87 } elseif (in_array('datetime', $evalList, true)) {
88 $format = 'datetime';
89 $defaultInputWidth = 13;
90 } elseif (in_array('time', $evalList, true)) {
91 $format = 'time';
92 } elseif (in_array('timesec', $evalList, true)) {
93 $format = 'timesec';
94 } else {
95 throw new \RuntimeException(
96 'Field "' . $fieldName . '" in table "' . $table . '" with renderType "inputDataTime" needs'
97 . '"eval" set to either "date", "datetime", "time" or "timesec"',
98 1483823746
99 );
100 }
101
102 $size = MathUtility::forceIntegerInRange($config['size'] ?? $defaultInputWidth, $this->minimumInputWidth, $this->maxInputWidth);
103 $width = (int)$this->formMaxWidth($size);
104
105 $fieldInformationResult = $this->renderFieldInformation();
106 $fieldInformationHtml = $fieldInformationResult['html'];
107 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
108
109 if (isset($config['readOnly']) && $config['readOnly']) {
110 // Early return for read only fields
111 $itemValue = $this->formatValue($format, $itemValue);
112 $html = [];
113 $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
114 $html[] = $fieldInformationHtml;
115 $html[] = '<div class="form-wizards-wrap">';
116 $html[] = '<div class="form-wizards-element">';
117 $html[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
118 $html[] = '<input class="form-control" value="' . htmlspecialchars($itemValue) . '" type="text" disabled>';
119 $html[] = '</div>';
120 $html[] = '</div>';
121 $html[] = '</div>';
122 $html[] = '</div>';
123 $resultArray['html'] = implode(LF, $html);
124 return $resultArray;
125 }
126
127 $attributes = [
128 'value' => '',
129 'id' => StringUtility::getUniqueId('formengine-input-'),
130 'class' => implode(' ', [
131 't3js-datetimepicker',
132 'form-control',
133 't3js-clearable',
134 'hasDefaultValue',
135 ]),
136 'data-date-type' => $format,
137 'data-formengine-validation-rules' => $this->getValidationDataAsJsonString($config),
138 'data-formengine-input-params' => json_encode([
139 'field' => $parameterArray['itemFormElName'],
140 'evalList' => implode(',', $evalList)
141 ]),
142 'data-formengine-input-name' => $parameterArray['itemFormElName'],
143 ];
144
145 $maxLength = $config['max'] ?? 0;
146 if ((int)$maxLength > 0) {
147 $attributes['maxlength'] = (int)$maxLength;
148 }
149 if (!empty($config['placeholder'])) {
150 $attributes['placeholder'] = trim($config['placeholder']);
151 }
152
153 if ($format === 'datetime' || $format === 'date') {
154 // This only handles integer timestamps; if the field is a SQL native date(time), it was already converted
155 // to an ISO-8601 date by the DatabaseRowDateTimeFields class. (those dates are stored as server local time)
156 if (MathUtility::canBeInterpretedAsInteger($itemValue) && $itemValue != 0) {
157 // We store UTC timestamps in the database.
158 // Convert the timestamp to a proper ISO-8601 date so we get rid of timezone issues on the client.
159 // Details: As the JS side is not capable of handling dates in the server's timezone
160 // (moment.js can only handle UTC or browser's local timezone), we need to offset the value
161 // to eliminate the timezone. JS will receive all dates as if they were UTC, which we undo on save in DataHandler
162 $adjustedValue = $itemValue + date('Z', (int)$itemValue);
163 // output date as a ISO-8601 date
164 $itemValue = gmdate('c', $adjustedValue);
165 }
166 if (isset($config['range']['lower'])) {
167 $attributes['data-date-min-date'] = (int)$config['range']['lower'] * 1000;
168 }
169 if (isset($config['range']['upper'])) {
170 $attributes['data-date-max-date'] = (int)$config['range']['upper'] * 1000;
171 }
172 }
173 if (($format === 'time' || $format === 'timesec') && MathUtility::canBeInterpretedAsInteger($itemValue) && $itemValue != 0) {
174 // time(sec) is stored as elapsed seconds in DB, hence we interpret it as UTC time on 1970-01-01
175 // and pass on the ISO format to JS.
176 $itemValue = gmdate('c', (int)$itemValue);
177 }
178
179 $fieldWizardResult = $this->renderFieldWizard();
180 $fieldWizardHtml = $fieldWizardResult['html'];
181 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
182
183 $fieldControlResult = $this->renderFieldControl();
184 $fieldControlHtml = $fieldControlResult['html'];
185 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldControlResult, false);
186
187 $expansionHtml = [];
188 $expansionHtml[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
189 $expansionHtml[] = '<div class="form-wizards-wrap">';
190 $expansionHtml[] = '<div class="form-wizards-element">';
191 $expansionHtml[] = '<div class="input-group">';
192 $expansionHtml[] = '<input type="text"' . GeneralUtility::implodeAttributes($attributes, true) . ' />';
193 $expansionHtml[] = '<input type="hidden" name="' . $parameterArray['itemFormElName'] . '" value="' . htmlspecialchars($itemValue) . '" />';
194 $expansionHtml[] = '<span class="input-group-btn">';
195 $expansionHtml[] = '<label class="btn btn-default" for="' . $attributes['id'] . '">';
196 $expansionHtml[] = $this->iconFactory->getIcon('actions-edit-pick-date', Icon::SIZE_SMALL)->render();
197 $expansionHtml[] = '</label>';
198 $expansionHtml[] = '</span>';
199 $expansionHtml[] = '</div>';
200 $expansionHtml[] = '</div>';
201 if (!empty($fieldControlHtml)) {
202 $expansionHtml[] = '<div class="form-wizards-items-aside">';
203 $expansionHtml[] = '<div class="btn-group">';
204 $expansionHtml[] = $fieldControlHtml;
205 $expansionHtml[] = '</div>';
206 $expansionHtml[] = '</div>';
207 }
208 if (!empty($fieldWizardHtml)) {
209 $expansionHtml[] = '<div class="form-wizards-items-bottom">';
210 $expansionHtml[] = $fieldWizardHtml;
211 $expansionHtml[] = '</div>';
212 }
213 $expansionHtml[] = '</div>';
214 $expansionHtml[] = '</div>';
215 $expansionHtml = implode(LF, $expansionHtml);
216
217 $fullElement = $expansionHtml;
218 if ($this->hasNullCheckboxButNoPlaceholder()) {
219 $checked = $itemValue !== null ? ' checked="checked"' : '';
220 $fullElement = [];
221 $fullElement[] = '<div class="t3-form-field-disable"></div>';
222 $fullElement[] = '<div class="checkbox t3-form-field-eval-null-checkbox">';
223 $fullElement[] = '<label for="' . $nullControlNameEscaped . '">';
224 $fullElement[] = '<input type="hidden" name="' . $nullControlNameEscaped . '" value="0" />';
225 $fullElement[] = '<input type="checkbox" name="' . $nullControlNameEscaped . '" id="' . $nullControlNameEscaped . '" value="1"' . $checked . ' />';
226 $fullElement[] = $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.nullCheckbox');
227 $fullElement[] = '</label>';
228 $fullElement[] = '</div>';
229 $fullElement[] = $expansionHtml;
230 $fullElement = implode(LF, $fullElement);
231 } elseif ($this->hasNullCheckboxWithPlaceholder()) {
232 $checked = $itemValue !== null ? ' checked="checked"' : '';
233 $placeholder = $shortenedPlaceholder = $config['placeholder'] ?? '';
234 $disabled = '';
235 $fallbackValue = 0;
236 if (strlen($placeholder) > 0) {
237 $shortenedPlaceholder = GeneralUtility::fixed_lgd_cs($placeholder, 20);
238 if ($placeholder !== $shortenedPlaceholder) {
239 $overrideLabel = sprintf(
240 $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
241 '<span title="' . htmlspecialchars($placeholder) . '">' . htmlspecialchars($shortenedPlaceholder) . '</span>'
242 );
243 } else {
244 $overrideLabel = sprintf(
245 $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
246 htmlspecialchars($placeholder)
247 );
248 }
249 } else {
250 $overrideLabel = $languageService->sL(
251 'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override_not_available'
252 );
253 }
254 $fullElement = [];
255 $fullElement[] = '<div class="checkbox t3js-form-field-eval-null-placeholder-checkbox">';
256 $fullElement[] = '<label for="' . $nullControlNameEscaped . '">';
257 $fullElement[] = '<input type="hidden" name="' . $nullControlNameEscaped . '" value="' . $fallbackValue . '" />';
258 $fullElement[] = '<input type="checkbox" name="' . $nullControlNameEscaped . '" id="' . $nullControlNameEscaped . '" value="1"' . $checked . $disabled . ' />';
259 $fullElement[] = $overrideLabel;
260 $fullElement[] = '</label>';
261 $fullElement[] = '</div>';
262 $fullElement[] = '<div class="t3js-formengine-placeholder-placeholder">';
263 $fullElement[] = '<div class="form-control-wrap" style="max-width:' . $width . 'px">';
264 $fullElement[] = '<input type="text" class="form-control" disabled="disabled" value="' . $shortenedPlaceholder . '" />';
265 $fullElement[] = '</div>';
266 $fullElement[] = '</div>';
267 $fullElement[] = '<div class="t3js-formengine-placeholder-formfield">';
268 $fullElement[] = $expansionHtml;
269 $fullElement[] = '</div>';
270 $fullElement = implode(LF, $fullElement);
271 }
272
273 $resultArray['html'] = '<div class="formengine-field-item t3js-formengine-field-item">' . $fieldInformationHtml . $fullElement . '</div>';
274 return $resultArray;
275 }
276
277 /**
278 * @return LanguageService
279 */
280 protected function getLanguageService(): LanguageService
281 {
282 return $GLOBALS['LANG'];
283 }
284 }