| 1 |
<?php |
<?php |
| 2 |
/* |
/** |
| 3 |
* Smarty plugin |
* Smarty plugin |
| 4 |
* ------------------------------------------------------------- |
* @package Smarty |
| 5 |
* Type: function |
* @subpackage plugins |
| 6 |
* Name: html_select_date |
*/ |
| 7 |
* Version: 1.3 |
|
| 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. |
* Purpose: Prints the dropdowns for date selection. |
|
* Author: Andrei Zmievski |
|
| 14 |
* |
* |
| 15 |
* ChangeLog: 1.0 initial release |
* ChangeLog:<br> |
| 16 |
* 1.1 added support for +/- N syntax for begin |
* - 1.0 initial release |
| 17 |
|
* - 1.1 added support for +/- N syntax for begin |
| 18 |
* and end year values. (Monte) |
* and end year values. (Monte) |
| 19 |
* 1.2 added support for yyyy-mm-dd syntax for |
* - 1.2 added support for yyyy-mm-dd syntax for |
| 20 |
* time value. (Jan Rosier) |
* time value. (Jan Rosier) |
| 21 |
* 1.3 added support for choosing format for |
* - 1.3 added support for choosing format for |
| 22 |
* month values (Gary Loescher) |
* 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.1 |
| 28 |
|
* @author Andrei Zmievski |
| 29 |
|
* @param array |
| 30 |
|
* @param Smarty |
| 31 |
|
* @return string |
| 32 |
*/ |
*/ |
|
require_once $this->_get_plugin_filepath('shared','make_timestamp'); |
|
|
require_once $this->_get_plugin_filepath('function','html_options'); |
|
| 33 |
function smarty_function_html_select_date($params, &$smarty) |
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. */ |
/* Default values. */ |
| 38 |
$prefix = "Date_"; |
$prefix = "Date_"; |
| 39 |
$start_year = strftime("%Y"); |
$start_year = strftime("%Y"); |
| 45 |
/* Write months as numbers by default GL */ |
/* Write months as numbers by default GL */ |
| 46 |
$month_value_format = "%m"; |
$month_value_format = "%m"; |
| 47 |
$day_format = "%02d"; |
$day_format = "%02d"; |
| 48 |
|
/* Write day values using this format MB */ |
| 49 |
|
$day_value_format = "%d"; |
| 50 |
$year_as_text = false; |
$year_as_text = false; |
| 51 |
/* Display years in reverse order? Ie. 2000,1999,.... */ |
/* Display years in reverse order? Ie. 2000,1999,.... */ |
| 52 |
$reverse_years = false; |
$reverse_years = false; |
| 68 |
$year_extra = null; |
$year_extra = null; |
| 69 |
/* Order in which to display the fields. |
/* Order in which to display the fields. |
| 70 |
"D" -> day, "M" -> month, "Y" -> year. */ |
"D" -> day, "M" -> month, "Y" -> year. */ |
| 71 |
$field_order = 'MDY'; |
$field_order = 'MDY'; |
| 72 |
/* String printed between the different fields. */ |
/* String printed between the different fields. */ |
| 73 |
$field_separator = "\n"; |
$field_separator = "\n"; |
| 74 |
$time = time(); |
$time = time(); |
| 75 |
|
$all_empty = null; |
| 76 |
|
$day_empty = null; |
| 77 |
|
$month_empty = null; |
| 78 |
|
$year_empty = null; |
| 79 |
|
|
| 80 |
|
foreach ($params as $_key=>$_value) { |
| 81 |
|
switch ($_key) { |
| 82 |
|
case 'prefix': |
| 83 |
|
case 'time': |
| 84 |
|
case 'start_year': |
| 85 |
|
case 'end_year': |
| 86 |
|
case 'month_format': |
| 87 |
|
case 'day_format': |
| 88 |
|
case 'day_value_format': |
| 89 |
|
case 'field_array': |
| 90 |
|
case 'day_size': |
| 91 |
|
case 'month_size': |
| 92 |
|
case 'year_size': |
| 93 |
|
case 'all_extra': |
| 94 |
|
case 'day_extra': |
| 95 |
|
case 'month_extra': |
| 96 |
|
case 'year_extra': |
| 97 |
|
case 'field_order': |
| 98 |
|
case 'field_separator': |
| 99 |
|
case 'month_value_format': |
| 100 |
|
case 'month_empty': |
| 101 |
|
case 'day_empty': |
| 102 |
|
case 'year_empty': |
| 103 |
|
$$_key = (string)$_value; |
| 104 |
|
break; |
| 105 |
|
|
| 106 |
|
case 'all_empty': |
| 107 |
|
$$_key = (string)$_value; |
| 108 |
|
$day_empty = $month_empty = $year_empty = $all_empty; |
| 109 |
|
break; |
| 110 |
|
|
| 111 |
|
case 'display_days': |
| 112 |
|
case 'display_months': |
| 113 |
|
case 'display_years': |
| 114 |
|
case 'year_as_text': |
| 115 |
|
case 'reverse_years': |
| 116 |
|
$$_key = (bool)$_value; |
| 117 |
|
break; |
| 118 |
|
|
| 119 |
|
default: |
| 120 |
|
$smarty->trigger_error("[html_select_date] unknown parameter $_key", E_USER_WARNING); |
| 121 |
|
|
| 122 |
|
} |
| 123 |
|
} |
| 124 |
|
|
| 125 |
extract($params); |
// If $time is not in format yyyy-mm-dd |
| 126 |
|
if (!preg_match('/^\d{0,4}-\d{0,2}-\d{0,2}$/', $time)) { |
| 127 |
|
// then $time is empty or unix timestamp or mysql timestamp |
| 128 |
|
// using smarty_make_timestamp to get an unix timestamp and |
| 129 |
|
// strftime to make yyyy-mm-dd |
| 130 |
|
$time = strftime('%Y-%m-%d', smarty_make_timestamp($time)); |
| 131 |
|
} |
| 132 |
|
// Now split this in pieces, which later can be used to set the select |
| 133 |
|
$time = explode("-", $time); |
| 134 |
|
|
| 135 |
|
// make syntax "+N" or "-N" work with start_year and end_year |
| 136 |
|
if (preg_match('!^(\+|\-)\s*(\d+)$!', $end_year, $match)) { |
| 137 |
|
if ($match[1] == '+') { |
| 138 |
|
$end_year = strftime('%Y') + $match[2]; |
| 139 |
|
} else { |
| 140 |
|
$end_year = strftime('%Y') - $match[2]; |
| 141 |
|
} |
| 142 |
|
} |
| 143 |
|
if (preg_match('!^(\+|\-)\s*(\d+)$!', $start_year, $match)) { |
| 144 |
|
if ($match[1] == '+') { |
| 145 |
|
$start_year = strftime('%Y') + $match[2]; |
| 146 |
|
} else { |
| 147 |
|
$start_year = strftime('%Y') - $match[2]; |
| 148 |
|
} |
| 149 |
|
} |
| 150 |
|
|
|
// If $time is not in format yyyy-mm-dd |
|
|
if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $time)) { |
|
|
// then $time is empty or unix timestamp or mysql timestamp |
|
|
// using smarty_make_timestamp to get an unix timestamp and |
|
|
// strftime to make yyyy-mm-dd |
|
|
$time = strftime('%Y-%m-%d', smarty_make_timestamp($time)); |
|
|
} |
|
|
// Now split this in pieces, which later can be used to set the select |
|
|
$time = explode("-", $time); |
|
|
|
|
|
// make syntax "+N" or "-N" work with start_year and end_year |
|
|
if (preg_match('!^(\+|\-)\s*(\d+)$!', $end_year, $match)) { |
|
|
if ($match[1] == '+') { |
|
|
$end_year = strftime('%Y') + $match[2]; |
|
|
} else { |
|
|
$end_year = strftime('%Y') - $match[2]; |
|
|
} |
|
|
} |
|
|
if (preg_match('!^(\+|\-)\s*(\d+)$!', $start_year, $match)) { |
|
|
if ($match[1] == '+') { |
|
|
$start_year = strftime('%Y') + $match[2]; |
|
|
} else { |
|
|
$start_year = strftime('%Y') - $match[2]; |
|
|
} |
|
|
} |
|
|
|
|
| 151 |
$field_order = strtoupper($field_order); |
$field_order = strtoupper($field_order); |
| 152 |
|
|
| 153 |
$html_result = $month_result = $day_result = $year_result = ""; |
$html_result = $month_result = $day_result = $year_result = ""; |
| 155 |
if ($display_months) { |
if ($display_months) { |
| 156 |
$month_names = array(); |
$month_names = array(); |
| 157 |
$month_values = array(); |
$month_values = array(); |
| 158 |
|
if(isset($month_empty)) { |
| 159 |
|
$month_names[''] = $month_empty; |
| 160 |
|
$month_values[''] = ''; |
| 161 |
|
} |
| 162 |
for ($i = 1; $i <= 12; $i++) { |
for ($i = 1; $i <= 12; $i++) { |
| 163 |
$month_names[] = strftime($month_format, mktime(0, 0, 0, $i, 1, 2000)); |
$month_names[$i] = strftime($month_format, mktime(0, 0, 0, $i, 1, 2000)); |
| 164 |
$month_values[] = strftime($month_value_format, mktime(0, 0, 0, $i, 1, 2000)); |
$month_values[$i] = strftime($month_value_format, mktime(0, 0, 0, $i, 1, 2000)); |
| 165 |
} |
} |
| 166 |
|
|
| 167 |
$month_result .= '<select name='; |
$month_result .= '<select name='; |
| 180 |
$month_result .= ' ' . $all_extra; |
$month_result .= ' ' . $all_extra; |
| 181 |
} |
} |
| 182 |
$month_result .= '>'."\n"; |
$month_result .= '>'."\n"; |
| 183 |
|
|
| 184 |
$month_result .= smarty_function_html_options(array('output' => $month_names, |
$month_result .= smarty_function_html_options(array('output' => $month_names, |
| 185 |
'values' => $month_values, |
'values' => $month_values, |
| 186 |
'selected' => $month_values[$time[1]-1], |
'selected' => $a=$time[1] ? strftime($month_value_format, mktime(0, 0, 0, (int)$time[1], 1, 2000)) : '', |
| 187 |
'print_result' => false), |
'print_result' => false), |
| 188 |
$smarty); |
$smarty); |
|
|
|
| 189 |
$month_result .= '</select>'; |
$month_result .= '</select>'; |
| 190 |
} |
} |
| 191 |
|
|
| 192 |
if ($display_days) { |
if ($display_days) { |
| 193 |
$days = array(); |
$days = array(); |
| 194 |
for ($i = 1; $i <= 31; $i++) |
if (isset($day_empty)) { |
| 195 |
|
$days[''] = $day_empty; |
| 196 |
|
$day_values[''] = ''; |
| 197 |
|
} |
| 198 |
|
for ($i = 1; $i <= 31; $i++) { |
| 199 |
$days[] = sprintf($day_format, $i); |
$days[] = sprintf($day_format, $i); |
| 200 |
|
$day_values[] = sprintf($day_value_format, $i); |
| 201 |
|
} |
| 202 |
|
|
| 203 |
$day_result .= '<select name='; |
$day_result .= '<select name='; |
| 204 |
if (null !== $field_array){ |
if (null !== $field_array){ |
| 217 |
} |
} |
| 218 |
$day_result .= '>'."\n"; |
$day_result .= '>'."\n"; |
| 219 |
$day_result .= smarty_function_html_options(array('output' => $days, |
$day_result .= smarty_function_html_options(array('output' => $days, |
| 220 |
'values' => range(1, 31), |
'values' => $day_values, |
| 221 |
'selected' => $time[2], |
'selected' => $time[2], |
| 222 |
'print_result' => false), |
'print_result' => false), |
| 223 |
$smarty); |
$smarty); |
| 244 |
if ($reverse_years) { |
if ($reverse_years) { |
| 245 |
rsort($years, SORT_NUMERIC); |
rsort($years, SORT_NUMERIC); |
| 246 |
} |
} |
| 247 |
|
$yearvals = $years; |
| 248 |
|
if(isset($year_empty)) { |
| 249 |
|
array_unshift($years, $year_empty); |
| 250 |
|
array_unshift($yearvals, ''); |
| 251 |
|
} |
| 252 |
$year_result .= '<select name="' . $year_name . '"'; |
$year_result .= '<select name="' . $year_name . '"'; |
| 253 |
if (null !== $year_size){ |
if (null !== $year_size){ |
| 254 |
$year_result .= ' size="' . $year_size . '"'; |
$year_result .= ' size="' . $year_size . '"'; |
| 261 |
} |
} |
| 262 |
$year_result .= '>'."\n"; |
$year_result .= '>'."\n"; |
| 263 |
$year_result .= smarty_function_html_options(array('output' => $years, |
$year_result .= smarty_function_html_options(array('output' => $years, |
| 264 |
'values' => $years, |
'values' => $yearvals, |
| 265 |
'selected' => $time[0], |
'selected' => $time[0], |
| 266 |
'print_result' => false), |
'print_result' => false), |
| 267 |
$smarty); |
$smarty); |
| 271 |
|
|
| 272 |
// Loop thru the field_order field |
// Loop thru the field_order field |
| 273 |
for ($i = 0; $i <= 2; $i++){ |
for ($i = 0; $i <= 2; $i++){ |
| 274 |
$c = substr($field_order, $i, 1); |
$c = substr($field_order, $i, 1); |
| 275 |
switch ($c){ |
switch ($c){ |
| 276 |
case 'D': |
case 'D': |
| 277 |
$html_result .= $day_result; |
$html_result .= $day_result; |
| 278 |
break; |
break; |
| 279 |
|
|
| 280 |
case 'M': |
case 'M': |
| 281 |
$html_result .= $month_result; |
$html_result .= $month_result; |
| 282 |
break; |
break; |
| 283 |
|
|
| 284 |
case 'Y': |
case 'Y': |
| 285 |
$html_result .= $year_result; |
$html_result .= $year_result; |
| 286 |
break; |
break; |
| 287 |
} |
} |
| 288 |
// Add the field seperator |
// Add the field seperator |
| 289 |
if($i != 2) { |
if($i != 2) { |
| 290 |
$html_result .= $field_separator; |
$html_result .= $field_separator; |
| 291 |
} |
} |
| 292 |
} |
} |
| 293 |
|
|
| 294 |
print $html_result; |
return $html_result; |
| 295 |
} |
} |
| 296 |
|
|
| 297 |
/* vim: set expandtab: */ |
/* vim: set expandtab: */ |