[BUGFIX] EXT:form - fix DatePicker html output
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / ViewHelpers / Form / DatePickerViewHelper.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Form\ViewHelpers\Form;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It originated from the Neos.Form package (www.neos.io)
9 *
10 * It is free software; you can redistribute it and/or modify it under
11 * the terms of the GNU General Public License, either version 2
12 * of the License, or any later version.
13 *
14 * For the full copyright and license information, please read the
15 * LICENSE.txt file that was distributed with this source code.
16 *
17 * The TYPO3 project - inspiring people to share!
18 */
19
20 use TYPO3\CMS\Core\Page\PageRenderer;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\CMS\Extbase\Property\PropertyMapper;
23 use TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper;
24 use TYPO3\CMS\Form\ViewHelpers\RenderRenderableViewHelper;
25
26 /**
27 * Display a jQuery date picker.
28 *
29 * Note: Requires jQuery UI to be included on the page.
30 *
31 * Scope: frontend
32 * @api
33 */
34 class DatePickerViewHelper extends AbstractFormFieldViewHelper
35 {
36
37 /**
38 * @var string
39 */
40 protected $tagName = 'input';
41
42 /**
43 * @var \TYPO3\CMS\Extbase\Property\PropertyMapper
44 */
45 protected $propertyMapper;
46
47 /**
48 * @param \TYPO3\CMS\Extbase\Property\PropertyMapper $propertyMapper
49 * @internal
50 */
51 public function injectPropertyMapper(\TYPO3\CMS\Extbase\Property\PropertyMapper $propertyMapper)
52 {
53 $this->propertyMapper = $propertyMapper;
54 }
55
56 /**
57 * Initialize the arguments.
58 *
59 * @api
60 */
61 public function initializeArguments()
62 {
63 parent::initializeArguments();
64 $this->registerTagAttribute('size', 'int', 'The size of the input field');
65 $this->registerTagAttribute('placeholder', 'string', 'Specifies a short hint that describes the expected value of an input element');
66 $this->registerArgument('errorClass', 'string', 'CSS class to set if there are errors for this view helper', false, 'f3-form-error');
67 $this->registerArgument('initialDate', 'string', 'Initial date (@see http://www.php.net/manual/en/datetime.formats.php for supported formats)');
68 $this->registerArgument('enableDatePicker', 'bool', 'Enable the Datepicker', false, true);
69 $this->registerArgument('previewMode', 'bool', 'Preview mde flag', true, false);
70 $this->registerArgument('dateFormat', 'string', 'The date format', false, 'Y-m-d');
71 $this->registerUniversalTagAttributes();
72 }
73
74 /**
75 * Renders the text field, hidden field and required javascript
76 *
77 * @return string
78 * @api
79 */
80 public function render()
81 {
82 $enableDatePicker = $this->arguments['enableDatePicker'];
83 $dateFormat = $this->arguments['dateFormat'];
84 $previewMode = (bool)$this->arguments['previewMode'];
85 $placeholder = $this->arguments['placeholder'];
86
87 $name = $this->getName();
88 $this->registerFieldNameForFormTokenGeneration($name);
89
90 $this->tag->addAttribute('type', 'text');
91 $this->tag->addAttribute('name', $name . '[date]');
92
93 if ($this->hasArgument('id')) {
94 $id = $this->arguments['id'];
95 } else {
96 $id = 'field' . md5(uniqid());
97 }
98
99 if (empty($placeholder)) {
100 $this->tag->addAttribute('placeholder', $dateFormat);
101 }
102
103 if ($enableDatePicker) {
104 $this->tag->addAttribute('readonly', 'readonly');
105 if (!$previewMode) {
106 $datePickerDateFormat = $this->convertDateFormatToDatePickerFormat($dateFormat);
107 $this->renderInlineJavascript($id, $datePickerDateFormat);
108 }
109 }
110 $date = $this->getSelectedDate();
111 if ($date !== null) {
112 $this->tag->addAttribute('value', $date->format($dateFormat));
113 }
114
115 $this->tag->addAttribute('id', $id);
116
117 $this->setErrorClassAttribute();
118 $content = '';
119 $content .= $this->tag->render();
120 $content .= '<input type="hidden" name="' . $name . '[dateFormat]" value="' . htmlspecialchars($dateFormat) . '" />';
121
122 return $content;
123 }
124
125 /**
126 * @return null|\DateTime
127 */
128 protected function getSelectedDate()
129 {
130 /** @var FormRuntime $formRuntime */
131 $formRuntime = $this->renderingContext
132 ->getViewHelperVariableContainer()
133 ->get(RenderRenderableViewHelper::class, 'formRuntime');
134
135 $formState = $formRuntime->getFormState();
136
137 $date = $formRuntime[$this->arguments['property']];
138 if ($date instanceof \DateTime) {
139 return $date;
140 }
141 if ($date !== null) {
142 $date = $this->propertyMapper->convert($date, 'DateTime');
143 if (!$date instanceof \DateTime) {
144 return null;
145 }
146 return $date;
147 }
148 if ($this->hasArgument('initialDate')) {
149 return new \DateTime($this->arguments['initialDate']);
150 }
151 }
152
153 /**
154 * @param string $dateFormat
155 * @return string
156 */
157 protected function convertDateFormatToDatePickerFormat(string $dateFormat): string
158 {
159 $replacements = [
160 'd' => 'dd',
161 'D' => 'D',
162 'j' => 'o',
163 'l' => 'DD',
164
165 'F' => 'MM',
166 'm' => 'mm',
167 'M' => 'M',
168 'n' => 'm',
169
170 'Y' => 'yy',
171 'y' => 'y'
172 ];
173 return strtr($dateFormat, $replacements);
174 }
175
176 /**
177 * @param string $uniqueIdentifier
178 * @param string $datePickerDateFormat
179 */
180 protected function renderInlineJavascript(string $uniqueIdentifier, string $datePickerDateFormat)
181 {
182 $this->getPageRenderer()->addJsFooterInlineCode(
183 'ext_form_datepicker-' . $uniqueIdentifier,
184 'if ("undefined" !== typeof $) {
185 $(function() {
186 $("#' . $uniqueIdentifier . '").datepicker({
187 dateFormat: "' . $datePickerDateFormat . '"
188 }).on("keydown", function(e) {
189 // By using "backspace" or "delete", you can clear the datepicker again.
190 if(e.keyCode == 8 || e.keyCode == 46) {
191 e.preventDefault();
192 $.datepicker._clearDate(this);
193 }
194 });
195 });
196 }
197 ');
198 }
199
200 /**
201 * @return PageRenderer
202 */
203 protected function getPageRenderer(): PageRenderer
204 {
205 return GeneralUtility::makeInstance(PageRenderer::class);
206 }
207 }