ILIAS  release_8 Revision v8.24
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,
57 \ILIAS\Refinery\Factory $refinery,
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
70 protected function getOptionalNullTransformation(\ILIAS\Refinery\Transformation $or_trafo): Transformation
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)
88 {
89 // TODO: It would be a lot nicer if the value would be held as DateTimeImmutable
90 // internally, but currently this is just to much. Added to the roadmap.
91 if ($value instanceof \DateTimeImmutable) {
92 $value = $this->format->applyTo($value);
93 }
94
95 $clone = clone $this;
96 $clone->value = $value;
97 return $clone;
98 }
99
100 public function withFormat(DateFormat $format): C\Input\Field\DateTime
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): C\Input\Field\DateTime
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): C\Input\Field\DateTime
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): C\Input\Field\DateTime
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): C\Input\Field\DateTime
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): C\Input\Field\DateTime
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 return is_string($value);
180 }
181
183 {
184 if ($this->requirement_constraint !== null) {
185 return $this->requirement_constraint;
186 }
187
188 return $this->refinery->string()->hasMinLength(1)
189 ->withProblemBuilder(fn ($txt, $value) => $txt("datetime_required"));
190 }
191
196 public function getAdditionalPickerconfig(): array
197 {
199 }
200
205 public function withAdditionalPickerconfig(array $config): C\Input\Field\DateTime
206 {
207 $clone = clone $this;
208 $clone->additional_picker_config = array_merge($clone->additional_picker_config, $config);
209 return $clone;
210 }
211
212 public function getUpdateOnLoadCode(): Closure
213 {
214 return fn ($id) => "$('#$id').on('input dp.change', function(event) {
215 il.UI.input.onFieldUpdate(event, '$id', $('#$id').find('input').val());
216 });
217 il.UI.input.onFieldUpdate(event, '$id', $('#$id').find('input').val());";
218 }
219}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
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:21
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:196
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:205
This is a legacy support of Implementation\Component\Input\Field\Input that has been moved to Impleme...
Definition: Input.php:32
$txt
Definition: error.php:13
A constraint encodes some resrtictions on values.
Definition: Constraint.php:32
This describes inputs that can be used in forms.
Definition: FormInput.php:32
if(!array_key_exists('PATH_INFO', $_SERVER)) $config
Definition: metadata.php:85
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
Refinery Factory $refinery
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Checkbox.php:21
trait JavaScriptBindable
Trait for components implementing JavaScriptBindable providing standard implementation.
Class ChatMainBarProvider \MainMenu\Provider.