PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/engine/_lib/smarty/plugins/function.html_select_date.php

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