6 * @subpackage PluginsFunction
10 * Smarty {html_select_date} plugin
13 * Name: html_select_date<br>
14 * Purpose: Prints the dropdowns for date selection.
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)
31 * @link http://smarty.php.net/manual/en/language.function.html.select.date.php {html_select_date}
32 * (Smarty online manual)
34 * @author Andrei Zmievski
35 * @author Monte Ohrt <monte at ohrt dot com>
36 * @param array $params parameters
37 * @param object $template template object
40 function smarty_function_html_select_date($params, $template)
42 require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
43 require_once(SMARTY_PLUGINS_DIR . 'shared.make_timestamp.php');
44 require_once(SMARTY_PLUGINS_DIR . 'function.html_options.php');
48 $start_year = strftime("%Y");
49 $end_year = $start_year;
51 $display_months = true;
52 $display_years = true;
54 /* Write months as numbers by default GL */
55 $month_value_format = "%m";
57 /* Write day values using this format MB */
58 $day_value_format = "%d";
59 $year_as_text = false;
60 /* Display years in reverse order? Ie. 2000,1999,.... */
61 $reverse_years = false;
62 /* Should the select boxes be part of an array when returned from PHP?
63 e.g. setting it to "birthday", would create "birthday[Day]",
64 "birthday[Month]" & "birthday[Year]". Can be combined with prefix */
66 /* <select size>'s of the different <select> tags.
67 If not set, uses default dropdown. */
71 /* Unparsed attributes common to *ALL* the <select>/<input> tags.
72 An example might be in the template: all_extra ='class ="foo"'. */
74 /* Separate attributes for the tags. */
78 /* Order in which to display the fields.
79 "D" -> day, "M" -> month, "Y" -> year. */
81 /* String printed between the different fields. */
82 $field_separator = "\n";
90 foreach ($params as $_key => $_value) {
98 case 'day_value_format':
108 case 'field_separator':
109 case 'month_value_format':
113 $$_key = (string)$_value;
117 $$_key = (string)$_value;
118 $day_empty = $month_empty = $year_empty = $all_empty;
122 case 'display_months':
123 case 'display_years':
125 case 'reverse_years':
126 $$_key = (bool)$_value;
130 if (!is_array($_value)) {
131 $extra_attrs .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_value) . '"';
133 trigger_error("html_select_date: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
139 if (preg_match('!^-\d+$!', $time)) {
140 // negative timestamp, use date()
141 $time = date('Y-m-d', $time);
143 // If $time is not in format yyyy-mm-dd
144 if (preg_match('/^(\d{0,4}-\d{0,2}-\d{0,2})/', $time, $found)) {
147 // use smarty_make_timestamp to get an unix timestamp and
148 // strftime to make yyyy-mm-dd
149 $time = strftime('%Y-%m-%d', smarty_make_timestamp($time));
151 // Now split this in pieces, which later can be used to set the select
152 $time = explode("-", $time);
153 // make syntax "+N" or "-N" work with start_year and end_year
154 if (preg_match('!^(\+|\-)\s*(\d+)$!', $end_year, $match)) {
155 if ($match[1] == '+') {
156 $end_year = strftime('%Y') + $match[2];
158 $end_year = strftime('%Y') - $match[2];
161 if (preg_match('!^(\+|\-)\s*(\d+)$!', $start_year, $match)) {
162 if ($match[1] == '+') {
163 $start_year = strftime('%Y') + $match[2];
165 $start_year = strftime('%Y') - $match[2];
168 if (strlen($time[0]) > 0) {
169 if ($start_year > $time[0] && !isset($params['start_year'])) {
170 // force start year to include given date if not explicitly set
171 $start_year = $time[0];
173 if ($end_year < $time[0] && !isset($params['end_year'])) {
174 // force end year to include given date if not explicitly set
175 $end_year = $time[0];
179 $field_order = strtoupper($field_order);
181 $html_result = $month_result = $day_result = $year_result = "";
183 $field_separator_count = -1;
184 if ($display_months) {
185 $field_separator_count++;
186 $month_names = array();
187 $month_values = array();
188 if (isset($month_empty)) {
189 $month_names[''] = $month_empty;
190 $month_values[''] = '';
192 for ($i = 1; $i <= 12; $i++) {
193 $month_names[$i] = strftime($month_format, mktime(0, 0, 0, $i, 1, 2000));
194 $month_values[$i] = strftime($month_value_format, mktime(0, 0, 0, $i, 1, 2000));
197 $month_result .= '<select name=';
198 if (null !== $field_array) {
199 $month_result .= '"' . $field_array . '[' . $prefix . 'Month]"';
201 $month_result .= '"' . $prefix . 'Month"';
203 if (null !== $month_size) {
204 $month_result .= ' size="' . $month_size . '"';
206 if (null !== $month_extra) {
207 $month_result .= ' ' . $month_extra;
209 if (null !== $all_extra) {
210 $month_result .= ' ' . $all_extra;
212 $month_result .= $extra_attrs . '>' . "\n";
214 $month_result .= smarty_function_html_options(array('output' => $month_names,
215 'values' => $month_values,
216 'selected' => (int)$time[1] ? strftime($month_value_format, mktime(0, 0, 0, (int)$time[1], 1, 2000)) : '',
217 'print_result' => false),
219 $month_result .= '</select>';
223 $field_separator_count++;
225 if (isset($day_empty)) {
226 $days[''] = $day_empty;
227 $day_values[''] = '';
229 for ($i = 1; $i <= 31; $i++) {
230 $days[] = sprintf($day_format, $i);
231 $day_values[] = sprintf($day_value_format, $i);
234 $day_result .= '<select name=';
235 if (null !== $field_array) {
236 $day_result .= '"' . $field_array . '[' . $prefix . 'Day]"';
238 $day_result .= '"' . $prefix . 'Day"';
240 if (null !== $day_size) {
241 $day_result .= ' size="' . $day_size . '"';
243 if (null !== $all_extra) {
244 $day_result .= ' ' . $all_extra;
246 if (null !== $day_extra) {
247 $day_result .= ' ' . $day_extra;
249 $day_result .= $extra_attrs . '>' . "\n";
250 $day_result .= smarty_function_html_options(array('output' => $days,
251 'values' => $day_values,
252 'selected' => $time[2],
253 'print_result' => false),
255 $day_result .= '</select>';
258 if ($display_years) {
259 $field_separator_count++;
260 if (null !== $field_array) {
261 $year_name = $field_array . '[' . $prefix . 'Year]';
263 $year_name = $prefix . 'Year';
266 $year_result .= '<input type="text" name="' . $year_name . '" value="' . $time[0] . '" size="4" maxlength="4"';
267 if (null !== $all_extra) {
268 $year_result .= ' ' . $all_extra;
270 if (null !== $year_extra) {
271 $year_result .= ' ' . $year_extra;
273 $year_result .= ' />';
275 $years = range((int)$start_year, (int)$end_year);
276 if ($reverse_years) {
277 rsort($years, SORT_NUMERIC);
279 sort($years, SORT_NUMERIC);
282 if (isset($year_empty)) {
283 array_unshift($years, $year_empty);
284 array_unshift($yearvals, '');
286 $year_result .= '<select name="' . $year_name . '"';
287 if (null !== $year_size) {
288 $year_result .= ' size="' . $year_size . '"';
290 if (null !== $all_extra) {
291 $year_result .= ' ' . $all_extra;
293 if (null !== $year_extra) {
294 $year_result .= ' ' . $year_extra;
296 $year_result .= $extra_attrs . '>' . "\n";
297 $year_result .= smarty_function_html_options(array('output' => $years,
298 'values' => $yearvals,
299 'selected' => $time[0],
300 'print_result' => false),
302 $year_result .= '</select>';
305 // Loop thru the field_order field
306 for ($i = 0; $i <= 2; $i++) {
307 $c = substr($field_order, $i, 1);
310 $html_result .= $day_result;
314 $html_result .= $month_result;
318 $html_result .= $year_result;
321 // Add the field seperator
322 if ($i < $field_separator_count) {
323 $html_result .= $field_separator;