PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/smarty_tiki/function.html_select_date.php

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