PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/vendors/smarty/plugins/function.html_select_date.php

https://github.com/kiang/olc_baker
PHP | 328 lines | 248 code | 18 blank | 62 comment | 51 complexity | a0fb4f70e3af88a61ea8c1ea078f7113 MD5 | raw file
  1. <?php
  2. /**
  3. * Smarty plugin
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsFunction
  7. */
  8. /**
  9. * Smarty {html_select_date} plugin
  10. *
  11. * Type: function<br>
  12. * Name: html_select_date<br>
  13. * Purpose: Prints the dropdowns for date selection.
  14. *
  15. * ChangeLog:<br>
  16. * - 1.0 initial release
  17. * - 1.1 added support for +/- N syntax for begin
  18. * and end year values. (Monte)
  19. * - 1.2 added support for yyyy-mm-dd syntax for
  20. * time value. (Jan Rosier)
  21. * - 1.3 added support for choosing format for
  22. * month values (Gary Loescher)
  23. * - 1.3.1 added support for choosing format for
  24. * day values (Marcus Bointon)
  25. * - 1.3.2 support negative timestamps, force year
  26. * dropdown to include given date unless explicitly set (Monte)
  27. * - 1.3.4 fix behaviour of 0000-00-00 00:00:00 dates to match that
  28. * of 0000-00-00 dates (cybot, boots)
  29. *
  30. * @link http://smarty.php.net/manual/en/language.function.html.select.date.php {html_select_date}
  31. * (Smarty online manual)
  32. * @version 1.3.4
  33. * @author Andrei Zmievski
  34. * @author Monte Ohrt <monte at ohrt dot com>
  35. * @param array $params parameters
  36. * @param object $template template object
  37. * @return string
  38. */
  39. function smarty_function_html_select_date($params, $template)
  40. {
  41. require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
  42. require_once(SMARTY_PLUGINS_DIR . 'shared.make_timestamp.php');
  43. require_once(SMARTY_PLUGINS_DIR . 'function.html_options.php');
  44. /* Default values. */
  45. $prefix = "Date_";
  46. $start_year = strftime("%Y");
  47. $end_year = $start_year;
  48. $display_days = true;
  49. $display_months = true;
  50. $display_years = true;
  51. $month_format = "%B";
  52. /* Write months as numbers by default GL */
  53. $month_value_format = "%m";
  54. $day_format = "%02d";
  55. /* Write day values using this format MB */
  56. $day_value_format = "%d";
  57. $year_as_text = false;
  58. /* Display years in reverse order? Ie. 2000,1999,.... */
  59. $reverse_years = false;
  60. /* Should the select boxes be part of an array when returned from PHP?
  61. e.g. setting it to "birthday", would create "birthday[Day]",
  62. "birthday[Month]" & "birthday[Year]". Can be combined with prefix */
  63. $field_array = null;
  64. /* <select size>'s of the different <select> tags.
  65. If not set, uses default dropdown. */
  66. $day_size = null;
  67. $month_size = null;
  68. $year_size = null;
  69. /* Unparsed attributes common to *ALL* the <select>/<input> tags.
  70. An example might be in the template: all_extra ='class ="foo"'. */
  71. $all_extra = null;
  72. /* Separate attributes for the tags. */
  73. $day_extra = null;
  74. $month_extra = null;
  75. $year_extra = null;
  76. /* Order in which to display the fields.
  77. "D" -> day, "M" -> month, "Y" -> year. */
  78. $field_order = 'MDY';
  79. /* String printed between the different fields. */
  80. $field_separator = "\n";
  81. $time = time();
  82. $all_empty = null;
  83. $day_empty = null;
  84. $month_empty = null;
  85. $year_empty = null;
  86. $extra_attrs = '';
  87. foreach ($params as $_key => $_value) {
  88. switch ($_key) {
  89. case 'prefix':
  90. case 'time':
  91. case 'start_year':
  92. case 'end_year':
  93. case 'month_format':
  94. case 'day_format':
  95. case 'day_value_format':
  96. case 'field_array':
  97. case 'day_size':
  98. case 'month_size':
  99. case 'year_size':
  100. case 'all_extra':
  101. case 'day_extra':
  102. case 'month_extra':
  103. case 'year_extra':
  104. case 'field_order':
  105. case 'field_separator':
  106. case 'month_value_format':
  107. case 'month_empty':
  108. case 'day_empty':
  109. case 'year_empty':
  110. $$_key = (string) $_value;
  111. break;
  112. case 'all_empty':
  113. $$_key = (string) $_value;
  114. $day_empty = $month_empty = $year_empty = $all_empty;
  115. break;
  116. case 'display_days':
  117. case 'display_months':
  118. case 'display_years':
  119. case 'year_as_text':
  120. case 'reverse_years':
  121. $$_key = (bool) $_value;
  122. break;
  123. default:
  124. if (!is_array($_value)) {
  125. $extra_attrs .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_value) . '"';
  126. } else {
  127. trigger_error("html_select_date: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
  128. }
  129. break;
  130. }
  131. }
  132. if (preg_match('!^-\d+$!', $time)) {
  133. // negative timestamp, use date()
  134. $time = date('Y-m-d', $time);
  135. }
  136. // If $time is not in format yyyy-mm-dd
  137. if (preg_match('/^(\d{0,4}-\d{0,2}-\d{0,2})/', $time, $found)) {
  138. $time = $found[1];
  139. } else {
  140. // use smarty_make_timestamp to get an unix timestamp and
  141. // strftime to make yyyy-mm-dd
  142. $time = strftime('%Y-%m-%d', smarty_make_timestamp($time));
  143. }
  144. // Now split this in pieces, which later can be used to set the select
  145. $time = explode("-", $time);
  146. // make syntax "+N" or "-N" work with start_year and end_year
  147. if (preg_match('!^(\+|\-)\s*(\d+)$!', $end_year, $match)) {
  148. if ($match[1] == '+') {
  149. $end_year = strftime('%Y') + $match[2];
  150. } else {
  151. $end_year = strftime('%Y') - $match[2];
  152. }
  153. }
  154. if (preg_match('!^(\+|\-)\s*(\d+)$!', $start_year, $match)) {
  155. if ($match[1] == '+') {
  156. $start_year = strftime('%Y') + $match[2];
  157. } else {
  158. $start_year = strftime('%Y') - $match[2];
  159. }
  160. }
  161. if (strlen($time[0]) > 0) {
  162. if ($start_year > $time[0] && !isset($params['start_year'])) {
  163. // force start year to include given date if not explicitly set
  164. $start_year = $time[0];
  165. }
  166. if ($end_year < $time[0] && !isset($params['end_year'])) {
  167. // force end year to include given date if not explicitly set
  168. $end_year = $time[0];
  169. }
  170. }
  171. $field_order = strtoupper($field_order);
  172. $html_result = $month_result = $day_result = $year_result = "";
  173. $field_separator_count = -1;
  174. if ($display_months) {
  175. $field_separator_count++;
  176. $month_names = array();
  177. $month_values = array();
  178. if (isset($month_empty)) {
  179. $month_names[''] = $month_empty;
  180. $month_values[''] = '';
  181. }
  182. for ($i = 1; $i <= 12; $i++) {
  183. $month_names[$i] = strftime($month_format, mktime(0, 0, 0, $i, 1, 2000));
  184. $month_values[$i] = strftime($month_value_format, mktime(0, 0, 0, $i, 1, 2000));
  185. }
  186. $month_result .= '<select name=';
  187. if (null !== $field_array) {
  188. $month_result .= '"' . $field_array . '[' . $prefix . 'Month]"';
  189. } else {
  190. $month_result .= '"' . $prefix . 'Month"';
  191. }
  192. if (null !== $month_size) {
  193. $month_result .= ' size="' . $month_size . '"';
  194. }
  195. if (null !== $month_extra) {
  196. $month_result .= ' ' . $month_extra;
  197. }
  198. if (null !== $all_extra) {
  199. $month_result .= ' ' . $all_extra;
  200. }
  201. $month_result .= $extra_attrs . '>' . "\n";
  202. $month_result .= smarty_function_html_options(array('output' => $month_names,
  203. 'values' => $month_values,
  204. 'selected' => (int) $time[1] ? strftime($month_value_format, mktime(0, 0, 0, (int) $time[1], 1, 2000)) : '',
  205. 'print_result' => false),
  206. $template);
  207. $month_result .= '</select>';
  208. }
  209. if ($display_days) {
  210. $field_separator_count++;
  211. $days = array();
  212. if (isset($day_empty)) {
  213. $days[''] = $day_empty;
  214. $day_values[''] = '';
  215. }
  216. for ($i = 1; $i <= 31; $i++) {
  217. $days[] = sprintf($day_format, $i);
  218. $day_values[] = sprintf($day_value_format, $i);
  219. }
  220. $day_result .= '<select name=';
  221. if (null !== $field_array) {
  222. $day_result .= '"' . $field_array . '[' . $prefix . 'Day]"';
  223. } else {
  224. $day_result .= '"' . $prefix . 'Day"';
  225. }
  226. if (null !== $day_size) {
  227. $day_result .= ' size="' . $day_size . '"';
  228. }
  229. if (null !== $all_extra) {
  230. $day_result .= ' ' . $all_extra;
  231. }
  232. if (null !== $day_extra) {
  233. $day_result .= ' ' . $day_extra;
  234. }
  235. $day_result .= $extra_attrs . '>' . "\n";
  236. $day_result .= smarty_function_html_options(array('output' => $days,
  237. 'values' => $day_values,
  238. 'selected' => $time[2],
  239. 'print_result' => false),
  240. $template);
  241. $day_result .= '</select>';
  242. }
  243. if ($display_years) {
  244. $field_separator_count++;
  245. if (null !== $field_array) {
  246. $year_name = $field_array . '[' . $prefix . 'Year]';
  247. } else {
  248. $year_name = $prefix . 'Year';
  249. }
  250. if ($year_as_text) {
  251. $year_result .= '<input type="text" name="' . $year_name . '" value="' . $time[0] . '" size="4" maxlength="4"';
  252. if (null !== $all_extra) {
  253. $year_result .= ' ' . $all_extra;
  254. }
  255. if (null !== $year_extra) {
  256. $year_result .= ' ' . $year_extra;
  257. }
  258. $year_result .= ' />';
  259. } else {
  260. $years = range((int) $start_year, (int) $end_year);
  261. if ($reverse_years) {
  262. rsort($years, SORT_NUMERIC);
  263. } else {
  264. sort($years, SORT_NUMERIC);
  265. }
  266. $yearvals = $years;
  267. if (isset($year_empty)) {
  268. array_unshift($years, $year_empty);
  269. array_unshift($yearvals, '');
  270. }
  271. $year_result .= '<select name="' . $year_name . '"';
  272. if (null !== $year_size) {
  273. $year_result .= ' size="' . $year_size . '"';
  274. }
  275. if (null !== $all_extra) {
  276. $year_result .= ' ' . $all_extra;
  277. }
  278. if (null !== $year_extra) {
  279. $year_result .= ' ' . $year_extra;
  280. }
  281. $year_result .= $extra_attrs . '>' . "\n";
  282. $year_result .= smarty_function_html_options(array('output' => $years,
  283. 'values' => $yearvals,
  284. 'selected' => $time[0],
  285. 'print_result' => false),
  286. $template);
  287. $year_result .= '</select>';
  288. }
  289. }
  290. // Loop thru the field_order field
  291. for ($i = 0; $i <= 2; $i++) {
  292. $c = substr($field_order, $i, 1);
  293. switch ($c) {
  294. case 'D':
  295. $html_result .= $day_result;
  296. break;
  297. case 'M':
  298. $html_result .= $month_result;
  299. break;
  300. case 'Y':
  301. $html_result .= $year_result;
  302. break;
  303. }
  304. // Add the field seperator
  305. if ($i < $field_separator_count) {
  306. $html_result .= $field_separator;
  307. }
  308. }
  309. return $html_result;
  310. }