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_date} plugin
10
 * Type:     function
11
 * Name:     html_select_date
12
 * Purpose:  Prints the dropdowns for date selection.
13
 * ChangeLog:
14
 *
15
 *            - 1.0 initial release
16
 *            - 1.1 added support for +/- N syntax for begin
17
 *              and end year values. (Monte)
18
 *            - 1.2 added support for yyyy-mm-dd syntax for
19
 *              time value. (Jan Rosier)
20
 *            - 1.3 added support for choosing format for
21
 *              month values (Gary Loescher)
22
 *            - 1.3.1 added support for choosing format for
23
 *              day values (Marcus Bointon)
24
 *            - 1.3.2 support negative timestamps, force year
25
 *              dropdown to include given date unless explicitly set (Monte)
26
 *            - 1.3.4 fix behaviour of 0000-00-00 00:00:00 dates to match that
27
 *              of 0000-00-00 dates (cybot, boots)
28
 *            - 2.0 complete rewrite for performance,
29
 *              added attributes month_names, *_id
30
 *
31
 * @link    http://www.smarty.net/manual/en/language.function.html.select.date.php {html_select_date}
32
 *           (Smarty online manual)
33
 * @version 2.0
34
 * @author  Andrei Zmievski
35
 * @author  Monte Ohrt <monte at ohrt dot com>
36
 * @author  Rodney Rehm
37
 *
38
 * @param array                     $params parameters
39
 *
40
 * @param \Smarty_Internal_Template $template
41
 *
42
 * @return string
43
 * @throws \SmartyException
44
 */
45
function smarty_function_html_select_date($params, Smarty_Internal_Template $template)
46
{
47
    $template->_checkPlugins(
48
        array(
49
            array(
50
                'function' => 'smarty_function_escape_special_chars',
51
                'file'     => SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php'
52
            )
53
        )
54
    );
55
    // generate timestamps used for month names only
56
    static $_month_timestamps = null;
57
    static $_current_year = null;
58
    if ($_month_timestamps === null) {
59
        $_current_year = date('Y');
60
        $_month_timestamps = array();
61
        for ($i = 1; $i <= 12; $i++) {
62
            $_month_timestamps[ $i ] = mktime(0, 0, 0, $i, 1, 2000);
63
        }
64
    }
65
    /* Default values. */
66
    $prefix = 'Date_';
67
    $start_year = null;
68
    $end_year = null;
69
    $display_days = true;
70
    $display_months = true;
71
    $display_years = true;
72
    $month_format = '%B';
73
    /* Write months as numbers by default  GL */
74
    $month_value_format = '%m';
75
    $day_format = '%02d';
76
    /* Write day values using this format MB */
77
    $day_value_format = '%d';
78
    $year_as_text = false;
79
    /* Display years in reverse order? Ie. 2000,1999,.... */
80
    $reverse_years = false;
81
    /* Should the select boxes be part of an array when returned from PHP?
82
       e.g. setting it to "birthday", would create "birthday[Day]",
83
       "birthday[Month]" & "birthday[Year]". Can be combined with prefix */
84
    $field_array = null;
85
    /* <select size>'s of the different <select> tags.
86
       If not set, uses default dropdown. */
87
    $day_size = null;
88
    $month_size = null;
89
    $year_size = null;
90
    /* Unparsed attributes common to *ALL* the <select>/<input> tags.
91
       An example might be in the template: all_extra ='class ="foo"'. */
92
    $all_extra = null;
93
    /* Separate attributes for the tags. */
94
    $day_extra = null;
95
    $month_extra = null;
96
    $year_extra = null;
97
    /* Order in which to display the fields.
98
       "D" -> day, "M" -> month, "Y" -> year. */
99
    $field_order = 'MDY';
100
    /* String printed between the different fields. */
101
    $field_separator = "\n";
102
    $option_separator = "\n";
103
    $time = null;
104
    // $all_empty = null;
105
    // $day_empty = null;
106
    // $month_empty = null;
107
    // $year_empty = null;
108
    $extra_attrs = '';
109
    $all_id = null;
110
    $day_id = null;
111
    $month_id = null;
112
    $year_id = null;
113
    foreach ($params as $_key => $_value) {
114
        switch ($_key) {
115
            case 'time':
116
                if (!is_array($_value) && $_value !== null) {
117
                    $template->_checkPlugins(
118
                        array(
119
                            array(
120
                                'function' => 'smarty_make_timestamp',
121
                                'file'     => SMARTY_PLUGINS_DIR . 'shared.make_timestamp.php'
122
                            )
123
                        )
124
                    );
125
                    $time = smarty_make_timestamp($_value);
126
                }
127
                break;
128
            case 'month_names':
129
                if (is_array($_value) && count($_value) === 12) {
130
                    $$_key = $_value;
131
                } else {
132
                    trigger_error('html_select_date: month_names must be an array of 12 strings', E_USER_NOTICE);
133
                }
134
                break;
135
            case 'prefix':
136
            case 'field_array':
137
            case 'start_year':
138
            case 'end_year':
139
            case 'day_format':
140
            case 'day_value_format':
141
            case 'month_format':
142
            case 'month_value_format':
143
            case 'day_size':
144
            case 'month_size':
145
            case 'year_size':
146
            case 'all_extra':
147
            case 'day_extra':
148
            case 'month_extra':
149
            case 'year_extra':
150
            case 'field_order':
151
            case 'field_separator':
152
            case 'option_separator':
153
            case 'all_empty':
154
            case 'month_empty':
155
            case 'day_empty':
156
            case 'year_empty':
157
            case 'all_id':
158
            case 'month_id':
159
            case 'day_id':
160
            case 'year_id':
161
                $$_key = (string)$_value;
162
                break;
163
            case 'display_days':
164
            case 'display_months':
165
            case 'display_years':
166
            case 'year_as_text':
167
            case 'reverse_years':
168
                $$_key = (bool)$_value;
169
                break;
170
            default:
171
                if (!is_array($_value)) {
172
                    $extra_attrs .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_value) . '"';
173
                } else {
174
                    trigger_error("html_select_date: extra attribute '{$_key}' cannot be an array", E_USER_NOTICE);
175
                }
176
                break;
177
        }
178
    }
179
    // Note: date() is faster than strftime()
180
    // Note: explode(date()) is faster than date() date() date()
181
    if (isset($params[ 'time' ]) && is_array($params[ 'time' ])) {
182
        if (isset($params[ 'time' ][ $prefix . 'Year' ])) {
183
            // $_REQUEST[$field_array] given
184
            foreach (array(
185
                'Y' => 'Year',
186
                'm' => 'Month',
187
                'd' => 'Day'
188
            ) as $_elementKey => $_elementName) {
189
                $_variableName = '_' . strtolower($_elementName);
190
                $$_variableName =
191
                    isset($params[ 'time' ][ $prefix . $_elementName ]) ? $params[ 'time' ][ $prefix . $_elementName ] :
192
                        date($_elementKey);
193
            }
194
        } elseif (isset($params[ 'time' ][ $field_array ][ $prefix . 'Year' ])) {
195
            // $_REQUEST given
196
            foreach (array(
197
                'Y' => 'Year',
198
                'm' => 'Month',
199
                'd' => 'Day'
200
            ) as $_elementKey => $_elementName) {
201
                $_variableName = '_' . strtolower($_elementName);
202
                $$_variableName = isset($params[ 'time' ][ $field_array ][ $prefix . $_elementName ]) ?
203
                    $params[ 'time' ][ $field_array ][ $prefix . $_elementName ] : date($_elementKey);
204
            }
205
        } else {
206
            // no date found, use NOW
207
            list($_year, $_month, $_day) = $time = explode('-', date('Y-m-d'));
208
        }
209
    } elseif ($time === null) {
210
        if (array_key_exists('time', $params)) {
211
            $_year = $_month = $_day = $time = null;
212
        } else {
213
            list($_year, $_month, $_day) = $time = explode('-', date('Y-m-d'));
214
        }
215
    } else {
216
        list($_year, $_month, $_day) = $time = explode('-', date('Y-m-d', $time));
217
    }
218
    // make syntax "+N" or "-N" work with $start_year and $end_year
219
    // Note preg_match('!^(\+|\-)\s*(\d+)$!', $end_year, $match) is slower than trim+substr
220
    foreach (array(
221
        'start',
222
        'end'
223
    ) as $key) {
224
        $key .= '_year';
225
        $t = $$key;
226
        if ($t === null) {
227
            $$key = (int)$_current_year;
228
        } elseif ($t[ 0 ] === '+') {
229
            $$key = (int)($_current_year + (int)trim(substr($t, 1)));
230
        } elseif ($t[ 0 ] === '-') {
231
            $$key = (int)($_current_year - (int)trim(substr($t, 1)));
232
        } else {
233
            $$key = (int)$$key;
234
        }
235
    }
236
    // flip for ascending or descending
237
    if (($start_year > $end_year && !$reverse_years) || ($start_year < $end_year && $reverse_years)) {
238
        $t = $end_year;
239
        $end_year = $start_year;
240
        $start_year = $t;
241
    }
242
    // generate year <select> or <input>
243
    if ($display_years) {
244
        $_extra = '';
245
        $_name = $field_array ? ($field_array . '[' . $prefix . 'Year]') : ($prefix . 'Year');
246
        if ($all_extra) {
247
            $_extra .= ' ' . $all_extra;
248
        }
249
        if ($year_extra) {
250
            $_extra .= ' ' . $year_extra;
251
        }
252
        if ($year_as_text) {
253
            $_html_years =
254
                '<input type="text" name="' . $_name . '" value="' . $_year . '" size="4" maxlength="4"' . $_extra .
255
                $extra_attrs . ' />';
256
        } else {
257
            $_html_years = '<select name="' . $_name . '"';
258
            if ($year_id !== null || $all_id !== null) {
259
                $_html_years .= ' id="' . smarty_function_escape_special_chars(
260
                        $year_id !== null ?
261
                            ($year_id ? $year_id : $_name) :
262
                            ($all_id ? ($all_id . $_name) :
263
                                $_name)
264
                    ) . '"';
265
            }
266
            if ($year_size) {
267
                $_html_years .= ' size="' . $year_size . '"';
268
            }
269
            $_html_years .= $_extra . $extra_attrs . '>' . $option_separator;
270
            if (isset($year_empty) || isset($all_empty)) {
271
                $_html_years .= '<option value="">' . (isset($year_empty) ? $year_empty : $all_empty) . '</option>' .
272
                                $option_separator;
273
            }
274
            $op = $start_year > $end_year ? -1 : 1;
275
            for ($i = $start_year; $op > 0 ? $i <= $end_year : $i >= $end_year; $i += $op) {
276
                $_html_years .= '<option value="' . $i . '"' . ($_year == $i ? ' selected="selected"' : '') . '>' . $i .
277
                                '</option>' . $option_separator;
278
            }
279
            $_html_years .= '</select>';
280
        }
281
    }
282
    // generate month <select> or <input>
283
    if ($display_months) {
284
        $_extra = '';
285
        $_name = $field_array ? ($field_array . '[' . $prefix . 'Month]') : ($prefix . 'Month');
286
        if ($all_extra) {
287
            $_extra .= ' ' . $all_extra;
288
        }
289
        if ($month_extra) {
290
            $_extra .= ' ' . $month_extra;
291
        }
292
        $_html_months = '<select name="' . $_name . '"';
293
        if ($month_id !== null || $all_id !== null) {
294
            $_html_months .= ' id="' . smarty_function_escape_special_chars(
295
                    $month_id !== null ?
296
                        ($month_id ? $month_id : $_name) :
297
                        ($all_id ? ($all_id . $_name) :
298
                            $_name)
299
                ) . '"';
300
        }
301
        if ($month_size) {
302
            $_html_months .= ' size="' . $month_size . '"';
303
        }
304
        $_html_months .= $_extra . $extra_attrs . '>' . $option_separator;
305
        if (isset($month_empty) || isset($all_empty)) {
306
            $_html_months .= '<option value="">' . (isset($month_empty) ? $month_empty : $all_empty) . '</option>' .
307
                             $option_separator;
308
        }
309
        for ($i = 1; $i <= 12; $i++) {
310
            $_val = sprintf('%02d', $i);
311
            $_text = isset($month_names) ? smarty_function_escape_special_chars($month_names[ $i ]) :
312
                ($month_format === '%m' ? $_val : strftime($month_format, $_month_timestamps[ $i ]));
313
            $_value = $month_value_format === '%m' ? $_val : strftime($month_value_format, $_month_timestamps[ $i ]);
314
            $_html_months .= '<option value="' . $_value . '"' . ($_val == $_month ? ' selected="selected"' : '') .
315
                             '>' . $_text . '</option>' . $option_separator;
316
        }
317
        $_html_months .= '</select>';
318
    }
319
    // generate day <select> or <input>
320
    if ($display_days) {
321
        $_extra = '';
322
        $_name = $field_array ? ($field_array . '[' . $prefix . 'Day]') : ($prefix . 'Day');
323
        if ($all_extra) {
324
            $_extra .= ' ' . $all_extra;
325
        }
326
        if ($day_extra) {
327
            $_extra .= ' ' . $day_extra;
328
        }
329
        $_html_days = '<select name="' . $_name . '"';
330
        if ($day_id !== null || $all_id !== null) {
331
            $_html_days .= ' id="' .
332
                           smarty_function_escape_special_chars(
333
                               $day_id !== null ? ($day_id ? $day_id : $_name) :
334
                                   ($all_id ? ($all_id . $_name) : $_name)
335
                           ) . '"';
336
        }
337
        if ($day_size) {
338
            $_html_days .= ' size="' . $day_size . '"';
339
        }
340
        $_html_days .= $_extra . $extra_attrs . '>' . $option_separator;
341
        if (isset($day_empty) || isset($all_empty)) {
342
            $_html_days .= '<option value="">' . (isset($day_empty) ? $day_empty : $all_empty) . '</option>' .
343
                           $option_separator;
344
        }
345
        for ($i = 1; $i <= 31; $i++) {
346
            $_val = sprintf('%02d', $i);
347
            $_text = $day_format === '%02d' ? $_val : sprintf($day_format, $i);
348
            $_value = $day_value_format === '%02d' ? $_val : sprintf($day_value_format, $i);
349
            $_html_days .= '<option value="' . $_value . '"' . ($_val == $_day ? ' selected="selected"' : '') . '>' .
350
                           $_text . '</option>' . $option_separator;
351
        }
352
        $_html_days .= '</select>';
353
    }
354
    // order the fields for output
355
    $_html = '';
356
    for ($i = 0; $i <= 2; $i++) {
357
        switch ($field_order[ $i ]) {
358
            case 'Y':
359
            case 'y':
360
                if (isset($_html_years)) {
361
                    if ($_html) {
362
                        $_html .= $field_separator;
363
                    }
364
                    $_html .= $_html_years;
365
                }
366
                break;
367
            case 'm':
368
            case 'M':
369
                if (isset($_html_months)) {
370
                    if ($_html) {
371
                        $_html .= $field_separator;
372
                    }
373
                    $_html .= $_html_months;
374
                }
375
                break;
376
            case 'd':
377
            case 'D':
378
                if (isset($_html_days)) {
379
                    if ($_html) {
380
                        $_html .= $field_separator;
381
                    }
382
                    $_html .= $_html_days;
383
                }
384
                break;
385
        }
386
    }
387
    return $_html;
388
}