ILIAS  release_8 Revision v8.23
Duration.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 use ilLanguage;
33 
37 class Duration extends Group implements C\Input\Field\Duration
38 {
39  use ComponentHelper;
41 
42  protected DateFormat $format;
45  protected bool $with_time = false;
46  protected bool $with_time_only = false;
47  protected ?string $timezone = null;
48 
49  public function __construct(
50  DataFactory $data_factory,
53  Factory $field_factory,
54  string $label,
55  ?string $byline
56  ) {
57  $inputs = [
58  $field_factory->dateTime($lng->txt('duration_default_label_start'), null)->withDedicatedName('start'),
59  $field_factory->dateTime($lng->txt('duration_default_label_end'), null)->withDedicatedName('end')
60  ];
61 
62  parent::__construct($data_factory, $refinery, $lng, $inputs, $label, $byline);
63 
64  $this->addTransformation();
65  $this->addValidation();
66  }
67 
74  protected function addTransformation(): void
75  {
76  $duration = $this->refinery->custom()->transformation(function ($v): ?array {
77  list($from, $until) = $v;
78  if ($from && $until) {
79  return ['start' => $from, 'end' => $until, 'interval' => $from->diff($until)];
80  }
81  return null;
82  });
83  $this->setAdditionalTransformation($duration);
84  }
85 
89  protected function addValidation(): void
90  {
91  $txt_id = 'duration_end_must_not_be_earlier_than_start';
92  $error = fn (callable $txt, $value) => $txt($txt_id, $value);
93  $is_ok = function ($v) {
94  if (is_null($v)) {
95  return true;
96  }
97  return $v['start'] <= $v['end'];
98  };
99 
100  $from_before_until = $this->refinery->custom()->constraint($is_ok, $error);
101  $this->setAdditionalTransformation($from_before_until);
102  }
103 
107  public function withFormat(DateFormat $format): C\Input\Field\Duration
108  {
109  $clone = clone $this;
110  $clone->format = $format;
111  $clone->applyFormat();
112  return $clone;
113  }
114 
118  public function getFormat(): DateFormat
119  {
120  return $this->format;
121  }
122 
126  protected function applyFormat(): void
127  {
128  $this->inputs = array_map(
129  fn ($input) => $input->withFormat($this->getFormat()),
130  $this->inputs
131  );
132  }
133 
137  public function withMinValue(DateTimeImmutable $date): C\Input\Field\Duration
138  {
139  $clone = clone $this;
140  $clone->min_date = $date;
141  $clone->applyMinValue();
142  return $clone;
143  }
144 
148  protected function applyMinValue(): void
149  {
150  $this->inputs = array_map(
151  fn ($input) => $input->withMinValue($this->getMinValue()),
152  $this->inputs
153  );
154  }
155 
159  public function getMinValue(): ?DateTimeImmutable
160  {
161  return $this->min_date;
162  }
163 
167  public function withMaxValue(DateTimeImmutable $date): C\Input\Field\Duration
168  {
169  $clone = clone $this;
170  $clone->max_date = $date;
171  $clone->applyMaxValue();
172  return $clone;
173  }
174 
178  protected function applyMaxValue(): void
179  {
180  $this->inputs = array_map(
181  fn ($inpt) => $inpt->withMaxValue($this->getMaxValue()),
182  $this->inputs
183  );
184  }
185 
189  public function getMaxValue(): ?DateTimeImmutable
190  {
191  return $this->max_date;
192  }
193 
197  public function withTimeOnly(bool $time_only): C\Input\Field\Duration
198  {
199  $clone = clone $this;
200  $clone->with_time_only = $time_only;
201  $clone->applyWithTimeOnly();
202  return $clone;
203  }
204 
208  protected function applyWithTimeOnly(): void
209  {
210  $this->inputs = array_map(
211  fn ($input) => $input->withTimeOnly($this->getTimeOnly()),
212  $this->inputs
213  );
214  }
215 
219  public function getTimeOnly(): bool
220  {
221  return $this->with_time_only;
222  }
223 
227  public function withUseTime(bool $with_time): C\Input\Field\Duration
228  {
229  $clone = clone $this;
230  $clone->with_time = $with_time;
231  $clone->applyWithUseTime();
232  return $clone;
233  }
234 
238  public function getUseTime(): bool
239  {
240  return $this->with_time;
241  }
242 
246  protected function applyWithUseTime(): void
247  {
248  $this->inputs = array_map(
249  fn ($input) => $input->withUseTime($this->getUseTime()),
250  $this->inputs
251  );
252  }
253 
257  public function withTimezone(string $tz): C\Input\Field\Duration
258  {
259  $clone = clone $this;
260  $clone->timezone = $tz;
261  $clone->inputs = array_map(
262  fn ($input) => $input->withTimezone($tz),
263  $clone->inputs
264  );
265  return $clone;
266  }
267 
271  public function getTimezone(): ?string
272  {
273  return $this->timezone;
274  }
275 
279  protected function isClientSideValueOk($value): bool
280  {
281  return true;
282  }
283 
288  {
289  if ($this->requirement_constraint !== null) {
290  return $this->requirement_constraint;
291  }
292 
293  return null;
294  }
295 
299  public function getUpdateOnLoadCode(): Closure
300  {
301  return fn ($id) => "var combinedDuration = function() {
302  var options = [];
303  $('#$id').find('input').each(function() {
304  options.push($(this).val());
305  });
306  return options.join(' - ');
307  }
308  $('#$id').on('input dp.change', function(event) {
309  il.UI.input.onFieldUpdate(event, '$id', combinedDuration());
310  });
311  il.UI.input.onFieldUpdate(event, '$id', combinedDuration());";
312  }
313 
314  public function withLabels(string $start_label, string $end_label): C\Input\Field\Duration
315  {
316  $clone = clone $this;
317  $clone->inputs = [
318  $clone->inputs[0]->withLabel($start_label),
319  $clone->inputs[1]->withLabel($end_label)
320  ];
321  return $clone;
322  }
323 }
This implements the group input.
Definition: Group.php:42
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: ByTrying.php:21
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
Class ChatMainBarProvider .
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
trait JavaScriptBindable
Trait for components implementing JavaScriptBindable providing standard implementation.
A constraint encodes some resrtictions on values.
Definition: Constraint.php:31
addTransformation()
Return-value of Duration is an assoc array with start, end and interval.
Definition: Duration.php:74
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Checkbox.php:21
withDedicatedName(string $dedicated_name)
Sets an optional dedicated name for this input which is used in the NAME attribute of the rendered in...
getUpdateOnLoadCode()
Get update code.This method has to return JS code that calls il.UI.filter.onFieldUpdate(event, &#39;$id&#39;, string_value);initially "onload" andon every input change. It must pass a readable string representation of its value in parameter &#39;string_value&#39;.
Definition: Duration.php:299
A Date Format provides a format definition akin to PHP&#39;s date formatting options, but stores the sing...
Definition: DateFormat.php:26
This implements the duration input group.
Definition: Duration.php:37
__construct(DataFactory $data_factory, \ILIAS\Refinery\Factory $refinery, ilLanguage $lng, Factory $field_factory, string $label, ?string $byline)
Definition: Duration.php:49
withLabels(string $start_label, string $end_label)
Definition: Duration.php:314
$txt
Definition: error.php:13
addValidation()
Input is valid, if start is before end.
Definition: Duration.php:89
__construct(Container $dic, ilPlugin $plugin)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
ilErrorHandling $error
Definition: class.ilias.php:55
Refinery Factory $refinery