5 * @subpackage plugins
\r
9 * Smarty {html_select_date} plugin
\r
11 * Type: function<br>
\r
12 * Name: html_select_date<br>
\r
13 * Purpose: Prints the dropdowns for date selection.
\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
28 * @author Andrei Zmievski
\r
33 function smarty_function_html_select_date($params, &$smarty)
\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
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
62 /* Unparsed attributes common to *ALL* the <select>/<input> tags.
\r
63 An example might be in the template: all_extra ='class ="foo"'. */
\r
65 /* Separate attributes for the tags. */
\r
67 $month_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
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
86 // Now split this in pieces, which later can be used to set the select
\r
87 $time = explode("-", $time);
\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
94 $end_year = strftime('%Y') - $match[2];
\r
97 if (preg_match('!^(\+|\-)\s*(\d+)$!', $start_year, $match)) {
\r
98 if ($match[1] == '+') {
\r
99 $start_year = strftime('%Y') + $match[2];
\r
101 $start_year = strftime('%Y') - $match[2];
\r
105 $field_order = strtoupper($field_order);
\r
107 $html_result = $month_result = $day_result = $year_result = "";
\r
109 if ($display_months) {
\r
110 $month_names = array();
\r
111 $month_values = array();
\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
118 $month_result .= '<select name=';
\r
119 if (null !== $field_array){
\r
120 $month_result .= '"' . $field_array . '[' . $prefix . 'Month]"';
\r
122 $month_result .= '"' . $prefix . 'Month"';
\r
124 if (null !== $month_size){
\r
125 $month_result .= ' size="' . $month_size . '"';
\r
127 if (null !== $month_extra){
\r
128 $month_result .= ' ' . $month_extra;
\r
130 if (null !== $all_extra){
\r
131 $month_result .= ' ' . $all_extra;
\r
133 $month_result .= '>'."\n";
\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
141 $month_result .= '</select>';
\r
144 if ($display_days) {
\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
151 $day_result .= '<select name=';
\r
152 if (null !== $field_array){
\r
153 $day_result .= '"' . $field_array . '[' . $prefix . 'Day]"';
\r
155 $day_result .= '"' . $prefix . 'Day"';
\r
157 if (null !== $day_size){
\r
158 $day_result .= ' size="' . $day_size . '"';
\r
160 if (null !== $all_extra){
\r
161 $day_result .= ' ' . $all_extra;
\r
163 if (null !== $day_extra){
\r
164 $day_result .= ' ' . $day_extra;
\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
172 $day_result .= '</select>';
\r
175 if ($display_years) {
\r
176 if (null !== $field_array){
\r
177 $year_name = $field_array . '[' . $prefix . 'Year]';
\r
179 $year_name = $prefix . 'Year';
\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
186 if (null !== $year_extra){
\r
187 $year_result .= ' ' . $year_extra;
\r
189 $year_result .= '>';
\r
191 $years = range((int)$start_year, (int)$end_year);
\r
192 if ($reverse_years) {
\r
193 rsort($years, SORT_NUMERIC);
\r
196 $year_result .= '<select name="' . $year_name . '"';
\r
197 if (null !== $year_size){
\r
198 $year_result .= ' size="' . $year_size . '"';
\r
200 if (null !== $all_extra){
\r
201 $year_result .= ' ' . $all_extra;
\r
203 if (null !== $year_extra){
\r
204 $year_result .= ' ' . $year_extra;
\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
212 $year_result .= '</select>';
\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
221 $html_result .= $day_result;
\r
225 $html_result .= $month_result;
\r
229 $html_result .= $year_result;
\r
232 // Add the field seperator
\r
234 $html_result .= $field_separator;
\r
238 return $html_result;
\r
241 /* vim: set expandtab: */
\r