function.html_select_date.php 11 KB

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