/www/lib/smarty/plugins/function.html_select_time.php

https://github.com/kop1/newznab · PHP · 194 lines · 148 code · 19 blank · 27 comment · 25 complexity · dd0f96860d82d5b2e8868c2c6b72ce3c MD5 · raw file

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