‪TYPO3CMS  10.4
TimePickerViewHelper.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
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 /*
19  * Inspired by and partially taken from the Neos.Form package (www.neos.io)
20  */
21 
23 
28 
35 {
36 
40  protected ‪$tagName = 'select';
41 
45  protected ‪$propertyMapper;
46 
52  {
53  $this->propertyMapper = ‪$propertyMapper;
54  }
55 
61  public function ‪initializeArguments()
62  {
63  parent::initializeArguments();
64  $this->registerTagAttribute('size', 'int', 'The size of the select field');
65  $this->registerTagAttribute('placeholder', 'string', 'Specifies a short hint that describes the expected value of an input element');
66  $this->registerTagAttribute('disabled', 'string', 'Specifies that the select element should be disabled when the page loads');
67  $this->registerArgument('errorClass', 'string', 'CSS class to set if there are errors for this ViewHelper', false, 'f3-form-error');
68  $this->registerArgument('initialDate', 'string', 'Initial time (@see http://www.php.net/manual/en/datetime.formats.php for supported formats)');
69  $this->registerArgument('timeType', 'string', '"hour" or "minute"');
70  $this->registerUniversalTagAttributes();
71  }
72 
78  public function ‪render()
79  {
80  $name = $this->‪getName();
82  $this->tag->addAttribute('name', $name . '[hour]');
83 
84  $date = $this->‪getSelectedDate();
86 
87  $content = '';
88 
89  if ($this->arguments['timeType'] === 'hour') {
90  $content .= $this->‪buildHourSelector($date);
91  } else {
92  $content .= $this->‪buildMinuteSelector($date);
93  }
94 
95  return $content;
96  }
97 
101  protected function ‪getSelectedDate()
102  {
104  $formRuntime = $this->renderingContext
105  ->getViewHelperVariableContainer()
106  ->get(RenderRenderableViewHelper::class, 'formRuntime');
107 
108  $date = $formRuntime[$this->arguments['property']];
109  if ($date instanceof \DateTime) {
110  return $date;
111  }
112  if ($date !== null) {
113  $date = $this->propertyMapper->convert($date, \DateTime::class);
114  if (!$date instanceof \DateTime) {
115  return null;
116  }
117  return $date;
118  }
119  if ($this->hasArgument('initialDate')) {
120  return new \DateTime($this->arguments['initialDate']);
121  }
122 
123  return null;
124  }
125 
130  protected function ‪buildHourSelector(\DateTime $date = null): string
131  {
132  $value = $date !== null ? $date->format('H') : null;
133  $hourSelector = clone $this->tag;
134  $hourSelector->addAttribute('name', sprintf('%s[hour]', $this->‪getName()));
135  $options = '';
136  foreach (range(0, 23) as $hour) {
137  $hour = str_pad((string)$hour, 2, '0', STR_PAD_LEFT);
138  $selected = $hour === $value ? ' selected="selected"' : '';
139  $options .= '<option value="' . htmlspecialchars($hour) . '" ' . $selected . '>' . htmlspecialchars($hour) . '</option>';
140  }
141  $hourSelector->setContent($options);
142  return $hourSelector->render();
143  }
144 
149  protected function ‪buildMinuteSelector(\DateTime $date = null): string
150  {
151  $value = $date !== null ? $date->format('i') : null;
152  $minuteSelector = clone $this->tag;
153  if ($this->hasArgument('id')) {
154  $minuteSelector->addAttribute('id', $this->arguments['id'] . '-minute');
155  }
156  $minuteSelector->addAttribute('name', sprintf('%s[minute]', $this->‪getName()));
157  $options = '';
158  foreach (range(0, 59) as $minute) {
159  $minute = str_pad((string)$minute, 2, '0', STR_PAD_LEFT);
160  $selected = $minute === $value ? ' selected="selected"' : '';
161  $options .= '<option value="' . htmlspecialchars($minute) . '"' . $selected . '>' . htmlspecialchars($minute) . '</option>';
162  }
163  $minuteSelector->setContent($options);
164  return $minuteSelector->render();
165  }
166 }
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\setErrorClassAttribute
‪setErrorClassAttribute()
Definition: AbstractFormFieldViewHelper.php:331
‪TYPO3\CMS\Form\ViewHelpers\Form\TimePickerViewHelper\injectPropertyMapper
‪injectPropertyMapper(PropertyMapper $propertyMapper)
Definition: TimePickerViewHelper.php:49
‪TYPO3\CMS\Form\ViewHelpers\Form\TimePickerViewHelper
Definition: TimePickerViewHelper.php:35
‪TYPO3\CMS\Form\ViewHelpers\Form\TimePickerViewHelper\getSelectedDate
‪DateTime null getSelectedDate()
Definition: TimePickerViewHelper.php:99
‪TYPO3\CMS\Form\ViewHelpers\Form\TimePickerViewHelper\$tagName
‪string $tagName
Definition: TimePickerViewHelper.php:39
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormViewHelper\registerFieldNameForFormTokenGeneration
‪registerFieldNameForFormTokenGeneration($fieldName)
Definition: AbstractFormViewHelper.php:106
‪TYPO3\CMS\Form\ViewHelpers\Form\TimePickerViewHelper\$propertyMapper
‪TYPO3 CMS Extbase Property PropertyMapper $propertyMapper
Definition: TimePickerViewHelper.php:43
‪TYPO3\CMS\Form\ViewHelpers\Form\TimePickerViewHelper\initializeArguments
‪initializeArguments()
Definition: TimePickerViewHelper.php:59
‪TYPO3\CMS\Form\ViewHelpers\RenderRenderableViewHelper
Definition: RenderRenderableViewHelper.php:41
‪TYPO3\CMS\Extbase\Property\PropertyMapper
Definition: PropertyMapper.php:37
‪TYPO3\CMS\Form\ViewHelpers\Form
Definition: DatePickerViewHelper.php:22
‪TYPO3\CMS\Form\Domain\Runtime\FormRuntime
Definition: FormSession.php:18
‪TYPO3\CMS\Form\ViewHelpers\Form\TimePickerViewHelper\buildMinuteSelector
‪string buildMinuteSelector(\DateTime $date=null)
Definition: TimePickerViewHelper.php:147
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper
Definition: AbstractFormFieldViewHelper.php:30
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\getName
‪string getName()
Definition: AbstractFormFieldViewHelper.php:90
‪TYPO3\CMS\Form\ViewHelpers\Form\TimePickerViewHelper\render
‪string render()
Definition: TimePickerViewHelper.php:76
‪TYPO3\CMS\Form\ViewHelpers\Form\TimePickerViewHelper\buildHourSelector
‪string buildHourSelector(\DateTime $date=null)
Definition: TimePickerViewHelper.php:128