PageRenderTime 61ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/smarty/libs/plugins/function.html_select_date.php

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