ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
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
8use ILIAS\Data\Factory as DataFactory;
10use ILIAS\Refinery\Transformation\Factory as TransformationFactory;
11use ILIAS\Refinery\Validation\Factory as ValidationFactory;
16
20class Duration extends Group implements C\Input\Field\Duration, JSBindabale
21{
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}
An exception for terminatinating execution or to throw for unit testing.
Builds data types.
Definition: Factory.php:20
__construct(DataFactory $data_factory, \ILIAS\Refinery\Factory $refinery, \ilLanguage $lng, Factory $field_factory, $label, $byline)
Definition: Duration.php:66
addValidation()
Input is valid, if start is before end.
Definition: Duration.php:106
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,...
Definition: Duration.php:328
language handling
$txt
Definition: error.php:13
This describes the datetime-field.
Definition: DateTime.php:13
This describes the duration input.
Definition: Duration.php:13
getTimeOnly()
Should the input be used to get a time only?
getMaxValue()
Return the maximum date the input accepts.
getMinValue()
Return the lowest value the input accepts.
getUseTime()
Should the input be used to get both date and time?
getFormat()
Get the date-format of this input.
This describes a group of inputs.
Definition: Group.php:12
This describes commonalities between all inputs.
Definition: Input.php:32
Interface to be extended by components that have the possibility to bind to Javascript.
$format
Definition: metadata.php:218
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
trait JavaScriptBindable
Trait for components implementing JavaScriptBindable providing standard implementation.
trait ComponentHelper
Provides common functionality for component implementations.
Class ChatMainBarProvider \MainMenu\Provider.
$lng