TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
TimePickerViewHelper.php
Go to the documentation of this file.
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 
20 
28 {
29 
33  protected $tagName = 'select';
34 
38  protected $propertyMapper;
39 
45  public function injectPropertyMapper(\TYPO3\CMS\Extbase\Property\PropertyMapper $propertyMapper)
46  {
47  $this->propertyMapper = $propertyMapper;
48  }
49 
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)');
65  }
66 
73  public function render()
74  {
75  $name = $this->getName();
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 
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 
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 
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 }
injectPropertyMapper(\TYPO3\CMS\Extbase\Property\PropertyMapper $propertyMapper)
registerTagAttribute($name, $type, $description, $required=false, $default=null)