1 | <?php |
||
2 | /** |
||
3 | * Elgg date input |
||
4 | * Displays a text field with a popup date picker. |
||
5 | * |
||
6 | * The elgg.ui JavaScript library initializes the jQueryUI datepicker based |
||
7 | * on the CSS class .elgg-input-date. It uses the ISO 8601 standard for date |
||
8 | * representation: yyyy-mm-dd. |
||
9 | * |
||
10 | * Unix timestamps are supported by setting the 'timestamp' parameter to true. |
||
11 | * The date is still displayed to the user in a text format but is submitted as |
||
12 | * a unix timestamp in seconds. |
||
13 | * |
||
14 | * @uses $vars['value'] The current value, if any (as a unix timestamp) |
||
15 | * @uses $vars['class'] Additional CSS class |
||
16 | * @uses $vars['timestamp'] Store as a Unix timestamp in seconds. Default = false |
||
17 | * @uses $vars['datepicker_options'] An array of options to pass to the jQuery UI datepicker |
||
18 | * @uses $vars['format'] Date format, default Y-m-d (2018-01-30) |
||
19 | */ |
||
20 | $vars['class'] = elgg_extract_class($vars, 'elgg-input-date'); |
||
21 | |||
22 | $defaults = [ |
||
23 | 'value' => '', |
||
24 | 'disabled' => false, |
||
25 | 'timestamp' => false, |
||
26 | 'type' => 'text', |
||
27 | 'format' => elgg_echo('input:date_format'), |
||
28 | ]; |
||
29 | |||
30 | $vars = array_merge($defaults, $vars); |
||
31 | |||
32 | $timestamp = $vars['timestamp']; |
||
33 | unset($vars['timestamp']); |
||
34 | |||
35 | $format = elgg_extract('format', $vars, $defaults['format'], false); |
||
36 | unset($vars['format']); |
||
37 | |||
38 | $value = elgg_extract('value', $vars); |
||
39 | |||
40 | $value_date = ''; |
||
41 | $value_timestamp = ''; |
||
42 | |||
43 | if ($value) { |
||
44 | try { |
||
45 | $value = \Elgg\Values::normalizeTime($value); |
||
46 | |||
47 | $value_date = $dt->format($format); |
||
48 | $value_timestamp = $dt->getTimestamp(); |
||
49 | } catch (DataFormatException $ex) { |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
Loading history...
|
|||
50 | } |
||
51 | } |
||
52 | |||
53 | if ($timestamp) { |
||
54 | if (!isset($vars['id'])) { |
||
55 | $vars['id'] = $vars['name']; |
||
56 | } |
||
57 | echo elgg_view('input/hidden', [ |
||
58 | 'name' => $vars['name'], |
||
59 | 'value' => $value_timestamp, |
||
60 | 'rel' => $vars['id'], |
||
61 | ]); |
||
62 | $vars['class'][] = 'elgg-input-timestamp'; |
||
63 | unset($vars['name']); |
||
64 | } |
||
65 | |||
66 | $vars['value'] = $value_date; |
||
67 | |||
68 | $datepicker_options = (array) elgg_extract('datepicker_options', $vars, []); |
||
69 | unset($vars['datepicker_options']); |
||
70 | |||
71 | if (empty($datepicker_options['dateFormat'])) { |
||
72 | $datepicker_options['dateFormat'] = elgg_echo('input:date_format:datepicker'); |
||
73 | } |
||
74 | |||
75 | $vars['data-datepicker-opts'] = $datepicker_options ? json_encode($datepicker_options) : ''; |
||
76 | |||
77 | echo elgg_format_element('input', $vars); |
||
78 | |||
79 | if (isset($vars['id'])) { |
||
80 | $selector = "#{$vars['id']}"; |
||
81 | } else { |
||
82 | $selector = ".elgg-input-date[name='{$vars['name']}']"; |
||
83 | } |
||
84 | ?> |
||
85 | <script> |
||
86 | require(['input/date'], function (datepicker) { |
||
87 | datepicker.init(<?= json_encode($selector) ?>); |
||
88 | }); |
||
89 | </script> |
||
90 |