ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
DateTime.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
24use ILIAS\Data\Factory as DataFactory;
26use ILIAS\UI\Implementation\Component\ComponentHelper;
28use DateTimeImmutable;
31use Closure;
32
37{
38 use ComponentHelper;
40
41 public const TIME_FORMAT = 'HH:mm';
42
44 protected ?DateTimeImmutable $min_date = null;
45 protected ?DateTimeImmutable $max_date = null;
46 protected bool $with_time = false;
47 protected bool $with_time_only = false;
48 protected ?string $timezone = null;
49
53 protected array $additional_picker_config = [];
54
55 public function __construct(
56 DataFactory $data_factory,
58 string $label,
59 ?string $byline
60 ) {
61 parent::__construct($data_factory, $refinery, $label, $byline);
62
63 $this->format = $data_factory->dateFormat()->standard();
64
65 $datetime_trafo = $refinery->to()->dateTime();
66 $trafo = $this->getOptionalNullTransformation($datetime_trafo);
67 $this->setAdditionalTransformation($trafo);
68 }
69
71 {
72 return $this->refinery->custom()->transformation(
73 function ($v) use ($or_trafo) {
74 if (!$v) {
75 return null;
76 }
77 return $or_trafo->transform($v);
78 }
79 );
80 }
81
87 public function withValue($value): self
88 {
89 if ($value === '') {
90 $value = null;
91 }
92 // TODO: It would be a lot nicer if the value would be held as DateTimeImmutable
93 // internally, but currently this is just to much. Added to the roadmap.
94 if ($value instanceof \DateTimeImmutable) {
95 $value = $value->format(Renderer::HTML5_NATIVE_DATETIME_FORMAT);
96 }
97 return parent::withValue($value);
98 }
99
100 public function withFormat(DateFormat $format): self
101 {
102 $clone = clone $this;
103 $clone->format = $format;
104 return $clone;
105 }
106
107 public function getFormat(): DateFormat
108 {
109 return $this->format;
110 }
111
112
113 public function withTimezone(string $tz): self
114 {
115 $timezone_trafo = $this->refinery->dateTime()->changeTimezone($tz);
116 $clone = clone $this;
117 $clone->timezone = $tz;
118
119 $trafo = $this->getOptionalNullTransformation($timezone_trafo);
120 $clone = $clone->withAdditionalTransformation($trafo);
121 return $clone;
122 }
123
124 public function getTimezone(): ?string
125 {
126 return $this->timezone;
127 }
128
129 public function withMinValue(DateTimeImmutable $datetime): self
130 {
131 $clone = clone $this;
132 $clone->min_date = $datetime;
133 return $clone;
134 }
135
136 public function getMinValue(): ?DateTimeImmutable
137 {
138 return $this->min_date;
139 }
140
141 public function withMaxValue(DateTimeImmutable $datetime): self
142 {
143 $clone = clone $this;
144 $clone->max_date = $datetime;
145 return $clone;
146 }
147
148 public function getMaxValue(): ?DateTimeImmutable
149 {
150 return $this->max_date;
151 }
152
153 public function withUseTime(bool $with_time): self
154 {
155 $clone = clone $this;
156 $clone->with_time = $with_time;
157 return $clone;
158 }
159
160 public function getUseTime(): bool
161 {
162 return $this->with_time;
163 }
164
165 public function withTimeOnly(bool $time_only): self
166 {
167 $clone = clone $this;
168 $clone->with_time_only = $time_only;
169 return $clone;
170 }
171
172 public function getTimeOnly(): bool
173 {
175 }
176
177 protected function isClientSideValueOk($value): bool
178 {
179 if ($value instanceof \DateTimeImmutable || is_null($value)) {
180 return true;
181 }
182
183 if (!is_string($value)) {
184 return false;
185 }
186
187 try {
188 new \DateTimeImmutable($value);
189 return true;
190 }
191 // We should mostly not catch generic Throwables because we will be masking
192 // a lot of error conditions in this way... But, unfortunately, I cannot
193 // currently see a better way to check if \DateTimeImmutable would accept
194 // the supplied $value, so here we go...
195 catch (\Throwable $e) {
196 return false;
197 }
198 }
199
201 {
202 if ($this->requirement_constraint !== null) {
203 return $this->requirement_constraint;
204 }
205
206 return $this->refinery->logical()->sequential([
207 $this->refinery->logical()->not($this->refinery->null()),
208 $this->refinery->string()->hasMinLength(1)
209 ])
210 ->withProblemBuilder(fn($txt, $value) => $txt("datetime_required"));
211
212 }
213
218 public function getAdditionalPickerconfig(): array
219 {
221 }
222
227 public function withAdditionalPickerconfig(array $config): self
228 {
229 $clone = clone $this;
230 $clone->additional_picker_config = array_merge($clone->additional_picker_config, $config);
231 return $clone;
232 }
233
234 public function getUpdateOnLoadCode(): Closure
235 {
236 return fn($id) => "$('#$id').on('input', function(event) {
237 il.UI.input.onFieldUpdate(event, '$id', $('#$id').find('input').val());
238 });
239 il.UI.input.onFieldUpdate(event, '$id', $('#$id').find('input').val());";
240 }
241
242 public function isComplex(): bool
243 {
244 return false;
245 }
246}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
$datetime
A Date Format provides a format definition akin to PHP's date formatting options, but stores the sing...
Definition: DateFormat.php:27
Factory for Date Formats.
Definition: Factory.php:27
Builds data types.
Definition: Factory.php:36
Transform values according to custom configuration.
getOptionalNullTransformation(\ILIAS\Refinery\Transformation $or_trafo)
Definition: DateTime.php:70
getAdditionalPickerconfig()
Get config to be passed to the bootstrap picker.
Definition: DateTime.php:218
withValue($value)
Get an input like this with another value displayed on the client side.static
Definition: DateTime.php:87
__construct(DataFactory $data_factory, \ILIAS\Refinery\Factory $refinery, string $label, ?string $byline)
Definition: DateTime.php:55
withAdditionalPickerconfig(array $config)
The bootstrap picker can be configured, e.g.
Definition: DateTime.php:227
$txt
Definition: error.php:31
A constraint encodes some resrtictions on values.
Definition: Constraint.php:32
This describes inputs that can be used in forms.
Definition: FormInput.php:33
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Checkbox.php:21
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:61
trait JavaScriptBindable
Trait for components implementing JavaScriptBindable providing standard implementation.
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.