Automatically exported from code.google.com/p/planningalerts
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.
 
 
 
 
 
 

195 rivejä
7.1 KiB

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