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.

function.html_radios.php 6.9KB

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