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