PageRenderTime 130ms CodeModel.GetById 40ms RepoModel.GetById 6ms app.codeStats 0ms

/modules/smarty/vendor/smarty/plugins/function.html_select_date.php

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