ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilDateTimeInputGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
11 include_once("./Services/Table/interfaces/interface.ilTableFilterItem.php");
13 {
14  protected $mode = null;
15  protected $date_obj = null;
16  protected $date;
17  protected $time = "00:00:00";
18  protected $showtime = false;
19  protected $showseconds = false;
20  protected $minute_step_size = 1;
21  protected $show_empty = false;
22  protected $startyear = '';
23 
24  protected $activation_title = '';
25  protected $activation_post_var = '';
26 
27  const MODE_SELECT = 1;
28  const MODE_INPUT = 2;
29 
36  function __construct($a_title = "", $a_postvar = "")
37  {
38  parent::__construct($a_title, $a_postvar);
39  $this->setType("datetime");
40  $this->setMode(self::MODE_SELECT);
41  }
42 
48  function setMode($mode)
49  {
50  if(in_array($mode, array(self::MODE_INPUT, self::MODE_SELECT)))
51  {
52  $this->mode = $mode;
53  }
54  }
55 
61  function getMode()
62  {
63  return $this->mode;
64  }
65 
76  public function enableDateActivation($a_title,$a_postvar,$a_checked = true)
77  {
78  $this->activation_title = $a_title;
79  $this->activation_post_var = $a_postvar;
80  $this->activation_checked = $a_checked;
81  }
82 
89  public function getActivationPostVar()
90  {
92  }
93 
105  function setDate(ilDateTime $a_date = NULL)
106  {
107  $this->date = $a_date;
108  }
109 
115  function getDate()
116  {
117  return $this->date;
118  }
119 
125  function setShowTime($a_showtime)
126  {
127  $this->showtime = $a_showtime;
128  }
129 
135  function getShowTime()
136  {
137  return $this->showtime;
138  }
139 
145  function setShowEmpty($a_empty)
146  {
147  $this->show_empty = $a_empty;
148  }
149 
155  function getShowEmpty()
156  {
157  return $this->show_empty;
158  }
159 
165  function setStartYear($a_year)
166  {
167  $this->startyear = $a_year;
168  }
169 
175  function getStartYear()
176  {
177  return $this->startyear;
178  }
179 
188  public function setMinuteStepSize($a_step_size)
189  {
190  $this->minute_step_size = $a_step_size;
191  }
192 
199  public function getMinuteStepSize()
200  {
202  }
203 
204 
205 
211  function setShowSeconds($a_showseconds)
212  {
213  $this->showseconds = $a_showseconds;
214  }
215 
221  function getShowSeconds()
222  {
223  return $this->showseconds;
224  }
225 
231  function setValueByArray($a_values)
232  {
233  global $ilUser;
234 
235  if($this->getMode() == self::MODE_INPUT &&
236  (!isset($a_values[$this->getPostVar()]["date"]) || $a_values[$this->getPostVar()]["date"] == ""))
237  {
238  $this->date = NULL;
239  }
240  else if(isset($a_values[$this->getPostVar()]["time"]))
241  {
242  $this->setDate(new ilDateTime($a_values[$this->getPostVar()]["date"].' '.$a_values[$this->getPostVar()]["time"],
243  IL_CAL_DATETIME,$ilUser->getTimeZone()));
244  }
245  else if (isset($a_values[$this->getPostVar()]["date"]))
246  {
247  $this->setDate(new ilDate($a_values[$this->getPostVar()]["date"],
248  IL_CAL_DATE));
249  }
250 
251  if($this->activation_post_var)
252  {
253  $this->activation_checked = (bool)$a_values[$this->activation_post_var];
254  }
255 
256  foreach($this->getSubItems() as $item)
257  {
258  $item->setValueByArray($a_values);
259  }
260  }
261 
267  function checkInput()
268  {
269  global $ilUser, $lng;
270 
271  if ($this->getDisabled())
272  {
273  return true;
274  }
275 
276  $post = $_POST[$this->getPostVar()];
277 
278  // empty date valid with input field
279  if(!$this->getRequired() && $this->getMode() == self::MODE_INPUT && $post["date"] == "")
280  {
281  return true;
282  }
283 
284  if($this->getMode() == self::MODE_SELECT)
285  {
286  $post["date"]["y"] = ilUtil::stripSlashes($post["date"]["y"]);
287  $post["date"]["m"] = ilUtil::stripSlashes($post["date"]["m"]);
288  $post["date"]["d"] = ilUtil::stripSlashes($post["date"]["d"]);
289  $dt['year'] = (int) $post['date']['y'];
290  $dt['mon'] = (int) $post['date']['m'];
291  $dt['mday'] = (int) $post['date']['d'];
292 
293  if($this->getShowTime())
294  {
295  $post["time"]["h"] = ilUtil::stripSlashes($post["time"]["h"]);
296  $post["time"]["m"] = ilUtil::stripSlashes($post["time"]["m"]);
297  $post["time"]["s"] = ilUtil::stripSlashes($post["time"]["s"]);
298  $dt['hours'] = (int) $post['time']['h'];
299  $dt['minutes'] = (int) $post['time']['m'];
300  $dt['seconds'] = (int) $post['time']['s'];
301  }
302  }
303  else
304  {
305  $post["date"] = ilUtil::stripSlashes($post["date"]);
306  $post["time"] = ilUtil::stripSlashes($post["time"]);
307 
308  if($post["date"])
309  {
310  switch($ilUser->getDateFormat())
311  {
313  $date = explode(".", $post["date"]);
314  $dt['mday'] = (int)$date[0];
315  $dt['mon'] = (int)$date[1];
316  $dt['year'] = (int)$date[2];
317  break;
318 
320  $date = explode("-", $post["date"]);
321  $dt['mday'] = (int)$date[2];
322  $dt['mon'] = (int)$date[1];
323  $dt['year'] = (int)$date[0];
324  break;
325 
327  $date = explode("/", $post["date"]);
328  $dt['mday'] = (int)$date[1];
329  $dt['mon'] = (int)$date[0];
330  $dt['year'] = (int)$date[2];
331  break;
332  }
333 
334  if($this->getShowTime())
335  {
336  if($ilUser->getTimeFormat() == ilCalendarSettings::TIME_FORMAT_12)
337  {
338  $seconds = "";
339  if($this->getShowSeconds())
340  {
341  $seconds = ":\s*([0-9]{1,2})\s*";
342  }
343  if(preg_match("/([0-9]{1,2})\s*:\s*([0-9]{1,2})\s*".$seconds."(am|pm)/", trim(strtolower($post["time"])), $matches))
344  {
345  $dt['hours'] = (int)$matches[1];
346  $dt['minutes'] = (int)$matches[2];
347  if($seconds)
348  {
349  $dt['seconds'] = (int)$time[2];
350  $ampm = $matches[4];
351  }
352  else
353  {
354  $dt['seconds'] = 0;
355  $ampm = $matches[3];
356  }
357  if($dt['hours'] == 12)
358  {
359  if($ampm == "am")
360  {
361  $dt['hours'] = 0;
362  }
363  }
364  else if($ampm == "pm")
365  {
366  $dt['hours'] += 12;
367  }
368  }
369  }
370  else
371  {
372  $time = explode(":", $post["time"]);
373  $dt['hours'] = (int)$time[0];
374  $dt['minutes'] = (int)$time[1];
375  $dt['seconds'] = (int)$time[2];
376  }
377  }
378  }
379  }
380 
381  // very basic validation
382  if($dt['mday'] == 0 || $dt['mon'] == 0 || $dt['year'] == 0 || $dt['mday'] > 31 || $dt['mon'] > 12)
383  {
384  $dt = false;
385  }
386  else if($this->getShowTime() && ($dt['hours'] > 23 || $dt['minutes'] > 59 || $dt['seconds'] > 59))
387  {
388  $dt = false;
389  }
390 
391  // #11847
392  if(!checkdate($dt['mon'], $dt['mday'], $dt['year']))
393  {
394  $this->invalid_input = $_POST[$this->getPostVar()]['date'];
395  $this->setAlert($lng->txt("exc_date_not_valid"));
396  $dt = false;
397  }
398 
399  $date = new ilDateTime($dt, IL_CAL_FKT_GETDATE, $ilUser->getTimeZone());
400  $this->setDate($date);
401 
402  // post values used to be overwritten anyways - cannot change behaviour
403  $_POST[$this->getPostVar()]['date'] = $date->get(IL_CAL_FKT_DATE, 'Y-m-d', $ilUser->getTimeZone());
404  $_POST[$this->getPostVar()]['time'] = $date->get(IL_CAL_FKT_DATE, 'H:i:s', $ilUser->getTimeZone());
405 
406  return (bool)$dt;
407  }
408 
413  function render()
414  {
415  global $lng,$ilUser;
416 
417  $tpl = new ilTemplate("tpl.prop_datetime.html", true, true, "Services/Form");
418 
419  $lng->loadLanguageModule("jscalendar");
420  require_once("./Services/Calendar/classes/class.ilCalendarUtil.php");
422 
423  if(strlen($this->getActivationPostVar()))
424  {
425  $tpl->setCurrentBlock('prop_date_activation');
426  $tpl->setVariable('CHECK_ENABLED_DATE',$this->getActivationPostVar());
427  $tpl->setVariable('TXT_DATE_ENABLED',$this->activation_title);
428  $tpl->setVariable('CHECKED_ENABLED',$this->activation_checked ? 'checked="checked"' : '');
429  $tpl->setVariable('CHECKED_DISABLED',$this->getDisabled() ? 'disabled="disabled" ' : '');
430  $tpl->parseCurrentBlock();
431  }
432 
433  if($this->getMode() == self::MODE_SELECT)
434  {
435  if(is_a($this->getDate(),'ilDate'))
436  {
437  $date_info = $this->getDate()->get(IL_CAL_FKT_GETDATE,'','UTC');
438  }
439  elseif(is_a($this->getDate(),'ilDateTime'))
440  {
441  $date_info = $this->getDate()->get(IL_CAL_FKT_GETDATE,'',$ilUser->getTimeZone());
442  }
443  else
444  {
445  $this->setDate(new ilDateTime(time(), IL_CAL_UNIX));
446  $date_info = $this->getDate()->get(IL_CAL_FKT_GETDATE,'',$ilUser->getTimeZone());
447  }
448 
449  // display invalid input again
450  if(is_array($this->invalid_input))
451  {
452  $date_info['year'] = $this->invalid_input['y'];
453  $date_info['mon'] = $this->invalid_input['m'];
454  $date_info['mday'] = $this->invalid_input['d'];
455  }
456  }
457 
458  if($this->getMode() == self::MODE_SELECT)
459  {
460  $tpl->setCurrentBlock("prop_date_input_select_setup");
461  $tpl->setVariable("INPUT_FIELDS_DATE", $this->getPostVar()."[date]");
462  $tpl->parseCurrentBlock();
463 
464  $tpl->setCurrentBlock("prop_date");
465  $tpl->setVariable("DATE_SELECT",
466  ilUtil::makeDateSelect($this->getPostVar()."[date]", $date_info['year'], $date_info['mon'], $date_info['mday'],
467  $this->startyear,true,array('disabled' => $this->getDisabled()), $this->getShowEmpty()));
468  }
469  else
470  {
471  $value = $this->getDate();
472  if($value)
473  {
474  $value = substr($this->getDate()->get(IL_CAL_DATETIME), 0, 10);
475  $day = substr($value, 8, 2);
476  $month = substr($value, 5, 2);
477  $year = substr($value, 0, 4);
478  }
479 
480  switch($ilUser->getDateFormat())
481  {
483  if($value)
484  {
485  $value = date("d.m.Y", mktime(0, 0, 0, $month, $day, $year));
486  }
487  $format = "%d.%m.%Y";
488  $input_hint = $lng->txt("dd_mm_yyyy");
489  break;
490 
492  if($value)
493  {
494  $value = date("Y-m-d", mktime(0, 0, 0, $month, $day, $year));
495  }
496  $format = "%Y-%m-%d";
497  $input_hint = $lng->txt("yyyy_mm_dd");
498  break;
499 
501  if($value)
502  {
503  $value = date("m/d/Y", mktime(0, 0, 0, $month, $day, $year));
504  }
505  $format = "%m/%d/%Y";
506  $input_hint = $lng->txt("mm_dd_yyyy");
507  break;
508  }
509 
510  $tpl->setCurrentBlock("prop_date_input_field");
511  $tpl->setVariable("DATE_ID", $this->getPostVar());
512  $tpl->setVariable("DATE_VALUE", $value);
513  $tpl->setVariable("DISABLED", $this->getDisabled() ? " disabled=\"disabled\"" : "");
514  $tpl->parseCurrentBlock();
515 
516  $tpl->setCurrentBlock("prop_date_input_field_info");
517  $tpl->setVariable("TXT_INPUT_FORMAT", $input_hint);
518  $tpl->parseCurrentBlock();
519 
520  $tpl->setCurrentBlock("prop_date_input_field_setup");
521  $tpl->setVariable("DATE_ID", $this->getPostVar());
522  $tpl->setVariable("DATE_FIELD_FORMAT", $format);
523  $tpl->parseCurrentBlock();
524  }
525 
526  $tpl->setCurrentBlock("prop_date");
527  include_once("./Services/UIComponent/Glyph/classes/class.ilGlyphGUI.php");
528  $tpl->setVariable("IMG_DATE_CALENDAR", ilGlyphGUI::get(ilGlyphGUI::CALENDAR, $lng->txt("open_calendar")));
529  $tpl->setVariable("DATE_ID", $this->getPostVar());
530 
531  include_once './Services/Calendar/classes/class.ilCalendarUserSettings.php';
532  $tpl->setVariable('DATE_FIRST_DAY',ilCalendarUserSettings::_getInstance()->getWeekStart());
533 
534  $tpl->parseCurrentBlock();
535 
536  if($this->getShowTime())
537  {
538  if($this->getMode() == self::MODE_INPUT)
539  {
540  $value = $this->getDate();
541  if($value)
542  {
543  if(!$this->getShowSeconds())
544  {
545  $value = substr($value->get(IL_CAL_DATETIME), 11, 5);
546  if($ilUser->getTimeFormat() == ilCalendarSettings::TIME_FORMAT_12)
547  {
548  $value = date("g:ia", mktime(substr($value, 0, 2), substr($value, 3, 2)));
549  }
550  }
551  else
552  {
553  $value = substr($value->get(IL_CAL_DATETIME), 11, 8);
554  if($ilUser->getTimeFormat() == ilCalendarSettings::TIME_FORMAT_12)
555  {
556  $value = date("g:i:sa", mktime(substr($value, 0, 2), substr($value, 3, 2), substr($value, 6, 2)));
557  }
558  }
559  }
560 
561  $tpl->setCurrentBlock("prop_time_input_field");
562  $tpl->setVariable("DATE_ID", $this->getPostVar());
563  $tpl->setVariable("TIME_VALUE", $value);
564  $tpl->setVariable("DISABLED", $this->getDisabled() ? " disabled=\"disabled\"" : "");
565  $tpl->parseCurrentBlock();
566  }
567 
568  $tpl->setCurrentBlock("prop_time");
569 
570  if($this->getMode() == self::MODE_SELECT)
571  {
572  $tpl->setVariable("TIME_SELECT",
573  ilUtil::makeTimeSelect($this->getPostVar()."[time]", !$this->getShowSeconds(),
574  $date_info['hours'], $date_info['minutes'], $date_info['seconds'],
575  true,array('minute_steps' => $this->getMinuteStepSize(),
576  'disabled' => $this->getDisabled())));
577  }
578 
579  $tpl->setVariable("TXT_TIME", $this->getShowSeconds()
580  ? "(".$lng->txt("hh_mm_ss").")"
581  : "(".$lng->txt("hh_mm").")");
582 
583  $tpl->parseCurrentBlock();
584  }
585 
586  return $tpl->get();
587  }
588 
594  function insert(&$a_tpl)
595  {
596  $html = $this->render();
597 
598  $a_tpl->setCurrentBlock("prop_generic");
599  $a_tpl->setVariable("PROP_GENERIC", $html);
600  $a_tpl->parseCurrentBlock();
601  }
602 
607  {
608  $html = $this->render();
609  return $html;
610  }
611 
615  function serializeData()
616  {
617  return serialize($this->getDate());
618  }
619 
623  function unserializeData($a_data)
624  {
625  $data = unserialize($a_data);
626 
627  if (is_object($data))
628  {
629  $this->setDate($data);
630  }
631  }
632 
639  {
640  return trim($_POST[$this->getPostVar()]["date"]." ". $_POST[$this->getPostVar()]["time"]);
641  }
642 
646  function getToolbarHTML()
647  {
648  $html = $this->render("toolbar");
649  return $html;
650  }
651 }
652 
653 ?>