[FEATURE] EXT:form - integrate new form framework
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / ViewHelpers / Form / TimePickerViewHelper.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 is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Extbase\Property\PropertyMapper;
19 use TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper;
20
21 /**
22 * Displays two select-boxes for hour and minute selection.
23 *
24 * Scope: frontend
25 * @api
26 */
27 class TimePickerViewHelper extends AbstractFormFieldViewHelper
28 {
29
30 /**
31 * @var string
32 */
33 protected $tagName = 'select';
34
35 /**
36 * @var \TYPO3\CMS\Extbase\Property\PropertyMapper
37 */
38 protected $propertyMapper;
39
40 /**
41 * @param \TYPO3\CMS\Extbase\Property\PropertyMapper $propertyMapper
42 * @return void
43 * @internal
44 */
45 public function injectPropertyMapper(\TYPO3\CMS\Extbase\Property\PropertyMapper $propertyMapper)
46 {
47 $this->propertyMapper = $propertyMapper;
48 }
49
50 /**
51 * Initialize the arguments.
52 *
53 * @return void
54 * @internal
55 */
56 public function initializeArguments()
57 {
58 parent::initializeArguments();
59 $this->registerTagAttribute('size', 'int', 'The size of the select field');
60 $this->registerTagAttribute('placeholder', 'string', 'Specifies a short hint that describes the expected value of an input element');
61 $this->registerTagAttribute('disabled', 'string', 'Specifies that the select element should be disabled when the page loads');
62 $this->registerArgument('errorClass', 'string', 'CSS class to set if there are errors for this view helper', false, 'f3-form-error');
63 $this->registerArgument('initialDate', 'string', 'Initial time (@see http://www.php.net/manual/en/datetime.formats.php for supported formats)');
64 $this->registerUniversalTagAttributes();
65 }
66
67 /**
68 * Renders the select fields for hour & minute
69 *
70 * @return string
71 * @api
72 */
73 public function render()
74 {
75 $name = $this->getName();
76 $this->registerFieldNameForFormTokenGeneration($name);
77
78 $this->tag->addAttribute('name', $name . '[hour]');
79 $date = $this->getSelectedDate();
80 $this->setErrorClassAttribute();
81
82 $content = '';
83 $content .= $this->buildHourSelector($date);
84 $content .= $this->buildMinuteSelector($date);
85 return $content;
86 }
87
88 /**
89 * @return \DateTime
90 */
91 protected function getSelectedDate(): \DateTime
92 {
93 $fluidFormRenderer = $this->viewHelperVariableContainer->getView();
94 $formRuntime = $fluidFormRenderer->getFormRuntime();
95
96 $date = $formRuntime[$this->arguments['property']];
97 if ($date instanceof \DateTime) {
98 return $date;
99 }
100 if ($date !== null) {
101 $date = $this->propertyMapper->convert($date, 'DateTime');
102 if (!$date instanceof \DateTime) {
103 return null;
104 }
105 return $date;
106 }
107 if ($this->hasArgument('initialDate')) {
108 return new \DateTime($this->arguments['initialDate']);
109 }
110 }
111
112 /**
113 * @param \DateTime $date
114 * @return string
115 */
116 protected function buildHourSelector(\DateTime $date = null): string
117 {
118 $value = $date !== null ? $date->format('H') : null;
119 $hourSelector = clone $this->tag;
120 $hourSelector->addAttribute('name', sprintf('%s[hour]', $this->getName()));
121 $options = '';
122 foreach (range(0, 23) as $hour) {
123 $hour = str_pad($hour, 2, '0', STR_PAD_LEFT);
124 $selected = $hour === $value ? ' selected="selected"' : '';
125 $options .= '<option value="' . $hour . '"' . $selected . '>' . $hour . '</option>';
126 }
127 $hourSelector->setContent($options);
128 return $hourSelector->render();
129 }
130
131 /**
132 * @param \DateTime $date
133 * @return string
134 */
135 protected function buildMinuteSelector(\DateTime $date = null): string
136 {
137 $value = $date !== null ? $date->format('i') : null;
138 $minuteSelector = clone $this->tag;
139 if ($this->hasArgument('id')) {
140 $minuteSelector->addAttribute('id', $this->arguments['id'] . '-minute');
141 }
142 $minuteSelector->addAttribute('name', sprintf('%s[minute]', $this->getName()));
143 $options = '';
144 foreach (range(0, 59) as $minute) {
145 $minute = str_pad($minute, 2, '0', STR_PAD_LEFT);
146 $selected = $minute === $value ? ' selected="selected"' : '';
147 $options .= '<option value="' . $minute . '"' . $selected . '>' . $minute . '</option>';
148 }
149 $minuteSelector->setContent($options);
150 return $minuteSelector->render();
151 }
152 }