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 15KB

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