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