PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/xianpipa/ThinkPHP/Library/Vendor/Smarty/plugins/function.html_radios.php

https://gitlab.com/fangfangchen/xianpipa
PHP | 200 lines | 132 code | 25 blank | 43 comment | 29 complexity | a0db7ad68cae9090860c894e59003a5f MD5 | raw file
  1. <?php
  2. /**
  3. * Smarty plugin
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsFunction
  7. */
  8. /**
  9. * Smarty {html_radios} function plugin
  10. *
  11. * File: function.html_radios.php<br>
  12. * Type: function<br>
  13. * Name: html_radios<br>
  14. * Date: 24.Feb.2003<br>
  15. * Purpose: Prints out a list of radio input types<br>
  16. * Params:
  17. * <pre>
  18. * - name (optional) - string default "radio"
  19. * - values (required) - array
  20. * - options (required) - associative array
  21. * - checked (optional) - array default not set
  22. * - separator (optional) - ie <br> or &nbsp;
  23. * - output (optional) - the output next to each radio button
  24. * - assign (optional) - assign the output as an array to this variable
  25. * - escape (optional) - escape the content (not value), defaults to true
  26. * </pre>
  27. * Examples:
  28. * <pre>
  29. * {html_radios values=$ids output=$names}
  30. * {html_radios values=$ids name='box' separator='<br>' output=$names}
  31. * {html_radios values=$ids checked=$checked separator='<br>' output=$names}
  32. * </pre>
  33. *
  34. * @link http://smarty.php.net/manual/en/language.function.html.radios.php {html_radios}
  35. * (Smarty online manual)
  36. * @author Christopher Kvarme <christopher.kvarme@flashjab.com>
  37. * @author credits to Monte Ohrt <monte at ohrt dot com>
  38. * @version 1.0
  39. * @param array $params parameters
  40. * @param Smarty_Internal_Template $template template object
  41. * @return string
  42. * @uses smarty_function_escape_special_chars()
  43. */
  44. function smarty_function_html_radios($params, $template)
  45. {
  46. require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
  47. $name = 'radio';
  48. $values = null;
  49. $options = null;
  50. $selected = null;
  51. $separator = '';
  52. $escape = true;
  53. $labels = true;
  54. $label_ids = false;
  55. $output = null;
  56. $extra = '';
  57. foreach($params as $_key => $_val) {
  58. switch ($_key) {
  59. case 'name':
  60. case 'separator':
  61. $$_key = (string) $_val;
  62. break;
  63. case 'checked':
  64. case 'selected':
  65. if (is_array($_val)) {
  66. trigger_error('html_radios: the "' . $_key . '" attribute cannot be an array', E_USER_WARNING);
  67. } elseif (is_object($_val)) {
  68. if (method_exists($_val, "__toString")) {
  69. $selected = smarty_function_escape_special_chars((string) $_val->__toString());
  70. } else {
  71. trigger_error("html_radios: selected attribute is an object of class '". get_class($_val) ."' without __toString() method", E_USER_NOTICE);
  72. }
  73. } else {
  74. $selected = (string) $_val;
  75. }
  76. break;
  77. case 'escape':
  78. case 'labels':
  79. case 'label_ids':
  80. $$_key = (bool) $_val;
  81. break;
  82. case 'options':
  83. $$_key = (array) $_val;
  84. break;
  85. case 'values':
  86. case 'output':
  87. $$_key = array_values((array) $_val);
  88. break;
  89. case 'radios':
  90. trigger_error('html_radios: the use of the "radios" attribute is deprecated, use "options" instead', E_USER_WARNING);
  91. $options = (array) $_val;
  92. break;
  93. case 'assign':
  94. break;
  95. default:
  96. if (!is_array($_val)) {
  97. $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
  98. } else {
  99. trigger_error("html_radios: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
  100. }
  101. break;
  102. }
  103. }
  104. if (!isset($options) && !isset($values)) {
  105. /* raise error here? */
  106. return '';
  107. }
  108. $_html_result = array();
  109. if (isset($options)) {
  110. foreach ($options as $_key => $_val) {
  111. $_html_result[] = smarty_function_html_radios_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids, $escape);
  112. }
  113. } else {
  114. foreach ($values as $_i => $_key) {
  115. $_val = isset($output[$_i]) ? $output[$_i] : '';
  116. $_html_result[] = smarty_function_html_radios_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids, $escape);
  117. }
  118. }
  119. if (!empty($params['assign'])) {
  120. $template->assign($params['assign'], $_html_result);
  121. } else {
  122. return implode("\n", $_html_result);
  123. }
  124. }
  125. function smarty_function_html_radios_output($name, $value, $output, $selected, $extra, $separator, $labels, $label_ids, $escape)
  126. {
  127. $_output = '';
  128. if (is_object($value)) {
  129. if (method_exists($value, "__toString")) {
  130. $value = (string) $value->__toString();
  131. } else {
  132. trigger_error("html_options: value is an object of class '". get_class($value) ."' without __toString() method", E_USER_NOTICE);
  133. return '';
  134. }
  135. } else {
  136. $value = (string) $value;
  137. }
  138. if (is_object($output)) {
  139. if (method_exists($output, "__toString")) {
  140. $output = (string) $output->__toString();
  141. } else {
  142. trigger_error("html_options: output is an object of class '". get_class($output) ."' without __toString() method", E_USER_NOTICE);
  143. return '';
  144. }
  145. } else {
  146. $output = (string) $output;
  147. }
  148. if ($labels) {
  149. if ($label_ids) {
  150. $_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!u', '_', $name . '_' . $value));
  151. $_output .= '<label for="' . $_id . '">';
  152. } else {
  153. $_output .= '<label>';
  154. }
  155. }
  156. $name = smarty_function_escape_special_chars($name);
  157. $value = smarty_function_escape_special_chars($value);
  158. if ($escape) {
  159. $output = smarty_function_escape_special_chars($output);
  160. }
  161. $_output .= '<input type="radio" name="' . $name . '" value="' . $value . '"';
  162. if ($labels && $label_ids) {
  163. $_output .= ' id="' . $_id . '"';
  164. }
  165. if ($value === $selected) {
  166. $_output .= ' checked="checked"';
  167. }
  168. $_output .= $extra . ' />' . $output;
  169. if ($labels) {
  170. $_output .= '</label>';
  171. }
  172. $_output .= $separator;
  173. return $_output;
  174. }
  175. ?>