‪TYPO3CMS  10.4
InputDateTimeElement.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
23 
28 {
34  protected ‪$defaultFieldInformation = [
35  'tcaDescription' => [
36  'renderType' => 'tcaDescription',
37  ],
38  ];
39 
45  protected ‪$defaultFieldWizard = [
46  'localizationStateSelector' => [
47  'renderType' => 'localizationStateSelector',
48  ],
49  'otherLanguageContent' => [
50  'renderType' => 'otherLanguageContent',
51  'after' => [
52  'localizationStateSelector'
53  ],
54  ],
55  'defaultLanguageDifferences' => [
56  'renderType' => 'defaultLanguageDifferences',
57  'after' => [
58  'otherLanguageContent',
59  ],
60  ],
61  ];
62 
69  public function ‪render()
70  {
71  $languageService = $this->‪getLanguageService();
72 
73  $table = $this->data['tableName'];
74  $fieldName = $this->data['fieldName'];
75  $row = $this->data['databaseRow'];
76  $parameterArray = $this->data['parameterArray'];
77  $resultArray = $this->‪initializeResultArray();
78  $config = $parameterArray['fieldConf']['config'];
79 
80  $itemValue = $parameterArray['itemFormElValue'];
82  $evalList = ‪GeneralUtility::trimExplode(',', $config['eval'], true);
83  $nullControlNameEscaped = htmlspecialchars('control[active][' . $table . '][' . $row['uid'] . '][' . $fieldName . ']');
84 
85  if (in_array('date', $evalList, true)) {
86  $format = 'date';
88  } elseif (in_array('datetime', $evalList, true)) {
89  $format = 'datetime';
91  } elseif (in_array('time', $evalList, true)) {
92  $format = 'time';
93  } elseif (in_array('timesec', $evalList, true)) {
94  $format = 'timesec';
95  } else {
96  throw new \RuntimeException(
97  'Field "' . $fieldName . '" in table "' . $table . '" with renderType "inputDataTime" needs'
98  . '"eval" set to either "date", "datetime", "time" or "timesec"',
99  1483823746
100  );
101  }
102 
103  $size = ‪MathUtility::forceIntegerInRange($config['size'] ?? ‪$defaultInputWidth, $this->minimumInputWidth, $this->maxInputWidth);
104  $width = (int)$this->‪formMaxWidth($size);
105 
106  $fieldInformationResult = $this->‪renderFieldInformation();
107  $fieldInformationHtml = $fieldInformationResult['html'];
108  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
109 
110  // Early return for read only fields
111  if (isset($config['readOnly']) && $config['readOnly']) {
112  // Ensure dbType values (see DatabaseRowDateTimeFields) are converted to a UNIX timestamp before rendering read-only
113  if (!empty($itemValue) && !‪MathUtility::canBeInterpretedAsInteger($itemValue)) {
114  $itemValue = (new \DateTime($itemValue))->getTimestamp();
115  }
116  // Format the unix-timestamp to the defined format (date/year etc)
117  $itemValue = $this->‪formatValue($format, $itemValue);
118  $html = [];
119  $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
120  $html[] = $fieldInformationHtml;
121  $html[] = '<div class="form-wizards-wrap">';
122  $html[] = '<div class="form-wizards-element">';
123  $html[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
124  $html[] = '<input class="form-control" value="' . htmlspecialchars($itemValue) . '" type="text" disabled>';
125  $html[] = '</div>';
126  $html[] = '</div>';
127  $html[] = '</div>';
128  $html[] = '</div>';
129  $resultArray['html'] = implode(LF, $html);
130  return $resultArray;
131  }
132 
133  $fieldId = ‪StringUtility::getUniqueId('formengine-input-');
134  $attributes = [
135  'value' => '',
136  'id' => $fieldId,
137  'class' => implode(' ', [
138  't3js-datetimepicker',
139  'form-control',
140  't3js-clearable',
141  'hasDefaultValue',
142  ]),
143  'data-date-type' => $format,
144  'data-formengine-validation-rules' => $this->‪getValidationDataAsJsonString($config),
145  'data-formengine-input-params' => (string)json_encode([
146  'field' => $parameterArray['itemFormElName'],
147  'evalList' => implode(',', $evalList)
148  ]),
149  'data-formengine-input-name' => $parameterArray['itemFormElName'],
150  ];
151 
152  $maxLength = $config['max'] ?? 0;
153  if ((int)$maxLength > 0) {
154  $attributes['maxlength'] = (string)(int)$maxLength;
155  }
156  if (!empty($config['placeholder'])) {
157  $attributes['placeholder'] = trim($config['placeholder']);
158  }
159 
160  if ($format === 'datetime' || $format === 'date') {
161  // This only handles integer timestamps; if the field is a SQL native date(time), it was already converted
162  // to an ISO-8601 date by the DatabaseRowDateTimeFields class. (those dates are stored as server local time)
163  if (‪MathUtility::canBeInterpretedAsInteger($itemValue) && $itemValue != 0) {
164  // We store UTC timestamps in the database.
165  // Convert the timestamp to a proper ISO-8601 date so we get rid of timezone issues on the client.
166  // Details: As the JS side is not capable of handling dates in the server's timezone
167  // (moment.js can only handle UTC or browser's local timezone), we need to offset the value
168  // to eliminate the timezone. JS will receive all dates as if they were UTC, which we undo on save in DataHandler
169  $adjustedValue = $itemValue + date('Z', (int)$itemValue);
170  // output date as an ISO-8601 date
171  $itemValue = gmdate('c', $adjustedValue);
172  }
173  if (isset($config['range']['lower'])) {
174  $attributes['data-date-min-date'] = (string)((int)$config['range']['lower'] * 1000);
175  }
176  if (isset($config['range']['upper'])) {
177  $attributes['data-date-max-date'] = (string)((int)$config['range']['upper'] * 1000);
178  }
179  }
180  if (($format === 'time' || $format === 'timesec') && ‪MathUtility::canBeInterpretedAsInteger($itemValue) && $itemValue != 0) {
181  // time(sec) is stored as elapsed seconds in DB, hence we interpret it as UTC time on 1970-01-01
182  // and pass on the ISO format to JS.
183  $itemValue = gmdate('c', (int)$itemValue);
184  }
185 
186  $fieldWizardResult = $this->‪renderFieldWizard();
187  $fieldWizardHtml = $fieldWizardResult['html'];
188  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
189 
190  $fieldControlResult = $this->‪renderFieldControl();
191  $fieldControlHtml = $fieldControlResult['html'];
192  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldControlResult, false);
193 
194  $expansionHtml = [];
195  $expansionHtml[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
196  $expansionHtml[] = '<div class="form-wizards-wrap">';
197  $expansionHtml[] = '<div class="form-wizards-element">';
198  $expansionHtml[] = '<div class="input-group">';
199  $expansionHtml[] = '<input type="text" ' . GeneralUtility::implodeAttributes($attributes, true) . ' />';
200  $expansionHtml[] = '<input type="hidden" name="' . $parameterArray['itemFormElName'] . '" value="' . htmlspecialchars($itemValue) . '" />';
201  $expansionHtml[] = '<span class="input-group-btn">';
202  $expansionHtml[] = '<label class="btn btn-default" for="' . $attributes['id'] . '">';
203  $expansionHtml[] = $this->iconFactory->getIcon('actions-edit-pick-date', ‪Icon::SIZE_SMALL)->render();
204  $expansionHtml[] = '</label>';
205  $expansionHtml[] = '</span>';
206  $expansionHtml[] = '</div>';
207  $expansionHtml[] = '</div>';
208  if (!empty($fieldControlHtml)) {
209  $expansionHtml[] = '<div class="form-wizards-items-aside">';
210  $expansionHtml[] = '<div class="btn-group">';
211  $expansionHtml[] = $fieldControlHtml;
212  $expansionHtml[] = '</div>';
213  $expansionHtml[] = '</div>';
214  }
215  if (!empty($fieldWizardHtml)) {
216  $expansionHtml[] = '<div class="form-wizards-items-bottom">';
217  $expansionHtml[] = $fieldWizardHtml;
218  $expansionHtml[] = '</div>';
219  }
220  $expansionHtml[] = '</div>';
221  $expansionHtml[] = '</div>';
222  $expansionHtml = implode(LF, $expansionHtml);
223 
224  $fullElement = $expansionHtml;
225  if ($this->‪hasNullCheckboxButNoPlaceholder()) {
226  $checked = $itemValue !== null ? ' checked="checked"' : '';
227  $fullElement = [];
228  $fullElement[] = '<div class="t3-form-field-disable"></div>';
229  $fullElement[] = '<div class="checkbox t3-form-field-eval-null-checkbox">';
230  $fullElement[] = '<label for="' . $nullControlNameEscaped . '">';
231  $fullElement[] = '<input type="hidden" name="' . $nullControlNameEscaped . '" value="0" />';
232  $fullElement[] = '<input type="checkbox" name="' . $nullControlNameEscaped . '" id="' . $nullControlNameEscaped . '" value="1"' . $checked . ' />';
233  $fullElement[] = $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.nullCheckbox');
234  $fullElement[] = '</label>';
235  $fullElement[] = '</div>';
236  $fullElement[] = $expansionHtml;
237  $fullElement = implode(LF, $fullElement);
238  } elseif ($this->‪hasNullCheckboxWithPlaceholder()) {
239  $checked = $itemValue !== null ? ' checked="checked"' : '';
240  $placeholder = $shortenedPlaceholder = $config['placeholder'] ?? '';
241  $disabled = '';
242  $fallbackValue = 0;
243  if (strlen($placeholder) > 0) {
244  $shortenedPlaceholder = GeneralUtility::fixed_lgd_cs($placeholder, 20);
245  if ($placeholder !== $shortenedPlaceholder) {
246  $overrideLabel = sprintf(
247  $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
248  '<span title="' . htmlspecialchars($placeholder) . '">' . htmlspecialchars($shortenedPlaceholder) . '</span>'
249  );
250  } else {
251  $overrideLabel = sprintf(
252  $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
253  htmlspecialchars($placeholder)
254  );
255  }
256  } else {
257  $overrideLabel = $languageService->sL(
258  'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override_not_available'
259  );
260  }
261  $fullElement = [];
262  $fullElement[] = '<div class="checkbox t3js-form-field-eval-null-placeholder-checkbox">';
263  $fullElement[] = '<label for="' . $nullControlNameEscaped . '">';
264  $fullElement[] = '<input type="hidden" name="' . $nullControlNameEscaped . '" value="' . $fallbackValue . '" />';
265  $fullElement[] = '<input type="checkbox" name="' . $nullControlNameEscaped . '" id="' . $nullControlNameEscaped . '" value="1"' . $checked . $disabled . ' />';
266  $fullElement[] = $overrideLabel;
267  $fullElement[] = '</label>';
268  $fullElement[] = '</div>';
269  $fullElement[] = '<div class="t3js-formengine-placeholder-placeholder">';
270  $fullElement[] = '<div class="form-control-wrap" style="max-width:' . $width . 'px">';
271  $fullElement[] = '<input type="text" class="form-control" disabled="disabled" value="' . htmlspecialchars($shortenedPlaceholder) . '" />';
272  $fullElement[] = '</div>';
273  $fullElement[] = '</div>';
274  $fullElement[] = '<div class="t3js-formengine-placeholder-formfield">';
275  $fullElement[] = $expansionHtml;
276  $fullElement[] = '</div>';
277  $fullElement = implode(LF, $fullElement);
278  }
279 
280  $resultArray['requireJsModules'][] = ['TYPO3/CMS/Backend/FormEngine/Element/InputDateTimeElement' => '
281  function(InputDateTimeElement) {
282  new InputDateTimeElement(' . GeneralUtility::quoteJSvalue($fieldId) . ');
283  }'
284  ];
285  $resultArray['html'] = '<div class="formengine-field-item t3js-formengine-field-item">' . $fieldInformationHtml . $fullElement . '</div>';
286  return $resultArray;
287  }
288 
292  protected function ‪getLanguageService(): ‪LanguageService
293  {
294  return ‪$GLOBALS['LANG'];
295  }
296 }
‪TYPO3\CMS\Core\Imaging\Icon\SIZE_SMALL
‪const SIZE_SMALL
Definition: Icon.php:30
‪TYPO3\CMS\Backend\Form\Element\InputDateTimeElement\render
‪array render()
Definition: InputDateTimeElement.php:67
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldInformation
‪array renderFieldInformation()
Definition: AbstractFormElement.php:72
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger($var)
Definition: MathUtility.php:74
‪TYPO3\CMS\Backend\Form\Element\InputDateTimeElement\$defaultFieldInformation
‪array $defaultFieldInformation
Definition: InputDateTimeElement.php:33
‪TYPO3\CMS\Backend\Form\AbstractNode\mergeChildReturnIntoExistingResult
‪array mergeChildReturnIntoExistingResult(array $existing, array $childReturn, bool $mergeHtml=true)
Definition: AbstractNode.php:116
‪TYPO3\CMS\Backend\Form\AbstractNode\initializeResultArray
‪array initializeResultArray()
Definition: AbstractNode.php:90
‪TYPO3\CMS\Core\Imaging\Icon
Definition: Icon.php:26
‪TYPO3\CMS\Core\Utility\MathUtility\forceIntegerInRange
‪static int forceIntegerInRange($theInt, $min, $max=2000000000, $defaultValue=0)
Definition: MathUtility.php:32
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement
Definition: AbstractFormElement.php:32
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\$defaultInputWidth
‪int $defaultInputWidth
Definition: AbstractFormElement.php:37
‪TYPO3\CMS\Backend\Form\Element
Definition: AbstractFormElement.php:16
‪TYPO3\CMS\Backend\Form\Element\InputDateTimeElement\getLanguageService
‪LanguageService getLanguageService()
Definition: InputDateTimeElement.php:290
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\formatValue
‪string formatValue($format, $itemValue, $formatOptions=[])
Definition: AbstractFormElement.php:190
‪TYPO3\CMS\Backend\Form\Element\InputDateTimeElement
Definition: InputDateTimeElement.php:28
‪TYPO3\CMS\Backend\Form\AbstractNode\getValidationDataAsJsonString
‪string getValidationDataAsJsonString(array $config)
Definition: AbstractNode.php:151
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldControl
‪array renderFieldControl()
Definition: AbstractFormElement.php:88
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\hasNullCheckboxWithPlaceholder
‪bool hasNullCheckboxWithPlaceholder()
Definition: AbstractFormElement.php:167
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\hasNullCheckboxButNoPlaceholder
‪bool hasNullCheckboxButNoPlaceholder()
Definition: AbstractFormElement.php:132
‪TYPO3\CMS\Backend\Form\Element\InputDateTimeElement\$defaultFieldWizard
‪array $defaultFieldWizard
Definition: InputDateTimeElement.php:43
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\formMaxWidth
‪int formMaxWidth($size=48)
Definition: AbstractFormElement.php:298
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static string[] trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:1059
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:92
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:22
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldWizard
‪array renderFieldWizard()
Definition: AbstractFormElement.php:104