‪TYPO3CMS  9.5
TimePickerViewHelper.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
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 
23 
30 {
31 
35  protected ‪$tagName = 'select';
36 
40  protected ‪$propertyMapper;
41 
46  public function ‪injectPropertyMapper(\‪TYPO3\CMS\‪Extbase\Property\PropertyMapper ‪$propertyMapper)
47  {
48  $this->propertyMapper = ‪$propertyMapper;
49  }
50 
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 ViewHelper', 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->registerArgument('timeType', 'string', '"hour" or "minute"');
65  $this->registerUniversalTagAttributes();
66  }
67 
73  public function ‪render()
74  {
75  $name = $this->‪getName();
77  $this->tag->addAttribute('name', $name . '[hour]');
78 
79  $date = $this->‪getSelectedDate();
81 
82  $content = '';
83 
84  if ($this->arguments['timeType'] === 'hour') {
85  $content .= $this->‪buildHourSelector($date);
86  } else {
87  $content .= $this->‪buildMinuteSelector($date);
88  }
89 
90  return $content;
91  }
92 
96  protected function ‪getSelectedDate()
97  {
99  $formRuntime = $this->renderingContext
100  ->getViewHelperVariableContainer()
101  ->get(RenderRenderableViewHelper::class, 'formRuntime');
102 
103  $date = $formRuntime[$this->arguments['property']];
104  if ($date instanceof \DateTime) {
105  return $date;
106  }
107  if ($date !== null) {
108  $date = $this->propertyMapper->convert($date, 'DateTime');
109  if (!$date instanceof \DateTime) {
110  return null;
111  }
112  return $date;
113  }
114  if ($this->hasArgument('initialDate')) {
115  return new \DateTime($this->arguments['initialDate']);
116  }
117  }
118 
123  protected function ‪buildHourSelector(\DateTime $date = null): string
124  {
125  $value = $date !== null ? $date->format('H') : null;
126  $hourSelector = clone $this->tag;
127  $hourSelector->addAttribute('name', sprintf('%s[hour]', $this->‪getName()));
128  $options = '';
129  foreach (range(0, 23) as $hour) {
130  $hour = str_pad((string)$hour, 2, '0', STR_PAD_LEFT);
131  $selected = $hour === $value ? ' selected="selected"' : '';
132  $options .= '<option value="' . htmlspecialchars($hour) . '" ' . $selected . '>' . htmlspecialchars($hour) . '</option>';
133  }
134  $hourSelector->setContent($options);
135  return $hourSelector->render();
136  }
137 
142  protected function ‪buildMinuteSelector(\DateTime $date = null): string
143  {
144  $value = $date !== null ? $date->format('i') : null;
145  $minuteSelector = clone $this->tag;
146  if ($this->hasArgument('id')) {
147  $minuteSelector->addAttribute('id', $this->arguments['id'] . '-minute');
148  }
149  $minuteSelector->addAttribute('name', sprintf('%s[minute]', $this->‪getName()));
150  $options = '';
151  foreach (range(0, 59) as $minute) {
152  $minute = str_pad((string)$minute, 2, '0', STR_PAD_LEFT);
153  $selected = $minute === $value ? ' selected="selected"' : '';
154  $options .= '<option value="' . htmlspecialchars($minute) . '"' . $selected . '>' . htmlspecialchars($minute) . '</option>';
155  }
156  $minuteSelector->setContent($options);
157  return $minuteSelector->render();
158  }
159 }
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\setErrorClassAttribute
‪setErrorClassAttribute()
Definition: AbstractFormFieldViewHelper.php:324
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:4
‪TYPO3
‪TYPO3\CMS\Form\ViewHelpers\Form\TimePickerViewHelper
Definition: TimePickerViewHelper.php:30
‪TYPO3\CMS\Form\ViewHelpers\Form\TimePickerViewHelper\getSelectedDate
‪DateTime null getSelectedDate()
Definition: TimePickerViewHelper.php:94
‪TYPO3\CMS\Form\ViewHelpers\Form\TimePickerViewHelper\$tagName
‪string $tagName
Definition: TimePickerViewHelper.php:34
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormViewHelper\registerFieldNameForFormTokenGeneration
‪registerFieldNameForFormTokenGeneration($fieldName)
Definition: AbstractFormViewHelper.php:101
‪TYPO3\CMS\Form\ViewHelpers\Form\TimePickerViewHelper\injectPropertyMapper
‪injectPropertyMapper(\TYPO3\CMS\Extbase\Property\PropertyMapper $propertyMapper)
Definition: TimePickerViewHelper.php:44
‪TYPO3\CMS\Form\ViewHelpers\Form\TimePickerViewHelper\$propertyMapper
‪TYPO3 CMS Extbase Property PropertyMapper $propertyMapper
Definition: TimePickerViewHelper.php:38
‪TYPO3\CMS\Form\ViewHelpers\Form\TimePickerViewHelper\initializeArguments
‪initializeArguments()
Definition: TimePickerViewHelper.php:54
‪TYPO3\CMS\Form\ViewHelpers\RenderRenderableViewHelper
Definition: RenderRenderableViewHelper.php:37
‪TYPO3\CMS\Form\ViewHelpers\Form
Definition: DatePickerViewHelper.php:3
‪TYPO3\CMS\Form\Domain\Runtime\FormRuntime
Definition: FormSession.php:3
‪TYPO3\CMS\Form\ViewHelpers\Form\TimePickerViewHelper\buildMinuteSelector
‪string buildMinuteSelector(\DateTime $date=null)
Definition: TimePickerViewHelper.php:140
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper
Definition: AbstractFormFieldViewHelper.php:26
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\getName
‪string getName()
Definition: AbstractFormFieldViewHelper.php:86
‪TYPO3\CMS\Form\ViewHelpers\Form\TimePickerViewHelper\render
‪string render()
Definition: TimePickerViewHelper.php:71
‪TYPO3\CMS\Form\ViewHelpers\Form\TimePickerViewHelper\buildHourSelector
‪string buildHourSelector(\DateTime $date=null)
Definition: TimePickerViewHelper.php:121