TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
DatePickerViewHelper.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 
30 {
31 
35  protected $tagName = 'input';
36 
40  protected $propertyMapper;
41 
47  public function injectPropertyMapper(\TYPO3\CMS\Extbase\Property\PropertyMapper $propertyMapper)
48  {
49  $this->propertyMapper = $propertyMapper;
50  }
51 
58  public function initializeArguments()
59  {
60  parent::initializeArguments();
61  $this->registerTagAttribute('size', 'int', 'The size of the input field');
62  $this->registerTagAttribute('placeholder', 'string', 'Specifies a short hint that describes the expected value of an input element');
63  $this->registerArgument('errorClass', 'string', 'CSS class to set if there are errors for this view helper', false, 'f3-form-error');
64  $this->registerArgument('initialDate', 'string', 'Initial date (@see http://www.php.net/manual/en/datetime.formats.php for supported formats)');
65  $this->registerArgument('enableDatePicker', 'bool', 'Enable the Datepicker', false, true);
66  $this->registerArgument('dateFormat', 'string', 'The date format', false, 'Y-m-d');
68  }
69 
76  public function render()
77  {
78  $enableDatePicker = $this->arguments['enableDatePicker'];
79  $dateFormat = $this->arguments['dateFormat'];
80 
81  $name = $this->getName();
83 
84  $this->tag->addAttribute('type', 'date');
85  $this->tag->addAttribute('name', $name . '[date]');
86  if ($enableDatePicker) {
87  $this->tag->addAttribute('readonly', true);
88  }
89  $date = $this->getSelectedDate();
90  if ($date !== null) {
91  $this->tag->addAttribute('value', $date->format($dateFormat));
92  }
93 
94  if ($this->hasArgument('id')) {
95  $id = $this->arguments['id'];
96  } else {
97  $id = 'field' . md5(uniqid());
98  $this->tag->addAttribute('id', $id);
99  }
100  $this->setErrorClassAttribute();
101  $content = '';
102  $content .= $this->tag->render();
103  $content .= '<input type="hidden" name="' . $name . '[dateFormat]" value="' . htmlspecialchars($dateFormat) . '" />';
104 
105  if ($enableDatePicker) {
106  $datePickerDateFormat = $this->convertDateFormatToDatePickerFormat($dateFormat);
107  $this->templateVariableContainer->add('datePickerDateFormat', $datePickerDateFormat);
108  $content .= $this->renderChildren();
109  $this->templateVariableContainer->remove('datePickerDateFormat');
110  }
111  return $content;
112  }
113 
117  protected function getSelectedDate()
118  {
119  $fluidFormRenderer = $this->viewHelperVariableContainer->getView();
120  $formRuntime = $fluidFormRenderer->getFormRuntime();
121  $formState = $formRuntime->getFormState();
122 
123  $date = $formRuntime[$this->arguments['property']];
124  if ($date instanceof \DateTime) {
125  return $date;
126  }
127  if ($date !== null) {
128  $date = $this->propertyMapper->convert($date, 'DateTime');
129  if (!$date instanceof \DateTime) {
130  return null;
131  }
132  return $date;
133  }
134  if ($this->hasArgument('initialDate')) {
135  return new \DateTime($this->arguments['initialDate']);
136  }
137  }
138 
143  protected function convertDateFormatToDatePickerFormat(string $dateFormat): string
144  {
145  $replacements = [
146  'd' => 'dd',
147  'D' => 'D',
148  'j' => 'o',
149  'l' => 'DD',
150 
151  'F' => 'MM',
152  'm' => 'mm',
153  'M' => 'M',
154  'n' => 'm',
155 
156  'Y' => 'yy',
157  'y' => 'y'
158  ];
159  return strtr($dateFormat, $replacements);
160  }
161 }
registerTagAttribute($name, $type, $description, $required=false, $default=null)
injectPropertyMapper(\TYPO3\CMS\Extbase\Property\PropertyMapper $propertyMapper)