ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
DateTime.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
23 use ILIAS\UI\Component as C;
31 use Closure;
32 
36 class DateTime extends FormInput implements C\Input\Field\DateTime
37 {
38  use ComponentHelper;
40 
41  public const TIME_FORMAT = 'HH:mm';
42 
43  protected DateFormat $format;
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  // This is necessary, otherwise DateTimeImmutable will replace
90  // the empty string with the current date during rendering.
91  // The empty string is the default value posted by the input, if
92  // it is left empty.
93  if ($value === '') {
94  $value = null;
95  }
96  // TODO: It would be a lot nicer if the value would be held as DateTimeImmutable
97  // internally, but currently this is just to much. Added to the roadmap.
98  if ($value instanceof \DateTimeImmutable) {
99  $value = $value->format(Renderer::HTML5_NATIVE_DATETIME_FORMAT);
100  }
101  return parent::withValue($value);
102  }
103 
104  public function withFormat(DateFormat $format): self
105  {
106  $clone = clone $this;
107  $clone->format = $format;
108  return $clone;
109  }
110 
111  public function getFormat(): DateFormat
112  {
113  return $this->format;
114  }
115 
116 
117  public function withTimezone(string $tz): self
118  {
119  $timezone_trafo = $this->refinery->dateTime()->changeTimezone($tz);
120  $clone = clone $this;
121  $clone->timezone = $tz;
122 
123  $trafo = $this->getOptionalNullTransformation($timezone_trafo);
124  $clone = $clone->withAdditionalTransformation($trafo);
125  return $clone;
126  }
127 
128  public function getTimezone(): ?string
129  {
130  return $this->timezone;
131  }
132 
133  public function withMinValue(DateTimeImmutable $datetime): self
134  {
135  $clone = clone $this;
136  $clone->min_date = $datetime;
137  return $clone;
138  }
139 
140  public function getMinValue(): ?DateTimeImmutable
141  {
142  return $this->min_date;
143  }
144 
145  public function withMaxValue(DateTimeImmutable $datetime): self
146  {
147  $clone = clone $this;
148  $clone->max_date = $datetime;
149  return $clone;
150  }
151 
152  public function getMaxValue(): ?DateTimeImmutable
153  {
154  return $this->max_date;
155  }
156 
157  public function withUseTime(bool $with_time): self
158  {
159  $clone = clone $this;
160  $clone->with_time = $with_time;
161  return $clone;
162  }
163 
164  public function getUseTime(): bool
165  {
166  return $this->with_time;
167  }
168 
169  public function withTimeOnly(bool $time_only): self
170  {
171  $clone = clone $this;
172  $clone->with_time_only = $time_only;
173  return $clone;
174  }
175 
176  public function getTimeOnly(): bool
177  {
178  return $this->with_time_only;
179  }
180 
181  protected function isClientSideValueOk($value): bool
182  {
183  if ($value instanceof \DateTimeImmutable || is_null($value)) {
184  return true;
185  }
186 
187  if (!is_string($value)) {
188  return false;
189  }
190 
191  try {
192  new \DateTimeImmutable($value);
193  return true;
194  }
195  // We should mostly not catch generic Throwables because we will be masking
196  // a lot of error conditions in this way... But, unfortunately, I cannot
197  // currently see a better way to check if \DateTimeImmutable would accept
198  // the supplied $value, so here we go...
199  catch (\Throwable $e) {
200  return false;
201  }
202  }
203 
205  {
206  if ($this->requirement_constraint !== null) {
207  return $this->requirement_constraint;
208  }
209 
210  return $this->refinery->logical()->sequential([
211  $this->refinery->logical()->not($this->refinery->null()),
212  $this->refinery->string()->hasMinLength(1)
213  ])
214  ->withProblemBuilder(fn($txt, $value) => $txt("datetime_required"));
215 
216  }
217 
222  public function getAdditionalPickerconfig(): array
223  {
225  }
226 
231  public function withAdditionalPickerconfig(array $config): self
232  {
233  $clone = clone $this;
234  $clone->additional_picker_config = array_merge($clone->additional_picker_config, $config);
235  return $clone;
236  }
237 
238  public function getUpdateOnLoadCode(): Closure
239  {
240  return fn($id) => "$('#$id').on('input dp.change', function(event) {
241  il.UI.input.onFieldUpdate(event, '$id', $('#$id').find('input').val());
242  });
243  il.UI.input.onFieldUpdate(event, '$id', $('#$id').find('input').val());";
244  }
245 }
withValue($value)
Get an input like this with another value displayed on the client side.if value does not fit client s...
Definition: DateTime.php:87
Transform values according to custom configuration.
getAdditionalPickerconfig()
Get config to be passed to the bootstrap picker.
Definition: DateTime.php:222
Class ChatMainBarProvider .
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$datetime
trait JavaScriptBindable
Trait for components implementing JavaScriptBindable providing standard implementation.
A constraint encodes some resrtictions on values.
Definition: Constraint.php:31
__construct(DataFactory $data_factory, \ILIAS\Refinery\Factory $refinery, string $label, ?string $byline)
Definition: DateTime.php:55
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Checkbox.php:21
__construct(VocabulariesInterface $vocabularies)
A Date Format provides a format definition akin to PHP&#39;s date formatting options, but stores the sing...
Definition: DateFormat.php:26
getOptionalNullTransformation(\ILIAS\Refinery\Transformation $or_trafo)
Definition: DateTime.php:70
$txt
Definition: error.php:14
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:58
withProblemBuilder(callable $builder)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
This describes inputs that can be used in forms.
Definition: FormInput.php:31
withAdditionalPickerconfig(array $config)
The bootstrap picker can be configured, e.g.
Definition: DateTime.php:231
Refinery Factory $refinery