|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the Nette Framework (https://nette.org) |
| 5 | + * Copyright (c) 2004 David Grudl (https://davidgrudl.com) |
| 6 | + */ |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | +namespace Nette\Forms\Controls; |
| 11 | + |
| 12 | +use Nette; |
| 13 | +use Nette\Forms\Form; |
| 14 | + |
| 15 | + |
| 16 | +/** |
| 17 | + * Selects date or time or date & time. |
| 18 | + */ |
| 19 | +class DateTimeControl extends BaseControl |
| 20 | +{ |
| 21 | + public const |
| 22 | + TypeDate = 1, |
| 23 | + TypeTime = 2, |
| 24 | + TypeDateTime = 3; |
| 25 | + |
| 26 | + public const |
| 27 | + FormatObject = 'object', |
| 28 | + FormatTimestamp = 'timestamp'; |
| 29 | + |
| 30 | + /** @var int */ |
| 31 | + private $type; |
| 32 | + |
| 33 | + /** @var bool */ |
| 34 | + private $withSeconds; |
| 35 | + |
| 36 | + /** @var string */ |
| 37 | + private $format = self::FormatObject; |
| 38 | + |
| 39 | + |
| 40 | + public function __construct($label = null, int $type = self::TypeDate, bool $withSeconds = false) |
| 41 | + { |
| 42 | + $this->type = $type; |
| 43 | + $this->withSeconds = $withSeconds; |
| 44 | + parent::__construct($label); |
| 45 | + $this->control->step = $withSeconds ? 1 : null; |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + public function getType(): int |
| 50 | + { |
| 51 | + return $this->type; |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | + /** |
| 56 | + * Format of returned value. Allowed values are string (ie 'Y-m-d'), DateTimeControl::FormatObject and DateTimeControl::FormatTimestamp. |
| 57 | + * @return static |
| 58 | + */ |
| 59 | + public function setFormat(string $format) |
| 60 | + { |
| 61 | + $this->format = $format; |
| 62 | + return $this; |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + /** |
| 67 | + * @param \DateTimeInterface|string|int|null $value |
| 68 | + * @return static |
| 69 | + */ |
| 70 | + public function setValue($value) |
| 71 | + { |
| 72 | + $this->value = $value === null ? null : $this->normalizeValue($value); |
| 73 | + return $this; |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + /** |
| 78 | + * @return \DateTimeImmutable|string|int|null |
| 79 | + */ |
| 80 | + public function getValue() |
| 81 | + { |
| 82 | + if ($this->format === self::FormatObject) { |
| 83 | + return $this->value; |
| 84 | + } elseif ($this->format === self::FormatTimestamp) { |
| 85 | + return $this->value ? $this->value->getTimestamp() : null; |
| 86 | + } else { |
| 87 | + return $this->value ? $this->value->format($this->format) : null; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + /** |
| 93 | + * @param \DateTimeInterface|string|int $value |
| 94 | + */ |
| 95 | + public function normalizeValue($value): \DateTimeImmutable |
| 96 | + { |
| 97 | + if (is_numeric($value)) { |
| 98 | + $dt = (new \DateTimeImmutable)->setTimestamp((int) $value); |
| 99 | + } elseif (is_string($value)) { |
| 100 | + $dt = new \DateTimeImmutable($value); // createFromFormat() must not be used because it allows invalid values |
| 101 | + } elseif ($value instanceof \DateTime) { |
| 102 | + $dt = \DateTimeImmutable::createFromMutable($value); |
| 103 | + } elseif ($value instanceof \DateTimeImmutable) { |
| 104 | + $dt = $value; |
| 105 | + } elseif (!$value instanceof \DateTimeInterface) { |
| 106 | + throw new Nette\InvalidArgumentException('Value must be DateTimeInterface or string or null, ' . gettype($value) . ' given.'); |
| 107 | + } |
| 108 | + |
| 109 | + [$h, $m, $s] = [(int) $dt->format('H'), (int) $dt->format('i'), $this->withSeconds ? (int) $dt->format('s') : 0]; |
| 110 | + if ($this->type === self::TypeDate) { |
| 111 | + return $dt->setTime(0, 0); |
| 112 | + } elseif ($this->type === self::TypeTime) { |
| 113 | + return $dt->setDate(0, 1, 1)->setTime($h, $m, $s); |
| 114 | + } elseif ($this->type === self::TypeDateTime) { |
| 115 | + return $dt->setTime($h, $m, $s); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + public function loadHttpData(): void |
| 121 | + { |
| 122 | + $value = $this->getHttpData(Nette\Forms\Form::DataText); |
| 123 | + try { |
| 124 | + $this->value = is_string($value) && preg_match('~^(\d{4}-\d{2}-\d{2})?T?(\d{2}:\d{2}(:\d{2}(\.\d+)?)?)?$~', $value) |
| 125 | + ? $this->normalizeValue($value) |
| 126 | + : null; |
| 127 | + } catch (\Throwable $e) { |
| 128 | + $this->value = null; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + |
| 133 | + public function getControl(): Nette\Utils\Html |
| 134 | + { |
| 135 | + return parent::getControl()->addAttributes([ |
| 136 | + 'value' => $this->value ? $this->formatHtmlValue($this->value) : null, |
| 137 | + 'type' => [self::TypeDate => 'date', self::TypeTime => 'time', self::TypeDateTime => 'datetime-local'][$this->type], |
| 138 | + ]); |
| 139 | + } |
| 140 | + |
| 141 | + |
| 142 | + /** |
| 143 | + * Formats a date/time for HTML attributes. |
| 144 | + */ |
| 145 | + public function formatHtmlValue(\DateTimeInterface $dt): string |
| 146 | + { |
| 147 | + return $dt->format([ |
| 148 | + self::TypeDate => 'Y-m-d', |
| 149 | + self::TypeTime => $this->withSeconds ? 'H:i:s' : 'H:i', |
| 150 | + self::TypeDateTime => $this->withSeconds ? 'Y-m-d\\TH:i:s' : 'Y-m-d\\TH:i', |
| 151 | + ][$this->type]); |
| 152 | + } |
| 153 | + |
| 154 | + |
| 155 | + /** |
| 156 | + * Formats a date/time according to the locale and formatting options. |
| 157 | + */ |
| 158 | + public function formatLocaleText(\DateTimeInterface $dt): string |
| 159 | + { |
| 160 | + if ($this->type === self::TypeDate) { |
| 161 | + return \IntlDateFormatter::formatObject($dt, [\IntlDateFormatter::MEDIUM, \IntlDateFormatter::NONE]); |
| 162 | + } elseif ($this->type === self::TypeTime) { |
| 163 | + return \IntlDateFormatter::formatObject($dt, [\IntlDateFormatter::NONE, $this->withSeconds ? \IntlDateFormatter::MEDIUM : \IntlDateFormatter::SHORT]); |
| 164 | + } elseif ($this->type === self::TypeDateTime) { |
| 165 | + return \IntlDateFormatter::formatObject($dt, [\IntlDateFormatter::MEDIUM, $this->withSeconds ? \IntlDateFormatter::MEDIUM : \IntlDateFormatter::SHORT]); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + |
| 170 | + /** @return static */ |
| 171 | + public function addRule($validator, $errorMessage = null, $arg = null) |
| 172 | + { |
| 173 | + if ($validator === Form::Min) { |
| 174 | + $this->control->min = $arg = $this->formatHtmlValue($this->normalizeValue($arg)); |
| 175 | + } elseif ($validator === Form::Max) { |
| 176 | + $this->control->max = $arg = $this->formatHtmlValue($this->normalizeValue($arg)); |
| 177 | + } elseif ($validator === Form::Range) { |
| 178 | + $this->control->min = isset($arg[0]) |
| 179 | + ? $arg[0] = $this->formatHtmlValue($this->normalizeValue($arg[0])) |
| 180 | + : null; |
| 181 | + $this->control->max = isset($arg[1]) |
| 182 | + ? $arg[1] = $this->formatHtmlValue($this->normalizeValue($arg[1])) |
| 183 | + : null; |
| 184 | + } |
| 185 | + |
| 186 | + return parent::addRule($validator, $errorMessage, $arg); |
| 187 | + } |
| 188 | +} |
0 commit comments