9 * Smarty {html_select_date} plugin
12 * Name: html_select_date<br>
13 * Purpose: Prints the dropdowns for date selection.
16 * - 1.0 initial release
17 * - 1.1 added support for +/- N syntax for begin
18 * and end year values. (Monte)
19 * - 1.2 added support for yyyy-mm-dd syntax for
20 * time value. (Jan Rosier)
21 * - 1.3 added support for choosing format for
22 * month values (Gary Loescher)
23 * - 1.3.1 added support for choosing format for
24 * day values (Marcus Bointon)
25 * - 1.3.2 suppport negative timestamps, force year
26 * dropdown to include given date unless explicitly set (Monte)
27 * @link http://smarty.php.net/manual/en/language.function.html.select.date.php {html_select_date}
28 * (Smarty online manual)
30 * @author Andrei Zmievski
35 function smarty_function_html_select_date($params, &$smarty)
37 require_once $smarty->_get_plugin_filepath('shared','make_timestamp');
38 require_once $smarty->_get_plugin_filepath('function','html_options');
41 $start_year = strftime("%Y");
42 $end_year = $start_year;
44 $display_months = true;
45 $display_years = true;
47 /* Write months as numbers by default GL */
48 $month_value_format = "%m";
50 /* Write day values using this format MB */
51 $day_value_format = "%d";
52 $year_as_text = false;
53 /* Display years in reverse order? Ie. 2000,1999,.... */
54 $reverse_years = false;
55 /* Should the select boxes be part of an array when returned from PHP?
56 e.g. setting it to "birthday", would create "birthday[Day]",
57 "birthday[Month]" & "birthday[Year]". Can be combined with prefix */
59 /* <select size>'s of the different <select> tags.
60 If not set, uses default dropdown. */
64 /* Unparsed attributes common to *ALL* the <select>/<input> tags.
65 An example might be in the template: all_extra ='class ="foo"'. */
67 /* Separate attributes for the tags. */
71 /* Order in which to display the fields.
72 "D" -> day, "M" -> month, "Y" -> year. */
74 /* String printed between the different fields. */
75 $field_separator = "\n";
82 foreach ($params as $_key=>$_value) {
90 case 'day_value_format':
100 case 'field_separator':
101 case 'month_value_format':
105 $
$_key = (string)$_value;
109 $
$_key = (string)$_value;
110 $day_empty = $month_empty = $year_empty = $all_empty;
114 case 'display_months':
115 case 'display_years':
117 case 'reverse_years':
118 $
$_key = (bool)$_value;
122 $smarty->trigger_error("[html_select_date] unknown parameter $_key", E_USER_WARNING
);
127 if(preg_match('!^-\d+$!',$time)) {
128 // negative timestamp, use date()
129 $time = date('Y-m-d',$time);
131 // If $time is not in format yyyy-mm-dd
132 if (!preg_match('/^\d{0,4}-\d{0,2}-\d{0,2}$/', $time)) {
133 // use smarty_make_timestamp to get an unix timestamp and
134 // strftime to make yyyy-mm-dd
135 $time = strftime('%Y-%m-%d', smarty_make_timestamp($time));
137 // Now split this in pieces, which later can be used to set the select
138 $time = explode("-", $time);
140 // make syntax "+N" or "-N" work with start_year and end_year
141 if (preg_match('!^(\+|\-)\s*(\d+)$!', $end_year, $match)) {
142 if ($match[1] == '+') {
143 $end_year = strftime('%Y') +
$match[2];
145 $end_year = strftime('%Y') - $match[2];
148 if (preg_match('!^(\+|\-)\s*(\d+)$!', $start_year, $match)) {
149 if ($match[1] == '+') {
150 $start_year = strftime('%Y') +
$match[2];
152 $start_year = strftime('%Y') - $match[2];
155 if (strlen($time[0]) > 0) {
156 if ($start_year > $time[0] && !isset($params['start_year'])) {
157 // force start year to include given date if not explicitly set
158 $start_year = $time[0];
160 if($end_year < $time[0] && !isset($params['end_year'])) {
161 // force end year to include given date if not explicitly set
162 $end_year = $time[0];
166 $field_order = strtoupper($field_order);
168 $html_result = $month_result = $day_result = $year_result = "";
170 if ($display_months) {
171 $month_names = array();
172 $month_values = array();
173 if(isset($month_empty)) {
174 $month_names[''] = $month_empty;
175 $month_values[''] = '';
177 for ($i = 1; $i <= 12; $i++
) {
178 $month_names[$i] = strftime($month_format, mktime(0, 0, 0, $i, 1, 2000));
179 $month_values[$i] = strftime($month_value_format, mktime(0, 0, 0, $i, 1, 2000));
182 $month_result .= '<select name=';
183 if (null !== $field_array){
184 $month_result .= '"' . $field_array . '[' . $prefix . 'Month]"';
186 $month_result .= '"' . $prefix . 'Month"';
188 if (null !== $month_size){
189 $month_result .= ' size="' . $month_size . '"';
191 if (null !== $month_extra){
192 $month_result .= ' ' . $month_extra;
194 if (null !== $all_extra){
195 $month_result .= ' ' . $all_extra;
197 $month_result .= '>'."\n";
199 $month_result .= smarty_function_html_options(array('output' => $month_names,
200 'values' => $month_values,
201 'selected' => $a=$time[1] ?
strftime($month_value_format, mktime(0, 0, 0, (int)$time[1], 1, 2000)) : '',
202 'print_result' => false),
204 $month_result .= '</select>';
209 if (isset($day_empty)) {
210 $days[''] = $day_empty;
211 $day_values[''] = '';
213 for ($i = 1; $i <= 31; $i++
) {
214 $days[] = sprintf($day_format, $i);
215 $day_values[] = sprintf($day_value_format, $i);
218 $day_result .= '<select name=';
219 if (null !== $field_array){
220 $day_result .= '"' . $field_array . '[' . $prefix . 'Day]"';
222 $day_result .= '"' . $prefix . 'Day"';
224 if (null !== $day_size){
225 $day_result .= ' size="' . $day_size . '"';
227 if (null !== $all_extra){
228 $day_result .= ' ' . $all_extra;
230 if (null !== $day_extra){
231 $day_result .= ' ' . $day_extra;
233 $day_result .= '>'."\n";
234 $day_result .= smarty_function_html_options(array('output' => $days,
235 'values' => $day_values,
236 'selected' => $time[2],
237 'print_result' => false),
239 $day_result .= '</select>';
242 if ($display_years) {
243 if (null !== $field_array){
244 $year_name = $field_array . '[' . $prefix . 'Year]';
246 $year_name = $prefix . 'Year';
249 $year_result .= '<input type="text" name="' . $year_name . '" value="' . $time[0] . '" size="4" maxlength="4"';
250 if (null !== $all_extra){
251 $year_result .= ' ' . $all_extra;
253 if (null !== $year_extra){
254 $year_result .= ' ' . $year_extra;
258 $years = range((int)$start_year, (int)$end_year);
259 if ($reverse_years) {
260 rsort($years, SORT_NUMERIC
);
262 sort($years, SORT_NUMERIC
);
265 if(isset($year_empty)) {
266 array_unshift($years, $year_empty);
267 array_unshift($yearvals, '');
269 $year_result .= '<select name="' . $year_name . '"';
270 if (null !== $year_size){
271 $year_result .= ' size="' . $year_size . '"';
273 if (null !== $all_extra){
274 $year_result .= ' ' . $all_extra;
276 if (null !== $year_extra){
277 $year_result .= ' ' . $year_extra;
279 $year_result .= '>'."\n";
280 $year_result .= smarty_function_html_options(array('output' => $years,
281 'values' => $yearvals,
282 'selected' => $time[0],
283 'print_result' => false),
285 $year_result .= '</select>';
289 // Loop thru the field_order field
290 for ($i = 0; $i <= 2; $i++
){
291 $c = substr($field_order, $i, 1);
294 $html_result .= $day_result;
298 $html_result .= $month_result;
302 $html_result .= $year_result;
305 // Add the field seperator
307 $html_result .= $field_separator;
314 /* vim: set expandtab: */