Subversion Repositories ALCASAR

Rev

Details | Last modification | View Log

Rev Author Line No. Line
2809 rexy 1
<?php
2
/**
3
 * Smarty plugin
4
 *
5
 * @package    Smarty
6
 * @subpackage PluginsFunction
7
 */
8
/**
9
 * Smarty {html_select_time} function plugin
10
 * Type:     function
11
 * Name:     html_select_time
12
 * Purpose:  Prints the dropdowns for time selection
13
 *
14
 * @link   http://www.smarty.net/manual/en/language.function.html.select.time.php {html_select_time}
15
 *           (Smarty online manual)
16
 * @author Roberto Berto <roberto@berto.net>
17
 * @author Monte Ohrt <monte AT ohrt DOT com>
18
 *
19
 * @param array                     $params parameters
20
 *
21
 * @param \Smarty_Internal_Template $template
22
 *
23
 * @return string
24
 * @uses   smarty_make_timestamp()
25
 * @throws \SmartyException
26
 */
27
function smarty_function_html_select_time($params, Smarty_Internal_Template $template)
28
{
29
    $template->_checkPlugins(
30
        array(
31
            array(
32
                'function' => 'smarty_function_escape_special_chars',
33
                'file'     => SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php'
34
            )
35
        )
36
    );
37
    $prefix = 'Time_';
38
    $field_array = null;
39
    $field_separator = "\n";
40
    $option_separator = "\n";
41
    $time = null;
42
    $display_hours = true;
43
    $display_minutes = true;
44
    $display_seconds = true;
45
    $display_meridian = true;
46
    $hour_format = '%02d';
47
    $hour_value_format = '%02d';
48
    $minute_format = '%02d';
49
    $minute_value_format = '%02d';
50
    $second_format = '%02d';
51
    $second_value_format = '%02d';
52
    $hour_size = null;
53
    $minute_size = null;
54
    $second_size = null;
55
    $meridian_size = null;
56
    $all_empty = null;
57
    $hour_empty = null;
58
    $minute_empty = null;
59
    $second_empty = null;
60
    $meridian_empty = null;
61
    $all_id = null;
62
    $hour_id = null;
63
    $minute_id = null;
64
    $second_id = null;
65
    $meridian_id = null;
66
    $use_24_hours = true;
67
    $minute_interval = 1;
68
    $second_interval = 1;
69
    $extra_attrs = '';
70
    $all_extra = null;
71
    $hour_extra = null;
72
    $minute_extra = null;
73
    $second_extra = null;
74
    $meridian_extra = null;
75
    foreach ($params as $_key => $_value) {
76
        switch ($_key) {
77
            case 'time':
78
                if (!is_array($_value) && $_value !== null) {
79
                    $template->_checkPlugins(
80
                        array(
81
                            array(
82
                                'function' => 'smarty_make_timestamp',
83
                                'file'     => SMARTY_PLUGINS_DIR . 'shared.make_timestamp.php'
84
                            )
85
                        )
86
                    );
87
                    $time = smarty_make_timestamp($_value);
88
                }
89
                break;
90
            case 'prefix':
91
            case 'field_array':
92
            case 'field_separator':
93
            case 'option_separator':
94
            case 'all_extra':
95
            case 'hour_extra':
96
            case 'minute_extra':
97
            case 'second_extra':
98
            case 'meridian_extra':
99
            case 'all_empty':
100
            case 'hour_empty':
101
            case 'minute_empty':
102
            case 'second_empty':
103
            case 'meridian_empty':
104
            case 'all_id':
105
            case 'hour_id':
106
            case 'minute_id':
107
            case 'second_id':
108
            case 'meridian_id':
109
            case 'hour_format':
110
            case 'hour_value_format':
111
            case 'minute_format':
112
            case 'minute_value_format':
113
            case 'second_format':
114
            case 'second_value_format':
115
                $$_key = (string)$_value;
116
                break;
117
            case 'display_hours':
118
            case 'display_minutes':
119
            case 'display_seconds':
120
            case 'display_meridian':
121
            case 'use_24_hours':
122
                $$_key = (bool)$_value;
123
                break;
124
            case 'minute_interval':
125
            case 'second_interval':
126
            case 'hour_size':
127
            case 'minute_size':
128
            case 'second_size':
129
            case 'meridian_size':
130
                $$_key = (int)$_value;
131
                break;
132
            default:
133
                if (!is_array($_value)) {
134
                    $extra_attrs .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_value) . '"';
135
                } else {
136
                    trigger_error("html_select_date: extra attribute '{$_key}' cannot be an array", E_USER_NOTICE);
137
                }
138
                break;
139
        }
140
    }
141
    if (isset($params[ 'time' ]) && is_array($params[ 'time' ])) {
142
        if (isset($params[ 'time' ][ $prefix . 'Hour' ])) {
143
            // $_REQUEST[$field_array] given
144
            foreach (array(
145
                'H' => 'Hour',
146
                'i' => 'Minute',
147
                's' => 'Second'
148
            ) as $_elementKey => $_elementName) {
149
                $_variableName = '_' . strtolower($_elementName);
150
                $$_variableName =
151
                    isset($params[ 'time' ][ $prefix . $_elementName ]) ? $params[ 'time' ][ $prefix . $_elementName ] :
152
                        date($_elementKey);
153
            }
154
            $_meridian =
155
                isset($params[ 'time' ][ $prefix . 'Meridian' ]) ? (' ' . $params[ 'time' ][ $prefix . 'Meridian' ]) :
156
                    '';
157
            $time = strtotime($_hour . ':' . $_minute . ':' . $_second . $_meridian);
158
            list($_hour, $_minute, $_second) = $time = explode('-', date('H-i-s', $time));
159
        } elseif (isset($params[ 'time' ][ $field_array ][ $prefix . 'Hour' ])) {
160
            // $_REQUEST given
161
            foreach (array(
162
                'H' => 'Hour',
163
                'i' => 'Minute',
164
                's' => 'Second'
165
            ) as $_elementKey => $_elementName) {
166
                $_variableName = '_' . strtolower($_elementName);
167
                $$_variableName = isset($params[ 'time' ][ $field_array ][ $prefix . $_elementName ]) ?
168
                    $params[ 'time' ][ $field_array ][ $prefix . $_elementName ] : date($_elementKey);
169
            }
170
            $_meridian = isset($params[ 'time' ][ $field_array ][ $prefix . 'Meridian' ]) ?
171
                (' ' . $params[ 'time' ][ $field_array ][ $prefix . 'Meridian' ]) : '';
172
            $time = strtotime($_hour . ':' . $_minute . ':' . $_second . $_meridian);
173
            list($_hour, $_minute, $_second) = $time = explode('-', date('H-i-s', $time));
174
        } else {
175
            // no date found, use NOW
176
            list($_year, $_month, $_day) = $time = explode('-', date('Y-m-d'));
177
        }
178
    } elseif ($time === null) {
179
        if (array_key_exists('time', $params)) {
180
            $_hour = $_minute = $_second = $time = null;
181
        } else {
182
            list($_hour, $_minute, $_second) = $time = explode('-', date('H-i-s'));
183
        }
184
    } else {
185
        list($_hour, $_minute, $_second) = $time = explode('-', date('H-i-s', $time));
186
    }
187
    // generate hour <select>
188
    if ($display_hours) {
189
        $_html_hours = '';
190
        $_extra = '';
191
        $_name = $field_array ? ($field_array . '[' . $prefix . 'Hour]') : ($prefix . 'Hour');
192
        if ($all_extra) {
193
            $_extra .= ' ' . $all_extra;
194
        }
195
        if ($hour_extra) {
196
            $_extra .= ' ' . $hour_extra;
197
        }
198
        $_html_hours = '<select name="' . $_name . '"';
199
        if ($hour_id !== null || $all_id !== null) {
200
            $_html_hours .= ' id="' .
201
                            smarty_function_escape_special_chars(
202
                                $hour_id !== null ? ($hour_id ? $hour_id : $_name) :
203
                                    ($all_id ? ($all_id . $_name) : $_name)
204
                            ) . '"';
205
        }
206
        if ($hour_size) {
207
            $_html_hours .= ' size="' . $hour_size . '"';
208
        }
209
        $_html_hours .= $_extra . $extra_attrs . '>' . $option_separator;
210
        if (isset($hour_empty) || isset($all_empty)) {
211
            $_html_hours .= '<option value="">' . (isset($hour_empty) ? $hour_empty : $all_empty) . '</option>' .
212
                            $option_separator;
213
        }
214
        $start = $use_24_hours ? 0 : 1;
215
        $end = $use_24_hours ? 23 : 12;
216
        for ($i = $start; $i <= $end; $i++) {
217
            $_val = sprintf('%02d', $i);
218
            $_text = $hour_format === '%02d' ? $_val : sprintf($hour_format, $i);
219
            $_value = $hour_value_format === '%02d' ? $_val : sprintf($hour_value_format, $i);
220
            if (!$use_24_hours) {
221
                $_hour12 = $_hour == 0 ? 12 : ($_hour <= 12 ? $_hour : $_hour - 12);
222
            }
223
            $selected = $_hour !== null ? ($use_24_hours ? $_hour == $_val : $_hour12 == $_val) : null;
224
            $_html_hours .= '<option value="' . $_value . '"' . ($selected ? ' selected="selected"' : '') . '>' .
225
                            $_text . '</option>' . $option_separator;
226
        }
227
        $_html_hours .= '</select>';
228
    }
229
    // generate minute <select>
230
    if ($display_minutes) {
231
        $_html_minutes = '';
232
        $_extra = '';
233
        $_name = $field_array ? ($field_array . '[' . $prefix . 'Minute]') : ($prefix . 'Minute');
234
        if ($all_extra) {
235
            $_extra .= ' ' . $all_extra;
236
        }
237
        if ($minute_extra) {
238
            $_extra .= ' ' . $minute_extra;
239
        }
240
        $_html_minutes = '<select name="' . $_name . '"';
241
        if ($minute_id !== null || $all_id !== null) {
242
            $_html_minutes .= ' id="' . smarty_function_escape_special_chars(
243
                    $minute_id !== null ?
244
                        ($minute_id ? $minute_id : $_name) :
245
                        ($all_id ? ($all_id . $_name) :
246
                            $_name)
247
                ) . '"';
248
        }
249
        if ($minute_size) {
250
            $_html_minutes .= ' size="' . $minute_size . '"';
251
        }
252
        $_html_minutes .= $_extra . $extra_attrs . '>' . $option_separator;
253
        if (isset($minute_empty) || isset($all_empty)) {
254
            $_html_minutes .= '<option value="">' . (isset($minute_empty) ? $minute_empty : $all_empty) . '</option>' .
255
                              $option_separator;
256
        }
257
        $selected = $_minute !== null ? ($_minute - $_minute % $minute_interval) : null;
258
        for ($i = 0; $i <= 59; $i += $minute_interval) {
259
            $_val = sprintf('%02d', $i);
260
            $_text = $minute_format === '%02d' ? $_val : sprintf($minute_format, $i);
261
            $_value = $minute_value_format === '%02d' ? $_val : sprintf($minute_value_format, $i);
262
            $_html_minutes .= '<option value="' . $_value . '"' . ($selected === $i ? ' selected="selected"' : '') .
263
                              '>' . $_text . '</option>' . $option_separator;
264
        }
265
        $_html_minutes .= '</select>';
266
    }
267
    // generate second <select>
268
    if ($display_seconds) {
269
        $_html_seconds = '';
270
        $_extra = '';
271
        $_name = $field_array ? ($field_array . '[' . $prefix . 'Second]') : ($prefix . 'Second');
272
        if ($all_extra) {
273
            $_extra .= ' ' . $all_extra;
274
        }
275
        if ($second_extra) {
276
            $_extra .= ' ' . $second_extra;
277
        }
278
        $_html_seconds = '<select name="' . $_name . '"';
279
        if ($second_id !== null || $all_id !== null) {
280
            $_html_seconds .= ' id="' . smarty_function_escape_special_chars(
281
                    $second_id !== null ?
282
                        ($second_id ? $second_id : $_name) :
283
                        ($all_id ? ($all_id . $_name) :
284
                            $_name)
285
                ) . '"';
286
        }
287
        if ($second_size) {
288
            $_html_seconds .= ' size="' . $second_size . '"';
289
        }
290
        $_html_seconds .= $_extra . $extra_attrs . '>' . $option_separator;
291
        if (isset($second_empty) || isset($all_empty)) {
292
            $_html_seconds .= '<option value="">' . (isset($second_empty) ? $second_empty : $all_empty) . '</option>' .
293
                              $option_separator;
294
        }
295
        $selected = $_second !== null ? ($_second - $_second % $second_interval) : null;
296
        for ($i = 0; $i <= 59; $i += $second_interval) {
297
            $_val = sprintf('%02d', $i);
298
            $_text = $second_format === '%02d' ? $_val : sprintf($second_format, $i);
299
            $_value = $second_value_format === '%02d' ? $_val : sprintf($second_value_format, $i);
300
            $_html_seconds .= '<option value="' . $_value . '"' . ($selected === $i ? ' selected="selected"' : '') .
301
                              '>' . $_text . '</option>' . $option_separator;
302
        }
303
        $_html_seconds .= '</select>';
304
    }
305
    // generate meridian <select>
306
    if ($display_meridian && !$use_24_hours) {
307
        $_html_meridian = '';
308
        $_extra = '';
309
        $_name = $field_array ? ($field_array . '[' . $prefix . 'Meridian]') : ($prefix . 'Meridian');
310
        if ($all_extra) {
311
            $_extra .= ' ' . $all_extra;
312
        }
313
        if ($meridian_extra) {
314
            $_extra .= ' ' . $meridian_extra;
315
        }
316
        $_html_meridian = '<select name="' . $_name . '"';
317
        if ($meridian_id !== null || $all_id !== null) {
318
            $_html_meridian .= ' id="' . smarty_function_escape_special_chars(
319
                    $meridian_id !== null ?
320
                        ($meridian_id ? $meridian_id :
321
                            $_name) :
322
                        ($all_id ? ($all_id . $_name) :
323
                            $_name)
324
                ) . '"';
325
        }
326
        if ($meridian_size) {
327
            $_html_meridian .= ' size="' . $meridian_size . '"';
328
        }
329
        $_html_meridian .= $_extra . $extra_attrs . '>' . $option_separator;
330
        if (isset($meridian_empty) || isset($all_empty)) {
331
            $_html_meridian .= '<option value="">' . (isset($meridian_empty) ? $meridian_empty : $all_empty) .
332
                               '</option>' . $option_separator;
333
        }
334
        $_html_meridian .= '<option value="am"' . ($_hour > 0 && $_hour < 12 ? ' selected="selected"' : '') .
335
                           '>AM</option>' . $option_separator . '<option value="pm"' .
336
                           ($_hour < 12 ? '' : ' selected="selected"') . '>PM</option>' . $option_separator .
337
                           '</select>';
338
    }
339
    $_html = '';
340
    foreach (array(
341
        '_html_hours',
342
        '_html_minutes',
343
        '_html_seconds',
344
        '_html_meridian'
345
    ) as $k) {
346
        if (isset($$k)) {
347
            if ($_html) {
348
                $_html .= $field_separator;
349
            }
350
            $_html .= $$k;
351
        }
352
    }
353
    return $_html;
354
}