Automatically exported from code.google.com/p/planningalerts
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

function.html_select_date.php 12 KiB

17 anni fa
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <?php
  2. /**
  3. * Smarty plugin
  4. * @package Smarty
  5. * @subpackage plugins
  6. */
  7. /**
  8. * Smarty {html_select_date} plugin
  9. *
  10. * Type: function<br>
  11. * Name: html_select_date<br>
  12. * Purpose: Prints the dropdowns for date selection.
  13. *
  14. * ChangeLog:<br>
  15. * - 1.0 initial release
  16. * - 1.1 added support for +/- N syntax for begin
  17. * and end year values. (Monte)
  18. * - 1.2 added support for yyyy-mm-dd syntax for
  19. * time value. (Jan Rosier)
  20. * - 1.3 added support for choosing format for
  21. * month values (Gary Loescher)
  22. * - 1.3.1 added support for choosing format for
  23. * day values (Marcus Bointon)
  24. * - 1.3.2 support negative timestamps, force year
  25. * dropdown to include given date unless explicitly set (Monte)
  26. * - 1.3.4 fix behaviour of 0000-00-00 00:00:00 dates to match that
  27. * of 0000-00-00 dates (cybot, boots)
  28. * @link http://smarty.php.net/manual/en/language.function.html.select.date.php {html_select_date}
  29. * (Smarty online manual)
  30. * @version 1.3.4
  31. * @author Andrei Zmievski
  32. * @author Monte Ohrt <monte at ohrt dot com>
  33. * @param array
  34. * @param Smarty
  35. * @return string
  36. */
  37. function smarty_function_html_select_date($params, &$smarty)
  38. {
  39. require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
  40. require_once $smarty->_get_plugin_filepath('shared','make_timestamp');
  41. require_once $smarty->_get_plugin_filepath('function','html_options');
  42. /* Default values. */
  43. $prefix = "Date_";
  44. $start_year = strftime("%Y");
  45. $end_year = $start_year;
  46. $display_days = true;
  47. $display_months = true;
  48. $display_years = true;
  49. $month_format = "%B";
  50. /* Write months as numbers by default GL */
  51. $month_value_format = "%m";
  52. $day_format = "%02d";
  53. /* Write day values using this format MB */
  54. $day_value_format = "%d";
  55. $year_as_text = false;
  56. /* Display years in reverse order? Ie. 2000,1999,.... */
  57. $reverse_years = false;
  58. /* Should the select boxes be part of an array when returned from PHP?
  59. e.g. setting it to "birthday", would create "birthday[Day]",
  60. "birthday[Month]" & "birthday[Year]". Can be combined with prefix */
  61. $field_array = null;
  62. /* <select size>'s of the different <select> tags.
  63. If not set, uses default dropdown. */
  64. $day_size = null;
  65. $month_size = null;
  66. $year_size = null;
  67. /* Unparsed attributes common to *ALL* the <select>/<input> tags.
  68. An example might be in the template: all_extra ='class ="foo"'. */
  69. $all_extra = null;
  70. /* Separate attributes for the tags. */
  71. $day_extra = null;
  72. $month_extra = null;
  73. $year_extra = null;
  74. /* Order in which to display the fields.
  75. "D" -> day, "M" -> month, "Y" -> year. */
  76. $field_order = 'MDY';
  77. /* String printed between the different fields. */
  78. $field_separator = "\n";
  79. $time = time();
  80. $all_empty = null;
  81. $day_empty = null;
  82. $month_empty = null;
  83. $year_empty = null;
  84. $extra_attrs = '';
  85. foreach ($params as $_key=>$_value) {
  86. switch ($_key) {
  87. case 'prefix':
  88. case 'time':
  89. case 'start_year':
  90. case 'end_year':
  91. case 'month_format':
  92. case 'day_format':
  93. case 'day_value_format':
  94. case 'field_array':
  95. case 'day_size':
  96. case 'month_size':
  97. case 'year_size':
  98. case 'all_extra':
  99. case 'day_extra':
  100. case 'month_extra':
  101. case 'year_extra':
  102. case 'field_order':
  103. case 'field_separator':
  104. case 'month_value_format':
  105. case 'month_empty':
  106. case 'day_empty':
  107. case 'year_empty':
  108. $$_key = (string)$_value;
  109. break;
  110. case 'all_empty':
  111. $$_key = (string)$_value;
  112. $day_empty = $month_empty = $year_empty = $all_empty;
  113. break;
  114. case 'display_days':
  115. case 'display_months':
  116. case 'display_years':
  117. case 'year_as_text':
  118. case 'reverse_years':
  119. $$_key = (bool)$_value;
  120. break;
  121. default:
  122. if(!is_array($_value)) {
  123. $extra_attrs .= ' '.$_key.'="'.smarty_function_escape_special_chars($_value).'"';
  124. } else {
  125. $smarty->trigger_error("html_select_date: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
  126. }
  127. break;
  128. }
  129. }
  130. if (preg_match('!^-\d+$!', $time)) {
  131. // negative timestamp, use date()
  132. $time = date('Y-m-d', $time);
  133. }
  134. // If $time is not in format yyyy-mm-dd
  135. if (preg_match('/^(\d{0,4}-\d{0,2}-\d{0,2})/', $time, $found)) {
  136. $time = $found[1];
  137. } else {
  138. // use smarty_make_timestamp to get an unix timestamp and
  139. // strftime to make yyyy-mm-dd
  140. $time = strftime('%Y-%m-%d', smarty_make_timestamp($time));
  141. }
  142. // Now split this in pieces, which later can be used to set the select
  143. $time = explode("-", $time);
  144. // make syntax "+N" or "-N" work with start_year and end_year
  145. if (preg_match('!^(\+|\-)\s*(\d+)$!', $end_year, $match)) {
  146. if ($match[1] == '+') {
  147. $end_year = strftime('%Y') + $match[2];
  148. } else {
  149. $end_year = strftime('%Y') - $match[2];
  150. }
  151. }
  152. if (preg_match('!^(\+|\-)\s*(\d+)$!', $start_year, $match)) {
  153. if ($match[1] == '+') {
  154. $start_year = strftime('%Y') + $match[2];
  155. } else {
  156. $start_year = strftime('%Y') - $match[2];
  157. }
  158. }
  159. if (strlen($time[0]) > 0) {
  160. if ($start_year > $time[0] && !isset($params['start_year'])) {
  161. // force start year to include given date if not explicitly set
  162. $start_year = $time[0];
  163. }
  164. if($end_year < $time[0] && !isset($params['end_year'])) {
  165. // force end year to include given date if not explicitly set
  166. $end_year = $time[0];
  167. }
  168. }
  169. $field_order = strtoupper($field_order);
  170. $html_result = $month_result = $day_result = $year_result = "";
  171. if ($display_months) {
  172. $month_names = array();
  173. $month_values = array();
  174. if(isset($month_empty)) {
  175. $month_names[''] = $month_empty;
  176. $month_values[''] = '';
  177. }
  178. for ($i = 1; $i <= 12; $i++) {
  179. $month_names[$i] = strftime($month_format, mktime(0, 0, 0, $i, 1, 2000));
  180. $month_values[$i] = strftime($month_value_format, mktime(0, 0, 0, $i, 1, 2000));
  181. }
  182. $month_result .= '<select name=';
  183. if (null !== $field_array){
  184. $month_result .= '"' . $field_array . '[' . $prefix . 'Month]"';
  185. } else {
  186. $month_result .= '"' . $prefix . 'Month"';
  187. }
  188. if (null !== $month_size){
  189. $month_result .= ' size="' . $month_size . '"';
  190. }
  191. if (null !== $month_extra){
  192. $month_result .= ' ' . $month_extra;
  193. }
  194. if (null !== $all_extra){
  195. $month_result .= ' ' . $all_extra;
  196. }
  197. $month_result .= $extra_attrs . '>'."\n";
  198. $month_result .= smarty_function_html_options(array('output' => $month_names,
  199. 'values' => $month_values,
  200. 'selected' => (int)$time[1] ? strftime($month_value_format, mktime(0, 0, 0, (int)$time[1], 1, 2000)) : '',
  201. 'print_result' => false),
  202. $smarty);
  203. $month_result .= '</select>';
  204. }
  205. if ($display_days) {
  206. $days = array();
  207. if (isset($day_empty)) {
  208. $days[''] = $day_empty;
  209. $day_values[''] = '';
  210. }
  211. for ($i = 1; $i <= 31; $i++) {
  212. $days[] = sprintf($day_format, $i);
  213. $day_values[] = sprintf($day_value_format, $i);
  214. }
  215. $day_result .= '<select name=';
  216. if (null !== $field_array){
  217. $day_result .= '"' . $field_array . '[' . $prefix . 'Day]"';
  218. } else {
  219. $day_result .= '"' . $prefix . 'Day"';
  220. }
  221. if (null !== $day_size){
  222. $day_result .= ' size="' . $day_size . '"';
  223. }
  224. if (null !== $all_extra){
  225. $day_result .= ' ' . $all_extra;
  226. }
  227. if (null !== $day_extra){
  228. $day_result .= ' ' . $day_extra;
  229. }
  230. $day_result .= $extra_attrs . '>'."\n";
  231. $day_result .= smarty_function_html_options(array('output' => $days,
  232. 'values' => $day_values,
  233. 'selected' => $time[2],
  234. 'print_result' => false),
  235. $smarty);
  236. $day_result .= '</select>';
  237. }
  238. if ($display_years) {
  239. if (null !== $field_array){
  240. $year_name = $field_array . '[' . $prefix . 'Year]';
  241. } else {
  242. $year_name = $prefix . 'Year';
  243. }
  244. if ($year_as_text) {
  245. $year_result .= '<input type="text" name="' . $year_name . '" value="' . $time[0] . '" size="4" maxlength="4"';
  246. if (null !== $all_extra){
  247. $year_result .= ' ' . $all_extra;
  248. }
  249. if (null !== $year_extra){
  250. $year_result .= ' ' . $year_extra;
  251. }
  252. $year_result .= ' />';
  253. } else {
  254. $years = range((int)$start_year, (int)$end_year);
  255. if ($reverse_years) {
  256. rsort($years, SORT_NUMERIC);
  257. } else {
  258. sort($years, SORT_NUMERIC);
  259. }
  260. $yearvals = $years;
  261. if(isset($year_empty)) {
  262. array_unshift($years, $year_empty);
  263. array_unshift($yearvals, '');
  264. }
  265. $year_result .= '<select name="' . $year_name . '"';
  266. if (null !== $year_size){
  267. $year_result .= ' size="' . $year_size . '"';
  268. }
  269. if (null !== $all_extra){
  270. $year_result .= ' ' . $all_extra;
  271. }
  272. if (null !== $year_extra){
  273. $year_result .= ' ' . $year_extra;
  274. }
  275. $year_result .= $extra_attrs . '>'."\n";
  276. $year_result .= smarty_function_html_options(array('output' => $years,
  277. 'values' => $yearvals,
  278. 'selected' => $time[0],
  279. 'print_result' => false),
  280. $smarty);
  281. $year_result .= '</select>';
  282. }
  283. }
  284. // Loop thru the field_order field
  285. for ($i = 0; $i <= 2; $i++){
  286. $c = substr($field_order, $i, 1);
  287. switch ($c){
  288. case 'D':
  289. $html_result .= $day_result;
  290. break;
  291. case 'M':
  292. $html_result .= $month_result;
  293. break;
  294. case 'Y':
  295. $html_result .= $year_result;
  296. break;
  297. }
  298. // Add the field seperator
  299. if($i != 2) {
  300. $html_result .= $field_separator;
  301. }
  302. }
  303. return $html_result;
  304. }
  305. /* vim: set expandtab: */
  306. ?>