PageRenderTime 163ms CodeModel.GetById 2ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/vendor/smarty3/lib/libs/plugins/function.html_select_date.php

http://zoop.googlecode.com/
PHP | 333 lines | 249 code | 18 blank | 66 comment | 51 complexity | f7e1b677d7eb1885e97a4727d65ca3b5 MD5 | raw file
  1<?php
  2/**
  3* Smarty plugin
  4* 
  5* @package Smarty
  6* @subpackage PluginsFunction
  7*/
  8
  9/**
 10* Smarty {html_select_date} plugin
 11* 
 12* Type:     function<br>
 13* Name:     html_select_date<br>
 14* Purpose:  Prints the dropdowns for date selection.
 15* 
 16* ChangeLog:<br>
 17*            - 1.0 initial release
 18*            - 1.1 added support for +/- N syntax for begin
 19*                 and end year values. (Monte)
 20*            - 1.2 added support for yyyy-mm-dd syntax for
 21*                 time value. (Jan Rosier)
 22*            - 1.3 added support for choosing format for
 23*                 month values (Gary Loescher)
 24*            - 1.3.1 added support for choosing format for
 25*                 day values (Marcus Bointon)
 26*            - 1.3.2 support negative timestamps, force year
 27*              dropdown to include given date unless explicitly set (Monte)
 28*            - 1.3.4 fix behaviour of 0000-00-00 00:00:00 dates to match that
 29*              of 0000-00-00 dates (cybot, boots)
 30* 
 31* @link http://smarty.php.net/manual/en/language.function.html.select.date.php {html_select_date}
 32      (Smarty online manual)
 33* @version 1.3.4
 34* @author Andrei Zmievski 
 35* @author Monte Ohrt <monte at ohrt dot com> 
 36* @param array $params parameters
 37* @param object $smarty Smarty object
 38* @param object $template template object
 39* @return string 
 40*/
 41function smarty_function_html_select_date($params, $smarty, $template)
 42{
 43    require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
 44    require_once(SMARTY_PLUGINS_DIR . 'shared.make_timestamp.php');
 45    require_once(SMARTY_PLUGINS_DIR . 'function.html_options.php');
 46    //$smarty->loadPlugin('Smarty_shared_escape_special_chars');
 47    //$smarty->loadPlugin('Smarty_shared_make_timestamp');
 48    //$smarty->loadPlugin('Smarty_function_html_options');
 49
 50    /* Default values. */
 51    $prefix = "Date_";
 52    $start_year = strftime("%Y");
 53    $end_year = $start_year;
 54    $display_days = true;
 55    $display_months = true;
 56    $display_years = true;
 57    $month_format = "%B";
 58    /* Write months as numbers by default  GL */
 59    $month_value_format = "%m";
 60    $day_format = "%02d";
 61    /* Write day values using this format MB */
 62    $day_value_format = "%d";
 63    $year_as_text = false;
 64    /* Display years in reverse order? Ie. 2000,1999,.... */
 65    $reverse_years = false;
 66    /* Should the select boxes be part of an array when returned from PHP?
 67       e.g. setting it to "birthday", would create "birthday[Day]",
 68       "birthday[Month]" & "birthday[Year]". Can be combined with prefix */
 69    $field_array = null;
 70    /* <select size>'s of the different <select> tags.
 71       If not set, uses default dropdown. */
 72    $day_size = null;
 73    $month_size = null;
 74    $year_size = null;
 75    /* Unparsed attributes common to *ALL* the <select>/<input> tags.
 76       An example might be in the template: all_extra ='class ="foo"'. */
 77    $all_extra = null;
 78    /* Separate attributes for the tags. */
 79    $day_extra = null;
 80    $month_extra = null;
 81    $year_extra = null;
 82    /* Order in which to display the fields.
 83       "D" -> day, "M" -> month, "Y" -> year. */
 84    $field_order = 'MDY';
 85    /* String printed between the different fields. */
 86    $field_separator = "\n";
 87    $time = time();
 88    $all_empty = null;
 89    $day_empty = null;
 90    $month_empty = null;
 91    $year_empty = null;
 92    $extra_attrs = '';
 93
 94    foreach ($params as $_key => $_value) {
 95        switch ($_key) {
 96            case 'prefix':
 97            case 'time':
 98            case 'start_year':
 99            case 'end_year':
100            case 'month_format':
101            case 'day_format':
102            case 'day_value_format':
103            case 'field_array':
104            case 'day_size':
105            case 'month_size':
106            case 'year_size':
107            case 'all_extra':
108            case 'day_extra':
109            case 'month_extra':
110            case 'year_extra':
111            case 'field_order':
112            case 'field_separator':
113            case 'month_value_format':
114            case 'month_empty':
115            case 'day_empty':
116            case 'year_empty':
117                $$_key = (string)$_value;
118                break;
119
120            case 'all_empty':
121                $$_key = (string)$_value;
122                $day_empty = $month_empty = $year_empty = $all_empty;
123                break;
124
125            case 'display_days':
126            case 'display_months':
127            case 'display_years':
128            case 'year_as_text':
129            case 'reverse_years':
130                $$_key = (bool)$_value;
131                break;
132
133            default:
134                if (!is_array($_value)) {
135                    $extra_attrs .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_value) . '"';
136                } else {
137                    trigger_error("html_select_date: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
138                } 
139                break;
140        } 
141    } 
142
143    if (preg_match('!^-\d+$!', $time)) {
144        // negative timestamp, use date()
145        $time = date('Y-m-d', $time);
146    } 
147    // If $time is not in format yyyy-mm-dd
148    if (preg_match('/^(\d{0,4}-\d{0,2}-\d{0,2})/', $time, $found)) {
149        $time = $found[1];
150    } else {
151        // use smarty_make_timestamp to get an unix timestamp and
152        // strftime to make yyyy-mm-dd
153        $time = strftime('%Y-%m-%d', smarty_make_timestamp($time));
154    } 
155    // Now split this in pieces, which later can be used to set the select
156    $time = explode("-", $time); 
157    // make syntax "+N" or "-N" work with start_year and end_year
158    if (preg_match('!^(\+|\-)\s*(\d+)$!', $end_year, $match)) {
159        if ($match[1] == '+') {
160            $end_year = strftime('%Y') + $match[2];
161        } else {
162            $end_year = strftime('%Y') - $match[2];
163        } 
164    } 
165    if (preg_match('!^(\+|\-)\s*(\d+)$!', $start_year, $match)) {
166        if ($match[1] == '+') {
167            $start_year = strftime('%Y') + $match[2];
168        } else {
169            $start_year = strftime('%Y') - $match[2];
170        } 
171    } 
172    if (strlen($time[0]) > 0) {
173        if ($start_year > $time[0] && !isset($params['start_year'])) {
174            // force start year to include given date if not explicitly set
175            $start_year = $time[0];
176        } 
177        if ($end_year < $time[0] && !isset($params['end_year'])) {
178            // force end year to include given date if not explicitly set
179            $end_year = $time[0];
180        } 
181    } 
182
183    $field_order = strtoupper($field_order);
184
185    $html_result = $month_result = $day_result = $year_result = "";
186
187    $field_separator_count = -1;
188    if ($display_months) {
189        $field_separator_count++;
190        $month_names = array();
191        $month_values = array();
192        if (isset($month_empty)) {
193            $month_names[''] = $month_empty;
194            $month_values[''] = '';
195        } 
196        for ($i = 1; $i <= 12; $i++) {
197            $month_names[$i] = strftime($month_format, mktime(0, 0, 0, $i, 1, 2000));
198            $month_values[$i] = strftime($month_value_format, mktime(0, 0, 0, $i, 1, 2000));
199        } 
200
201        $month_result .= '<select name=';
202        if (null !== $field_array) {
203            $month_result .= '"' . $field_array . '[' . $prefix . 'Month]"';
204        } else {
205            $month_result .= '"' . $prefix . 'Month"';
206        } 
207        if (null !== $month_size) {
208            $month_result .= ' size="' . $month_size . '"';
209        } 
210        if (null !== $month_extra) {
211            $month_result .= ' ' . $month_extra;
212        } 
213        if (null !== $all_extra) {
214            $month_result .= ' ' . $all_extra;
215        } 
216        $month_result .= $extra_attrs . '>' . "\n";
217
218        $month_result .= smarty_function_html_options(array('output' => $month_names,
219                'values' => $month_values,
220                'selected' => (int)$time[1] ? strftime($month_value_format, mktime(0, 0, 0, (int)$time[1], 1, 2000)) : '',
221                'print_result' => false),
222            $smarty, $template);
223        $month_result .= '</select>';
224    } 
225
226    if ($display_days) {
227        $field_separator_count++;
228        $days = array();
229        if (isset($day_empty)) {
230            $days[''] = $day_empty;
231            $day_values[''] = '';
232        } 
233        for ($i = 1; $i <= 31; $i++) {
234            $days[] = sprintf($day_format, $i);
235            $day_values[] = sprintf($day_value_format, $i);
236        } 
237
238        $day_result .= '<select name=';
239        if (null !== $field_array) {
240            $day_result .= '"' . $field_array . '[' . $prefix . 'Day]"';
241        } else {
242            $day_result .= '"' . $prefix . 'Day"';
243        } 
244        if (null !== $day_size) {
245            $day_result .= ' size="' . $day_size . '"';
246        } 
247        if (null !== $all_extra) {
248            $day_result .= ' ' . $all_extra;
249        } 
250        if (null !== $day_extra) {
251            $day_result .= ' ' . $day_extra;
252        } 
253        $day_result .= $extra_attrs . '>' . "\n";
254        $day_result .= smarty_function_html_options(array('output' => $days,
255                'values' => $day_values,
256                'selected' => $time[2],
257                'print_result' => false),
258            $smarty, $template);
259        $day_result .= '</select>';
260    } 
261
262    if ($display_years) {
263        $field_separator_count++;
264        if (null !== $field_array) {
265            $year_name = $field_array . '[' . $prefix . 'Year]';
266        } else {
267            $year_name = $prefix . 'Year';
268        } 
269        if ($year_as_text) {
270            $year_result .= '<input type="text" name="' . $year_name . '" value="' . $time[0] . '" size="4" maxlength="4"';
271            if (null !== $all_extra) {
272                $year_result .= ' ' . $all_extra;
273            } 
274            if (null !== $year_extra) {
275                $year_result .= ' ' . $year_extra;
276            } 
277            $year_result .= ' />';
278        } else {
279            $years = range((int)$start_year, (int)$end_year);
280            if ($reverse_years) {
281                rsort($years, SORT_NUMERIC);
282            } else {
283                sort($years, SORT_NUMERIC);
284            } 
285            $yearvals = $years;
286            if (isset($year_empty)) {
287                array_unshift($years, $year_empty);
288                array_unshift($yearvals, '');
289            } 
290            $year_result .= '<select name="' . $year_name . '"';
291            if (null !== $year_size) {
292                $year_result .= ' size="' . $year_size . '"';
293            } 
294            if (null !== $all_extra) {
295                $year_result .= ' ' . $all_extra;
296            } 
297            if (null !== $year_extra) {
298                $year_result .= ' ' . $year_extra;
299            } 
300            $year_result .= $extra_attrs . '>' . "\n";
301            $year_result .= smarty_function_html_options(array('output' => $years,
302                    'values' => $yearvals,
303                    'selected' => $time[0],
304                    'print_result' => false),
305                $smarty, $template);
306            $year_result .= '</select>';
307        } 
308    } 
309    // Loop thru the field_order field
310    for ($i = 0; $i <= 2; $i++) {
311        $c = substr($field_order, $i, 1);
312        switch ($c) {
313            case 'D':
314                $html_result .= $day_result;
315                break;
316
317            case 'M':
318                $html_result .= $month_result;
319                break;
320
321            case 'Y':
322                $html_result .= $year_result;
323                break;
324        } 
325        // Add the field seperator
326        if ($i < $field_separator_count) {
327            $html_result .= $field_separator;
328        } 
329    } 
330
331    return $html_result;
332} 
333?>