ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 
43  public function __construct($a_title = "", $a_postvar = "")
44  {
45  global $DIC;
46 
47  $this->lng = $DIC->language();
48  $this->user = $DIC->user();
49  parent::__construct($a_title, $a_postvar);
50  $this->setType("dateduration");
51  }
52 
59  public function enableToggleFullTime($a_title, $a_checked)
60  {
61  $this->toggle_fulltime_txt = $a_title;
62  $this->toggle_fulltime_checked = $a_checked;
63  $this->toggle_fulltime = true;
64  }
65 
70  public function enabledToggleFullTime()
71  {
73  }
74 
86  public function setStart(ilDateTime $a_date = null)
87  {
88  $this->start = $a_date;
89  }
90 
96  public function setStartText($a_txt)
97  {
98  $this->start_text = $a_txt;
99  }
100 
105  public function getStartText()
106  {
107  return $this->start_text;
108  }
109 
115  public function setEndText($a_txt)
116  {
117  $this->end_text = $a_txt;
118  }
119 
124  public function getEndText()
125  {
126  return $this->end_text;
127  }
128 
134  public function getStart()
135  {
136  return $this->start;
137  }
138 
150  public function setEnd(ilDateTime $a_date = null)
151  {
152  $this->end = $a_date;
153  }
154 
160  public function getEnd()
161  {
162  return $this->end;
163  }
164 
170  public function setShowTime($a_showtime)
171  {
172  $this->showtime = $a_showtime;
173  }
174 
180  public function getShowTime()
181  {
182  return $this->showtime;
183  }
184 
189  public function getShowSeconds()
190  {
191  return false;
192  }
193 
199  public function setStartYear($a_year)
200  {
201  $this->startyear = $a_year;
202  }
203 
209  public function getStartYear()
210  {
211  return $this->startyear;
212  }
213 
222  public function setMinuteStepSize($a_step_size)
223  {
224  $this->minute_step_size = $a_step_size;
225  }
226 
233  public function getMinuteStepSize()
234  {
236  }
237 
243  public function setValueByArray($a_values)
244  {
245  $incoming = $a_values[$this->getPostVar()];
246  if (is_array($incoming)) {
247  $format = $incoming['tgl'] ? 0 : $this->getDatePickerTimeFormat();
248  $this->toggle_fulltime_checked = (bool) $incoming['tgl'];
249  $this->setStart(ilCalendarUtil::parseIncomingDate($incoming["start"], $format));
250  $this->setEnd(ilCalendarUtil::parseIncomingDate($incoming["end"], $format));
251  }
252 
253  foreach ($this->getSubItems() as $item) {
254  $item->setValueByArray($a_values);
255  }
256  }
257 
263  public function checkInput()
264  {
265  $lng = $this->lng;
266 
267  if ($this->getDisabled()) {
268  return true;
269  }
270 
271  $post = $_POST[$this->getPostVar()];
272  if (!is_array($post)) {
273  return false;
274  }
275 
276  $start = $post["start"];
277  $end = $post["end"];
278 
279  // if full day is active, ignore time format
280  $format = $post['tgl']
281  ? 0
282  : $this->getDatePickerTimeFormat();
283 
284  // always done to make sure there are no obsolete values left
285  $this->setStart(null);
286  $this->setEnd(null);
287 
288  $valid_start = false;
289  if (trim($start)) {
291  if ($parsed) {
292  $this->setStart($parsed);
293  $valid_start = true;
294  }
295  } elseif (!$this->getRequired() && !trim($end)) {
296  $valid_start = true;
297  }
298 
299  $valid_end = false;
300  if (trim($end)) {
302  if ($parsed) {
303  $this->setEnd($parsed);
304  $valid_end = true;
305  }
306  } elseif (!$this->getRequired() && !trim($start)) {
307  $valid_end = true;
308  }
309 
310  if ($this->getStartYear()) {
311  if ($valid_start &&
312  $this->getStart()->get(IL_CAL_FKT_DATE, "Y") < $this->getStartYear()) {
313  $valid_start = false;
314  }
315  if ($valid_end &&
316  $this->getEnd()->get(IL_CAL_FKT_DATE, "Y") < $this->getStartYear()) {
317  $valid_end = false;
318  }
319  }
320 
321  $valid = ($valid_start && $valid_end);
322 
323  if ($valid &&
324  $this->getStart() &&
325  $this->getEnd() &&
326  ilDateTime::_after($this->getStart(), $this->getEnd())) {
327  $valid = false;
328  }
329 
330  if (!$valid) {
331  $this->invalid_input_start = $start;
332  $this->invalid_input_end = $end;
333 
334  $_POST[$this->getPostVar()]["start"] = null;
335  $_POST[$this->getPostVar()]["end"] = null;
336 
337  $this->setAlert($lng->txt("form_msg_wrong_date"));
338  } else {
339  if (
340  !$this->getStart() ||
341  !$this->getEnd()
342  ) {
343  $_POST[$this->getPostVar()]["start"] = null;
344  $_POST[$this->getPostVar()]["end"] = null;
345  }
346  }
347 
348  if ($valid) {
349  $valid = $this->checkSubItemsInput();
350  }
351 
352  return $valid;
353  }
354 
355  protected function getDatePickerTimeFormat()
356  {
357  return (int) $this->getShowTime() + (int) $this->getShowSeconds();
358  }
359 
365  protected function parseDatePickerConfig()
366  {
367  $config = null;
368  if ($this->getMinuteStepSize()) {
369  $config['stepping'] = (int) $this->getMinuteStepSize();
370  }
371  if ($this->getStartYear()) {
372  $config['minDate'] = $this->getStartYear() . '-01-01';
373  }
374  return $config;
375  }
376 
381  public function render()
382  {
384  $lng = $this->lng;
385 
386  $tpl = new ilTemplate("tpl.prop_datetime_duration.html", true, true, "Services/Form");
387 
388  if ($this->enabledToggleFullTime()) {
389  $this->setShowTime(true);
390 
391  $toggle_id = md5($this->getPostVar() . '_fulltime'); // :TODO: unique?
392 
393  $tpl->setCurrentBlock('toggle_fullday');
394  $tpl->setVariable('DATE_TOGGLE_ID', $this->getPostVar() . '[tgl]');
395  $tpl->setVariable('FULLDAY_TOGGLE_ID', $toggle_id);
396  $tpl->setVariable('FULLDAY_TOGGLE_CHECKED', $this->toggle_fulltime_checked ? 'checked="checked"' : '');
397  $tpl->setVariable('FULLDAY_TOGGLE_DISABLED', $this->getDisabled() ? 'disabled="disabled"' : '');
398  $tpl->setVariable('TXT_TOGGLE_FULLDAY', $this->toggle_fulltime_txt);
399  $tpl->parseCurrentBlock();
400  }
401 
402  // config picker
403  if (!$this->getDisabled()) {
404  // :TODO: unique?
405  $picker_start_id = md5($this->getPostVar() . '_start');
406  $picker_end_id = md5($this->getPostVar() . '_end');
407 
408  $tpl->setVariable('DATEPICKER_START_ID', $picker_start_id);
409  $tpl->setVariable('DATEPICKER_END_ID', $picker_end_id);
410 
412  $picker_start_id,
413  $this->getDatePickerTimeFormat(),
414  $this->parseDatePickerConfig(),
415  $picker_end_id,
416  $this->parseDatePickerConfig(),
417  $toggle_id,
418  "subform_" . $this->getPostVar()
419  );
420  } else {
421  $tpl->setVariable('DATEPICKER_START_DISABLED', 'disabled="disabled" ');
422  $tpl->setVariable('DATEPICKER_END_DISABLED', 'disabled="disabled" ');
423  }
424 
425  $start_txt = $this->getStartText();
426  if ($start_txt === null) {
427  $start_txt = $lng->txt("form_date_duration_start");
428  }
429  if (trim($start_txt)) {
430  $tpl->setVariable('START_LABEL', $start_txt);
431  $tpl->touchBlock('start_width_bl');
432  }
433 
434  $end_txt = $this->getEndText();
435  if ($end_txt === null) {
436  $end_txt = $lng->txt("form_date_duration_end");
437  }
438  if (trim($end_txt)) {
439  $tpl->setVariable('END_LABEL', $end_txt);
440  $tpl->touchBlock('end_width_bl');
441  }
442 
443 
444  $tpl->setVariable('DATE_START_ID', $this->getPostVar() . '[start]');
445  $tpl->setVariable('DATE_END_ID', $this->getPostVar() . '[end]');
446 
447  // placeholder
448  // :TODO: i18n?
450  $tpl->setVariable('START_PLACEHOLDER', $pl_format);
451  $tpl->setVariable('END_PLACEHOLDER', $pl_format);
452 
453 
454  // values
455 
456  $date_value = htmlspecialchars($this->invalid_input_start);
457  if (!$date_value &&
458  $this->getStart()) {
459  $out_format = ilCalendarUtil::getUserDateFormat($this->getDatePickerTimeFormat(), true);
460  $date_value = $this->getStart()->get(IL_CAL_FKT_DATE, $out_format, $ilUser->getTimeZone());
461  }
462  $tpl->setVariable('DATEPICKER_START_VALUE', $date_value);
463 
464  $date_value = htmlspecialchars($this->invalid_input_end);
465  if (!$date_value &&
466  $this->getEnd()) {
467  $out_format = ilCalendarUtil::getUserDateFormat($this->getDatePickerTimeFormat(), true);
468  $date_value = $this->getEnd()->get(IL_CAL_FKT_DATE, $out_format, $ilUser->getTimeZone());
469  }
470  $tpl->setVariable('DATEPICKER_END_VALUE', $date_value);
471 
472  if ($this->getRequired()) {
473  $tpl->setVariable("START_REQUIRED", "required=\"required\"");
474  $tpl->setVariable("END_REQUIRED", "required=\"required\"");
475  }
476 
477  return $tpl->get();
478  }
479 
485  public function insert($a_tpl)
486  {
487  $html = $this->render();
488 
489  $a_tpl->setCurrentBlock("prop_generic");
490  $a_tpl->setVariable("PROP_GENERIC", $html);
491  $a_tpl->parseCurrentBlock();
492  }
493 
498  public function getTableFilterHTML()
499  {
500  return $this->render();
501  }
502 
507  public function getValue()
508  {
509  return array(
510  'start' => $this->getStart()->get(IL_CAL_UNIX),
511  'end' => $this->getEnd()->get(IL_CAL_UNIX)
512  );
513  }
514 
521  public function setValue($value)
522  {
523  if (is_array($value)) {
524  $this->setStart(new ilDateTime($value['start'], IL_CAL_UNIX));
525  $this->setEnd(new ilDateTime($value['end'], IL_CAL_UNIX));
526  }
527  }
528 
529  public function hideSubForm()
530  {
531  if ($this->invalid_input_start ||
532  $this->invalid_input_end) {
533  return false;
534  }
535 
536  return ((!$this->getStart() || $this->getStart()->isNull()) &&
537  (!$this->getEnd() || $this->getEnd()->isNull()));
538  }
539 }
getShowSeconds()
Show seconds not implemented yet.
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
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.
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...
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.
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.
Create styles array
The data for the language used.
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.