ILIAS  release_7 Revision v7.30-3-g800a261c036
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 as Refinery;
15
19class Duration extends Group implements C\Input\Field\Duration
20{
23
27 protected $format;
28
32 protected $min_date;
33
37 protected $max_date;
38
42 protected $with_time = false;
43
47 protected $with_time_only = false;
48
52 protected $timezone;
53
60 public function __construct(
61 DataFactory $data_factory,
62 \ILIAS\Refinery\Factory $refinery,
64 Factory $field_factory,
65 $label,
66 $byline
67 ) {
68 $inputs = [
69 $field_factory->dateTime('start'),
70 $field_factory->dateTime('end')
71 ];
72
73 parent::__construct($data_factory, $refinery, $lng, $inputs, $label, $byline);
74
75 $this->addTransformation();
76 $this->addValidation();
77 }
78
85 protected function addTransformation()
86 {
87 $duration = $this->refinery->custom()->transformation(function ($v) {
88 list($from, $until) = $v;
89 if ($from && $until) {
90 return ['start' => $from, 'end' => $until, 'interval' => $from->diff($until)];
91 }
92 return null;
93 });
94 $this->setAdditionalTransformation($duration);
95 }
96
100 protected function addValidation()
101 {
102 $txt_id = 'duration_end_must_not_be_earlier_than_start';
103 $error = function (callable $txt, $value) use ($txt_id) {
104 return $txt($txt_id, $value);
105 };
106 $is_ok = function ($v) {
107 if (is_null($v)) {
108 return true;
109 }
110 return $v['start'] < $v['end'];
111 };
112
113 $from_before_until = $this->refinery->custom()->constraint($is_ok, $error);
114 $this->setAdditionalTransformation($from_before_until);
115 }
116
121 {
122 $clone = clone $this;
123 $clone->format = $format;
124 $clone->applyFormat();
125 return $clone;
126 }
127
131 public function getFormat() : DateFormat\DateFormat
132 {
133 return $this->format;
134 }
135
139 protected function applyFormat()
140 {
141 $this->inputs = array_map(
142 function ($inpt) {
143 return $inpt->withFormat($this->getFormat());
144 },
145 $this->inputs
146 );
147 }
148
152 public function withMinValue(\DateTimeImmutable $date) : C\Input\Field\Duration
153 {
154 $clone = clone $this;
155 $clone->min_date = $date;
156 $clone->applyMinValue();
157 return $clone;
158 }
159
163 protected function applyMinValue()
164 {
165 $this->inputs = array_map(
166 function ($inpt) {
167 return $inpt->withMinValue($this->getMinValue());
168 },
169 $this->inputs
170 );
171 }
172
176 public function getMinValue()
177 {
178 return $this->min_date;
179 }
180
184 public function withMaxValue(\DateTimeImmutable $date) : C\Input\Field\Duration
185 {
186 $clone = clone $this;
187 $clone->max_date = $date;
188 $clone->applyMaxValue();
189 return $clone;
190 }
191
195 protected function applyMaxValue()
196 {
197 $this->inputs = array_map(
198 function ($inpt) {
199 return $inpt->withMaxValue($this->getMaxValue());
200 },
201 $this->inputs
202 );
203 }
204
208 public function getMaxValue()
209 {
210 return $this->max_date;
211 }
212
216 public function withTimeOnly(bool $with_time_only) : C\Input\Field\Duration
217 {
218 $clone = clone $this;
219 $clone->with_time_only = $with_time_only;
220 $clone->applyWithTimeOnly();
221 return $clone;
222 }
223
227 protected function applyWithTimeOnly()
228 {
229 $this->inputs = array_map(
230 function ($inpt) {
231 return $inpt->withTimeOnly($this->getTimeOnly());
232 },
233 $this->inputs
234 );
235 }
236
240 public function getTimeOnly() : bool
241 {
242 return $this->with_time_only;
243 }
244
248 public function withUseTime(bool $with_time) : C\Input\Field\Duration
249 {
250 $clone = clone $this;
251 $clone->with_time = $with_time;
252 $clone->applyWithUseTime();
253 return $clone;
254 }
255
259 public function getUseTime() : bool
260 {
261 return $this->with_time;
262 }
263
267 protected function applyWithUseTime()
268 {
269 $this->inputs = array_map(
270 function ($inpt) {
271 return $inpt->withUseTime($this->getUseTime());
272 },
273 $this->inputs
274 );
275 }
276
280 public function withTimezone(string $tz) : C\Input\Field\Duration
281 {
282 $clone = clone $this;
283 $clone->timezone = $tz;
284 $clone->inputs = array_map(
285 function ($inpt) use ($tz) {
286 return $inpt->withTimezone($tz);
287 },
288 $clone->inputs
289 );
290 return $clone;
291 }
292
296 public function getTimezone()
297 {
298 return $this->timezone;
299 }
300
304 protected function isClientSideValueOk($value) : bool
305 {
306 return true;
307 }
308
312 protected function getConstraintForRequirement()
313 {
314 return null;
315 }
316
320 public function getUpdateOnLoadCode() : \Closure
321 {
322 return function ($id) {
323 $code = "var combinedDuration = function() {
324 var options = [];
325 $('#$id').find('input').each(function() {
326 options.push($(this).val());
327 });
328 return options.join(' - ');
329 }
330 $('#$id').on('input dp.change', function(event) {
331 il.UI.input.onFieldUpdate(event, '$id', combinedDuration());
332 });
333 il.UI.input.onFieldUpdate(event, '$id', combinedDuration());";
334 return $code;
335 };
336 }
337}
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:60
addValidation()
Input is valid, if start is before end.
Definition: Duration.php:100
addTransformation()
Return-value of Duration is an assoc array with start, end and interval.
Definition: Duration.php:85
getUpdateOnLoadCode()
Get update code.This method has to return JS code that calls il.UI.filter.onFieldUpdate(event,...
Definition: Duration.php:320
language handling
$txt
Definition: error.php:13
This describes the datetime-field.
Definition: DateTime.php:14
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:14
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