a607f5558b4253e89141c9d783fbac09cca64905
[atutor.git] / mods / phpdoc2 / PhpDocumentor / phpDocumentor / Smarty-2.6.0 / libs / plugins / function.html_select_date.php
1 <?php\r
2 /**\r
3  * Smarty plugin\r
4  * @package Smarty\r
5  * @subpackage plugins\r
6  */\r
7 \r
8 /**\r
9  * Smarty {html_select_date} plugin\r
10  *\r
11  * Type:     function<br>\r
12  * Name:     html_select_date<br>\r
13  * Purpose:  Prints the dropdowns for date selection.\r
14  *\r
15  * ChangeLog:<br>\r
16  *           - 1.0 initial release\r
17  *           - 1.1 added support for +/- N syntax for begin\r
18  *                and end year values. (Monte)\r
19  *           - 1.2 added support for yyyy-mm-dd syntax for\r
20  *                time value. (Jan Rosier)\r
21  *           - 1.3 added support for choosing format for \r
22  *                month values (Gary Loescher)\r
23  *           - 1.3.1 added support for choosing format for\r
24  *                day values (Marcus Bointon)\r
25  * @link http://smarty.php.net/manual/en/language.function.html.select.date.php {html_select_date}\r
26  *      (Smarty online manual)\r
27  * @version 1.3\r
28  * @author   Andrei Zmievski\r
29  * @param array\r
30  * @param Smarty\r
31  * @return string\r
32  */\r
33 function smarty_function_html_select_date($params, &$smarty)\r
34 {\r
35     require_once $smarty->_get_plugin_filepath('shared','make_timestamp');\r
36     require_once $smarty->_get_plugin_filepath('function','html_options');\r
37     /* Default values. */\r
38     $prefix          = "Date_";\r
39     $start_year      = strftime("%Y");\r
40     $end_year        = $start_year;\r
41     $display_days    = true;\r
42     $display_months  = true;\r
43     $display_years   = true;\r
44     $month_format    = "%B";\r
45     /* Write months as numbers by default  GL */\r
46     $month_value_format = "%m";\r
47     $day_format      = "%02d";\r
48     /* Write day values using this format MB */\r
49     $day_value_format = "%d";\r
50     $year_as_text    = false;\r
51     /* Display years in reverse order? Ie. 2000,1999,.... */\r
52     $reverse_years   = false;\r
53     /* Should the select boxes be part of an array when returned from PHP?\r
54        e.g. setting it to "birthday", would create "birthday[Day]",\r
55        "birthday[Month]" & "birthday[Year]". Can be combined with prefix */\r
56     $field_array     = null;\r
57     /* <select size>'s of the different <select> tags.\r
58        If not set, uses default dropdown. */\r
59     $day_size        = null;\r
60     $month_size      = null;\r
61     $year_size       = null;\r
62     /* Unparsed attributes common to *ALL* the <select>/<input> tags.\r
63        An example might be in the template: all_extra ='class ="foo"'. */\r
64     $all_extra       = null;\r
65     /* Separate attributes for the tags. */\r
66     $day_extra       = null;\r
67     $month_extra     = null;\r
68     $year_extra      = null;\r
69     /* Order in which to display the fields.\r
70        "D" -> day, "M" -> month, "Y" -> year. */\r
71     $field_order      = 'MDY';\r
72     /* String printed between the different fields. */\r
73     $field_separator = "\n";\r
74         $time = time();\r
75 \r
76 \r
77     extract($params);\r
78 \r
79         // If $time is not in format yyyy-mm-dd\r
80         if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $time)) {\r
81                 // then $time is empty or unix timestamp or mysql timestamp\r
82                 // using smarty_make_timestamp to get an unix timestamp and\r
83                 // strftime to make yyyy-mm-dd\r
84                 $time = strftime('%Y-%m-%d', smarty_make_timestamp($time));\r
85         }\r
86         // Now split this in pieces, which later can be used to set the select\r
87         $time = explode("-", $time);\r
88   \r
89         // make syntax "+N" or "-N" work with start_year and end_year\r
90         if (preg_match('!^(\+|\-)\s*(\d+)$!', $end_year, $match)) {\r
91                 if ($match[1] == '+') {\r
92                         $end_year = strftime('%Y') + $match[2];\r
93                 } else {\r
94                         $end_year = strftime('%Y') - $match[2];\r
95                 }\r
96         }\r
97         if (preg_match('!^(\+|\-)\s*(\d+)$!', $start_year, $match)) {\r
98                 if ($match[1] == '+') {\r
99                         $start_year = strftime('%Y') + $match[2];\r
100                 } else {\r
101                         $start_year = strftime('%Y') - $match[2];\r
102                 }\r
103         }\r
104   \r
105     $field_order = strtoupper($field_order);\r
106 \r
107     $html_result = $month_result = $day_result = $year_result = "";\r
108 \r
109     if ($display_months) {\r
110         $month_names = array();\r
111         $month_values = array();\r
112 \r
113         for ($i = 1; $i <= 12; $i++) {\r
114             $month_names[] = strftime($month_format, mktime(0, 0, 0, $i, 1, 2000));\r
115             $month_values[] = strftime($month_value_format, mktime(0, 0, 0, $i, 1, 2000));\r
116         }\r
117 \r
118         $month_result .= '<select name=';\r
119         if (null !== $field_array){\r
120             $month_result .= '"' . $field_array . '[' . $prefix . 'Month]"';\r
121         } else {\r
122             $month_result .= '"' . $prefix . 'Month"';\r
123         }\r
124         if (null !== $month_size){\r
125             $month_result .= ' size="' . $month_size . '"';\r
126         }\r
127         if (null !== $month_extra){\r
128             $month_result .= ' ' . $month_extra;\r
129         }\r
130         if (null !== $all_extra){\r
131             $month_result .= ' ' . $all_extra;\r
132         }\r
133         $month_result .= '>'."\n";\r
134         \r
135         $month_result .= smarty_function_html_options(array('output'     => $month_names,\r
136                                                             'values'     => $month_values,\r
137                                                             'selected'   => $month_values[$time[1]-1],\r
138                                                             'print_result' => false),\r
139                                                       $smarty);\r
140         \r
141         $month_result .= '</select>';\r
142     }\r
143 \r
144     if ($display_days) {\r
145         $days = array();\r
146         for ($i = 1; $i <= 31; $i++) {\r
147             $days[] = sprintf($day_format, $i);\r
148             $day_values[] = sprintf($day_value_format, $i);\r
149         }\r
150 \r
151         $day_result .= '<select name=';\r
152         if (null !== $field_array){\r
153             $day_result .= '"' . $field_array . '[' . $prefix . 'Day]"';\r
154         } else {\r
155             $day_result .= '"' . $prefix . 'Day"';\r
156         }\r
157         if (null !== $day_size){\r
158             $day_result .= ' size="' . $day_size . '"';\r
159         }\r
160         if (null !== $all_extra){\r
161             $day_result .= ' ' . $all_extra;\r
162         }\r
163         if (null !== $day_extra){\r
164             $day_result .= ' ' . $day_extra;\r
165         }\r
166         $day_result .= '>'."\n";\r
167         $day_result .= smarty_function_html_options(array('output'     => $days,\r
168                                                           'values'     => $day_values,\r
169                                                           'selected'   => $time[2],\r
170                                                           'print_result' => false),\r
171                                                     $smarty);\r
172         $day_result .= '</select>';\r
173     }\r
174 \r
175     if ($display_years) {\r
176         if (null !== $field_array){\r
177             $year_name = $field_array . '[' . $prefix . 'Year]';\r
178         } else {\r
179             $year_name = $prefix . 'Year';\r
180         }\r
181         if ($year_as_text) {\r
182             $year_result .= '<input type="text" name="' . $year_name . '" value="' . $time[0] . '" size="4" maxlength="4"';\r
183             if (null !== $all_extra){\r
184                 $year_result .= ' ' . $all_extra;\r
185             }\r
186             if (null !== $year_extra){\r
187                 $year_result .= ' ' . $year_extra;\r
188             }\r
189             $year_result .= '>';\r
190         } else {\r
191             $years = range((int)$start_year, (int)$end_year);\r
192             if ($reverse_years) {\r
193                 rsort($years, SORT_NUMERIC);\r
194             }\r
195 \r
196             $year_result .= '<select name="' . $year_name . '"';\r
197             if (null !== $year_size){\r
198                 $year_result .= ' size="' . $year_size . '"';\r
199             }\r
200             if (null !== $all_extra){\r
201                 $year_result .= ' ' . $all_extra;\r
202             }\r
203             if (null !== $year_extra){\r
204                 $year_result .= ' ' . $year_extra;\r
205             }\r
206             $year_result .= '>'."\n";\r
207             $year_result .= smarty_function_html_options(array('output' => $years,\r
208                                                                'values' => $years,\r
209                                                                'selected'   => $time[0],\r
210                                                                'print_result' => false),\r
211                                                          $smarty);\r
212             $year_result .= '</select>';\r
213         }\r
214     }\r
215 \r
216     // Loop thru the field_order field\r
217     for ($i = 0; $i <= 2; $i++){\r
218       $c = substr($field_order, $i, 1);\r
219       switch ($c){\r
220         case 'D':\r
221             $html_result .= $day_result;\r
222             break;\r
223 \r
224         case 'M':\r
225             $html_result .= $month_result;\r
226             break;\r
227 \r
228         case 'Y':\r
229             $html_result .= $year_result;\r
230             break;\r
231       }\r
232       // Add the field seperator\r
233           if($i != 2) {\r
234         $html_result .= $field_separator;\r
235           }\r
236     }\r
237 \r
238     return $html_result;\r
239 }\r
240 \r
241 /* vim: set expandtab: */\r
242 \r
243 ?>\r