ILIAS  release_8 Revision v8.23
class.ilCalendarUtil.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2007 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
29 {
30  private static ?ilDateTime $today = null;
31  private static array $default_calendar = array();
32  public static string $init_done;
33  protected static bool $init_datetimepicker = false;
34 
35  public static function convertDateToUtcDBTimestamp(\ilDateTime $date = null): ?string
36  {
37  if (is_null($date)) {
38  return null;
39  }
40  if ($date instanceof \ilDate) {
41  return $date->get(IL_CAL_DATE);
42  }
43  return $date->get(IL_CAL_DATETIME, '', ilTimeZone::UTC);
44  }
45 
46  public static function _isToday(ilDateTime $date): bool
47  {
48  global $DIC;
49 
50  $ilUser = $DIC['ilUser'];
51  if (!is_object(self::$today)) {
52  self::$today = new ilDateTime(time(), IL_CAL_UNIX, $ilUser->getTimeZone());
53  }
54  return ilDateTime::_equals(self::$today, $date, IL_CAL_DAY, $ilUser->getTimeZone());
55  }
56 
62  public static function _numericMonthToString(int $a_month, bool $a_long = true): string
63  {
64  global $DIC;
65 
66  $lng = $DIC['lng'];
67  $month = $a_month < 10 ? '0' . $a_month : $a_month;
68  return $a_long ? $lng->txt('month_' . $month . '_long') : $lng->txt('month_' . $month . '_short');
69  }
70 
75  public static function _numericDayToString(int $a_day, bool $a_long = true): string
76  {
77  global $DIC;
78 
79  $lng = $DIC['lng'];
80  $lng->loadLanguageModule('dateplaner');
81  static $days = array('Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su');
82 
83  return $a_long ? $lng->txt($days[$a_day] . '_long') : $lng->txt($days[$a_day] . '_short');
84  }
85 
93  public static function _buildWeekDayList(ilDate $a_day, int $a_weekstart): ilDateList
94  {
95  $day_list = new ilDateList(ilDateList::TYPE_DATE);
96 
97  $start = clone $a_day;
98  $start_info = $start->get(IL_CAL_FKT_GETDATE, '', 'UTC');
99  $day_diff = $a_weekstart - $start_info['isoday'];
100  if (abs($day_diff) === 7) {
101  $day_diff = 0;
102  }
103  $start->increment(IL_CAL_DAY, $day_diff);
104  $day_list->add($start);
105  for ($i = 1; $i < 7; $i++) {
106  $start->increment(IL_CAL_DAY, 1);
107  $day_list->add($start);
108  }
109  return $day_list;
110  }
111 
119  public static function _buildMonthDayList(int $a_month, int $a_year, int $weekstart): ilDateList
120  {
121  $day_list = new ilDateList(ilDateList::TYPE_DATE);
122 
123  $prev_month = ($a_month == 1) ? 12 : $a_month - 1;
124  $prev_year = ($prev_month == 12) ? $a_year - 1 : $a_year;
125  $next_month = $a_month == 12 ? 1 : $a_month + 1;
126  $next_year = $a_month == 12 ? $a_year + 1 : $a_year;
127 
128  $days_in_month = self::_getMaxDayOfMonth($a_year, $a_month);
129  $days_in_prev_month = self::_getMaxDayOfMonth($a_year, $prev_month);
130 
131  $week_day['year'] = $a_year;
132  $week_day['mon'] = $a_month;
133  $week_day['mday'] = 1;
134  $week_day['hours'] = 0;
135  $week_day['minutes'] = 0;
136  $week_day = new ilDate($week_day, IL_CAL_FKT_GETDATE);
137 
138  $weekday = $week_day->get(IL_CAL_FKT_DATE, 'w');
139  $first_day_offset = (($weekday - $weekstart) < 0) ? 6 : $weekday - $weekstart;
140 
141  for ($i = 0; $i < 42; $i++) {
142  if ($i < $first_day_offset) {
143  $day = $days_in_prev_month - $first_day_offset + $i + 1;
144 
145  $day_list->add(new ilDate(
146  gmmktime(
147  0,
148  0,
149  0,
150  $prev_month,
151  $days_in_prev_month - $first_day_offset + $i + 1,
152  $prev_year
153  ),
155  ));
156  } elseif ($i < $days_in_month + $first_day_offset) {
157  $day = $i - $first_day_offset + 1;
158 
159  $day_list->add(new ilDate(
160  gmmktime(
161  0,
162  0,
163  0,
164  $a_month,
165  $i - $first_day_offset + 1,
166  $a_year
167  ),
169  ));
170  } else {
171  $day = $i - $days_in_month - $first_day_offset + 1;
172  $day_list->add(new ilDate(
173  gmmktime(
174  0,
175  0,
176  0,
177  $next_month,
178  $i - $days_in_month - $first_day_offset + 1,
179  $next_year
180  ),
182  ));
183  }
184  if ($i == 34 and ($day < 15 or $day == $days_in_month)) {
185  break;
186  }
187  }
188  return $day_list;
189  }
190 
191  public static function initJSCalendar(): void
192  {
193  global $DIC;
194 
195  $tpl = $DIC['tpl'];
196  $lng = $DIC['lng'];
197 
198  if (self::$init_done == "done") {
199  return;
200  }
201 
202  $lng->loadLanguageModule("jscalendar");
203  $tpl->addBlockFile(
204  "CALENDAR_LANG_JAVASCRIPT",
205  "calendar_javascript",
206  "tpl.calendar.html",
207  "Services/Calendar"
208  );
209  $tpl->setCurrentBlock("calendar_javascript");
210  $tpl->setVariable("FULL_SUNDAY", $lng->txt("l_su"));
211  $tpl->setVariable("FULL_MONDAY", $lng->txt("l_mo"));
212  $tpl->setVariable("FULL_TUESDAY", $lng->txt("l_tu"));
213  $tpl->setVariable("FULL_WEDNESDAY", $lng->txt("l_we"));
214  $tpl->setVariable("FULL_THURSDAY", $lng->txt("l_th"));
215  $tpl->setVariable("FULL_FRIDAY", $lng->txt("l_fr"));
216  $tpl->setVariable("FULL_SATURDAY", $lng->txt("l_sa"));
217  $tpl->setVariable("SHORT_SUNDAY", $lng->txt("s_su"));
218  $tpl->setVariable("SHORT_MONDAY", $lng->txt("s_mo"));
219  $tpl->setVariable("SHORT_TUESDAY", $lng->txt("s_tu"));
220  $tpl->setVariable("SHORT_WEDNESDAY", $lng->txt("s_we"));
221  $tpl->setVariable("SHORT_THURSDAY", $lng->txt("s_th"));
222  $tpl->setVariable("SHORT_FRIDAY", $lng->txt("s_fr"));
223  $tpl->setVariable("SHORT_SATURDAY", $lng->txt("s_sa"));
224  $tpl->setVariable("FULL_JANUARY", $lng->txt("l_01"));
225  $tpl->setVariable("FULL_FEBRUARY", $lng->txt("l_02"));
226  $tpl->setVariable("FULL_MARCH", $lng->txt("l_03"));
227  $tpl->setVariable("FULL_APRIL", $lng->txt("l_04"));
228  $tpl->setVariable("FULL_MAY", $lng->txt("l_05"));
229  $tpl->setVariable("FULL_JUNE", $lng->txt("l_06"));
230  $tpl->setVariable("FULL_JULY", $lng->txt("l_07"));
231  $tpl->setVariable("FULL_AUGUST", $lng->txt("l_08"));
232  $tpl->setVariable("FULL_SEPTEMBER", $lng->txt("l_09"));
233  $tpl->setVariable("FULL_OCTOBER", $lng->txt("l_10"));
234  $tpl->setVariable("FULL_NOVEMBER", $lng->txt("l_11"));
235  $tpl->setVariable("FULL_DECEMBER", $lng->txt("l_12"));
236  $tpl->setVariable("SHORT_JANUARY", $lng->txt("s_01"));
237  $tpl->setVariable("SHORT_FEBRUARY", $lng->txt("s_02"));
238  $tpl->setVariable("SHORT_MARCH", $lng->txt("s_03"));
239  $tpl->setVariable("SHORT_APRIL", $lng->txt("s_04"));
240  $tpl->setVariable("SHORT_MAY", $lng->txt("s_05"));
241  $tpl->setVariable("SHORT_JUNE", $lng->txt("s_06"));
242  $tpl->setVariable("SHORT_JULY", $lng->txt("s_07"));
243  $tpl->setVariable("SHORT_AUGUST", $lng->txt("s_08"));
244  $tpl->setVariable("SHORT_SEPTEMBER", $lng->txt("s_09"));
245  $tpl->setVariable("SHORT_OCTOBER", $lng->txt("s_10"));
246  $tpl->setVariable("SHORT_NOVEMBER", $lng->txt("s_11"));
247  $tpl->setVariable("SHORT_DECEMBER", $lng->txt("s_12"));
248  $tpl->setVariable("ABOUT_CALENDAR", $lng->txt("about_calendar"));
249  $tpl->setVariable("ABOUT_CALENDAR_LONG", $lng->txt("about_calendar_long"));
250  $tpl->setVariable("ABOUT_TIME_LONG", $lng->txt("about_time"));
251  $tpl->setVariable("PREV_YEAR", $lng->txt("prev_year"));
252  $tpl->setVariable("PREV_MONTH", $lng->txt("prev_month"));
253  $tpl->setVariable("GO_TODAY", $lng->txt("go_today"));
254  $tpl->setVariable("NEXT_MONTH", $lng->txt("next_month"));
255  $tpl->setVariable("NEXT_YEAR", $lng->txt("next_year"));
256  $tpl->setVariable("SEL_DATE", $lng->txt("select_date"));
257  $tpl->setVariable("DRAG_TO_MOVE", $lng->txt("drag_to_move"));
258  $tpl->setVariable("PART_TODAY", $lng->txt("part_today"));
259  $tpl->setVariable("DAY_FIRST", $lng->txt("day_first"));
260  $tpl->setVariable("CLOSE", $lng->txt("close"));
261  $tpl->setVariable("TODAY", $lng->txt("today"));
262  $tpl->setVariable("TIME_PART", $lng->txt("time_part"));
263  $tpl->setVariable("DEF_DATE_FORMAT", $lng->txt("def_date_format"));
264  $tpl->setVariable("TT_DATE_FORMAT", $lng->txt("tt_date_format"));
265  $tpl->setVariable("WK", $lng->txt("wk"));
266  $tpl->setVariable("TIME", $lng->txt("time"));
267  $tpl->parseCurrentBlock();
268  $tpl->setCurrentBlock("CalendarJS");
269  $tpl->setVariable("LOCATION_JAVASCRIPT_CALENDAR", "./Services/Calendar/js/calendar.js");
270  $tpl->setVariable("LOCATION_JAVASCRIPT_CALENDAR_SETUP", "./Services/Calendar/js/calendar-setup.js");
271  $tpl->parseCurrentBlock();
272 
273  self::$init_done = "done";
274  }
275 
276  public static function getZoneInfoFile($a_tz): string
277  {
278  if (!array_key_exists($a_tz, self::_getShortTimeZoneList())) {
279  return '';
280  }
281  $timezone_filename = str_replace('/', '_', $a_tz);
282  $timezone_filename .= '.ics';
283  return './Services/Calendar/zoneinfo/' . $timezone_filename;
284  }
285 
289  public static function _getShortTimeZoneList(): array
290  {
291  return array(
292  'Pacific/Samoa' => 'GMT-11: Midway Islands, Samoa',
293  'US/Hawaii' => 'GMT-10:00: Hawaii, Polynesia',
294  'US/Alaska' => 'GMT-9:00: Alaska',
295  'America/Los_Angeles' => 'GMT-8:00: Tijuana, Los Angeles, Seattle, Vancouver',
296  'US/Arizona' => 'GMT-7:00: Arizona',
297  'America/Chihuahua' => 'GMT-7:00: Chihuahua, La Paz, Mazatlan',
298  'America/Denver' => 'GMT-7:00: Arizona, Denver, Salt Lake City, Calgary',
299  'America/Chicago' => 'GMT-6:00: Chicago, Dallas, Kansas City, Winnipeg',
300  'America/Monterrey' => 'GMT-6:00: Guadalajara, Mexico City, Monterrey',
301  'Canada/Saskatchewan' => 'GMT-6:00: Saskatchewan',
302  'US/Central' => 'GMT-6:00: Central America',
303  'America/Bogota' => 'GMT-5:00: Bogota, Lima, Quito',
304  'US/East-Indiana' => 'GMT-5:00: East-Indiana',
305  'America/New_York' => 'GMT-5:00: New York, Miami, Atlanta, Detroit, Toronto',
306  'Canada/Atlantic' => 'GMT-4:00: Atlantic (Canada)',
307  'America/La_Paz' => 'GMT-4:00: Carcas, La Paz',
308  'America/Santiago' => 'GMT-4:00: Santiago',
309  'Canada/Newfoundland' => 'GMT-3:00: Newfoundland',
310  'Brazil/East' => 'GMT-3:00: Sao Paulo',
311  'America/Argentina/Buenos_Aires' => 'GMT-3:00: Buenes Aires, Georgtown',
312  'Etc/GMT+3' => 'GMT-3:00: Greenland, Uruguay, Surinam',
313  'Atlantic/Cape_Verde' => 'GMT-2:00: Cape Verde, Greenland, South Georgia',
314  'Atlantic/Azores' => 'GMT-1:00: Azores',
315  'Africa/Casablanca' => 'GMT+0:00: Casablanca, Monrovia',
316  'Europe/London' => 'GMT+0:00: Dublin, Edinburgh, Lisbon, London',
317  'Europe/Berlin' => 'GMT+1:00: Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna',
318  'Europe/Belgrade' => 'GMT+1:00: Belgrade, Bratislava, Budapest, Ljubljana, Prague',
319  'Europe/Paris' => 'GMT+1:00: Brussels, Copenhagen, Paris, Madrid',
320  'Europe/Sarajevo' => 'GMT+1:00: Sarajevo, Skopje, Warsaw, Zagreb',
321  'Africa/Lagos' => 'GMT+1:00: West-Central Africa',
322  'Europe/Athens' => 'GMT+2:00: Athens, Beirut, Istanbul, Minsk',
323  'Europe/Bucharest' => 'GMT+2:00: Bucharest',
324  'Africa/Harare' => 'GMT+2:00: Harare, Pratoria',
325  'Europe/Helsinki' => 'GMT+2:00: Helsinki, Kiev, Riga, Sofia, Tallinn, Vilnius',
326  'Asia/Jerusalem' => 'GMT+2:00: Jerusalem',
327  'Africa/Cairo' => 'GMT+2:00: Cairo',
328  'Asia/Baghdad' => 'GMT+3:00: Baghdad',
329  'Asia/Kuwait' => 'GMT+3:00: Kuwait, Riyadh',
330  'Europe/Moscow' => 'GMT+3:00: Moscow, Saint Petersburg',
331  'Africa/Nairobi' => 'GMT+3:00: Nairobi',
332  'Asia/Tehran' => 'GMT+3:30: Tehran',
333  'Asia/Muscat' => 'GMT+4:00: Abu Dhabi, Muscat',
334  'Asia/Baku' => 'GMT+4:00: Baku, Tbilisi, Erivan',
335  'Asia/Kabul' => 'GMT+4:00: Kabul',
336  'Asia/Karachi' => 'GMT+5:00: Islamabad, Karachi, Taschkent',
337  'Asia/Yekaterinburg' => 'GMT+5:00: Yekaterinburg',
338  'Asia/Calcutta' => 'GMT+5:30: New Dehli',
339  'Asia/Katmandu' => 'GMT+5:45: Katmandu',
340  'Asia/Novosibirsk' => 'GMT+6:00: Almaty, Novosibirsk',
341  'Asia/Dhaka' => 'GMT+6:00: Astana, Dhaka',
342  'Asia/Rangoon' => 'GMT+6:00: Sri Jayawardenepura, Rangoon',
343  'Asia/Jakarta' => 'GMT+7:00: Bangkok, Hanoi, Jakarta',
344  'Asia/Krasnoyarsk' => 'GMT+7:00: Krasnoyarsk',
345  'Asia/Irkutsk' => 'GMT+8:00: Irkutsk, Ulan Bator',
346  'Asia/Singapore' => 'GMT+8:00: Kuala Lumpour, Singapore',
347  'Asia/Hong_Kong' => 'GMT+8:00: Beijing, Chongqing, Hong kong, Urumchi',
348  'Australia/Perth' => 'GMT+8:00: Perth',
349  'Asia/Taipei' => 'GMT+8:00: Taipei',
350  'Asia/Yakutsk' => 'GMT+9:00: Yakutsk',
351  'Asia/Tokyo' => 'GMT+9:00: Osaka, Sapporo, Tokyo',
352  'Asia/Seoul' => 'GMT+9:00: Seoul, Darwin, Adelaide',
353  'Australia/Brisbane' => 'GMT+10:00: Brisbane',
354  'Australia/Sydney' => 'GMT+10:00: Canberra, Melbourne, Sydney',
355  'Pacific/Guam' => 'GMT+10:00: Guam, Port Moresby',
356  'Australia/Hobart' => 'GMT+10:00: Hobart',
357  'Asia/Vladivostok' => 'GMT+10:00: Vladivostok',
358  'Asia/Magadan' => 'GMT+11:00: Salomon Islands, New Caledonia, Magadan',
359  'Pacific/Auckland' => 'GMT+12:00: Auckland, Wellington',
360  'Pacific/Fiji' => 'GMT+12:00: Fiji, Kamchatka, Marshall-Islands'
361  );
362  }
363 
367  public static function _isLeapYear(int $a_year): bool
368  {
369  $is_leap = false;
370  if ($a_year % 4 == 0) {
371  $is_leap = true;
372  if ($a_year % 100 == 0) {
373  $is_leap = false;
374  if ($a_year % 400) {
375  return true;
376  }
377  }
378  }
379  return $is_leap;
380  }
381 
386  public static function _getMaxDayOfMonth(int $a_year, int $a_month): int
387  {
388  if (function_exists('cal_days_in_month')) {
389  return cal_days_in_month(CAL_GREGORIAN, $a_month, $a_year);
390  }
391  return (int) date('t', mktime(0, 0, 0, $a_month, 1, $a_year));
392  }
393 
399  public static function calculateFontColor(string $a_html_color_code): string
400  {
401  if (strpos($a_html_color_code, '#') !== 0 or strlen($a_html_color_code) != 7) {
402  return '#000000';
403  }
404 
405  // http://en.wikipedia.org/wiki/Luminance_(relative)
406  $lum = round(hexdec(substr($a_html_color_code, 1, 2)) * 0.2126 +
407  hexdec(substr($a_html_color_code, 3, 2)) * 0.7152 +
408  hexdec(substr($a_html_color_code, 5, 2)) * 0.0722);
409  return ($lum <= 128) ? "#FFFFFF" : "#000000";
410  }
411 
415  public static function getHourSelection(int $a_format): array
416  {
417  $options = [];
418  switch ($a_format) {
420  for ($i = 0; $i < 24; $i++) {
421  $options[$i] = sprintf('%02d:00', $i);
422  }
423  break;
424 
426  for ($i = 0; $i < 24; $i++) {
427  $options[$i] = date('h a', mktime($i, 0, 0, 1, 1, 2000));
428  }
429  break;
430  }
431  return $options;
432  }
433 
437  public static function initDefaultCalendarByType(
438  int $a_type_id,
439  int $a_usr_id,
440  string $a_title,
441  bool $a_create = false
442  ): ?ilCalendarCategory {
443  global $DIC;
444 
445  $ilDB = $DIC['ilDB'];
446  if (isset(self::$default_calendar[$a_usr_id]) and isset(self::$default_calendar[$a_usr_id][$a_type_id])) {
447  return self::$default_calendar[$a_usr_id][$a_type_id];
448  }
449 
450  $query = "SELECT cat_id FROM cal_categories " .
451  "WHERE obj_id = " . $ilDB->quote($a_usr_id, 'integer') . " " .
452  "AND type = " . $ilDB->quote($a_type_id, 'integer');
453  $res = $ilDB->query($query);
454  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
455  return self::$default_calendar[$a_usr_id][$a_type_id] = new ilCalendarCategory($row->cat_id);
456  }
457 
458  if (!$a_create) {
459  return null;
460  }
461 
462  // Create default calendar
463  self::$default_calendar[$a_usr_id][$a_type_id] = new ilCalendarCategory();
464  self::$default_calendar[$a_usr_id][$a_type_id]->setType($a_type_id);
465  self::$default_calendar[$a_usr_id][$a_type_id]->setColor(ilCalendarCategory::DEFAULT_COLOR);
466  self::$default_calendar[$a_usr_id][$a_type_id]->setTitle($a_title);
467  self::$default_calendar[$a_usr_id][$a_type_id]->setObjId($a_usr_id);
468  self::$default_calendar[$a_usr_id][$a_type_id]->add();
469 
470  return self::$default_calendar[$a_usr_id][$a_type_id];
471  }
472 
479  public static function getUserDateFormat(int $a_add_time = 0, bool $a_for_parsing = false): string
480  {
481  global $DIC;
482 
483  $ilUser = $DIC['ilUser'];
484  $format = '';
485  switch ($ilUser->getDateFormat()) {
487  $format = "DD.MM.YYYY";
488  break;
489 
491  $format = "YYYY-MM-DD";
492  break;
493 
495  $format = "MM/DD/YYYY";
496  break;
497  }
498  if ($a_add_time) {
499  $format .= " " . (($ilUser->getTimeFormat() == ilCalendarSettings::TIME_FORMAT_24)
500  ? "HH:mm"
501  : "hh:mma");
502  if ($a_add_time == 2) {
503  $format .= ":ss";
504  }
505  }
506 
507  // translate datepicker format to PHP format
508  if ($a_for_parsing) {
509  $format = str_replace("DD", "d", $format);
510  $format = str_replace("MM", "m", $format);
511  $format = str_replace("mm", "i", $format);
512  $format = str_replace("YYYY", "Y", $format);
513  $format = str_replace("HH", "H", $format);
514  $format = str_replace("hh", "h", $format);
515  }
516 
517  return $format;
518  }
519 
520  public static function initDateTimePicker(): void
521  {
522  global $DIC;
523  $tpl = $DIC->ui()->mainTemplate();
524 
525  if (!self::$init_datetimepicker) {
526  $tpl->addJavaScript("./node_modules/moment/min/moment-with-locales.min.js");
527  // unminified version does not work with jQuery 3.0
528  // https://github.com/Eonasdan/bootstrap-datetimepicker/issues/1684
529  $tpl->addJavaScript("./node_modules/eonasdan-bootstrap-datetimepicker/build/js/bootstrap-datetimepicker.min.js");
530  $tpl->addJavaScript("Services/Form/js/Form.js"); // see ilPropertyFormGUI
531  self::$init_datetimepicker = true;
532  }
533  }
534 
545  public static function addDateTimePicker(
546  string $a_id,
547  ?int $a_add_time = null,
548  ?array $a_custom_config = null,
549  ?string $a_id2 = null,
550  ?array $a_custom_config2 = null,
551  ?string $a_toggle_id = null,
552  ?string $a_subform_id = null
553  ): void {
554  global $DIC;
555 
556  $tpl = $DIC->ui()->mainTemplate();
557  foreach (self::getCodeForPicker(
558  $a_id,
559  $a_add_time,
560  $a_custom_config,
561  $a_id2,
562  $a_custom_config2,
563  $a_toggle_id,
564  $a_subform_id
565  ) as $code) {
566  $tpl->addOnLoadCode($code);
567  }
568  }
569 
581  public static function getCodeForPicker(
582  string $a_id,
583  ?int $a_add_time = null,
584  ?array $a_custom_config = null,
585  ?string $a_id2 = null,
586  ?array $a_custom_config2 = null,
587  ?string $a_toggle_id = null,
588  ?string $a_subform_id = null
589  ): array {
590  global $DIC;
591 
592  $ilUser = $DIC['ilUser'];
593  self::initDateTimePicker();
594 
595  // fix for mantis 22994 => default to english language
596  $language = 'en';
597  if ($ilUser->getLanguage() != 'ar') {
598  $language = $ilUser->getLanguage();
599  }
600  $default = array(
601  'locale' => $language,
602  'stepping' => 5,
603  'useCurrent' => false,
604  'calendarWeeks' => true,
605  'toolbarPlacement' => 'top',
606  //'showTodayButton' => true,
607  'showClear' => true,
608  //'showClose' => true,
609  'keepInvalid' => true,
610  'sideBySide' => true,
611  //'collapse' => false,
612  'format' => self::getUserDateFormat((bool) $a_add_time)
613  );
614 
615  $config = (!$a_custom_config)
616  ? $default
617  : array_merge($default, $a_custom_config);
618 
619  $code = [];
620 
629  $start_of_week = ilCalendarUserSettings::_getInstanceByUserId($ilUser->getId())->getWeekStart();
630  $code[] =
631  'if (moment) {
632  moment.updateLocale("' . $language . '", {week: {dow: ' . $start_of_week . '}});
633  }';
634 
635  $code[] = '$("#' . $a_id . '").datetimepicker(' . json_encode($config) . ')';
636 
637  // optional 2nd picker aka duration
638  if ($a_id2) {
639  $config2 = (!$a_custom_config2)
640  ? $default
641  : array_merge($default, $a_custom_config2);
642 
643  $config2["useCurrent"] = false; //Important! See issue #1075
644 
645  $code[] = '$("#' . $a_id2 . '").datetimepicker(' . json_encode($config2) . ')';
646 
647  // duration limits, diff and subform handling
648  $code[] = 'il.Form.initDateDurationPicker("' . $a_id . '","' . $a_id2 . '","' . $a_toggle_id . '","' . $a_subform_id . '");';
649  } elseif ($a_subform_id) {
650  // subform handling
651  $code[] = 'il.Form.initDatePicker("' . $a_id . '","' . $a_subform_id . '");';
652  }
653  return $code;
654  }
655 
664  public static function parseDateString(string $a_date, bool $a_add_time = false, bool $a_use_generic_format = false): array
665  {
666  global $DIC;
667 
668  $ilUser = $DIC['ilUser'];
669  if (!$a_use_generic_format) {
670  $out_format = self::getUserDateFormat($a_add_time, true);
671  } else {
672  $out_format = $a_add_time
673  ? "Y-m-d H:i:s"
674  : "Y-m-d";
675  }
676  $tmp = date_parse_from_format($out_format, $a_date);
677  $date = null;
678 
679  if (!$tmp["error_count"] &&
680  !$tmp["warning_count"]) {
681  $format = $tmp["year"] . "-" .
682  str_pad($tmp["month"], 2, "0", STR_PAD_LEFT) . "-" .
683  str_pad($tmp["day"], 2, "0", STR_PAD_LEFT);
684 
685  if ($a_add_time) {
686  $format .= " " .
687  str_pad($tmp["hour"], 2, "0", STR_PAD_LEFT) . ":" .
688  str_pad($tmp["minute"], 2, "0", STR_PAD_LEFT) . ":" .
689  str_pad($tmp["second"], 2, "0", STR_PAD_LEFT);
690 
691  $date = new ilDateTime($format, IL_CAL_DATETIME, $ilUser->getTimeZone());
692  } else {
693  $date = new ilDate($format, IL_CAL_DATE);
694  }
695  }
696 
697  return array(
698  "date" => $date
699  ,
700  "warnings" => sizeof($tmp["warnings"])
701  ? $tmp["warnings"]
702  : null
703  ,
704  "errors" => sizeof($tmp["errors"])
705  ? $tmp["errors"]
706  : null
707  );
708  }
709 
716  public static function parseIncomingDate($a_value, bool $a_add_time = false): ?ilDateTime
717  {
718  // already datetime object?
719  if ($a_value instanceof ilDateTime) {
720  return $a_value;
721  } elseif (trim($a_value)) {
722  // try user-specific format
723  $parsed = self::parseDateString($a_value, $a_add_time);
724  if (is_object($parsed["date"])) {
725  return $parsed["date"];
726  } else {
727  // try generic format
728  $parsed = self::parseDateString($a_value, $a_add_time, true);
729  if (is_object($parsed["date"])) {
730  return $parsed["date"];
731  }
732  }
733  }
734  return null;
735  }
736 }
$res
Definition: ltiservices.php:69
static _getMaxDayOfMonth(int $a_year, int $a_month)
get max day of month 2008,2 => 29
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.
static _buildMonthDayList(int $a_month, int $a_year, int $weekstart)
Build a month day list.
const IL_CAL_DATETIME
static convertDateToUtcDBTimestamp(\ilDateTime $date=null)
static _numericMonthToString(int $a_month, bool $a_long=true)
numeric month to string
$lng
static parseDateString(string $a_date, bool $a_add_time=false, bool $a_use_generic_format=false)
Parse (incoming) string to date/time object.
if(!array_key_exists('PATH_INFO', $_SERVER)) $config
Definition: metadata.php:85
get(int $a_format, string $a_format_str='', string $a_tz='')
static _buildWeekDayList(ilDate $a_day, int $a_weekstart)
build week day list public
static getZoneInfoFile($a_tz)
static bool $init_datetimepicker
Stores calendar categories.
const IL_CAL_UNIX
static _getShortTimeZoneList()
get short timezone list
static getUserDateFormat(int $a_add_time=0, bool $a_for_parsing=false)
Parse current user setting into date/time format.
static getHourSelection(int $a_format)
Get hour selection depending on user specific hour format.
static _getInstanceByUserId(int $a_user_id)
static ilDateTime $today
global $DIC
Definition: feed.php:28
const IL_CAL_DAY
List of dates.
static initDefaultCalendarByType(int $a_type_id, int $a_usr_id, string $a_title, bool $a_create=false)
Init the default calendar for given type and user.
static _isLeapYear(int $a_year)
check if a given year is a leap year
const IL_CAL_FKT_DATE
$format
Definition: metadata.php:235
static array $default_calendar
$query
static _numericDayToString(int $a_day, bool $a_long=true)
const IL_CAL_FKT_GETDATE
const IL_CAL_DATE
static string $init_done
static calculateFontColor(string $a_html_color_code)
Calculate best font color from html hex color code.
$ilUser
Definition: imgupload.php:34
static _equals(ilDateTime $start, ilDateTime $end, string $a_compare_field='', string $a_tz='')
Check if two date are equal.
Class ilCalendarUtil.
if($DIC->http() ->request() ->getMethod()=="GET" &&isset($DIC->http() ->request() ->getQueryParams()['tex'])) $tpl
Definition: latex.php:41
static parseIncomingDate($a_value, bool $a_add_time=false)
Try to parse incoming value to date object.
static getCodeForPicker(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.
static _isToday(ilDateTime $date)
$i
Definition: metadata.php:41