Elgg  Version master
date.php
Go to the documentation of this file.
1 <?php
22 
23 $vars['class'] = elgg_extract_class($vars, 'elgg-input-date');
24 
26  'timestamp' => false,
27  'autocomplete' => 'off',
28  'type' => 'text',
29  'format' => elgg_get_config('date_format', elgg_echo('input:date_format')),
30 ];
31 
32 $vars = array_merge($defaults, $vars);
33 
34 $timestamp = elgg_extract('timestamp', $vars);
35 unset($vars['timestamp']);
36 
37 $format = elgg_extract('format', $vars, $defaults['format'], false);
38 unset($vars['format']);
39 
42 
45 
46 if ($value) {
47  try {
49 
50  $value_date = $dt->format($format);
51  $value_timestamp = $dt->getTimestamp();
52  } catch (DataFormatException $ex) {
53  // $value is not a valid datetime, using blank value
54  }
55 }
56 
57 if ($timestamp) {
58  if (!isset($vars['id'])) {
59  $vars['id'] = $name;
60  }
61 
62  echo elgg_view('input/hidden', [
63  'name' => $name,
64  'value' => $value_timestamp,
65  'rel' => elgg_extract('id', $vars),
66  ]);
67  $vars['class'][] = 'elgg-input-timestamp';
68  unset($vars['name']);
69 }
70 
71 $vars['value'] = $value_date;
72 
73 $datepicker_options = (array) elgg_extract('datepicker_options', $vars, []);
74 unset($vars['datepicker_options']);
75 
76 if (empty($datepicker_options['dateFormat'])) {
77  $datepicker_options['dateFormat'] = elgg_get_config('date_format_datepicker', elgg_echo('input:date_format:datepicker'));
78 }
79 
80 $vars['data-datepicker-opts'] = $datepicker_options ? json_encode($datepicker_options) : '';
81 
82 echo elgg_format_element('input', $vars);
83 
84 if (isset($vars['id'])) {
85  $selector = "#{$vars['id']}";
86 } else {
87  $selector = ".elgg-input-date[name='{$name}']";
88 }
89 ?>
90 <script>
91  import('input/date').then((datepicker) => {
92  datepicker.default.init(<?= json_encode($selector) ?>);
93  });
94 </script>
$format
Definition: date.php:37
elgg_get_config(string $name, $default=null)
Get an Elgg configuration value.
elgg_echo(string $message_key, array $args=[], string $language= '')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
$timestamp
Definition: date.php:34
$value
Definition: date.php:41
$name
Definition: date.php:40
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:254
$selector
Definition: tags.php:54
$vars['class']
Definition: date.php:23
elgg_view(string $view, array $vars=[], string $viewtype= '')
Return a parsed view.
Definition: views.php:156
$dt
Definition: time.php:70
$datepicker_options
Definition: date.php:73
An exception thrown when there is a problem in the format of some data.
static normalizeTime($time)
Returns DateTime object based on time representation.
Definition: Values.php:75
$value_timestamp
Definition: date.php:44
elgg_format_element(string $tag_name, array $attributes=[], string $text= '', array $options=[])
Format an HTML element.
Definition: output.php:145
elgg_extract_class(array $array, array|string $existing=[], string $extract_key= 'class')
Extract class names from an array, optionally merging into a preexisting set.
Definition: elgglib.php:277
$value_date
Definition: date.php:43
$defaults
Definition: date.php:25