ILIAS  release_8 Revision v8.25-1-g13de6a5eca6
class.ilDateDurationInputGUI.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
27{
28 protected ilObjUser $user;
29
30 protected ?ilDateTime $start = null;
31 protected ?int $startyear = null;
32 protected ?string $start_text = null;
33 protected ?string $end_text = null;
34 protected int $minute_step_size = 5;
35 protected ?ilDateTime $end = null;
36 protected bool $showtime = false;
37 protected bool $toggle_fulltime = false;
38 protected string $toggle_fulltime_txt = '';
39 protected bool $toggle_fulltime_checked = false;
40 protected bool $allowOpenIntervals = false;
41 protected string $invalid_input_start = '';
42 protected string $invalid_input_end = '';
43
44 public function __construct(
45 string $a_title = "",
46 string $a_postvar = ""
47 ) {
48 global $DIC;
49
50 $this->lng = $DIC->language();
51 $this->user = $DIC->user();
52 parent::__construct($a_title, $a_postvar);
53 $this->setType("dateduration");
54 }
55
56 // Enable toggling between date and time
57 public function enableToggleFullTime(
58 string $a_title,
59 bool $a_checked
60 ): void {
61 $this->toggle_fulltime_txt = $a_title;
62 $this->toggle_fulltime_checked = $a_checked;
63 $this->toggle_fulltime = true;
64 }
65
66 public function enabledToggleFullTime(): bool
67 {
68 return $this->toggle_fulltime;
69 }
70
80 public function setStart(ilDateTime $a_date = null): void
81 {
82 $this->start = $a_date;
83 }
84
85 public function setStartText(string $a_txt): void
86 {
87 $this->start_text = $a_txt;
88 }
89
90 public function getStartText(): ?string
91 {
92 return $this->start_text;
93 }
94
95 public function setEndText(string $a_txt): void
96 {
97 $this->end_text = $a_txt;
98 }
99
100 public function getEndText(): ?string
101 {
102 return $this->end_text;
103 }
104
105 public function getStart(): ?ilDateTime
106 {
107 return $this->start;
108 }
109
119 public function setEnd(ilDateTime $a_date = null): void
120 {
121 $this->end = $a_date;
122 }
123
124 public function getEnd(): ?ilDateTime
125 {
126 return $this->end;
127 }
128
129 public function setShowTime(bool $a_showtime): void
130 {
131 $this->showtime = $a_showtime;
132 }
133
134 public function getShowTime(): bool
135 {
136 return $this->showtime;
137 }
138
139 public function getShowSeconds(): bool
140 {
141 return false;
142 }
143
144 public function setStartYear(int $a_year): void
145 {
146 $this->startyear = $a_year;
147 }
148
149 public function getStartYear(): ?int
150 {
151 return $this->startyear;
152 }
153
159 public function setMinuteStepSize(int $a_step_size): void
160 {
161 $this->minute_step_size = $a_step_size;
162 }
163
164 public function getMinuteStepSize(): int
165 {
166 return $this->minute_step_size;
167 }
168
169 public function setValueByArray(array $a_values): void
170 {
171 $incoming = $a_values[$this->getPostVar()] ?? [];
172 if (is_array($incoming) && $incoming !== []) {
173 $format = isset($incoming['tgl']) ? 0 : $this->getDatePickerTimeFormat();
174 $this->toggle_fulltime_checked = (bool) ($incoming['tgl'] ?? false);
175
176 if ($this->openIntervalsAllowed()) {
177 if (isset($incoming['start']) && is_string($incoming['start']) && trim($incoming['start']) !== '') {
178 $this->setStart(ilCalendarUtil::parseIncomingDate($incoming["start"], (bool) $format));
179 } else {
180 $this->setStart(new ilDate(null, IL_CAL_UNIX));
181 }
182
183 if (isset($incoming['end']) && is_string($incoming['end']) && trim($incoming['end']) !== '') {
184 $this->setEnd(ilCalendarUtil::parseIncomingDate($incoming["end"], (bool) $format));
185 } else {
186 $this->setEnd(new ilDate(null, IL_CAL_UNIX));
187 }
188 } else {
189 # 0033160
190 if ($incoming['start'] instanceof ilDateTime) {
191 $this->setStart($incoming['start']);
192 } else {
193 $this->setStart(ilCalendarUtil::parseIncomingDate((string) $incoming["start"], (bool) $format));
194 }
195 if ($incoming['end'] instanceof ilDateTime) {
196 $this->setEnd($incoming['end']);
197 } else {
198 $this->setEnd(ilCalendarUtil::parseIncomingDate((string) $incoming["end"], (bool) $format));
199 }
200 }
201 }
202
203 foreach ($this->getSubItems() as $item) {
204 $item->setValueByArray($a_values);
205 }
206 }
207
208 public function checkInput(): bool
209 {
211
212 if ($this->getDisabled()) {
213 return true;
214 }
215
216 $post = $this->strArray($this->getPostVar());
217
218 $start = $post["start"] ?? '';
219 $end = $post["end"] ?? '';
220
221 // if full day is active, ignore time format
222 $format = isset($post['tgl'])
223 ? 0
224 : $this->getDatePickerTimeFormat();
225
226 // always done to make sure there are no obsolete values left
227 $this->setStart();
228 $this->setEnd();
229
230 $valid_start = false;
231 if (trim($start)) {
232 $parsed = ilCalendarUtil::parseIncomingDate($start, (bool) $format);
233 if ($parsed) {
234 $this->setStart($parsed);
235 $valid_start = true;
236 }
237 } else {
238 if (!$this->getRequired() && !trim($end)) {
239 $valid_start = true;
240 } else {
241 if ($this->openIntervalsAllowed() && !strlen(trim($start))) {
242 $valid_start = true;
243 }
244 }
245 }
246
247 $valid_end = false;
248 if (trim($end)) {
249 $parsed = ilCalendarUtil::parseIncomingDate($end, (bool) $format);
250 if ($parsed) {
251 $this->setEnd($parsed);
252 $valid_end = true;
253 }
254 } else {
255 if (!$this->getRequired() && !trim($start)) {
256 $valid_end = true;
257 } else {
258 if ($this->openIntervalsAllowed() && !strlen(trim($end))) {
259 $valid_end = true;
260 }
261 }
262 }
263
264 if ($this->getStartYear()) {
265 if ($valid_start &&
266 $this->getStart()->get(IL_CAL_FKT_DATE, "Y") < $this->getStartYear()) {
267 $valid_start = false;
268 }
269 if ($valid_end &&
270 $this->getEnd()->get(IL_CAL_FKT_DATE, "Y") < $this->getStartYear()) {
271 $valid_end = false;
272 }
273 }
274 $valid = ($valid_start && $valid_end);
275
276 if ($valid &&
277 $this->getStart() &&
278 $this->getEnd() &&
279 ilDateTime::_after($this->getStart(), $this->getEnd())) {
280 $valid = false;
281 }
282
283 if ($this->openIntervalsAllowed()) {
284 $valid = true;
285 } elseif (!$valid) {
286 $this->invalid_input_start = $start;
287 $this->invalid_input_end = $end;
288 $this->setAlert($lng->txt("form_msg_wrong_date"));
289 }
290
291 if ($valid) {
292 $valid = $this->checkSubItemsInput();
293 }
294
295 return $valid;
296 }
297
298 public function getInput(): array
299 {
300 $ret = $this->strArray($this->getPostVar());
301
302 if ($this->openIntervalsAllowed()) {
303 if (!$this->getStart()) {
304 $ret["start"] = null;
305 }
306
307 if (!$this->getEnd()) {
308 $ret["end"] = null;
309 }
310 } else {
311 if (
312 !$this->getStart() ||
313 !$this->getEnd()
314 ) {
315 $ret["start"] = null;
316 $ret["end"] = null;
317 }
318 }
319 $ret["fullday"] = (bool) ($ret["tgl"] ?? false);
320 return $ret;
321 }
322
323 protected function getDatePickerTimeFormat(): int
324 {
325 return (int) $this->getShowTime() + (int) $this->getShowSeconds();
326 }
327
331 protected function parseDatePickerConfig(): array
332 {
333 $config = null;
334 if ($this->getMinuteStepSize()) {
335 $config['stepping'] = $this->getMinuteStepSize();
336 }
337 if ($this->getStartYear()) {
338 $config['minDate'] = $this->getStartYear() . '-01-01';
339 }
340 return $config;
341 }
342
343 public function render(): string
344 {
345 $ilUser = $this->user;
347 $toggle_id = null;
348
349 $tpl = new ilTemplate("tpl.prop_datetime_duration.html", true, true, "Services/Form");
350
351 if ($this->enabledToggleFullTime()) {
352 $this->setShowTime(true);
353
354 $toggle_id = md5($this->getPostVar() . '_fulltime'); // :TODO: unique?
355
356 $tpl->setCurrentBlock('toggle_fullday');
357 $tpl->setVariable('DATE_TOGGLE_ID', $this->getPostVar() . '[tgl]');
358 $tpl->setVariable('FULLDAY_TOGGLE_ID', $toggle_id);
359 $tpl->setVariable('FULLDAY_TOGGLE_CHECKED', $this->toggle_fulltime_checked ? 'checked="checked"' : '');
360 $tpl->setVariable('FULLDAY_TOGGLE_DISABLED', $this->getDisabled() ? 'disabled="disabled"' : '');
361 $tpl->setVariable('TXT_TOGGLE_FULLDAY', $this->toggle_fulltime_txt);
362 $tpl->parseCurrentBlock();
363 }
364
365 // config picker
366 if (!$this->getDisabled()) {
367 // :TODO: unique?
368 $picker_start_id = md5($this->getPostVar() . '_start');
369 $picker_end_id = md5($this->getPostVar() . '_end');
370
371 $tpl->setVariable('DATEPICKER_START_ID', $picker_start_id);
372 $tpl->setVariable('DATEPICKER_END_ID', $picker_end_id);
373
375 $picker_start_id,
376 $this->getDatePickerTimeFormat(),
377 $this->parseDatePickerConfig(),
378 $picker_end_id,
379 $this->parseDatePickerConfig(),
380 $toggle_id,
381 "subform_" . $this->getPostVar()
382 );
383 } else {
384 $tpl->setVariable('DATEPICKER_START_DISABLED', 'disabled="disabled" ');
385 $tpl->setVariable('DATEPICKER_END_DISABLED', 'disabled="disabled" ');
386 }
387
388 $start_txt = $this->getStartText();
389 if ($start_txt === null) {
390 $start_txt = $lng->txt("form_date_duration_start");
391 }
392 if (trim($start_txt)) {
393 $tpl->setVariable('START_LABEL', $start_txt);
394 $tpl->setVariable('START_ARIA_LABEL', ilLegacyFormElementsUtil::prepareFormOutput($start_txt));
395 $tpl->touchBlock('start_width_bl');
396 }
397
398 $end_txt = $this->getEndText();
399 if ($end_txt === null) {
400 $end_txt = $lng->txt("form_date_duration_end");
401 }
402 if (trim($end_txt)) {
403 $tpl->setVariable('END_LABEL', $end_txt);
404 $tpl->setVariable('END_ARIA_LABEL', ilLegacyFormElementsUtil::prepareFormOutput($end_txt));
405 $tpl->touchBlock('end_width_bl');
406 }
407
408
409 $tpl->setVariable('DATE_START_ID', $this->getPostVar() . '[start]');
410 $tpl->setVariable('DATE_END_ID', $this->getPostVar() . '[end]');
411
412 // placeholder
413 // :TODO: i18n?
414 $pl_format = ilCalendarUtil::getUserDateFormat($this->getDatePickerTimeFormat());
415 $tpl->setVariable('START_PLACEHOLDER', $pl_format);
416 $tpl->setVariable('END_PLACEHOLDER', $pl_format);
417
418 // accessibility description
419 $tpl->setVariable(
420 'DESCRIPTION',
421 ilLegacyFormElementsUtil::prepareFormOutput($lng->txt("form_date_aria_desc") . " " . $pl_format)
422 );
423
424
425 // values
426
427 $date_value = htmlspecialchars($this->invalid_input_start);
428 if (!$date_value &&
429 $this->getStart()) {
430 $out_format = ilCalendarUtil::getUserDateFormat($this->getDatePickerTimeFormat(), true);
431 $date_value = $this->getStart()->get(IL_CAL_FKT_DATE, $out_format, $ilUser->getTimeZone());
432 }
433 $tpl->setVariable('DATEPICKER_START_VALUE', $date_value);
434
435 $date_value = htmlspecialchars($this->invalid_input_end);
436 if (!$date_value &&
437 $this->getEnd()) {
438 $out_format = ilCalendarUtil::getUserDateFormat($this->getDatePickerTimeFormat(), true);
439 $date_value = $this->getEnd()->get(IL_CAL_FKT_DATE, $out_format, $ilUser->getTimeZone());
440 }
441 $tpl->setVariable('DATEPICKER_END_VALUE', $date_value);
442
443 if ($this->getRequired()) {
444 $tpl->setVariable("START_REQUIRED", "required=\"required\"");
445 $tpl->setVariable("END_REQUIRED", "required=\"required\"");
446 }
447
448 return $tpl->get();
449 }
450
451
452 public function insert(ilTemplate $a_tpl): void
453 {
454 $html = $this->render();
455
456 $a_tpl->setCurrentBlock("prop_generic");
457 $a_tpl->setVariable("PROP_GENERIC", $html);
458 $a_tpl->parseCurrentBlock();
459 }
460
461 public function getTableFilterHTML(): string
462 {
463 return $this->render();
464 }
465
466 public function getValue(): array
467 {
468 return array(
469 'start' => $this->getStart() ? $this->getStart()->get(IL_CAL_UNIX) : null,
470 'end' => $this->getEnd() ? $this->getEnd()->get(IL_CAL_UNIX) : null
471 );
472 }
473
480 public function setValue($value): void
481 {
482 if (is_array($value)) {
483 $this->setStart(new ilDateTime($value['start'], IL_CAL_UNIX));
484 $this->setEnd(new ilDateTime($value['end'], IL_CAL_UNIX));
485 }
486 }
487
488 public function hideSubForm(): bool
489 {
490 if ($this->invalid_input_start ||
491 $this->invalid_input_end) {
492 return false;
493 }
494
495 return ((!$this->getStart() || $this->getStart()->isNull()) &&
496 (!$this->getEnd() || $this->getEnd()->isNull()));
497 }
498
499 public function openIntervalsAllowed(): bool
500 {
501 return $this->allowOpenIntervals;
502 }
503
504 public function setAllowOpenIntervals(bool $allowOpenInterval): void
505 {
506 $this->allowOpenIntervals = $allowOpenInterval;
507 }
508
509 public function getTableFilterLabelFor(): string
510 {
511 return $this->getFieldId() . "[start]";
512 }
513
514 public function getFormLabelFor(): string
515 {
516 return $this->getFieldId() . "[start]";
517 }
518}
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:514
const IL_CAL_UNIX
const IL_CAL_FKT_DATE
static getUserDateFormat(int $a_add_time=0, bool $a_for_parsing=false)
Parse current user setting into date/time format.
static parseIncomingDate($a_value, bool $a_add_time=false)
Try to parse incoming value to date object.
static addDateTimePicker(string $a_id, ?int $a_add_time=null, ?array $a_custom_config=null, ?string $a_id2=null, ?array $a_custom_config2=null, ?string $a_toggle_id=null, ?string $a_subform_id=null)
Add date time picker to element.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
checkInput()
Check input, strip slashes etc.
setAllowOpenIntervals(bool $allowOpenInterval)
getTableFilterLabelFor()
Get label "for" attribute value.
__construct(string $a_title="", string $a_postvar="")
setStart(ilDateTime $a_date=null)
Set start date E.g $dt_form->setDate(new ilDateTime(time(),IL_CAL_UTC)); or $dt_form->setDate(new ilD...
getFormLabelFor()
Get label "for" attribute value for form.
setMinuteStepSize(int $a_step_size)
Set minute step size E.g 5 => The selection will only show 00,05,10... minutes.
setEnd(ilDateTime $a_date=null)
Set end date E.g $dt_form->setDate(new ilDateTime(time(),IL_CAL_UTC)); or $dt_form->setDate(new ilDat...
setValue($value)
Called from table gui with the stored session value Attention: If the user resets the table filter,...
parseDatePickerConfig()
parse properties to datepicker config
enableToggleFullTime(string $a_title, bool $a_checked)
getTableFilterHTML()
Get input item HTML to be inserted into table filters.
@classDescription Date and time handling
static _after(ilDateTime $start, ilDateTime $end, string $a_compare_field='', string $a_tz='')
compare two dates and check start is after end This method does not consider tz offsets.
Class for single dates.
static prepareFormOutput($a_str, bool $a_strip=false)
User class.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
special template class to simplify handling of ITX/PEAR
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
$valid
global $DIC
Definition: feed.php:28
$ilUser
Definition: imgupload.php:34
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
if($DIC->http() ->request() ->getMethod()=="GET" &&isset($DIC->http() ->request() ->getQueryParams()['tex'])) $tpl
Definition: latex.php:41
$post
Definition: ltitoken.php:49
$format
Definition: metadata.php:235
if(!array_key_exists('PATH_INFO', $_SERVER)) $config
Definition: metadata.php:85
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
$lng