You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

function.html_select_time.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <?php
  2. /**
  3. * Smarty plugin
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsFunction
  7. */
  8. /**
  9. * @ignore
  10. */
  11. require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
  12. /**
  13. * @ignore
  14. */
  15. require_once(SMARTY_PLUGINS_DIR . 'shared.make_timestamp.php');
  16. /**
  17. * Smarty {html_select_time} function plugin
  18. * Type: function<br>
  19. * Name: html_select_time<br>
  20. * Purpose: Prints the dropdowns for time selection
  21. *
  22. * @link http://www.smarty.net/manual/en/language.function.html.select.time.php {html_select_time}
  23. * (Smarty online manual)
  24. * @author Roberto Berto <roberto@berto.net>
  25. * @author Monte Ohrt <monte AT ohrt DOT com>
  26. *
  27. * @param array $params parameters
  28. *
  29. * @return string
  30. * @uses smarty_make_timestamp()
  31. */
  32. function smarty_function_html_select_time($params)
  33. {
  34. $prefix = "Time_";
  35. $field_array = null;
  36. $field_separator = "\n";
  37. $option_separator = "\n";
  38. $time = null;
  39. $display_hours = true;
  40. $display_minutes = true;
  41. $display_seconds = true;
  42. $display_meridian = true;
  43. $hour_format = '%02d';
  44. $hour_value_format = '%02d';
  45. $minute_format = '%02d';
  46. $minute_value_format = '%02d';
  47. $second_format = '%02d';
  48. $second_value_format = '%02d';
  49. $hour_size = null;
  50. $minute_size = null;
  51. $second_size = null;
  52. $meridian_size = null;
  53. $all_empty = null;
  54. $hour_empty = null;
  55. $minute_empty = null;
  56. $second_empty = null;
  57. $meridian_empty = null;
  58. $all_id = null;
  59. $hour_id = null;
  60. $minute_id = null;
  61. $second_id = null;
  62. $meridian_id = null;
  63. $use_24_hours = true;
  64. $minute_interval = 1;
  65. $second_interval = 1;
  66. $extra_attrs = '';
  67. $all_extra = null;
  68. $hour_extra = null;
  69. $minute_extra = null;
  70. $second_extra = null;
  71. $meridian_extra = null;
  72. foreach ($params as $_key => $_value) {
  73. switch ($_key) {
  74. case 'time':
  75. if (!is_array($_value) && $_value !== null) {
  76. $time = smarty_make_timestamp($_value);
  77. }
  78. break;
  79. case 'prefix':
  80. case 'field_array':
  81. case 'field_separator':
  82. case 'option_separator':
  83. case 'all_extra':
  84. case 'hour_extra':
  85. case 'minute_extra':
  86. case 'second_extra':
  87. case 'meridian_extra':
  88. case 'all_empty':
  89. case 'hour_empty':
  90. case 'minute_empty':
  91. case 'second_empty':
  92. case 'meridian_empty':
  93. case 'all_id':
  94. case 'hour_id':
  95. case 'minute_id':
  96. case 'second_id':
  97. case 'meridian_id':
  98. case 'hour_format':
  99. case 'hour_value_format':
  100. case 'minute_format':
  101. case 'minute_value_format':
  102. case 'second_format':
  103. case 'second_value_format':
  104. $$_key = (string) $_value;
  105. break;
  106. case 'display_hours':
  107. case 'display_minutes':
  108. case 'display_seconds':
  109. case 'display_meridian':
  110. case 'use_24_hours':
  111. $$_key = (bool) $_value;
  112. break;
  113. case 'minute_interval':
  114. case 'second_interval':
  115. case 'hour_size':
  116. case 'minute_size':
  117. case 'second_size':
  118. case 'meridian_size':
  119. $$_key = (int) $_value;
  120. break;
  121. default:
  122. if (!is_array($_value)) {
  123. $extra_attrs .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_value) . '"';
  124. } else {
  125. trigger_error("html_select_date: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
  126. }
  127. break;
  128. }
  129. }
  130. if (isset($params['time']) && is_array($params['time'])) {
  131. if (isset($params['time'][$prefix . 'Hour'])) {
  132. // $_REQUEST[$field_array] given
  133. foreach (array('H' => 'Hour', 'i' => 'Minute', 's' => 'Second') as $_elementKey => $_elementName) {
  134. $_variableName = '_' . strtolower($_elementName);
  135. $$_variableName = isset($params['time'][$prefix . $_elementName])
  136. ? $params['time'][$prefix . $_elementName]
  137. : date($_elementKey);
  138. }
  139. $_meridian = isset($params['time'][$prefix . 'Meridian'])
  140. ? (' ' . $params['time'][$prefix . 'Meridian'])
  141. : '';
  142. $time = strtotime($_hour . ':' . $_minute . ':' . $_second . $_meridian);
  143. list($_hour, $_minute, $_second) = $time = explode('-', date('H-i-s', $time));
  144. } elseif (isset($params['time'][$field_array][$prefix . 'Hour'])) {
  145. // $_REQUEST given
  146. foreach (array('H' => 'Hour', 'i' => 'Minute', 's' => 'Second') as $_elementKey => $_elementName) {
  147. $_variableName = '_' . strtolower($_elementName);
  148. $$_variableName = isset($params['time'][$field_array][$prefix . $_elementName])
  149. ? $params['time'][$field_array][$prefix . $_elementName]
  150. : date($_elementKey);
  151. }
  152. $_meridian = isset($params['time'][$field_array][$prefix . 'Meridian'])
  153. ? (' ' . $params['time'][$field_array][$prefix . 'Meridian'])
  154. : '';
  155. $time = strtotime($_hour . ':' . $_minute . ':' . $_second . $_meridian);
  156. list($_hour, $_minute, $_second) = $time = explode('-', date('H-i-s', $time));
  157. } else {
  158. // no date found, use NOW
  159. list($_year, $_month, $_day) = $time = explode('-', date('Y-m-d'));
  160. }
  161. } elseif ($time === null) {
  162. if (array_key_exists('time', $params)) {
  163. $_hour = $_minute = $_second = $time = null;
  164. } else {
  165. list($_hour, $_minute, $_second) = $time = explode('-', date('H-i-s'));
  166. }
  167. } else {
  168. list($_hour, $_minute, $_second) = $time = explode('-', date('H-i-s', $time));
  169. }
  170. // generate hour <select>
  171. if ($display_hours) {
  172. $_html_hours = '';
  173. $_extra = '';
  174. $_name = $field_array ? ($field_array . '[' . $prefix . 'Hour]') : ($prefix . 'Hour');
  175. if ($all_extra) {
  176. $_extra .= ' ' . $all_extra;
  177. }
  178. if ($hour_extra) {
  179. $_extra .= ' ' . $hour_extra;
  180. }
  181. $_html_hours = '<select name="' . $_name . '"';
  182. if ($hour_id !== null || $all_id !== null) {
  183. $_html_hours .= ' id="' . smarty_function_escape_special_chars(
  184. $hour_id !== null ? ($hour_id ? $hour_id : $_name) : ($all_id ? ($all_id . $_name) : $_name)
  185. ) . '"';
  186. }
  187. if ($hour_size) {
  188. $_html_hours .= ' size="' . $hour_size . '"';
  189. }
  190. $_html_hours .= $_extra . $extra_attrs . '>' . $option_separator;
  191. if (isset($hour_empty) || isset($all_empty)) {
  192. $_html_hours .= '<option value="">' . (isset($hour_empty) ? $hour_empty : $all_empty) . '</option>' . $option_separator;
  193. }
  194. $start = $use_24_hours ? 0 : 1;
  195. $end = $use_24_hours ? 23 : 12;
  196. for ($i = $start; $i <= $end; $i ++) {
  197. $_val = sprintf('%02d', $i);
  198. $_text = $hour_format == '%02d' ? $_val : sprintf($hour_format, $i);
  199. $_value = $hour_value_format == '%02d' ? $_val : sprintf($hour_value_format, $i);
  200. if (!$use_24_hours) {
  201. $_hour12 = $_hour == 0
  202. ? 12
  203. : ($_hour <= 12 ? $_hour : $_hour - 12);
  204. }
  205. $selected = $_hour !== null ? ($use_24_hours ? $_hour == $_val : $_hour12 == $_val) : null;
  206. $_html_hours .= '<option value="' . $_value . '"'
  207. . ($selected ? ' selected="selected"' : '')
  208. . '>' . $_text . '</option>' . $option_separator;
  209. }
  210. $_html_hours .= '</select>';
  211. }
  212. // generate minute <select>
  213. if ($display_minutes) {
  214. $_html_minutes = '';
  215. $_extra = '';
  216. $_name = $field_array ? ($field_array . '[' . $prefix . 'Minute]') : ($prefix . 'Minute');
  217. if ($all_extra) {
  218. $_extra .= ' ' . $all_extra;
  219. }
  220. if ($minute_extra) {
  221. $_extra .= ' ' . $minute_extra;
  222. }
  223. $_html_minutes = '<select name="' . $_name . '"';
  224. if ($minute_id !== null || $all_id !== null) {
  225. $_html_minutes .= ' id="' . smarty_function_escape_special_chars(
  226. $minute_id !== null ? ($minute_id ? $minute_id : $_name) : ($all_id ? ($all_id . $_name) : $_name)
  227. ) . '"';
  228. }
  229. if ($minute_size) {
  230. $_html_minutes .= ' size="' . $minute_size . '"';
  231. }
  232. $_html_minutes .= $_extra . $extra_attrs . '>' . $option_separator;
  233. if (isset($minute_empty) || isset($all_empty)) {
  234. $_html_minutes .= '<option value="">' . (isset($minute_empty) ? $minute_empty : $all_empty) . '</option>' . $option_separator;
  235. }
  236. $selected = $_minute !== null ? ($_minute - $_minute % $minute_interval) : null;
  237. for ($i = 0; $i <= 59; $i += $minute_interval) {
  238. $_val = sprintf('%02d', $i);
  239. $_text = $minute_format == '%02d' ? $_val : sprintf($minute_format, $i);
  240. $_value = $minute_value_format == '%02d' ? $_val : sprintf($minute_value_format, $i);
  241. $_html_minutes .= '<option value="' . $_value . '"'
  242. . ($selected === $i ? ' selected="selected"' : '')
  243. . '>' . $_text . '</option>' . $option_separator;
  244. }
  245. $_html_minutes .= '</select>';
  246. }
  247. // generate second <select>
  248. if ($display_seconds) {
  249. $_html_seconds = '';
  250. $_extra = '';
  251. $_name = $field_array ? ($field_array . '[' . $prefix . 'Second]') : ($prefix . 'Second');
  252. if ($all_extra) {
  253. $_extra .= ' ' . $all_extra;
  254. }
  255. if ($second_extra) {
  256. $_extra .= ' ' . $second_extra;
  257. }
  258. $_html_seconds = '<select name="' . $_name . '"';
  259. if ($second_id !== null || $all_id !== null) {
  260. $_html_seconds .= ' id="' . smarty_function_escape_special_chars(
  261. $second_id !== null ? ($second_id ? $second_id : $_name) : ($all_id ? ($all_id . $_name) : $_name)
  262. ) . '"';
  263. }
  264. if ($second_size) {
  265. $_html_seconds .= ' size="' . $second_size . '"';
  266. }
  267. $_html_seconds .= $_extra . $extra_attrs . '>' . $option_separator;
  268. if (isset($second_empty) || isset($all_empty)) {
  269. $_html_seconds .= '<option value="">' . (isset($second_empty) ? $second_empty : $all_empty) . '</option>' . $option_separator;
  270. }
  271. $selected = $_second !== null ? ($_second - $_second % $second_interval) : null;
  272. for ($i = 0; $i <= 59; $i += $second_interval) {
  273. $_val = sprintf('%02d', $i);
  274. $_text = $second_format == '%02d' ? $_val : sprintf($second_format, $i);
  275. $_value = $second_value_format == '%02d' ? $_val : sprintf($second_value_format, $i);
  276. $_html_seconds .= '<option value="' . $_value . '"'
  277. . ($selected === $i ? ' selected="selected"' : '')
  278. . '>' . $_text . '</option>' . $option_separator;
  279. }
  280. $_html_seconds .= '</select>';
  281. }
  282. // generate meridian <select>
  283. if ($display_meridian && !$use_24_hours) {
  284. $_html_meridian = '';
  285. $_extra = '';
  286. $_name = $field_array ? ($field_array . '[' . $prefix . 'Meridian]') : ($prefix . 'Meridian');
  287. if ($all_extra) {
  288. $_extra .= ' ' . $all_extra;
  289. }
  290. if ($meridian_extra) {
  291. $_extra .= ' ' . $meridian_extra;
  292. }
  293. $_html_meridian = '<select name="' . $_name . '"';
  294. if ($meridian_id !== null || $all_id !== null) {
  295. $_html_meridian .= ' id="' . smarty_function_escape_special_chars(
  296. $meridian_id !== null ? ($meridian_id ? $meridian_id : $_name) : ($all_id ? ($all_id . $_name) : $_name)
  297. ) . '"';
  298. }
  299. if ($meridian_size) {
  300. $_html_meridian .= ' size="' . $meridian_size . '"';
  301. }
  302. $_html_meridian .= $_extra . $extra_attrs . '>' . $option_separator;
  303. if (isset($meridian_empty) || isset($all_empty)) {
  304. $_html_meridian .= '<option value="">' . (isset($meridian_empty) ? $meridian_empty : $all_empty) . '</option>' . $option_separator;
  305. }
  306. $_html_meridian .= '<option value="am"' . ($_hour > 0 && $_hour < 12 ? ' selected="selected"' : '') . '>AM</option>' . $option_separator
  307. . '<option value="pm"' . ($_hour < 12 ? '' : ' selected="selected"') . '>PM</option>' . $option_separator
  308. . '</select>';
  309. }
  310. $_html = '';
  311. foreach (array('_html_hours', '_html_minutes', '_html_seconds', '_html_meridian') as $k) {
  312. if (isset($$k)) {
  313. if ($_html) {
  314. $_html .= $field_separator;
  315. }
  316. $_html .= $$k;
  317. }
  318. }
  319. return $_html;
  320. }