ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilDateDurationInputGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once 'Services/Table/interfaces/interface.ilTableFilterItem.php';
5 
15 {
19  protected $lng;
20 
24  protected $user;
25 
26  protected $start = null;
27  protected $startyear = null;
28  protected $start_text = null;
29  protected $end_text = null;
30  protected $minute_step_size = 5;
31  protected $end = null;
32  protected $showtime = false;
33  protected $toggle_fulltime = false;
34  protected $toggle_fulltime_txt = '';
35  protected $toggle_fulltime_checked = false;
36  protected $allowOpenIntervals = false;
37 
44  public function __construct($a_title = "", $a_postvar = "")
45  {
46  global $DIC;
47 
48  $this->lng = $DIC->language();
49  $this->user = $DIC->user();
50  parent::__construct($a_title, $a_postvar);
51  $this->setType("dateduration");
52  }
53 
60  public function enableToggleFullTime($a_title, $a_checked)
61  {
62  $this->toggle_fulltime_txt = $a_title;
63  $this->toggle_fulltime_checked = $a_checked;
64  $this->toggle_fulltime = true;
65  }
66 
71  public function enabledToggleFullTime()
72  {
74  }
75 
87  public function setStart(ilDateTime $a_date = null)
88  {
89  $this->start = $a_date;
90  }
91 
97  public function setStartText($a_txt)
98  {
99  $this->start_text = $a_txt;
100  }
101 
106  public function getStartText()
107  {
108  return $this->start_text;
109  }
110 
116  public function setEndText($a_txt)
117  {
118  $this->end_text = $a_txt;
119  }
120 
125  public function getEndText()
126  {
127  return $this->end_text;
128  }
129 
135  public function getStart()
136  {
137  return $this->start;
138  }
139 
151  public function setEnd(ilDateTime $a_date = null)
152  {
153  $this->end = $a_date;
154  }
155 
161  public function getEnd()
162  {
163  return $this->end;
164  }
165 
171  public function setShowTime($a_showtime)
172  {
173  $this->showtime = $a_showtime;
174  }
175 
181  public function getShowTime()
182  {
183  return $this->showtime;
184  }
185 
190  public function getShowSeconds()
191  {
192  return false;
193  }
194 
200  public function setStartYear($a_year)
201  {
202  $this->startyear = $a_year;
203  }
204 
210  public function getStartYear()
211  {
212  return $this->startyear;
213  }
214 
223  public function setMinuteStepSize($a_step_size)
224  {
225  $this->minute_step_size = $a_step_size;
226  }
227 
234  public function getMinuteStepSize()
235  {
237  }
238 
244  public function setValueByArray($a_values)
245  {
246  $incoming = $a_values[$this->getPostVar()];
247  if (is_array($incoming)) {
248  $format = $incoming['tgl'] ? 0 : $this->getDatePickerTimeFormat();
249  $this->toggle_fulltime_checked = (bool) $incoming['tgl'];
250 
251  if ($this->openIntervalsAllowed()) {
252  if (is_string($incoming['start']) && trim($incoming['start']) !== '') {
253  $this->setStart(ilCalendarUtil::parseIncomingDate($incoming["start"], $format));
254  } else {
255  $this->setStart(new ilDate(null, IL_CAL_UNIX));
256  }
257 
258  if (is_string($incoming['end']) && trim($incoming['end']) !== '') {
259  $this->setEnd(ilCalendarUtil::parseIncomingDate($incoming["end"], $format));
260  } else {
261  $this->setEnd(new ilDate(null, IL_CAL_UNIX));
262  }
263  } else {
264  $this->setStart(ilCalendarUtil::parseIncomingDate($incoming["start"], $format));
265  $this->setEnd(ilCalendarUtil::parseIncomingDate($incoming["end"], $format));
266  }
267  }
268 
269  foreach ($this->getSubItems() as $item) {
270  $item->setValueByArray($a_values);
271  }
272  }
273 
279  public function checkInput()
280  {
281  $lng = $this->lng;
282 
283  if ($this->getDisabled()) {
284  return true;
285  }
286 
287  $post = $_POST[$this->getPostVar()];
288  if (!is_array($post)) {
289  return false;
290  }
291 
292  $start = $post["start"];
293  $end = $post["end"];
294 
295  // if full day is active, ignore time format
296  $format = $post['tgl']
297  ? 0
298  : $this->getDatePickerTimeFormat();
299 
300  // always done to make sure there are no obsolete values left
301  $this->setStart(null);
302  $this->setEnd(null);
303 
304  $valid_start = false;
305  if (trim($start)) {
307  if ($parsed) {
308  $this->setStart($parsed);
309  $valid_start = true;
310  }
311  } else {
312  if (!$this->getRequired() && !trim($end)) {
313  $valid_start = true;
314  } else {
315  if ($this->openIntervalsAllowed() && !strlen(trim($start))) {
316  $valid_start = true;
317  }
318  }
319  }
320 
321  $valid_end = false;
322  if (trim($end)) {
324  if ($parsed) {
325  $this->setEnd($parsed);
326  $valid_end = true;
327  }
328  } else {
329  if (!$this->getRequired() && !trim($start)) {
330  $valid_end = true;
331  } else {
332  if ($this->openIntervalsAllowed() && !strlen(trim($end))) {
333  $valid_end = true;
334  }
335  }
336  }
337 
338  if ($this->getStartYear()) {
339  if ($valid_start &&
340  $this->getStart()->get(IL_CAL_FKT_DATE, "Y") < $this->getStartYear()) {
341  $valid_start = false;
342  }
343  if ($valid_end &&
344  $this->getEnd()->get(IL_CAL_FKT_DATE, "Y") < $this->getStartYear()) {
345  $valid_end = false;
346  }
347  }
348  $valid = ($valid_start && $valid_end);
349 
350  if ($valid &&
351  $this->getStart() &&
352  $this->getEnd() &&
353  ilDateTime::_after($this->getStart(), $this->getEnd())) {
354  $valid = false;
355  }
356 
357  if ($this->openIntervalsAllowed()) {
358  if (!$this->getStart()) {
359  $_POST[$this->getPostVar()]["start"] = null;
360  }
361 
362  if (!$this->getEnd()) {
363  $_POST[$this->getPostVar()]["end"] = null;
364  }
365  $valid = true;
366  } elseif (!$valid) {
367  $this->invalid_input_start = $start;
368  $this->invalid_input_end = $end;
369 
370  $_POST[$this->getPostVar()]["start"] = null;
371  $_POST[$this->getPostVar()]["end"] = null;
372 
373  $this->setAlert($lng->txt("form_msg_wrong_date"));
374  } else {
375  if (
376  !$this->getStart() ||
377  !$this->getEnd()
378  ) {
379  $_POST[$this->getPostVar()]["start"] = null;
380  $_POST[$this->getPostVar()]["end"] = null;
381  }
382  }
383 
384  if ($valid) {
385  $valid = $this->checkSubItemsInput();
386  }
387 
388  return $valid;
389  }
390 
391  protected function getDatePickerTimeFormat()
392  {
393  return (int) $this->getShowTime() + (int) $this->getShowSeconds();
394  }
395 
401  protected function parseDatePickerConfig()
402  {
403  $config = null;
404  if ($this->getMinuteStepSize()) {
405  $config['stepping'] = (int) $this->getMinuteStepSize();
406  }
407  if ($this->getStartYear()) {
408  $config['minDate'] = $this->getStartYear() . '-01-01';
409  }
410  return $config;
411  }
412 
417  public function render()
418  {
420  $lng = $this->lng;
421 
422  $tpl = new ilTemplate("tpl.prop_datetime_duration.html", true, true, "Services/Form");
423 
424  if ($this->enabledToggleFullTime()) {
425  $this->setShowTime(true);
426 
427  $toggle_id = md5($this->getPostVar() . '_fulltime'); // :TODO: unique?
428 
429  $tpl->setCurrentBlock('toggle_fullday');
430  $tpl->setVariable('DATE_TOGGLE_ID', $this->getPostVar() . '[tgl]');
431  $tpl->setVariable('FULLDAY_TOGGLE_ID', $toggle_id);
432  $tpl->setVariable('FULLDAY_TOGGLE_CHECKED', $this->toggle_fulltime_checked ? 'checked="checked"' : '');
433  $tpl->setVariable('FULLDAY_TOGGLE_DISABLED', $this->getDisabled() ? 'disabled="disabled"' : '');
434  $tpl->setVariable('TXT_TOGGLE_FULLDAY', $this->toggle_fulltime_txt);
435  $tpl->parseCurrentBlock();
436  }
437 
438  // config picker
439  if (!$this->getDisabled()) {
440  // :TODO: unique?
441  $picker_start_id = md5($this->getPostVar() . '_start');
442  $picker_end_id = md5($this->getPostVar() . '_end');
443 
444  $tpl->setVariable('DATEPICKER_START_ID', $picker_start_id);
445  $tpl->setVariable('DATEPICKER_END_ID', $picker_end_id);
446 
448  $picker_start_id,
449  $this->getDatePickerTimeFormat(),
450  $this->parseDatePickerConfig(),
451  $picker_end_id,
452  $this->parseDatePickerConfig(),
453  $toggle_id,
454  "subform_" . $this->getPostVar()
455  );
456  } else {
457  $tpl->setVariable('DATEPICKER_START_DISABLED', 'disabled="disabled" ');
458  $tpl->setVariable('DATEPICKER_END_DISABLED', 'disabled="disabled" ');
459  }
460 
461  $start_txt = $this->getStartText();
462  if ($start_txt === null) {
463  $start_txt = $lng->txt("form_date_duration_start");
464  }
465  if (trim($start_txt)) {
466  $tpl->setVariable('START_LABEL', $start_txt);
467  $tpl->setVariable('START_ARIA_LABEL', ilUtil::prepareFormOutput($start_txt));
468  $tpl->touchBlock('start_width_bl');
469  }
470 
471  $end_txt = $this->getEndText();
472  if ($end_txt === null) {
473  $end_txt = $lng->txt("form_date_duration_end");
474  }
475  if (trim($end_txt)) {
476  $tpl->setVariable('END_LABEL', $end_txt);
477  $tpl->setVariable('END_ARIA_LABEL', ilUtil::prepareFormOutput($end_txt));
478  $tpl->touchBlock('end_width_bl');
479  }
480 
481 
482  $tpl->setVariable('DATE_START_ID', $this->getPostVar() . '[start]');
483  $tpl->setVariable('DATE_END_ID', $this->getPostVar() . '[end]');
484 
485  // placeholder
486  // :TODO: i18n?
488  $tpl->setVariable('START_PLACEHOLDER', $pl_format);
489  $tpl->setVariable('END_PLACEHOLDER', $pl_format);
490 
491  // accessibility description
492  $tpl->setVariable(
493  'DESCRIPTION',
494  ilUtil::prepareFormOutput($lng->txt("form_date_aria_desc") . " " . $pl_format)
495  );
496 
497 
498  // values
499 
500  $date_value = htmlspecialchars($this->invalid_input_start);
501  if (!$date_value &&
502  $this->getStart()) {
503  $out_format = ilCalendarUtil::getUserDateFormat($this->getDatePickerTimeFormat(), true);
504  $date_value = $this->getStart()->get(IL_CAL_FKT_DATE, $out_format, $ilUser->getTimeZone());
505  }
506  $tpl->setVariable('DATEPICKER_START_VALUE', $date_value);
507 
508  $date_value = htmlspecialchars($this->invalid_input_end);
509  if (!$date_value &&
510  $this->getEnd()) {
511  $out_format = ilCalendarUtil::getUserDateFormat($this->getDatePickerTimeFormat(), true);
512  $date_value = $this->getEnd()->get(IL_CAL_FKT_DATE, $out_format, $ilUser->getTimeZone());
513  }
514  $tpl->setVariable('DATEPICKER_END_VALUE', $date_value);
515 
516  if ($this->getRequired()) {
517  $tpl->setVariable("START_REQUIRED", "required=\"required\"");
518  $tpl->setVariable("END_REQUIRED", "required=\"required\"");
519  }
520 
521  return $tpl->get();
522  }
523 
529  public function insert($a_tpl)
530  {
531  $html = $this->render();
532 
533  $a_tpl->setCurrentBlock("prop_generic");
534  $a_tpl->setVariable("PROP_GENERIC", $html);
535  $a_tpl->parseCurrentBlock();
536  }
537 
542  public function getTableFilterHTML()
543  {
544  return $this->render();
545  }
546 
551  public function getValue()
552  {
553  return array(
554  'start' => $this->getStart()->get(IL_CAL_UNIX),
555  'end' => $this->getEnd()->get(IL_CAL_UNIX)
556  );
557  }
558 
565  public function setValue($value)
566  {
567  if (is_array($value)) {
568  $this->setStart(new ilDateTime($value['start'], IL_CAL_UNIX));
569  $this->setEnd(new ilDateTime($value['end'], IL_CAL_UNIX));
570  }
571  }
572 
573  public function hideSubForm()
574  {
575  if ($this->invalid_input_start ||
576  $this->invalid_input_end) {
577  return false;
578  }
579 
580  return ((!$this->getStart() || $this->getStart()->isNull()) &&
581  (!$this->getEnd() || $this->getEnd()->isNull()));
582  }
583 
587  public function openIntervalsAllowed() : bool
588  {
590  }
591 
595  public function setAllowOpenIntervals(bool $allowOpenInterval)
596  {
597  $this->allowOpenIntervals = $allowOpenInterval;
598  }
599 
603  public function getTableFilterLabelFor() {
604  return $this->getFieldId()."[start]";
605  }
606 
610  public function getFormLabelFor() {
611  return $this->getFieldId()."[start]";
612  }
613 
614 }
getShowSeconds()
Show seconds not implemented yet.
static prepareFormOutput($a_str, $a_strip=false)
prepares string output for html forms public
Interface for property form input GUI classes that can be used in table filters.
__construct($a_title="", $a_postvar="")
Constructor.
$format
Definition: metadata.php:141
$config
Definition: bootstrap.php:15
global $DIC
Definition: saml.php:7
$tpl
Definition: ilias.php:10
checkInput()
Check input, strip slashes etc.
setStartYear($a_year)
Set start year.
getPostVar()
Get Post Variable.
setAllowOpenIntervals(bool $allowOpenInterval)
enabledToggleFullTime()
Check if toggling between date and time enabled.
static _after(ilDateTime $start, ilDateTime $end, $a_compare_field='', $a_tz='')
compare two dates and check start is after end This method does not consider tz offsets.
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...
$valid
parseDatePickerConfig()
parse properties to datepicker config
getTableFilterHTML()
Used for table filter presentation.
const IL_CAL_UNIX
getValue()
Used for storing the date duration data in session for table gui filters.
user()
Definition: user.php:4
setAlert($a_alert)
Set Alert Text.
getShowTime()
Get Show Time Information.
setType($a_type)
Set Type.
getMinuteStepSize()
Get minute step size.
input GUI for a time span (start and end date)
setMinuteStepSize($a_step_size)
Set minute step size E.g 5 => The selection will only show 00,05,10...
Class for single dates.
static getUserDateFormat($a_add_time=false, $a_for_parsing=false)
Parse current user setting into date/time format.
static addDateTimePicker($a_id, $a_add_time=null, array $a_custom_config=null, $a_id2=null, $a_custom_config2=null, $a_toggle_id=null, $a_subform_id=null)
Add date time picker to element.
getFieldId()
Get Post Variable.
special template class to simplify handling of ITX/PEAR
const IL_CAL_FKT_DATE
$post
Definition: post.php:34
Date and time handling
$ilUser
Definition: imgupload.php:18
setValueByArray($a_values)
Set value by array.
setShowTime($a_showtime)
Set Show Time Information.
setStartText($a_txt)
Set text, which will be shown before the start date.
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...
setEndText($a_txt)
Set text, which will be shown before the end date.
This class represents a property that may include a sub form.
setValue($value)
Called from table gui with the stored session value Attention: If the user resets the table filter...
insert($a_tpl)
Insert property html.
enableToggleFullTime($a_title, $a_checked)
Enable toggling between date and time.
$_POST["username"]
$html
Definition: example_001.php:87
static parseIncomingDate($a_value, $a_add_time=null)
Try to parse incoming value to date object.