1 | <?php |
||
2 | /** |
||
3 | * Elgg time input |
||
4 | * Displays a select field with time options. |
||
5 | * |
||
6 | * Unix timestamps are supported by setting the 'timestamp' parameter to true. |
||
7 | * |
||
8 | * @uses $vars['value'] The current value, if any (as a unix timestamp) |
||
9 | * @uses $vars['class'] Additional CSS class |
||
10 | * @uses $vars['timestamp'] Store as a Unix timestamp in seconds. Default = false |
||
11 | * @uses $vars['format'] Date format, default g:ia (4:44pm) |
||
12 | * @uses $vars['step'] Interval/step (in seconds) between available time options (e.g. 15*60 for 15min) |
||
13 | * @uses $vars['min'] Min available time in seconds (e.g. 2*60*60 for 2am) |
||
14 | * @uses $vars['max'] Max available time in seconds (e.g. 23*60*60 for 11pm) |
||
15 | */ |
||
16 | $vars['class'] = elgg_extract_class($vars, 'elgg-input-time'); |
||
17 | |||
18 | $defaults = [ |
||
19 | 'value' => '', |
||
20 | 'timestamp' => false, |
||
21 | 'type' => 'select', |
||
22 | 'format' => elgg_echo('input:time_format'), |
||
23 | ]; |
||
24 | |||
25 | $vars = array_merge($defaults, $vars); |
||
26 | |||
27 | $timestamp = $vars['timestamp']; |
||
28 | unset($vars['timestamp']); |
||
29 | |||
30 | $format = elgg_extract('format', $vars, $defaults['format'], false); |
||
31 | unset($vars['format']); |
||
32 | |||
33 | $min = (int) elgg_extract('min', $vars, 0); |
||
34 | unset($vars['min']); |
||
35 | |||
36 | $max = (int) elgg_extract('max', $vars, 24 * 60 * 60); |
||
37 | unset($vars['max']); |
||
38 | |||
39 | $step = (int) elgg_extract('step', $vars, 15 * 60); |
||
40 | unset($vars['step']); |
||
41 | |||
42 | $value = elgg_extract('value', $vars); |
||
43 | $value_time = ''; |
||
44 | $value_timestamp = ''; |
||
45 | if ($value) { |
||
46 | try { |
||
47 | $dt = \Elgg\Values::normalizeTime($value); |
||
48 | |||
49 | // round value to the closest divisible of a step |
||
50 | $next_step_ts = (int) ceil($dt->getTimestamp() / $step) * $step; |
||
51 | $dt->setTimestamp($next_step_ts); |
||
52 | |||
53 | $value_timestamp = $dt->format('H') * 60 * 60 + $dt->format('i') * 60; |
||
54 | $value_time = $dt->format($format); |
||
55 | } catch (DataFormatException $ex) { |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
Loading history...
|
|||
56 | } |
||
57 | } |
||
58 | |||
59 | if ($timestamp) { |
||
60 | $vars['value'] = $value_timestamp; |
||
61 | } else { |
||
62 | $vars['value'] = $value_time; |
||
63 | } |
||
64 | |||
65 | $hour_options = []; |
||
66 | $hour_options_ts = range($min, $max, $step); |
||
67 | |||
68 | $dt = new DateTime(null, new DateTimeZone('UTC')); |
||
69 | |||
70 | foreach ($hour_options_ts as $ts) { |
||
71 | $dt->setTimestamp($ts); |
||
72 | $key = ($timestamp) ? $dt->getTimestamp() : $dt->format($format); |
||
73 | $hour_options[$key] = $dt->format($format); |
||
74 | } |
||
75 | |||
76 | $vars['options_values'] = $hour_options; |
||
77 | |||
78 | echo elgg_view('input/select', $vars); |
||
79 |