PageRenderTime 26ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/vendor/smarty3/lib/libs/plugins/function.html_select_date.php

https://github.com/jlesueur/zinc
PHP | 390 lines | 280 code | 30 blank | 80 comment | 71 complexity | 1bfb270a0719176db5af0f9091efde6c MD5 | raw file
Possible License(s): LGPL-3.0, BSD-3-Clause, GPL-2.0, LGPL-2.1, CC-BY-SA-3.0
  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)) {
  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. list($_year, $_month, $_day) = $time = explode('-', date('Y-m-d'));
  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. } else if ($t[0] == '+') {
  208. $$key = (int)($_current_year + trim(substr($t, 1)));
  209. } else if ($t[0] == '-') {
  210. $$key = (int)($_current_year - 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. $_html_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. $_html_month = '';
  260. $_extra = '';
  261. $_name = $field_array ? ($field_array . '[' . $prefix . 'Month]') : ($prefix . 'Month');
  262. if ($all_extra) {
  263. $_extra .= ' ' . $all_extra;
  264. }
  265. if ($month_extra) {
  266. $_extra .= ' ' . $month_extra;
  267. }
  268. $_html_months = '<select name="' . $_name . '"';
  269. if ($month_id !== null || $all_id !== null) {
  270. $_html_months .= ' id="' . smarty_function_escape_special_chars(
  271. $month_id !== null ? ( $month_id ? $month_id : $_name ) : ( $all_id ? ($all_id . $_name) : $_name )
  272. ) . '"';
  273. }
  274. if ($month_size) {
  275. $_html_months .= ' size="' . $month_size . '"';
  276. }
  277. $_html_months .= $_extra . $extra_attrs . '>' . $option_separator;
  278. if (isset($month_empty) || isset($all_empty)) {
  279. $_html_months .= '<option value="">' . ( isset($month_empty) ? $month_empty : $all_empty ) . '</option>' . $option_separator;
  280. }
  281. for ($i = 1; $i <= 12; $i++) {
  282. $_val = sprintf('%02d', $i);
  283. $_text = isset($month_names) ? smarty_function_escape_special_chars($month_names[$i]) : ($month_format == "%m" ? $_val : strftime($month_format, $_month_timestamps[$i]));
  284. $_value = $month_value_format == "%m" ? $_val : strftime($month_value_format, $_month_timestamps[$i]);
  285. $_html_months .= '<option value="' . $_value . '"'
  286. . ($_val == $_month ? ' selected="selected"' : '')
  287. . '>' . $_text . '</option>' . $option_separator;
  288. }
  289. $_html_months .= '</select>';
  290. }
  291. // generate day <select> or <input>
  292. if ($display_days) {
  293. $_html_day = '';
  294. $_extra = '';
  295. $_name = $field_array ? ($field_array . '[' . $prefix . 'Day]') : ($prefix . 'Day');
  296. if ($all_extra) {
  297. $_extra .= ' ' . $all_extra;
  298. }
  299. if ($day_extra) {
  300. $_extra .= ' ' . $day_extra;
  301. }
  302. $_html_days = '<select name="' . $_name . '"';
  303. if ($day_id !== null || $all_id !== null) {
  304. $_html_days .= ' id="' . smarty_function_escape_special_chars(
  305. $day_id !== null ? ( $day_id ? $day_id : $_name ) : ( $all_id ? ($all_id . $_name) : $_name )
  306. ) . '"';
  307. }
  308. if ($day_size) {
  309. $_html_days .= ' size="' . $day_size . '"';
  310. }
  311. $_html_days .= $_extra . $extra_attrs . '>' . $option_separator;
  312. if (isset($day_empty) || isset($all_empty)) {
  313. $_html_days .= '<option value="">' . ( isset($day_empty) ? $day_empty : $all_empty ) . '</option>' . $option_separator;
  314. }
  315. for ($i = 1; $i <= 31; $i++) {
  316. $_val = sprintf('%02d', $i);
  317. $_text = $day_format == '%02d' ? $_val : sprintf($day_format, $i);
  318. $_value = $day_value_format == '%02d' ? $_val : sprintf($day_value_format, $i);
  319. $_html_days .= '<option value="' . $_value . '"'
  320. . ($_val == $_day ? ' selected="selected"' : '')
  321. . '>' . $_text . '</option>' . $option_separator;
  322. }
  323. $_html_days .= '</select>';
  324. }
  325. // order the fields for output
  326. $_html = '';
  327. for ($i=0; $i <= 2; $i++) {
  328. switch ($field_order[$i]) {
  329. case 'Y':
  330. case 'y':
  331. if (isset($_html_years)) {
  332. if ($_html) {
  333. $_html .= $field_separator;
  334. }
  335. $_html .= $_html_years;
  336. }
  337. break;
  338. case 'm':
  339. case 'M':
  340. if (isset($_html_months)) {
  341. if ($_html) {
  342. $_html .= $field_separator;
  343. }
  344. $_html .= $_html_months;
  345. }
  346. break;
  347. case 'd':
  348. case 'D':
  349. if (isset($_html_days)) {
  350. if ($_html) {
  351. $_html .= $field_separator;
  352. }
  353. $_html .= $_html_days;
  354. }
  355. break;
  356. }
  357. }
  358. return $_html;
  359. }
  360. ?>