PageRenderTime 25ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/aoliz/core/include/smartyplugins/function.html_select_date.php

http://phpfor.googlecode.com/
PHP | 269 lines | 208 code | 16 blank | 45 comment | 32 complexity | e9dd6ec9db6bafa56d579b00bbe43d6e MD5 | raw file
  1. <?php
  2. /*
  3. * Template Lite plugin
  4. * -------------------------------------------------------------
  5. * Type: function
  6. * Name: html_select_date
  7. * Version: 1.3
  8. * Purpose: Prints the dropdowns for date selection.
  9. * Author: Andrei Zmievski
  10. *
  11. * ChangeLog: 1.0 initial release
  12. * 1.1 added support for +/- N syntax for begin
  13. * and end year values. (Monte)
  14. * 1.2 added support for yyyy-mm-dd syntax for
  15. * time value. (Jan Rosier)
  16. * 1.3 added support for choosing format for
  17. * month values (Gary Loescher)
  18. * 1.3.1 added support for choosing format for
  19. * day values (Marcus Bointon)
  20. * Taken from the original Smarty
  21. * http://smarty.php.net
  22. * -------------------------------------------------------------
  23. */
  24. function tpl_function_html_select_date($params, &$template_object)
  25. {
  26. require_once("shared.make_timestamp.php");
  27. require_once("function.html_options.php");
  28. /* Default values. */
  29. $prefix = "Date_";
  30. $start_year = strftime("%Y");
  31. $end_year = $start_year;
  32. $display_days = true;
  33. $display_months = true;
  34. $display_years = true;
  35. $month_format = "%B";
  36. /* Write months as numbers by default GL */
  37. $month_value_format = "%m";
  38. $day_format = "%02d";
  39. /* Write day values using this format MB */
  40. $day_value_format = "%d";
  41. $year_as_text = false;
  42. /* Display years in reverse order? Ie. 2000,1999,.... */
  43. $reverse_years = false;
  44. /* Should the select boxes be part of an array when returned from PHP?
  45. e.g. setting it to "birthday", would create "birthday[Day]",
  46. "birthday[Month]" & "birthday[Year]". Can be combined with prefix */
  47. $field_array = null;
  48. /* <select size>'s of the different <select> tags.
  49. If not set, uses default dropdown. */
  50. $day_size = null;
  51. $month_size = null;
  52. $year_size = null;
  53. /* Unparsed attributes common to *ALL* the <select>/<input> tags.
  54. An example might be in the template: all_extra ='class ="foo"'. */
  55. $all_extra = null;
  56. /* Separate attributes for the tags. */
  57. $day_extra = null;
  58. $month_extra = null;
  59. $year_extra = null;
  60. /* Order in which to display the fields.
  61. "D" -> day, "M" -> month, "Y" -> year. */
  62. $field_order = 'MDY';
  63. /* String printed between the different fields. */
  64. $field_separator = "\n";
  65. $time = time();
  66. extract($params);
  67. // If $time is not in format yyyy-mm-dd
  68. if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $time))
  69. {
  70. // then $time is empty or unix timestamp or mysql timestamp
  71. // using tpl_make_timestamp to get an unix timestamp and
  72. // strftime to make yyyy-mm-dd
  73. $time = strftime('%Y-%m-%d', tpl_make_timestamp($time));
  74. }
  75. // Now split this in pieces, which later can be used to set the select
  76. $time = explode("-", $time);
  77. // make syntax "+N" or "-N" work with start_year and end_year
  78. if (preg_match('!^(\+|\-)\s*(\d+)$!', $end_year, $match))
  79. {
  80. if ($match[1] == '+')
  81. {
  82. $end_year = strftime('%Y') + $match[2];
  83. }
  84. else
  85. {
  86. $end_year = strftime('%Y') - $match[2];
  87. }
  88. }
  89. if (preg_match('!^(\+|\-)\s*(\d+)$!', $start_year, $match))
  90. {
  91. if ($match[1] == '+')
  92. {
  93. $start_year = strftime('%Y') + $match[2];
  94. }
  95. else
  96. {
  97. $start_year = strftime('%Y') - $match[2];
  98. }
  99. }
  100. $field_order = strtoupper($field_order);
  101. $html_result = $month_result = $day_result = $year_result = "";
  102. if ($display_months)
  103. {
  104. $month_names = array();
  105. $month_values = array();
  106. for ($i = 1; $i <= 12; $i++)
  107. {
  108. $month_names[] = strftime($month_format, mktime(0, 0, 0, $i, 1, 2000));
  109. $month_values[] = strftime($month_value_format, mktime(0, 0, 0, $i, 1, 2000));
  110. }
  111. $month_result .= '<select name=';
  112. if (null !== $field_array)
  113. {
  114. $month_result .= '"' . $field_array . '[' . $prefix . 'Month]"';
  115. }
  116. else
  117. {
  118. $month_result .= '"' . $prefix . 'Month"';
  119. }
  120. if (null !== $month_size)
  121. {
  122. $month_result .= ' size="' . $month_size . '"';
  123. }
  124. if (null !== $month_extra)
  125. {
  126. $month_result .= ' ' . $month_extra;
  127. }
  128. if (null !== $all_extra)
  129. {
  130. $month_result .= ' ' . $all_extra;
  131. }
  132. $month_result .= '>'."\n";
  133. $month_result .= tpl_function_html_options(array('output' => $month_names,
  134. 'values' => $month_values,
  135. 'selected' => $month_values[$time[1]-1],
  136. 'print_result' => false),
  137. $template_object);
  138. $month_result .= '</select>';
  139. }
  140. if ($display_days)
  141. {
  142. $days = array();
  143. for ($i = 1; $i <= 31; $i++)
  144. {
  145. $days[] = sprintf($day_format, $i);
  146. $day_values[] = sprintf($day_value_format, $i);
  147. }
  148. $day_result .= '<select name=';
  149. if (null !== $field_array)
  150. {
  151. $day_result .= '"' . $field_array . '[' . $prefix . 'Day]"';
  152. }
  153. else
  154. {
  155. $day_result .= '"' . $prefix . 'Day"';
  156. }
  157. if (null !== $day_size)
  158. {
  159. $day_result .= ' size="' . $day_size . '"';
  160. }
  161. if (null !== $all_extra)
  162. {
  163. $day_result .= ' ' . $all_extra;
  164. }
  165. if (null !== $day_extra)
  166. {
  167. $day_result .= ' ' . $day_extra;
  168. }
  169. $day_result .= '>'."\n";
  170. $day_result .= tpl_function_html_options(array('output' => $days,
  171. 'values' => $day_values,
  172. 'selected' => $time[2],
  173. 'print_result' => false),
  174. $template_object);
  175. $day_result .= '</select>';
  176. }
  177. if ($display_years)
  178. {
  179. if (null !== $field_array)
  180. {
  181. $year_name = $field_array . '[' . $prefix . 'Year]';
  182. }
  183. else
  184. {
  185. $year_name = $prefix . 'Year';
  186. }
  187. if ($year_as_text)
  188. {
  189. $year_result .= '<input type="text" name="' . $year_name . '" value="' . $time[0] . '" size="4" maxlength="4"';
  190. if (null !== $all_extra)
  191. {
  192. $year_result .= ' ' . $all_extra;
  193. }
  194. if (null !== $year_extra)
  195. {
  196. $year_result .= ' ' . $year_extra;
  197. }
  198. $year_result .= '>';
  199. }
  200. else
  201. {
  202. $years = range((int)$start_year, (int)$end_year);
  203. if ($reverse_years)
  204. {
  205. rsort($years, SORT_NUMERIC);
  206. }
  207. $year_result .= '<select name="' . $year_name . '"';
  208. if (null !== $year_size)
  209. {
  210. $year_result .= ' size="' . $year_size . '"';
  211. }
  212. if (null !== $all_extra)
  213. {
  214. $year_result .= ' ' . $all_extra;
  215. }
  216. if (null !== $year_extra)
  217. {
  218. $year_result .= ' ' . $year_extra;
  219. }
  220. $year_result .= '>'."\n";
  221. $year_result .= tpl_function_html_options(array('output' => $years,
  222. 'values' => $years,
  223. 'selected' => $time[0],
  224. 'print_result' => false),
  225. $template_object);
  226. $year_result .= '</select>';
  227. }
  228. }
  229. // Loop thru the field_order field
  230. for ($i = 0; $i <= 2; $i++)
  231. {
  232. $c = substr($field_order, $i, 1);
  233. switch ($c)
  234. {
  235. case 'D':
  236. $html_result .= $day_result;
  237. break;
  238. case 'M':
  239. $html_result .= $month_result;
  240. break;
  241. case 'Y':
  242. $html_result .= $year_result;
  243. break;
  244. }
  245. // Add the field seperator
  246. if($i != 2)
  247. {
  248. $html_result .= $field_separator;
  249. }
  250. }
  251. return $html_result;
  252. }
  253. ?>