ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
Duration.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 2018 Nils Haagen <nils.haagen@concepts-and-training.de> Extended GPL, see docs/LICENSE */
4 
6 
7 use ILIAS\UI\Component as C;
10 use ILIAS\Refinery\Transformation\Factory as TransformationFactory;
11 use ILIAS\Refinery\Validation\Factory as ValidationFactory;
16 
20 class Duration extends Group implements C\Input\Field\Duration, JSBindabale
21 {
22  use ComponentHelper;
24 
28  protected $format;
29 
33  protected $min_date;
34 
38  protected $max_date;
39 
43  protected $with_time = false;
44 
48  protected $with_time_only = false;
49 
53  protected $timezone;
54 
59 
66  public function __construct(
67  DataFactory $data_factory,
68  \ILIAS\Refinery\Factory $refinery,
70  Factory $field_factory,
71  $label,
72  $byline
73  ) {
74  $inputs = [
75  $field_factory->dateTime('start'),
76  $field_factory->dateTime('end')
77  ];
78 
79  parent::__construct($data_factory, $refinery, $lng, $inputs, $label, $byline);
80 
81  $this->addTransformation();
82  $this->addValidation();
83  }
84 
91  protected function addTransformation()
92  {
93  $duration = $this->refinery->custom()->transformation(function ($v) {
94  list($from, $until) = $v;
95  if ($from && $until) {
96  return ['start' => $from, 'end' => $until, 'interval' => $from->diff($until)];
97  }
98  return null;
99  });
100  $this->setAdditionalTransformation($duration);
101  }
102 
106  protected function addValidation()
107  {
108  $txt_id = 'duration_end_must_not_be_earlier_than_start';
109  $error = function (callable $txt, $value) use ($txt_id) {
110  return $txt($txt_id, $value);
111  };
112  $is_ok = function ($v) {
113  if (is_null($v)) {
114  return true;
115  }
116  return $v['start'] < $v['end'];
117  };
118 
119  $from_before_until = $this->refinery->custom()->constraint($is_ok, $error);
120  $this->setAdditionalTransformation($from_before_until);
121  }
122 
127  {
128  $clone = clone $this;
129  $clone->format = $format;
130  $clone->applyFormat();
131  return $clone;
132  }
133 
137  public function getFormat() : DateFormat\DateFormat
138  {
139  return $this->format;
140  }
141 
145  protected function applyFormat()
146  {
147  $this->inputs = array_map(
148  function ($inpt) {
149  return $inpt->withFormat($this->getFormat());
150  },
151  $this->inputs
152  );
153  }
154 
158  public function withMinValue(\DateTimeImmutable $date) : C\Input\Field\Duration
159  {
160  $clone = clone $this;
161  $clone->min_date = $date;
162  $clone->applyMinValue();
163  return $clone;
164  }
165 
169  protected function applyMinValue()
170  {
171  $this->inputs = array_map(
172  function ($inpt) {
173  return $inpt->withMinValue($this->getMinValue());
174  },
175  $this->inputs
176  );
177  }
178 
182  public function getMinValue()
183  {
184  return $this->min_date;
185  }
186 
190  public function withMaxValue(\DateTimeImmutable $date) : C\Input\Field\Duration
191  {
192  $clone = clone $this;
193  $clone->max_date = $date;
194  $clone->applyMaxValue();
195  return $clone;
196  }
197 
201  protected function applyMaxValue()
202  {
203  $this->inputs = array_map(
204  function ($inpt) {
205  return $inpt->withMaxValue($this->getMaxValue());
206  },
207  $this->inputs
208  );
209  }
210 
214  public function getMaxValue()
215  {
216  return $this->max_date;
217  }
218 
222  public function withTimeOnly(bool $with_time_only) : C\Input\Field\Duration
223  {
224  $clone = clone $this;
225  $clone->with_time_only = $with_time_only;
226  $clone->applyWithTimeOnly();
227  return $clone;
228  }
229 
233  protected function applyWithTimeOnly()
234  {
235  $this->inputs = array_map(
236  function ($inpt) {
237  return $inpt->withTimeOnly($this->getTimeOnly());
238  },
239  $this->inputs
240  );
241  }
242 
246  public function getTimeOnly() : bool
247  {
248  return $this->with_time_only;
249  }
250 
254  public function withUseTime(bool $with_time) : C\Input\Field\Duration
255  {
256  $clone = clone $this;
257  $clone->with_time = $with_time;
258  $clone->applyWithUseTime();
259  return $clone;
260  }
261 
265  public function getUseTime() : bool
266  {
267  return $this->with_time;
268  }
269 
273  protected function applyWithUseTime()
274  {
275  $this->inputs = array_map(
276  function ($inpt) {
277  return $inpt->withUseTime($this->getUseTime());
278  },
279  $this->inputs
280  );
281  }
282 
286  public function withTimezone(string $tz) : C\Input\Field\Duration
287  {
288  $trafo = $this->refinery->dateTime()->changeTimezone($tz);
289  $clone = clone $this;
290  $clone->timezone = $tz;
291 
292  $clone->inputs = array_map(
293  function ($inpt) use ($trafo) {
294  return $inpt->withAdditionalTransformation($trafo);
295  },
296  $clone->inputs
297  );
298  return $clone;
299  }
300 
304  public function getTimezone()
305  {
306  return $this->timezone;
307  }
308 
312  protected function isClientSideValueOk($value) : bool
313  {
314  return true;
315  }
316 
320  protected function getConstraintForRequirement()
321  {
322  return null;
323  }
324 
328  public function getUpdateOnLoadCode() : \Closure
329  {
330  return function ($id) {
331  $code = "var combinedDuration = function() {
332  var options = [];
333  $('#$id').find('input').each(function() {
334  options.push($(this).val());
335  });
336  return options.join(' - ');
337  }
338  $('#$id').on('input dp.change', function(event) {
339  il.UI.input.onFieldUpdate(event, '$id', combinedDuration());
340  });
341  il.UI.input.onFieldUpdate(event, '$id', combinedDuration());";
342  return $code;
343  };
344  }
345 }
This describes the duration input.
Definition: Duration.php:12
This describes a group of inputs.
Definition: Group.php:11
Class ChatMainBarProvider .
trait JavaScriptBindable
Trait for components implementing JavaScriptBindable providing standard implementation.
trait ComponentHelper
Provides common functionality for component implementations.
getMaxValue()
Return the maximum date the input accepts.
addTransformation()
Return-value of Duration is an assoc array with start, end and interval.
Definition: Duration.php:91
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;.string
Definition: Duration.php:328
$lng
$format
Definition: metadata.php:218
Builds data types.
Definition: Factory.php:19
__construct(DataFactory $data_factory, \ILIAS\Refinery\Factory $refinery, \ilLanguage $lng, Factory $field_factory, $label, $byline)
Definition: Duration.php:66
$txt
Definition: error.php:13
addValidation()
Input is valid, if start is before end.
Definition: Duration.php:106
getFormat()
Get the date-format of this input.
getUseTime()
Should the input be used to get both date and time?
getMinValue()
Return the lowest value the input accepts.
__construct(Container $dic, ilPlugin $plugin)
getTimeOnly()
Should the input be used to get a time only?
This describes the datetime-field.
Definition: DateTime.php:12
language handling