PageRenderTime 76ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

/fo/classes/view/smarty/dist/plugins/function.html_select_date.php

https://github.com/openstate/HNS.dev
PHP | 374 lines | 289 code | 24 blank | 61 comment | 54 complexity | 97893d9161b25e15d8a415712e0a0f05 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. * - 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. $GLOBALS['strftimeNames'] = array(
  38. 'nl' => array(
  39. 'fullMonthNames' => array(1 => 'januari', 'februari', 'maart', 'april', 'mei', 'juni',
  40. 'juli', 'augustus', 'september', 'oktober', 'november', 'december'),
  41. 'shortMonthNames' => array(1 => 'jan', 'feb', 'maa', 'apr', 'mei', 'jun',
  42. 'jul', 'aug', 'sep', 'okt', 'nov', 'dec'),
  43. 'fullDayNames' => array('zondag', 'maandag', 'dinsdag', 'woensdag', 'donderdag', 'vrijdag', 'zaterdag'),
  44. 'shortDayNames' => array('zo', 'ma', 'di', 'wo', 'do', 'vr', 'za')
  45. ),
  46. 'en' => array(
  47. 'fullMonthNames' => array(1 => 'January', 'February', 'March', 'April', 'May', 'June',
  48. 'July', 'August', 'September', 'October', 'November', 'December'),
  49. 'shortMonthNames' => array(1 => 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
  50. 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'),
  51. 'fullDayNames' => array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'),
  52. 'shortDayNames' => array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat')
  53. ),
  54. 'de' => array(
  55. 'fullMonthNames' => array(1 => 'Januar', 'Februar', 'März', 'April', 'Mai', 'Juni',
  56. 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember'),
  57. 'shortMonthNames' => array(1 => 'Jan', 'Feb', 'Mär', 'Apr', 'Mai', 'Jun',
  58. 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dez'),
  59. 'fullDayNames' => array('Sonntag', 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag'),
  60. 'shortDayNames' => array('So', 'Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa')
  61. )
  62. );
  63. function smarty_function_html_select_date($params, &$smarty)
  64. {
  65. require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
  66. require_once $smarty->_get_plugin_filepath('shared','make_timestamp');
  67. require_once $smarty->_get_plugin_filepath('function','html_options');
  68. /* Default values. */
  69. $prefix = "Date_";
  70. $start_year = strftime("%Y");
  71. $end_year = $start_year;
  72. $display_days = true;
  73. $display_months = true;
  74. $display_years = true;
  75. $month_format = "%B";
  76. /* Write months as numbers by default GL */
  77. $month_value_format = "%m";
  78. $day_format = "%02d";
  79. /* Write day values using this format MB */
  80. $day_value_format = "%d";
  81. $year_as_text = false;
  82. /* Display years in reverse order? Ie. 2000,1999,.... */
  83. $reverse_years = false;
  84. /* Should the select boxes be part of an array when returned from PHP?
  85. e.g. setting it to "birthday", would create "birthday[Day]",
  86. "birthday[Month]" & "birthday[Year]". Can be combined with prefix */
  87. $field_array = null;
  88. /* <select size>'s of the different <select> tags.
  89. If not set, uses default dropdown. */
  90. $day_size = null;
  91. $month_size = null;
  92. $year_size = null;
  93. /* Unparsed attributes common to *ALL* the <select>/<input> tags.
  94. An example might be in the template: all_extra ='class ="foo"'. */
  95. $all_extra = null;
  96. /* Separate attributes for the tags. */
  97. $day_extra = null;
  98. $month_extra = null;
  99. $year_extra = null;
  100. /* Order in which to display the fields.
  101. "D" -> day, "M" -> month, "Y" -> year. */
  102. $field_order = 'MDY';
  103. /* String printed between the different fields. */
  104. $field_separator = "\n";
  105. $time = time();
  106. $all_empty = null;
  107. $day_empty = null;
  108. $month_empty = null;
  109. $year_empty = null;
  110. $extra_attrs = '';
  111. $language = $smarty instanceof CustomSmarty ? reset(explode('_', $smarty->locale)) : 'en';
  112. foreach ($params as $_key=>$_value) {
  113. switch ($_key) {
  114. case 'prefix':
  115. case 'time':
  116. case 'start_year':
  117. case 'end_year':
  118. case 'month_format':
  119. case 'day_format':
  120. case 'day_value_format':
  121. case 'field_array':
  122. case 'day_size':
  123. case 'month_size':
  124. case 'year_size':
  125. case 'all_extra':
  126. case 'day_extra':
  127. case 'month_extra':
  128. case 'year_extra':
  129. case 'field_order':
  130. case 'field_separator':
  131. case 'month_value_format':
  132. case 'month_empty':
  133. case 'day_empty':
  134. case 'year_empty':
  135. case 'language':
  136. $$_key = (string)$_value;
  137. break;
  138. case 'all_empty':
  139. $$_key = (string)$_value;
  140. $day_empty = $month_empty = $year_empty = $all_empty;
  141. break;
  142. case 'display_days':
  143. case 'display_months':
  144. case 'display_years':
  145. case 'year_as_text':
  146. case 'reverse_years':
  147. $$_key = (bool)$_value;
  148. break;
  149. default:
  150. if(!is_array($_value)) {
  151. $extra_attrs .= ' '.$_key.'="'.smarty_function_escape_special_chars($_value).'"';
  152. } else {
  153. $smarty->trigger_error("html_select_date: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
  154. }
  155. break;
  156. }
  157. }
  158. if (preg_match('!^-\d+$!', $time)) {
  159. // negative timestamp, use date()
  160. $time = date('Y-m-d', $time);
  161. }
  162. // If $time is not in format yyyy-mm-dd
  163. if (preg_match('/^(\d{0,4}-\d{0,2}-\d{0,2})/', $time, $found)) {
  164. $time = $found[1];
  165. } else {
  166. // use smarty_make_timestamp to get an unix timestamp and
  167. // strftime to make yyyy-mm-dd
  168. $time = strftime('%Y-%m-%d', smarty_make_timestamp($time));
  169. }
  170. // Now split this in pieces, which later can be used to set the select
  171. $time = explode("-", $time);
  172. // make syntax "+N" or "-N" work with start_year and end_year
  173. if (preg_match('!^(\+|\-)\s*(\d+)$!', $end_year, $match)) {
  174. if ($match[1] == '+') {
  175. $end_year = strftime('%Y') + $match[2];
  176. } else {
  177. $end_year = strftime('%Y') - $match[2];
  178. }
  179. }
  180. if (preg_match('!^(\+|\-)\s*(\d+)$!', $start_year, $match)) {
  181. if ($match[1] == '+') {
  182. $start_year = strftime('%Y') + $match[2];
  183. } else {
  184. $start_year = strftime('%Y') - $match[2];
  185. }
  186. }
  187. if (strlen($time[0]) > 0) {
  188. if ($start_year > $time[0] && !isset($params['start_year'])) {
  189. // force start year to include given date if not explicitly set
  190. $start_year = $time[0];
  191. }
  192. if($end_year < $time[0] && !isset($params['end_year'])) {
  193. // force end year to include given date if not explicitly set
  194. $end_year = $time[0];
  195. }
  196. }
  197. $field_order = strtoupper($field_order);
  198. $html_result = $month_result = $day_result = $year_result = "";
  199. $field_separator_count = -1;
  200. if ($display_months) {
  201. $field_separator_count++;
  202. $month_names = array();
  203. $month_values = array();
  204. if(isset($month_empty)) {
  205. $month_names[''] = $month_empty;
  206. $month_values[''] = '';
  207. }
  208. for ($i = 1; $i <= 12; $i++) {
  209. $_loc_from = array();
  210. $_loc_to = array();
  211. if (isset($GLOBALS['strftimeNames'][$language])) {
  212. if (strpos($month_format, '%B') !== false) {
  213. $_loc_from[] = '%B';
  214. $_loc_to[] = $GLOBALS['strftimeNames'][$language]['fullMonthNames'][$i];
  215. }
  216. if (strpos($month_format, '%b') !== false) {
  217. $_loc_from[] = '%b';
  218. $_loc_to[] = $GLOBALS['strftimeNames'][$language]['shortMonthNames'][$i];
  219. }
  220. }
  221. $month_names[$i] = strftime(str_replace($_loc_from, $_loc_to, $month_format), mktime(0, 0, 0, $i, 1, 2000));
  222. $month_values[$i] = strftime($month_value_format, mktime(0, 0, 0, $i, 1, 2000));
  223. }
  224. $month_result .= '<select name=';
  225. if (null !== $field_array){
  226. $month_result .= '"' . $field_array . '[' . $prefix . 'Month]"';
  227. } else {
  228. $month_result .= '"' . $prefix . 'Month"';
  229. }
  230. if (null !== $month_size){
  231. $month_result .= ' size="' . $month_size . '"';
  232. }
  233. if (null !== $month_extra){
  234. $month_result .= ' ' . $month_extra;
  235. }
  236. if (null !== $all_extra){
  237. $month_result .= ' ' . $all_extra;
  238. }
  239. $month_result .= $extra_attrs . '>'."\n";
  240. $month_result .= smarty_function_html_options(array('output' => $month_names,
  241. 'values' => $month_values,
  242. 'selected' => (int)$time[1] ? strftime($month_value_format, mktime(0, 0, 0, (int)$time[1], 1, 2000)) : '',
  243. 'print_result' => false),
  244. $smarty);
  245. $month_result .= '</select>';
  246. }
  247. if ($display_days) {
  248. $field_separator_count++;
  249. $days = array();
  250. if (isset($day_empty)) {
  251. $days[''] = $day_empty;
  252. $day_values[''] = '';
  253. }
  254. for ($i = 1; $i <= 31; $i++) {
  255. $days[] = sprintf($day_format, $i);
  256. $day_values[] = sprintf($day_value_format, $i);
  257. }
  258. $day_result .= '<select name=';
  259. if (null !== $field_array){
  260. $day_result .= '"' . $field_array . '[' . $prefix . 'Day]"';
  261. } else {
  262. $day_result .= '"' . $prefix . 'Day"';
  263. }
  264. if (null !== $day_size){
  265. $day_result .= ' size="' . $day_size . '"';
  266. }
  267. if (null !== $all_extra){
  268. $day_result .= ' ' . $all_extra;
  269. }
  270. if (null !== $day_extra){
  271. $day_result .= ' ' . $day_extra;
  272. }
  273. $day_result .= $extra_attrs . '>'."\n";
  274. $day_result .= smarty_function_html_options(array('output' => $days,
  275. 'values' => $day_values,
  276. 'selected' => $time[2],
  277. 'print_result' => false),
  278. $smarty);
  279. $day_result .= '</select>';
  280. }
  281. if ($display_years) {
  282. $field_separator_count++;
  283. if (null !== $field_array){
  284. $year_name = $field_array . '[' . $prefix . 'Year]';
  285. } else {
  286. $year_name = $prefix . 'Year';
  287. }
  288. if ($year_as_text) {
  289. $year_result .= '<input type="text" name="' . $year_name . '" value="' . $time[0] . '" size="4" maxlength="4"';
  290. if (null !== $all_extra){
  291. $year_result .= ' ' . $all_extra;
  292. }
  293. if (null !== $year_extra){
  294. $year_result .= ' ' . $year_extra;
  295. }
  296. $year_result .= ' />';
  297. } else {
  298. $years = range((int)$start_year, (int)$end_year);
  299. if ($reverse_years) {
  300. rsort($years, SORT_NUMERIC);
  301. } else {
  302. sort($years, SORT_NUMERIC);
  303. }
  304. $yearvals = $years;
  305. if(isset($year_empty)) {
  306. array_unshift($years, $year_empty);
  307. array_unshift($yearvals, '');
  308. }
  309. $year_result .= '<select name="' . $year_name . '"';
  310. if (null !== $year_size){
  311. $year_result .= ' size="' . $year_size . '"';
  312. }
  313. if (null !== $all_extra){
  314. $year_result .= ' ' . $all_extra;
  315. }
  316. if (null !== $year_extra){
  317. $year_result .= ' ' . $year_extra;
  318. }
  319. $year_result .= $extra_attrs . '>'."\n";
  320. $year_result .= smarty_function_html_options(array('output' => $years,
  321. 'values' => $yearvals,
  322. 'selected' => $time[0],
  323. 'print_result' => false),
  324. $smarty);
  325. $year_result .= '</select>';
  326. }
  327. }
  328. // Loop thru the field_order field
  329. for ($i = 0; $i <= 2; $i++){
  330. $c = substr($field_order, $i, 1);
  331. switch ($c){
  332. case 'D':
  333. $html_result .= $day_result;
  334. break;
  335. case 'M':
  336. $html_result .= $month_result;
  337. break;
  338. case 'Y':
  339. $html_result .= $year_result;
  340. break;
  341. }
  342. // Add the field seperator
  343. if($i < $field_separator_count) {
  344. $html_result .= $field_separator;
  345. }
  346. }
  347. return $html_result;
  348. }
  349. /* vim: set expandtab: */
  350. ?>