TYPO3 CMS  TYPO3_8-7
InputDateTimeElement.php
Go to the documentation of this file.
1 <?php
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 
22 
27 {
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 
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  $nullControlNameEscaped = 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  // This only handles integer timestamps; if the field is a SQL native date(time), it was already converted
139  // to an ISO-8601 date by the DatabaseRowDateTimeFields class. (those dates are stored as server local time)
140  if (MathUtility::canBeInterpretedAsInteger($itemValue) && $itemValue != 0) {
141  // We store UTC timestamps in the database.
142  // Convert the timestamp to a proper ISO-8601 date so we get rid of timezone issues on the client.
143  // Details: As the JS side is not capable of handling dates in the server's timezone
144  // (moment.js can only handle UTC or browser's local timezone), we need to offset the value
145  // to eliminate the timezone. JS will receive all dates as if they were UTC, which we undo on save in DataHandler
146  $adjustedValue = $itemValue + date('Z', (int)$itemValue);
147  // output date as a ISO-8601 date
148  $itemValue = gmdate('c', $adjustedValue);
149  }
150  if (isset($config['range']['lower'])) {
151  $attributes['data-date-min-date'] = (int)$config['range']['lower'] * 1000;
152  }
153  if (isset($config['range']['upper'])) {
154  $attributes['data-date-max-date'] = (int)$config['range']['upper'] * 1000;
155  }
156  }
157  if (($format === 'time' || $format === 'timesec') && MathUtility::canBeInterpretedAsInteger($itemValue) && $itemValue != 0) {
158  // time(sec) is stored as elapsed seconds in DB, hence we interpret it as UTC time on 1970-01-01
159  // and pass on the ISO format to JS.
160  $itemValue = gmdate('c', (int)$itemValue);
161  }
162 
163  $legacyWizards = $this->renderWizards();
164  $legacyFieldControlHtml = implode(LF, $legacyWizards['fieldControl']);
165  $legacyFieldWizardHtml = implode(LF, $legacyWizards['fieldWizard']);
166 
167  $fieldInformationResult = $this->renderFieldInformation();
168  $fieldInformationHtml = $fieldInformationResult['html'];
169  $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
170 
171  $fieldWizardResult = $this->renderFieldWizard();
172  $fieldWizardHtml = $legacyFieldWizardHtml . $fieldWizardResult['html'];
173  $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
174 
175  $fieldControlResult = $this->renderFieldControl();
176  $fieldControlHtml = $legacyFieldControlHtml . $fieldControlResult['html'];
177  $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldControlResult, false);
178 
179  $expansionHtml = [];
180  $expansionHtml[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
181  $expansionHtml[] = '<div class="form-wizards-wrap">';
182  $expansionHtml[] = '<div class="form-wizards-element">';
183  $expansionHtml[] = '<div class="input-group">';
184  $expansionHtml[] = '<input type="text"' . GeneralUtility::implodeAttributes($attributes, true) . ' />';
185  $expansionHtml[] = '<input type="hidden" name="' . $parameterArray['itemFormElName'] . '" value="' . htmlspecialchars($itemValue) . '" />';
186  $expansionHtml[] = '<span class="input-group-btn">';
187  $expansionHtml[] = '<label class="btn btn-default" for="' . $attributes['id'] . '">';
188  $expansionHtml[] = $this->iconFactory->getIcon('actions-edit-pick-date', Icon::SIZE_SMALL)->render();
189  $expansionHtml[] = '</label>';
190  $expansionHtml[] = '</span>';
191  $expansionHtml[] = '</div>';
192  $expansionHtml[] = '</div>';
193  $expansionHtml[] = '<div class="form-wizards-items-aside">';
194  $expansionHtml[] = '<div class="btn-group">';
195  $expansionHtml[] = $fieldControlHtml;
196  $expansionHtml[] = '</div>';
197  $expansionHtml[] = '</div>';
198  $expansionHtml[] = '<div class="form-wizards-items-bottom">';
199  $expansionHtml[] = $fieldWizardHtml;
200  $expansionHtml[] = '</div>';
201  $expansionHtml[] = '</div>';
202  $expansionHtml[] = '</div>';
203  $expansionHtml = implode(LF, $expansionHtml);
204 
205  $fullElement = $expansionHtml;
206  if ($this->hasNullCheckboxButNoPlaceholder()) {
207  $checked = $itemValue !== null ? ' checked="checked"' : '';
208  $fullElement = [];
209  $fullElement[] = '<div class="t3-form-field-disable"></div>';
210  $fullElement[] = '<div class="checkbox t3-form-field-eval-null-checkbox">';
211  $fullElement[] = '<label for="' . $nullControlNameEscaped . '">';
212  $fullElement[] = '<input type="hidden" name="' . $nullControlNameEscaped . '" value="0" />';
213  $fullElement[] = '<input type="checkbox" name="' . $nullControlNameEscaped . '" id="' . $nullControlNameEscaped . '" value="1"' . $checked . ' />';
214  $fullElement[] = $languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.nullCheckbox');
215  $fullElement[] = '</label>';
216  $fullElement[] = '</div>';
217  $fullElement[] = $expansionHtml;
218  $fullElement = implode(LF, $fullElement);
219  } elseif ($this->hasNullCheckboxWithPlaceholder()) {
220  $checked = $itemValue !== null ? ' checked="checked"' : '';
221  $placeholder = $shortenedPlaceholder = $config['placeholder'] ?? '';
222  $disabled = '';
223  $fallbackValue = 0;
224  if (strlen($placeholder) > 0) {
225  $shortenedPlaceholder = GeneralUtility::fixed_lgd_cs($placeholder, 20);
226  if ($placeholder !== $shortenedPlaceholder) {
227  $overrideLabel = sprintf(
228  $languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
229  '<span title="' . htmlspecialchars($placeholder) . '">' . htmlspecialchars($shortenedPlaceholder) . '</span>'
230  );
231  } else {
232  $overrideLabel = sprintf(
233  $languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
234  htmlspecialchars($placeholder)
235  );
236  }
237  } else {
238  $overrideLabel = $languageService->sL(
239  'LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override_not_available'
240  );
241  }
242  $fullElement = [];
243  $fullElement[] = '<div class="checkbox t3js-form-field-eval-null-placeholder-checkbox">';
244  $fullElement[] = '<label for="' . $nullControlNameEscaped . '">';
245  $fullElement[] = '<input type="hidden" name="' . $nullControlNameEscaped . '" value="' . $fallbackValue . '" />';
246  $fullElement[] = '<input type="checkbox" name="' . $nullControlNameEscaped . '" id="' . $nullControlNameEscaped . '" value="1"' . $checked . $disabled . ' />';
247  $fullElement[] = $overrideLabel;
248  $fullElement[] = '</label>';
249  $fullElement[] = '</div>';
250  $fullElement[] = '<div class="t3js-formengine-placeholder-placeholder">';
251  $fullElement[] = '<div class="form-control-wrap" style="max-width:' . $width . 'px">';
252  $fullElement[] = '<input type="text" class="form-control" disabled="disabled" value="' . $shortenedPlaceholder . '" />';
253  $fullElement[] = '</div>';
254  $fullElement[] = '</div>';
255  $fullElement[] = '<div class="t3js-formengine-placeholder-formfield">';
256  $fullElement[] = $expansionHtml;
257  $fullElement[] = '</div>';
258  $fullElement = implode(LF, $fullElement);
259  }
260 
261  $resultArray['html'] = '<div class="formengine-field-item t3js-formengine-field-item">' . $fieldInformationHtml . $fullElement . '</div>';
262  return $resultArray;
263  }
264 
268  protected function getLanguageService(): LanguageService
269  {
270  return $GLOBALS['LANG'];
271  }
272 }
static implodeAttributes(array $arr, $xhtmlSafe=false, $dontOmitBlankAttribs=false)
static forceIntegerInRange($theInt, $min, $max=2000000000, $defaultValue=0)
Definition: MathUtility.php:31
renderWizards( $itemKinds=null, $wizConf=null, $table=null, $row=null, $fieldName=null, $PA=null, $itemName=null, $specConf=null, $RTE=null)
static trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
static fixed_lgd_cs($string, $chars, $appendString='...')
mergeChildReturnIntoExistingResult(array $existing, array $childReturn, bool $mergeHtml=true)
formatValue($format, $itemValue, $formatOptions=[])
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']