PageRenderTime 32ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 1ms

/library/Smarty/plugins/function.html_select_date.php

https://gitlab.com/Shenglian/SmartyProject
PHP | 391 lines | 282 code | 30 blank | 79 comment | 70 complexity | 8138f27826f0a00f0f38112f250d6ec2 MD5 | raw file
  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 =
  177. isset($params[ 'time' ][ $prefix . $_elementName ]) ? $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 ] : date($_elementKey);
  186. }
  187. } else {
  188. // no date found, use NOW
  189. list($_year, $_month, $_day) = $time = explode('-', date('Y-m-d'));
  190. }
  191. } elseif ($time === null) {
  192. if (array_key_exists('time', $params)) {
  193. $_year = $_month = $_day = $time = null;
  194. } else {
  195. list($_year, $_month, $_day) = $time = explode('-', date('Y-m-d'));
  196. }
  197. } else {
  198. list($_year, $_month, $_day) = $time = explode('-', date('Y-m-d', $time));
  199. }
  200. // make syntax "+N" or "-N" work with $start_year and $end_year
  201. // Note preg_match('!^(\+|\-)\s*(\d+)$!', $end_year, $match) is slower than trim+substr
  202. foreach (array('start', 'end') as $key) {
  203. $key .= '_year';
  204. $t = $$key;
  205. if ($t === null) {
  206. $$key = (int) $_current_year;
  207. } elseif ($t[ 0 ] == '+') {
  208. $$key = (int) ($_current_year + (int) trim(substr($t, 1)));
  209. } elseif ($t[ 0 ] == '-') {
  210. $$key = (int) ($_current_year - (int) trim(substr($t, 1)));
  211. } else {
  212. $$key = (int) $$key;
  213. }
  214. }
  215. // flip for ascending or descending
  216. if (($start_year > $end_year && !$reverse_years) || ($start_year < $end_year && $reverse_years)) {
  217. $t = $end_year;
  218. $end_year = $start_year;
  219. $start_year = $t;
  220. }
  221. // generate year <select> or <input>
  222. if ($display_years) {
  223. $_extra = '';
  224. $_name = $field_array ? ($field_array . '[' . $prefix . 'Year]') : ($prefix . 'Year');
  225. if ($all_extra) {
  226. $_extra .= ' ' . $all_extra;
  227. }
  228. if ($year_extra) {
  229. $_extra .= ' ' . $year_extra;
  230. }
  231. if ($year_as_text) {
  232. $_html_years =
  233. '<input type="text" name="' . $_name . '" value="' . $_year . '" size="4" maxlength="4"' . $_extra .
  234. $extra_attrs . ' />';
  235. } else {
  236. $_html_years = '<select name="' . $_name . '"';
  237. if ($year_id !== null || $all_id !== null) {
  238. $_html_years .= ' id="' . smarty_function_escape_special_chars($year_id !== null ?
  239. ($year_id ? $year_id : $_name) :
  240. ($all_id ? ($all_id . $_name) :
  241. $_name)) . '"';
  242. }
  243. if ($year_size) {
  244. $_html_years .= ' size="' . $year_size . '"';
  245. }
  246. $_html_years .= $_extra . $extra_attrs . '>' . $option_separator;
  247. if (isset($year_empty) || isset($all_empty)) {
  248. $_html_years .= '<option value="">' . (isset($year_empty) ? $year_empty : $all_empty) . '</option>' .
  249. $option_separator;
  250. }
  251. $op = $start_year > $end_year ? - 1 : 1;
  252. for ($i = $start_year; $op > 0 ? $i <= $end_year : $i >= $end_year; $i += $op) {
  253. $_html_years .= '<option value="' . $i . '"' . ($_year == $i ? ' selected="selected"' : '') . '>' . $i .
  254. '</option>' . $option_separator;
  255. }
  256. $_html_years .= '</select>';
  257. }
  258. }
  259. // generate month <select> or <input>
  260. if ($display_months) {
  261. $_extra = '';
  262. $_name = $field_array ? ($field_array . '[' . $prefix . 'Month]') : ($prefix . 'Month');
  263. if ($all_extra) {
  264. $_extra .= ' ' . $all_extra;
  265. }
  266. if ($month_extra) {
  267. $_extra .= ' ' . $month_extra;
  268. }
  269. $_html_months = '<select name="' . $_name . '"';
  270. if ($month_id !== null || $all_id !== null) {
  271. $_html_months .= ' id="' . smarty_function_escape_special_chars($month_id !== null ?
  272. ($month_id ? $month_id : $_name) :
  273. ($all_id ? ($all_id . $_name) :
  274. $_name)) . '"';
  275. }
  276. if ($month_size) {
  277. $_html_months .= ' size="' . $month_size . '"';
  278. }
  279. $_html_months .= $_extra . $extra_attrs . '>' . $option_separator;
  280. if (isset($month_empty) || isset($all_empty)) {
  281. $_html_months .= '<option value="">' . (isset($month_empty) ? $month_empty : $all_empty) . '</option>' .
  282. $option_separator;
  283. }
  284. for ($i = 1; $i <= 12; $i ++) {
  285. $_val = sprintf('%02d', $i);
  286. $_text = isset($month_names) ? smarty_function_escape_special_chars($month_names[ $i ]) :
  287. ($month_format == "%m" ? $_val : strftime($month_format, $_month_timestamps[ $i ]));
  288. $_value = $month_value_format == "%m" ? $_val : strftime($month_value_format, $_month_timestamps[ $i ]);
  289. $_html_months .= '<option value="' . $_value . '"' . ($_val == $_month ? ' selected="selected"' : '') .
  290. '>' . $_text . '</option>' . $option_separator;
  291. }
  292. $_html_months .= '</select>';
  293. }
  294. // generate day <select> or <input>
  295. if ($display_days) {
  296. $_extra = '';
  297. $_name = $field_array ? ($field_array . '[' . $prefix . 'Day]') : ($prefix . 'Day');
  298. if ($all_extra) {
  299. $_extra .= ' ' . $all_extra;
  300. }
  301. if ($day_extra) {
  302. $_extra .= ' ' . $day_extra;
  303. }
  304. $_html_days = '<select name="' . $_name . '"';
  305. if ($day_id !== null || $all_id !== null) {
  306. $_html_days .= ' id="' .
  307. smarty_function_escape_special_chars($day_id !== null ? ($day_id ? $day_id : $_name) :
  308. ($all_id ? ($all_id . $_name) : $_name)) . '"';
  309. }
  310. if ($day_size) {
  311. $_html_days .= ' size="' . $day_size . '"';
  312. }
  313. $_html_days .= $_extra . $extra_attrs . '>' . $option_separator;
  314. if (isset($day_empty) || isset($all_empty)) {
  315. $_html_days .= '<option value="">' . (isset($day_empty) ? $day_empty : $all_empty) . '</option>' .
  316. $option_separator;
  317. }
  318. for ($i = 1; $i <= 31; $i ++) {
  319. $_val = sprintf('%02d', $i);
  320. $_text = $day_format == '%02d' ? $_val : sprintf($day_format, $i);
  321. $_value = $day_value_format == '%02d' ? $_val : sprintf($day_value_format, $i);
  322. $_html_days .= '<option value="' . $_value . '"' . ($_val == $_day ? ' selected="selected"' : '') . '>' .
  323. $_text . '</option>' . $option_separator;
  324. }
  325. $_html_days .= '</select>';
  326. }
  327. // order the fields for output
  328. $_html = '';
  329. for ($i = 0; $i <= 2; $i ++) {
  330. switch ($field_order[ $i ]) {
  331. case 'Y':
  332. case 'y':
  333. if (isset($_html_years)) {
  334. if ($_html) {
  335. $_html .= $field_separator;
  336. }
  337. $_html .= $_html_years;
  338. }
  339. break;
  340. case 'm':
  341. case 'M':
  342. if (isset($_html_months)) {
  343. if ($_html) {
  344. $_html .= $field_separator;
  345. }
  346. $_html .= $_html_months;
  347. }
  348. break;
  349. case 'd':
  350. case 'D':
  351. if (isset($_html_days)) {
  352. if ($_html) {
  353. $_html .= $field_separator;
  354. }
  355. $_html .= $_html_days;
  356. }
  357. break;
  358. }
  359. }
  360. return $_html;
  361. }