PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/library/plugins/function.html_select_date.php

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