00001 <?php 00038 function smarty_function_html_select_date($params, &$smarty) 00039 { 00040 require_once $smarty->_get_plugin_filepath('shared','escape_special_chars'); 00041 require_once $smarty->_get_plugin_filepath('shared','make_timestamp'); 00042 require_once $smarty->_get_plugin_filepath('function','html_options'); 00043 /* Default values. */ 00044 $prefix = "Date_"; 00045 $start_year = strftime("%Y"); 00046 $end_year = $start_year; 00047 $display_days = true; 00048 $display_months = true; 00049 $display_years = true; 00050 $month_format = "%B"; 00051 /* Write months as numbers by default GL */ 00052 $month_value_format = "%m"; 00053 $day_format = "%02d"; 00054 /* Write day values using this format MB */ 00055 $day_value_format = "%d"; 00056 $year_as_text = false; 00057 /* Display years in reverse order? Ie. 2000,1999,.... */ 00058 $reverse_years = false; 00059 /* Should the select boxes be part of an array when returned from PHP? 00060 e.g. setting it to "birthday", would create "birthday[Day]", 00061 "birthday[Month]" & "birthday[Year]". Can be combined with prefix */ 00062 $field_array = null; 00063 /* <select size>'s of the different <select> tags. 00064 If not set, uses default dropdown. */ 00065 $day_size = null; 00066 $month_size = null; 00067 $year_size = null; 00068 /* Unparsed attributes common to *ALL* the <select>/<input> tags. 00069 An example might be in the template: all_extra ='class ="foo"'. */ 00070 $all_extra = null; 00071 /* Separate attributes for the tags. */ 00072 $day_extra = null; 00073 $month_extra = null; 00074 $year_extra = null; 00075 /* Order in which to display the fields. 00076 "D" -> day, "M" -> month, "Y" -> year. */ 00077 $field_order = 'MDY'; 00078 /* String printed between the different fields. */ 00079 $field_separator = "\n"; 00080 $time = time(); 00081 $all_empty = null; 00082 $day_empty = null; 00083 $month_empty = null; 00084 $year_empty = null; 00085 $extra_attrs = ''; 00086 00087 foreach ($params as $_key=>$_value) { 00088 switch ($_key) { 00089 case 'prefix': 00090 case 'time': 00091 case 'start_year': 00092 case 'end_year': 00093 case 'month_format': 00094 case 'day_format': 00095 case 'day_value_format': 00096 case 'field_array': 00097 case 'day_size': 00098 case 'month_size': 00099 case 'year_size': 00100 case 'all_extra': 00101 case 'day_extra': 00102 case 'month_extra': 00103 case 'year_extra': 00104 case 'field_order': 00105 case 'field_separator': 00106 case 'month_value_format': 00107 case 'month_empty': 00108 case 'day_empty': 00109 case 'year_empty': 00110 $$_key = (string)$_value; 00111 break; 00112 00113 case 'all_empty': 00114 $$_key = (string)$_value; 00115 $day_empty = $month_empty = $year_empty = $all_empty; 00116 break; 00117 00118 case 'display_days': 00119 case 'display_months': 00120 case 'display_years': 00121 case 'year_as_text': 00122 case 'reverse_years': 00123 $$_key = (bool)$_value; 00124 break; 00125 00126 default: 00127 if(!is_array($_value)) { 00128 $extra_attrs .= ' '.$_key.'="'.smarty_function_escape_special_chars($_value).'"'; 00129 } else { 00130 $smarty->trigger_error("html_select_date: extra attribute '$_key' cannot be an array", E_USER_NOTICE); 00131 } 00132 break; 00133 } 00134 } 00135 00136 if (preg_match('!^-\d+$!', $time)) { 00137 // negative timestamp, use date() 00138 $time = date('Y-m-d', $time); 00139 } 00140 // If $time is not in format yyyy-mm-dd 00141 if (preg_match('/^(\d{0,4}-\d{0,2}-\d{0,2})/', $time, $found)) { 00142 $time = $found[1]; 00143 } else { 00144 // use smarty_make_timestamp to get an unix timestamp and 00145 // strftime to make yyyy-mm-dd 00146 $time = strftime('%Y-%m-%d', smarty_make_timestamp($time)); 00147 } 00148 // Now split this in pieces, which later can be used to set the select 00149 $time = explode("-", $time); 00150 00151 // make syntax "+N" or "-N" work with start_year and end_year 00152 if (preg_match('!^(\+|\-)\s*(\d+)$!', $end_year, $match)) { 00153 if ($match[1] == '+') { 00154 $end_year = strftime('%Y') + $match[2]; 00155 } else { 00156 $end_year = strftime('%Y') - $match[2]; 00157 } 00158 } 00159 if (preg_match('!^(\+|\-)\s*(\d+)$!', $start_year, $match)) { 00160 if ($match[1] == '+') { 00161 $start_year = strftime('%Y') + $match[2]; 00162 } else { 00163 $start_year = strftime('%Y') - $match[2]; 00164 } 00165 } 00166 if (strlen($time[0]) > 0) { 00167 if ($start_year > $time[0] && !isset($params['start_year'])) { 00168 // force start year to include given date if not explicitly set 00169 $start_year = $time[0]; 00170 } 00171 if($end_year < $time[0] && !isset($params['end_year'])) { 00172 // force end year to include given date if not explicitly set 00173 $end_year = $time[0]; 00174 } 00175 } 00176 00177 $field_order = strtoupper($field_order); 00178 00179 $html_result = $month_result = $day_result = $year_result = ""; 00180 00181 $field_separator_count = -1; 00182 if ($display_months) { 00183 $field_separator_count++; 00184 $month_names = array(); 00185 $month_values = array(); 00186 if(isset($month_empty)) { 00187 $month_names[''] = $month_empty; 00188 $month_values[''] = ''; 00189 } 00190 for ($i = 1; $i <= 12; $i++) { 00191 $month_names[$i] = strftime($month_format, mktime(0, 0, 0, $i, 1, 2000)); 00192 $month_values[$i] = strftime($month_value_format, mktime(0, 0, 0, $i, 1, 2000)); 00193 } 00194 00195 $month_result .= '<select name='; 00196 if (null !== $field_array){ 00197 $month_result .= '"' . $field_array . '[' . $prefix . 'Month]"'; 00198 } else { 00199 $month_result .= '"' . $prefix . 'Month"'; 00200 } 00201 if (null !== $month_size){ 00202 $month_result .= ' size="' . $month_size . '"'; 00203 } 00204 if (null !== $month_extra){ 00205 $month_result .= ' ' . $month_extra; 00206 } 00207 if (null !== $all_extra){ 00208 $month_result .= ' ' . $all_extra; 00209 } 00210 $month_result .= $extra_attrs . '>'."\n"; 00211 00212 $month_result .= smarty_function_html_options(array('output' => $month_names, 00213 'values' => $month_values, 00214 'selected' => (int)$time[1] ? strftime($month_value_format, mktime(0, 0, 0, (int)$time[1], 1, 2000)) : '', 00215 'print_result' => false), 00216 $smarty); 00217 $month_result .= '</select>'; 00218 } 00219 00220 if ($display_days) { 00221 $field_separator_count++; 00222 $days = array(); 00223 if (isset($day_empty)) { 00224 $days[''] = $day_empty; 00225 $day_values[''] = ''; 00226 } 00227 for ($i = 1; $i <= 31; $i++) { 00228 $days[] = sprintf($day_format, $i); 00229 $day_values[] = sprintf($day_value_format, $i); 00230 } 00231 00232 $day_result .= '<select name='; 00233 if (null !== $field_array){ 00234 $day_result .= '"' . $field_array . '[' . $prefix . 'Day]"'; 00235 } else { 00236 $day_result .= '"' . $prefix . 'Day"'; 00237 } 00238 if (null !== $day_size){ 00239 $day_result .= ' size="' . $day_size . '"'; 00240 } 00241 if (null !== $all_extra){ 00242 $day_result .= ' ' . $all_extra; 00243 } 00244 if (null !== $day_extra){ 00245 $day_result .= ' ' . $day_extra; 00246 } 00247 $day_result .= $extra_attrs . '>'."\n"; 00248 $day_result .= smarty_function_html_options(array('output' => $days, 00249 'values' => $day_values, 00250 'selected' => $time[2], 00251 'print_result' => false), 00252 $smarty); 00253 $day_result .= '</select>'; 00254 } 00255 00256 if ($display_years) { 00257 $field_separator_count++; 00258 if (null !== $field_array){ 00259 $year_name = $field_array . '[' . $prefix . 'Year]'; 00260 } else { 00261 $year_name = $prefix . 'Year'; 00262 } 00263 if ($year_as_text) { 00264 $year_result .= '<input type="text" name="' . $year_name . '" value="' . $time[0] . '" size="4" maxlength="4"'; 00265 if (null !== $all_extra){ 00266 $year_result .= ' ' . $all_extra; 00267 } 00268 if (null !== $year_extra){ 00269 $year_result .= ' ' . $year_extra; 00270 } 00271 $year_result .= ' />'; 00272 } else { 00273 $years = range((int)$start_year, (int)$end_year); 00274 if ($reverse_years) { 00275 rsort($years, SORT_NUMERIC); 00276 } else { 00277 sort($years, SORT_NUMERIC); 00278 } 00279 $yearvals = $years; 00280 if(isset($year_empty)) { 00281 array_unshift($years, $year_empty); 00282 array_unshift($yearvals, ''); 00283 } 00284 $year_result .= '<select name="' . $year_name . '"'; 00285 if (null !== $year_size){ 00286 $year_result .= ' size="' . $year_size . '"'; 00287 } 00288 if (null !== $all_extra){ 00289 $year_result .= ' ' . $all_extra; 00290 } 00291 if (null !== $year_extra){ 00292 $year_result .= ' ' . $year_extra; 00293 } 00294 $year_result .= $extra_attrs . '>'."\n"; 00295 $year_result .= smarty_function_html_options(array('output' => $years, 00296 'values' => $yearvals, 00297 'selected' => $time[0], 00298 'print_result' => false), 00299 $smarty); 00300 $year_result .= '</select>'; 00301 } 00302 } 00303 00304 // Loop thru the field_order field 00305 for ($i = 0; $i <= 2; $i++){ 00306 $c = substr($field_order, $i, 1); 00307 switch ($c){ 00308 case 'D': 00309 $html_result .= $day_result; 00310 break; 00311 00312 case 'M': 00313 $html_result .= $month_result; 00314 break; 00315 00316 case 'Y': 00317 $html_result .= $year_result; 00318 break; 00319 } 00320 // Add the field seperator 00321 if($i < $field_separator_count) { 00322 $html_result .= $field_separator; 00323 } 00324 } 00325 00326 return $html_result; 00327 } 00328 00329 /* vim: set expandtab: */ 00330 00331 ?>
| Copyright © 2003 - 2009 MyOOS [Shopsystem]. All rights reserved. MyOOS [Shopsystem] is Free Software released under the GNU/GPL License. Webmaster: info@r23.de (Impressum) |
|
