function.html_select_time.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * Smarty plugin
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsFunction
  7. */
  8. /**
  9. * Smarty {html_select_time} function plugin
  10. *
  11. * Type: function<br>
  12. * Name: html_select_time<br>
  13. * Purpose: Prints the dropdowns for time selection
  14. *
  15. * @link http://smarty.php.net/manual/en/language.function.html.select.time.php {html_select_time}
  16. (Smarty online manual)
  17. * @author Roberto Berto <roberto@berto.net>
  18. * @credits Monte Ohrt <monte AT ohrt DOT com>
  19. * @param array $params parameters
  20. * @param object $smarty Smarty object
  21. * @param object $template template object
  22. * @return string
  23. * @uses smarty_make_timestamp()
  24. */
  25. function smarty_function_html_select_time($params, $smarty, $template)
  26. {
  27. require_once(SMARTY_PLUGINS_DIR . 'shared.make_timestamp.php');
  28. require_once(SMARTY_PLUGINS_DIR . 'function.html_options.php');
  29. //$smarty->loadPlugin('Smarty_shared_make_timestamp');
  30. //$smarty->loadPlugin('Smarty_function_html_options');
  31. /* Default values. */
  32. $prefix = "Time_";
  33. $time = time();
  34. $display_hours = true;
  35. $display_minutes = true;
  36. $display_seconds = true;
  37. $display_meridian = true;
  38. $use_24_hours = true;
  39. $minute_interval = 1;
  40. $second_interval = 1;
  41. /* Should the select boxes be part of an array when returned from PHP?
  42. e.g. setting it to "birthday", would create "birthday[Hour]",
  43. "birthday[Minute]", "birthday[Seconds]" & "birthday[Meridian]".
  44. Can be combined with prefix. */
  45. $field_array = null;
  46. $all_extra = null;
  47. $hour_extra = null;
  48. $minute_extra = null;
  49. $second_extra = null;
  50. $meridian_extra = null;
  51. foreach ($params as $_key => $_value) {
  52. switch ($_key) {
  53. case 'prefix':
  54. case 'time':
  55. case 'field_array':
  56. case 'all_extra':
  57. case 'hour_extra':
  58. case 'minute_extra':
  59. case 'second_extra':
  60. case 'meridian_extra':
  61. $$_key = (string)$_value;
  62. break;
  63. case 'display_hours':
  64. case 'display_minutes':
  65. case 'display_seconds':
  66. case 'display_meridian':
  67. case 'use_24_hours':
  68. $$_key = (bool)$_value;
  69. break;
  70. case 'minute_interval':
  71. case 'second_interval':
  72. $$_key = (int)$_value;
  73. break;
  74. default:
  75. trigger_error("[html_select_time] unknown parameter $_key", E_USER_WARNING);
  76. }
  77. }
  78. $time = smarty_make_timestamp($time);
  79. $html_result = '';
  80. if ($display_hours) {
  81. $hours = $use_24_hours ? range(0, 23) : range(1, 12);
  82. $hour_fmt = $use_24_hours ? '%H' : '%I';
  83. for ($i = 0, $for_max = count($hours); $i < $for_max; $i++)
  84. $hours[$i] = sprintf('%02d', $hours[$i]);
  85. $html_result .= '<select name=';
  86. if (null !== $field_array) {
  87. $html_result .= '"' . $field_array . '[' . $prefix . 'Hour]"';
  88. } else {
  89. $html_result .= '"' . $prefix . 'Hour"';
  90. }
  91. if (null !== $hour_extra) {
  92. $html_result .= ' ' . $hour_extra;
  93. }
  94. if (null !== $all_extra) {
  95. $html_result .= ' ' . $all_extra;
  96. }
  97. $html_result .= '>' . "\n";
  98. $html_result .= smarty_function_html_options(array('output' => $hours,
  99. 'values' => $hours,
  100. 'selected' => strftime($hour_fmt, $time),
  101. 'print_result' => false),
  102. $smarty, $template);
  103. $html_result .= "</select>\n";
  104. }
  105. if ($display_minutes) {
  106. $all_minutes = range(0, 59);
  107. for ($i = 0, $for_max = count($all_minutes); $i < $for_max; $i += $minute_interval)
  108. $minutes[] = sprintf('%02d', $all_minutes[$i]);
  109. $selected = intval(floor(strftime('%M', $time) / $minute_interval) * $minute_interval);
  110. $html_result .= '<select name=';
  111. if (null !== $field_array) {
  112. $html_result .= '"' . $field_array . '[' . $prefix . 'Minute]"';
  113. } else {
  114. $html_result .= '"' . $prefix . 'Minute"';
  115. }
  116. if (null !== $minute_extra) {
  117. $html_result .= ' ' . $minute_extra;
  118. }
  119. if (null !== $all_extra) {
  120. $html_result .= ' ' . $all_extra;
  121. }
  122. $html_result .= '>' . "\n";
  123. $html_result .= smarty_function_html_options(array('output' => $minutes,
  124. 'values' => $minutes,
  125. 'selected' => $selected,
  126. 'print_result' => false),
  127. $smarty, $template);
  128. $html_result .= "</select>\n";
  129. }
  130. if ($display_seconds) {
  131. $all_seconds = range(0, 59);
  132. for ($i = 0, $for_max = count($all_seconds); $i < $for_max; $i += $second_interval)
  133. $seconds[] = sprintf('%02d', $all_seconds[$i]);
  134. $selected = intval(floor(strftime('%S', $time) / $second_interval) * $second_interval);
  135. $html_result .= '<select name=';
  136. if (null !== $field_array) {
  137. $html_result .= '"' . $field_array . '[' . $prefix . 'Second]"';
  138. } else {
  139. $html_result .= '"' . $prefix . 'Second"';
  140. }
  141. if (null !== $second_extra) {
  142. $html_result .= ' ' . $second_extra;
  143. }
  144. if (null !== $all_extra) {
  145. $html_result .= ' ' . $all_extra;
  146. }
  147. $html_result .= '>' . "\n";
  148. $html_result .= smarty_function_html_options(array('output' => $seconds,
  149. 'values' => $seconds,
  150. 'selected' => $selected,
  151. 'print_result' => false),
  152. $smarty, $template);
  153. $html_result .= "</select>\n";
  154. }
  155. if ($display_meridian && !$use_24_hours) {
  156. $html_result .= '<select name=';
  157. if (null !== $field_array) {
  158. $html_result .= '"' . $field_array . '[' . $prefix . 'Meridian]"';
  159. } else {
  160. $html_result .= '"' . $prefix . 'Meridian"';
  161. }
  162. if (null !== $meridian_extra) {
  163. $html_result .= ' ' . $meridian_extra;
  164. }
  165. if (null !== $all_extra) {
  166. $html_result .= ' ' . $all_extra;
  167. }
  168. $html_result .= '>' . "\n";
  169. $html_result .= smarty_function_html_options(array('output' => array('AM', 'PM'),
  170. 'values' => array('am', 'pm'),
  171. 'selected' => strtolower(strftime('%p', $time)),
  172. 'print_result' => false),
  173. $smarty, $template);
  174. $html_result .= "</select>\n";
  175. }
  176. return $html_result;
  177. }
  178. ?>