function.html_select_time.php 7.0 KB

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