You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

function.html_select_date.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <?php
  2. /**
  3. * Smarty plugin
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsFunction
  7. */
  8. /**
  9. * @ignore
  10. */
  11. require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
  12. /**
  13. * @ignore
  14. */
  15. require_once(SMARTY_PLUGINS_DIR . 'shared.make_timestamp.php');
  16. /**
  17. * Smarty {html_select_date} plugin
  18. * Type: function<br>
  19. * Name: html_select_date<br>
  20. * Purpose: Prints the dropdowns for date selection.
  21. * ChangeLog:
  22. * <pre>
  23. * - 1.0 initial release
  24. * - 1.1 added support for +/- N syntax for begin
  25. * and end year values. (Monte)
  26. * - 1.2 added support for yyyy-mm-dd syntax for
  27. * time value. (Jan Rosier)
  28. * - 1.3 added support for choosing format for
  29. * month values (Gary Loescher)
  30. * - 1.3.1 added support for choosing format for
  31. * day values (Marcus Bointon)
  32. * - 1.3.2 support negative timestamps, force year
  33. * dropdown to include given date unless explicitly set (Monte)
  34. * - 1.3.4 fix behaviour of 0000-00-00 00:00:00 dates to match that
  35. * of 0000-00-00 dates (cybot, boots)
  36. * - 2.0 complete rewrite for performance,
  37. * added attributes month_names, *_id
  38. * </pre>
  39. *
  40. * @link http://www.smarty.net/manual/en/language.function.html.select.date.php {html_select_date}
  41. * (Smarty online manual)
  42. * @version 2.0
  43. * @author Andrei Zmievski
  44. * @author Monte Ohrt <monte at ohrt dot com>
  45. * @author Rodney Rehm
  46. *
  47. * @param array $params parameters
  48. *
  49. * @return string
  50. */
  51. function smarty_function_html_select_date($params)
  52. {
  53. // generate timestamps used for month names only
  54. static $_month_timestamps = null;
  55. static $_current_year = null;
  56. if ($_month_timestamps === null) {
  57. $_current_year = date('Y');
  58. $_month_timestamps = array();
  59. for ($i = 1; $i <= 12; $i ++) {
  60. $_month_timestamps[$i] = mktime(0, 0, 0, $i, 1, 2000);
  61. }
  62. }
  63. /* Default values. */
  64. $prefix = "Date_";
  65. $start_year = null;
  66. $end_year = null;
  67. $display_days = true;
  68. $display_months = true;
  69. $display_years = true;
  70. $month_format = "%B";
  71. /* Write months as numbers by default GL */
  72. $month_value_format = "%m";
  73. $day_format = "%02d";
  74. /* Write day values using this format MB */
  75. $day_value_format = "%d";
  76. $year_as_text = false;
  77. /* Display years in reverse order? Ie. 2000,1999,.... */
  78. $reverse_years = false;
  79. /* Should the select boxes be part of an array when returned from PHP?
  80. e.g. setting it to "birthday", would create "birthday[Day]",
  81. "birthday[Month]" & "birthday[Year]". Can be combined with prefix */
  82. $field_array = null;
  83. /* <select size>'s of the different <select> tags.
  84. If not set, uses default dropdown. */
  85. $day_size = null;
  86. $month_size = null;
  87. $year_size = null;
  88. /* Unparsed attributes common to *ALL* the <select>/<input> tags.
  89. An example might be in the template: all_extra ='class ="foo"'. */
  90. $all_extra = null;
  91. /* Separate attributes for the tags. */
  92. $day_extra = null;
  93. $month_extra = null;
  94. $year_extra = null;
  95. /* Order in which to display the fields.
  96. "D" -> day, "M" -> month, "Y" -> year. */
  97. $field_order = 'MDY';
  98. /* String printed between the different fields. */
  99. $field_separator = "\n";
  100. $option_separator = "\n";
  101. $time = null;
  102. // $all_empty = null;
  103. // $day_empty = null;
  104. // $month_empty = null;
  105. // $year_empty = null;
  106. $extra_attrs = '';
  107. $all_id = null;
  108. $day_id = null;
  109. $month_id = null;
  110. $year_id = null;
  111. foreach ($params as $_key => $_value) {
  112. switch ($_key) {
  113. case 'time':
  114. if (!is_array($_value) && $_value !== null) {
  115. $time = smarty_make_timestamp($_value);
  116. }
  117. break;
  118. case 'month_names':
  119. if (is_array($_value) && count($_value) == 12) {
  120. $$_key = $_value;
  121. } else {
  122. trigger_error("html_select_date: month_names must be an array of 12 strings", E_USER_NOTICE);
  123. }
  124. break;
  125. case 'prefix':
  126. case 'field_array':
  127. case 'start_year':
  128. case 'end_year':
  129. case 'day_format':
  130. case 'day_value_format':
  131. case 'month_format':
  132. case 'month_value_format':
  133. case 'day_size':
  134. case 'month_size':
  135. case 'year_size':
  136. case 'all_extra':
  137. case 'day_extra':
  138. case 'month_extra':
  139. case 'year_extra':
  140. case 'field_order':
  141. case 'field_separator':
  142. case 'option_separator':
  143. case 'all_empty':
  144. case 'month_empty':
  145. case 'day_empty':
  146. case 'year_empty':
  147. case 'all_id':
  148. case 'month_id':
  149. case 'day_id':
  150. case 'year_id':
  151. $$_key = (string) $_value;
  152. break;
  153. case 'display_days':
  154. case 'display_months':
  155. case 'display_years':
  156. case 'year_as_text':
  157. case 'reverse_years':
  158. $$_key = (bool) $_value;
  159. break;
  160. default:
  161. if (!is_array($_value)) {
  162. $extra_attrs .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_value) . '"';
  163. } else {
  164. trigger_error("html_select_date: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
  165. }
  166. break;
  167. }
  168. }
  169. // Note: date() is faster than strftime()
  170. // Note: explode(date()) is faster than date() date() date()
  171. if (isset($params['time']) && is_array($params['time'])) {
  172. if (isset($params['time'][$prefix . 'Year'])) {
  173. // $_REQUEST[$field_array] given
  174. foreach (array('Y' => 'Year', 'm' => 'Month', 'd' => 'Day') as $_elementKey => $_elementName) {
  175. $_variableName = '_' . strtolower($_elementName);
  176. $$_variableName = isset($params['time'][$prefix . $_elementName])
  177. ? $params['time'][$prefix . $_elementName]
  178. : date($_elementKey);
  179. }
  180. } elseif (isset($params['time'][$field_array][$prefix . 'Year'])) {
  181. // $_REQUEST given
  182. foreach (array('Y' => 'Year', 'm' => 'Month', 'd' => 'Day') as $_elementKey => $_elementName) {
  183. $_variableName = '_' . strtolower($_elementName);
  184. $$_variableName = isset($params['time'][$field_array][$prefix . $_elementName])
  185. ? $params['time'][$field_array][$prefix . $_elementName]
  186. : date($_elementKey);
  187. }
  188. } else {
  189. // no date found, use NOW
  190. list($_year, $_month, $_day) = $time = explode('-', date('Y-m-d'));
  191. }
  192. } elseif ($time === null) {
  193. if (array_key_exists('time', $params)) {
  194. $_year = $_month = $_day = $time = null;
  195. } else {
  196. list($_year, $_month, $_day) = $time = explode('-', date('Y-m-d'));
  197. }
  198. } else {
  199. list($_year, $_month, $_day) = $time = explode('-', date('Y-m-d', $time));
  200. }
  201. // make syntax "+N" or "-N" work with $start_year and $end_year
  202. // Note preg_match('!^(\+|\-)\s*(\d+)$!', $end_year, $match) is slower than trim+substr
  203. foreach (array('start', 'end') as $key) {
  204. $key .= '_year';
  205. $t = $$key;
  206. if ($t === null) {
  207. $$key = (int) $_current_year;
  208. } elseif ($t[0] == '+') {
  209. $$key = (int) ($_current_year + (int)trim(substr($t, 1)));
  210. } elseif ($t[0] == '-') {
  211. $$key = (int) ($_current_year - (int)trim(substr($t, 1)));
  212. } else {
  213. $$key = (int) $$key;
  214. }
  215. }
  216. // flip for ascending or descending
  217. if (($start_year > $end_year && !$reverse_years) || ($start_year < $end_year && $reverse_years)) {
  218. $t = $end_year;
  219. $end_year = $start_year;
  220. $start_year = $t;
  221. }
  222. // generate year <select> or <input>
  223. if ($display_years) {
  224. $_extra = '';
  225. $_name = $field_array ? ($field_array . '[' . $prefix . 'Year]') : ($prefix . 'Year');
  226. if ($all_extra) {
  227. $_extra .= ' ' . $all_extra;
  228. }
  229. if ($year_extra) {
  230. $_extra .= ' ' . $year_extra;
  231. }
  232. if ($year_as_text) {
  233. $_html_years = '<input type="text" name="' . $_name . '" value="' . $_year . '" size="4" maxlength="4"' . $_extra . $extra_attrs . ' />';
  234. } else {
  235. $_html_years = '<select name="' . $_name . '"';
  236. if ($year_id !== null || $all_id !== null) {
  237. $_html_years .= ' id="' . smarty_function_escape_special_chars(
  238. $year_id !== null ? ($year_id ? $year_id : $_name) : ($all_id ? ($all_id . $_name) : $_name)
  239. ) . '"';
  240. }
  241. if ($year_size) {
  242. $_html_years .= ' size="' . $year_size . '"';
  243. }
  244. $_html_years .= $_extra . $extra_attrs . '>' . $option_separator;
  245. if (isset($year_empty) || isset($all_empty)) {
  246. $_html_years .= '<option value="">' . (isset($year_empty) ? $year_empty : $all_empty) . '</option>' . $option_separator;
  247. }
  248. $op = $start_year > $end_year ? - 1 : 1;
  249. for ($i = $start_year; $op > 0 ? $i <= $end_year : $i >= $end_year; $i += $op) {
  250. $_html_years .= '<option value="' . $i . '"'
  251. . ($_year == $i ? ' selected="selected"' : '')
  252. . '>' . $i . '</option>' . $option_separator;
  253. }
  254. $_html_years .= '</select>';
  255. }
  256. }
  257. // generate month <select> or <input>
  258. if ($display_months) {
  259. $_extra = '';
  260. $_name = $field_array ? ($field_array . '[' . $prefix . 'Month]') : ($prefix . 'Month');
  261. if ($all_extra) {
  262. $_extra .= ' ' . $all_extra;
  263. }
  264. if ($month_extra) {
  265. $_extra .= ' ' . $month_extra;
  266. }
  267. $_html_months = '<select name="' . $_name . '"';
  268. if ($month_id !== null || $all_id !== null) {
  269. $_html_months .= ' id="' . smarty_function_escape_special_chars(
  270. $month_id !== null ? ($month_id ? $month_id : $_name) : ($all_id ? ($all_id . $_name) : $_name)
  271. ) . '"';
  272. }
  273. if ($month_size) {
  274. $_html_months .= ' size="' . $month_size . '"';
  275. }
  276. $_html_months .= $_extra . $extra_attrs . '>' . $option_separator;
  277. if (isset($month_empty) || isset($all_empty)) {
  278. $_html_months .= '<option value="">' . (isset($month_empty) ? $month_empty : $all_empty) . '</option>' . $option_separator;
  279. }
  280. for ($i = 1; $i <= 12; $i ++) {
  281. $_val = sprintf('%02d', $i);
  282. $_text = isset($month_names) ? smarty_function_escape_special_chars($month_names[$i]) : ($month_format == "%m" ? $_val : strftime($month_format, $_month_timestamps[$i]));
  283. $_value = $month_value_format == "%m" ? $_val : strftime($month_value_format, $_month_timestamps[$i]);
  284. $_html_months .= '<option value="' . $_value . '"'
  285. . ($_val == $_month ? ' selected="selected"' : '')
  286. . '>' . $_text . '</option>' . $option_separator;
  287. }
  288. $_html_months .= '</select>';
  289. }
  290. // generate day <select> or <input>
  291. if ($display_days) {
  292. $_extra = '';
  293. $_name = $field_array ? ($field_array . '[' . $prefix . 'Day]') : ($prefix . 'Day');
  294. if ($all_extra) {
  295. $_extra .= ' ' . $all_extra;
  296. }
  297. if ($day_extra) {
  298. $_extra .= ' ' . $day_extra;
  299. }
  300. $_html_days = '<select name="' . $_name . '"';
  301. if ($day_id !== null || $all_id !== null) {
  302. $_html_days .= ' id="' . smarty_function_escape_special_chars(
  303. $day_id !== null ? ($day_id ? $day_id : $_name) : ($all_id ? ($all_id . $_name) : $_name)
  304. ) . '"';
  305. }
  306. if ($day_size) {
  307. $_html_days .= ' size="' . $day_size . '"';
  308. }
  309. $_html_days .= $_extra . $extra_attrs . '>' . $option_separator;
  310. if (isset($day_empty) || isset($all_empty)) {
  311. $_html_days .= '<option value="">' . (isset($day_empty) ? $day_empty : $all_empty) . '</option>' . $option_separator;
  312. }
  313. for ($i = 1; $i <= 31; $i ++) {
  314. $_val = sprintf('%02d', $i);
  315. $_text = $day_format == '%02d' ? $_val : sprintf($day_format, $i);
  316. $_value = $day_value_format == '%02d' ? $_val : sprintf($day_value_format, $i);
  317. $_html_days .= '<option value="' . $_value . '"'
  318. . ($_val == $_day ? ' selected="selected"' : '')
  319. . '>' . $_text . '</option>' . $option_separator;
  320. }
  321. $_html_days .= '</select>';
  322. }
  323. // order the fields for output
  324. $_html = '';
  325. for ($i = 0; $i <= 2; $i ++) {
  326. switch ($field_order[$i]) {
  327. case 'Y':
  328. case 'y':
  329. if (isset($_html_years)) {
  330. if ($_html) {
  331. $_html .= $field_separator;
  332. }
  333. $_html .= $_html_years;
  334. }
  335. break;
  336. case 'm':
  337. case 'M':
  338. if (isset($_html_months)) {
  339. if ($_html) {
  340. $_html .= $field_separator;
  341. }
  342. $_html .= $_html_months;
  343. }
  344. break;
  345. case 'd':
  346. case 'D':
  347. if (isset($_html_days)) {
  348. if ($_html) {
  349. $_html .= $field_separator;
  350. }
  351. $_html .= $_html_days;
  352. }
  353. break;
  354. }
  355. }
  356. return $_html;
  357. }