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