PageRenderTime 55ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/Smarty/libs/plugins/function.html_select_date.php

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