ILIAS  release_4-3 Revision
 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 $showdate = true;
18  protected $time = "00:00:00";
19  protected $showtime = false;
20  protected $showseconds = false;
21  protected $minute_step_size = 1;
22  protected $show_empty = false;
23  protected $startyear = '';
24 
25  protected $activation_title = '';
26  protected $activation_post_var = '';
27 
28  const MODE_SELECT = 1;
29  const MODE_INPUT = 2;
30 
37  function __construct($a_title = "", $a_postvar = "")
38  {
39  parent::__construct($a_title, $a_postvar);
40  $this->setType("datetime");
41  $this->setMode(self::MODE_SELECT);
42  }
43 
49  function setMode($mode)
50  {
51  if(in_array($mode, array(self::MODE_INPUT, self::MODE_SELECT)))
52  {
53  $this->mode = $mode;
54  }
55  }
56 
62  function getMode()
63  {
64  return $this->mode;
65  }
66 
77  public function enableDateActivation($a_title,$a_postvar,$a_checked = true)
78  {
79  $this->activation_title = $a_title;
80  $this->activation_post_var = $a_postvar;
81  $this->activation_checked = $a_checked;
82  }
83 
90  public function getActivationPostVar()
91  {
93  }
94 
106  function setDate(ilDateTime $a_date = NULL)
107  {
108  $this->date = $a_date;
109  }
110 
116  function getDate()
117  {
118  return $this->date;
119  }
120 
126  function setShowDate($a_showdate)
127  {
128  $this->showdate = $a_showdate;
129  }
130 
136  function getShowDate()
137  {
138  return $this->showdate;
139  }
140 
146  function setShowTime($a_showtime)
147  {
148  $this->showtime = $a_showtime;
149  }
150 
156  function getShowTime()
157  {
158  return $this->showtime;
159  }
160 
166  function setShowEmpty($a_empty)
167  {
168  $this->show_empty = $a_empty;
169  }
170 
176  function getShowEmpty()
177  {
178  return $this->show_empty;
179  }
180 
186  function setStartYear($a_year)
187  {
188  $this->startyear = $a_year;
189  }
190 
196  function getStartYear()
197  {
198  return $this->startyear;
199  }
200 
209  public function setMinuteStepSize($a_step_size)
210  {
211  $this->minute_step_size = $a_step_size;
212  }
213 
220  public function getMinuteStepSize()
221  {
223  }
224 
225 
226 
232  function setShowSeconds($a_showseconds)
233  {
234  $this->showseconds = $a_showseconds;
235  }
236 
242  function getShowSeconds()
243  {
244  return $this->showseconds;
245  }
246 
252  function setValueByArray($a_values)
253  {
254  global $ilUser;
255 
256  if($this->getMode() == self::MODE_INPUT &&
257  (!isset($a_values[$this->getPostVar()]["date"]) || $a_values[$this->getPostVar()]["date"] == ""))
258  {
259  $this->date = NULL;
260  }
261  else if(isset($a_values[$this->getPostVar()]["time"]))
262  {
263  $this->setDate(new ilDateTime($a_values[$this->getPostVar()]["date"].' '.$a_values[$this->getPostVar()]["time"],
264  IL_CAL_DATETIME,$ilUser->getTimeZone()));
265  }
266  else if (isset($a_values[$this->getPostVar()]["date"]))
267  {
268  $this->setDate(new ilDate($a_values[$this->getPostVar()]["date"],
269  IL_CAL_DATE));
270  }
271 
272  foreach($this->getSubItems() as $item)
273  {
274  $item->setValueByArray($a_values);
275  }
276  }
277 
283  function checkInput()
284  {
285  global $ilUser;
286 
287  if ($this->getDisabled())
288  {
289  return true;
290  }
291 
292  $post = $_POST[$this->getPostVar()];
293 
294  // empty date valid with input field
295  if(!$this->getRequired() && $this->getMode() == self::MODE_INPUT && $post["date"] == "")
296  {
297  return true;
298  }
299 
300  if($this->getMode() == self::MODE_SELECT)
301  {
302  $post["date"]["y"] = ilUtil::stripSlashes($post["date"]["y"]);
303  $post["date"]["m"] = ilUtil::stripSlashes($post["date"]["m"]);
304  $post["date"]["d"] = ilUtil::stripSlashes($post["date"]["d"]);
305  $dt['year'] = (int) $post['date']['y'];
306  $dt['mon'] = (int) $post['date']['m'];
307  $dt['mday'] = (int) $post['date']['d'];
308 
309  if($this->getShowTime())
310  {
311  $post["time"]["h"] = ilUtil::stripSlashes($post["time"]["h"]);
312  $post["time"]["m"] = ilUtil::stripSlashes($post["time"]["m"]);
313  $post["time"]["s"] = ilUtil::stripSlashes($post["time"]["s"]);
314  $dt['hours'] = (int) $post['time']['h'];
315  $dt['minutes'] = (int) $post['time']['m'];
316  $dt['seconds'] = (int) $post['time']['s'];
317  }
318  }
319  else
320  {
321  $post["date"] = ilUtil::stripSlashes($post["date"]);
322  $post["time"] = ilUtil::stripSlashes($post["time"]);
323 
324  if($post["date"])
325  {
326  switch($ilUser->getDateFormat())
327  {
329  $date = explode(".", $post["date"]);
330  $dt['mday'] = (int)$date[0];
331  $dt['mon'] = (int)$date[1];
332  $dt['year'] = (int)$date[2];
333  break;
334 
336  $date = explode("-", $post["date"]);
337  $dt['mday'] = (int)$date[2];
338  $dt['mon'] = (int)$date[1];
339  $dt['year'] = (int)$date[0];
340  break;
341 
343  $date = explode("/", $post["date"]);
344  $dt['mday'] = (int)$date[1];
345  $dt['mon'] = (int)$date[0];
346  $dt['year'] = (int)$date[2];
347  break;
348  }
349 
350  if($this->getShowTime())
351  {
352  if($ilUser->getTimeFormat() == ilCalendarSettings::TIME_FORMAT_12)
353  {
354  $seconds = "";
355  if($this->getShowSeconds())
356  {
357  $seconds = ":\s*([0-9]{1,2})\s*";
358  }
359  if(preg_match("/([0-9]{1,2})\s*:\s*([0-9]{1,2})\s*".$seconds."(am|pm)/", trim(strtolower($post["time"])), $matches))
360  {
361  $dt['hours'] = (int)$matches[1];
362  $dt['minutes'] = (int)$matches[2];
363  if($seconds)
364  {
365  $dt['seconds'] = (int)$time[2];
366  $ampm = $matches[4];
367  }
368  else
369  {
370  $dt['seconds'] = 0;
371  $ampm = $matches[3];
372  }
373  if($dt['hours'] == 12)
374  {
375  if($ampm == "am")
376  {
377  $dt['hours'] = 0;
378  }
379  }
380  else if($ampm == "pm")
381  {
382  $dt['hours'] += 12;
383  }
384  }
385  }
386  else
387  {
388  $time = explode(":", $post["time"]);
389  $dt['hours'] = (int)$time[0];
390  $dt['minutes'] = (int)$time[1];
391  $dt['seconds'] = (int)$time[2];
392  }
393  }
394  }
395  }
396 
397  // very basic validation
398  if($dt['mday'] == 0 || $dt['mon'] == 0 || $dt['year'] == 0 || $dt['mday'] > 31 || $dt['mon'] > 12)
399  {
400  $dt = false;
401  }
402  else if($this->getShowTime() && ($dt['hours'] > 23 || $dt['minutes'] > 59 || $dt['seconds'] > 59))
403  {
404  $dt = false;
405  }
406 
407  $date = new ilDateTime($dt, IL_CAL_FKT_GETDATE, $ilUser->getTimeZone());
408  $this->setDate($date);
409 
410  // post values used to be overwritten anyways - cannot change behaviour
411  $_POST[$this->getPostVar()]['date'] = $date->get(IL_CAL_FKT_DATE, 'Y-m-d', $ilUser->getTimeZone());
412  $_POST[$this->getPostVar()]['time'] = $date->get(IL_CAL_FKT_DATE, 'H:i:s', $ilUser->getTimeZone());
413 
414  return (bool)$dt;
415  }
416 
421  function render()
422  {
423  global $lng,$ilUser;
424 
425  $tpl = new ilTemplate("tpl.prop_datetime.html", true, true, "Services/Form");
426 
427  $lng->loadLanguageModule("jscalendar");
428  require_once("./Services/Calendar/classes/class.ilCalendarUtil.php");
430 
431  if(strlen($this->getActivationPostVar()))
432  {
433  $tpl->setCurrentBlock('prop_date_activation');
434  $tpl->setVariable('CHECK_ENABLED_DATE',$this->getActivationPostVar());
435  $tpl->setVariable('TXT_DATE_ENABLED',$this->activation_title);
436  $tpl->setVariable('CHECKED_ENABLED',$this->activation_checked ? 'checked="checked"' : '');
437  $tpl->setVariable('CHECKED_DISABLED',$this->getDisabled() ? 'disabled="disabled" ' : '');
438  $tpl->parseCurrentBlock();
439  }
440 
441  if($this->getMode() == self::MODE_SELECT)
442  {
443  if(is_a($this->getDate(),'ilDate'))
444  {
445  $date_info = $this->getDate()->get(IL_CAL_FKT_GETDATE,'','UTC');
446  }
447  elseif(is_a($this->getDate(),'ilDateTime'))
448  {
449  $date_info = $this->getDate()->get(IL_CAL_FKT_GETDATE,'',$ilUser->getTimeZone());
450  }
451  else
452  {
453  $this->setDate(new ilDateTime(time(), IL_CAL_UNIX));
454  $date_info = $this->getDate()->get(IL_CAL_FKT_GETDATE,'',$ilUser->getTimeZone());
455  }
456  }
457 
458  if ($this->getShowDate())
459  {
460  if($this->getMode() == self::MODE_SELECT)
461  {
462  $tpl->setCurrentBlock("prop_date_input_select_setup");
463  $tpl->setVariable("INPUT_FIELDS_DATE", $this->getPostVar()."[date]");
464  $tpl->parseCurrentBlock();
465 
466  $tpl->setCurrentBlock("prop_date");
467  $tpl->setVariable("DATE_SELECT",
468  ilUtil::makeDateSelect($this->getPostVar()."[date]", $date_info['year'], $date_info['mon'], $date_info['mday'],
469  $this->startyear,true,array('disabled' => $this->getDisabled()), $this->getShowEmpty()));
470  }
471  else
472  {
473  $value = $this->getDate();
474  if($value)
475  {
476  $value = substr($this->getDate()->get(IL_CAL_DATETIME), 0, 10);
477  $day = substr($value, 8, 2);
478  $month = substr($value, 5, 2);
479  $year = substr($value, 0, 4);
480  }
481 
482  switch($ilUser->getDateFormat())
483  {
485  if($value)
486  {
487  $value = date("d.m.Y", mktime(0, 0, 0, $month, $day, $year));
488  }
489  $format = "%d.%m.%Y";
490  $input_hint = $lng->txt("dd_mm_yyyy");
491  break;
492 
494  if($value)
495  {
496  $value = date("Y-m-d", mktime(0, 0, 0, $month, $day, $year));
497  }
498  $format = "%Y-%m-%d";
499  $input_hint = $lng->txt("yyyy_mm_dd");
500  break;
501 
503  if($value)
504  {
505  $value = date("m/d/Y", mktime(0, 0, 0, $month, $day, $year));
506  }
507  $format = "%m/%d/%Y";
508  $input_hint = $lng->txt("mm_dd_yyyy");
509  break;
510  }
511 
512  $tpl->setCurrentBlock("prop_date_input_field");
513  $tpl->setVariable("DATE_ID", $this->getPostVar());
514  $tpl->setVariable("DATE_VALUE", $value);
515  $tpl->setVariable("DISABLED", $this->getDisabled() ? " disabled=\"disabled\"" : "");
516  $tpl->parseCurrentBlock();
517 
518  $tpl->setCurrentBlock("prop_date_input_field_info");
519  $tpl->setVariable("TXT_INPUT_FORMAT", $input_hint);
520  $tpl->parseCurrentBlock();
521 
522  $tpl->setCurrentBlock("prop_date_input_field_setup");
523  $tpl->setVariable("DATE_ID", $this->getPostVar());
524  $tpl->setVariable("DATE_FIELD_FORMAT", $format);
525  $tpl->parseCurrentBlock();
526  }
527 
528  $tpl->setCurrentBlock("prop_date");
529  $tpl->setVariable("IMG_DATE_CALENDAR", ilUtil::getImagePath("calendar.png"));
530  $tpl->setVariable("TXT_DATE_CALENDAR", $lng->txt("open_calendar"));
531  $tpl->setVariable("DATE_ID", $this->getPostVar());
532 
533  include_once './Services/Calendar/classes/class.ilCalendarUserSettings.php';
534  $tpl->setVariable('DATE_FIRST_DAY',ilCalendarUserSettings::_getInstance()->getWeekStart());
535 
536  $tpl->parseCurrentBlock();
537  }
538  if($this->getShowTime())
539  {
540  if($this->getMode() == self::MODE_INPUT)
541  {
542  $value = $this->getDate();
543  if($value)
544  {
545  if(!$this->getShowSeconds())
546  {
547  $value = substr($value->get(IL_CAL_DATETIME), 11, 5);
548  if($ilUser->getTimeFormat() == ilCalendarSettings::TIME_FORMAT_12)
549  {
550  $value = date("g:ia", mktime(substr($value, 0, 2), substr($value, 3, 2)));
551  }
552  }
553  else
554  {
555  $value = substr($value->get(IL_CAL_DATETIME), 11, 8);
556  if($ilUser->getTimeFormat() == ilCalendarSettings::TIME_FORMAT_12)
557  {
558  $value = date("g:i:sa", mktime(substr($value, 0, 2), substr($value, 3, 2), substr($value, 6, 2)));
559  }
560  }
561  }
562 
563  $tpl->setCurrentBlock("prop_time_input_field");
564  $tpl->setVariable("DATE_ID", $this->getPostVar());
565  $tpl->setVariable("TIME_VALUE", $value);
566  $tpl->setVariable("DISABLED", $this->getDisabled() ? " disabled=\"disabled\"" : "");
567  $tpl->parseCurrentBlock();
568  }
569 
570  $tpl->setCurrentBlock("prop_time");
571 
572  if($this->getMode() == self::MODE_SELECT)
573  {
574  $tpl->setVariable("TIME_SELECT",
575  ilUtil::makeTimeSelect($this->getPostVar()."[time]", !$this->getShowSeconds(),
576  $date_info['hours'], $date_info['minutes'], $date_info['seconds'],
577  true,array('minute_steps' => $this->getMinuteStepSize(),
578  'disabled' => $this->getDisabled())));
579  }
580 
581  $tpl->setVariable("TXT_TIME", $this->getShowSeconds()
582  ? "(".$lng->txt("hh_mm_ss").")"
583  : "(".$lng->txt("hh_mm").")");
584 
585  $tpl->parseCurrentBlock();
586  }
587 
588  if ($this->getShowTime() && $this->getShowDate() && $this->getMode() == self::MODE_SELECT)
589  {
590  $tpl->setVariable("DELIM", "<br />");
591  }
592 
593  return $tpl->get();
594  }
595 
601  function insert(&$a_tpl)
602  {
603  $html = $this->render();
604 
605  $a_tpl->setCurrentBlock("prop_generic");
606  $a_tpl->setVariable("PROP_GENERIC", $html);
607  $a_tpl->parseCurrentBlock();
608  }
609 
614  {
615  $html = $this->render();
616  return $html;
617  }
618 
622  function serializeData()
623  {
624  return serialize($this->getDate());
625  }
626 
630  function unserializeData($a_data)
631  {
632  $data = unserialize($a_data);
633 
634  if (is_object($data))
635  {
636  $this->setDate($data);
637  }
638  }
639 
646  {
647  return trim($_POST[$this->getPostVar()]["date"]." ". $_POST[$this->getPostVar()]["time"]);
648  }
649 
653  function getToolbarHTML()
654  {
655  $html = $this->render("toolbar");
656  return $html;
657  }
658 }
659 
660 ?>