function.html_select_date.php 15 KB

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