function.html_radios.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. if (!isset($template->smarty->_cache[ '_required_sesc' ])) {
  48. require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
  49. $template->smarty->_cache[ '_required_sesc' ] = true;
  50. }
  51. $name = 'radio';
  52. $values = null;
  53. $options = null;
  54. $selected = null;
  55. $separator = '';
  56. $escape = true;
  57. $labels = true;
  58. $label_ids = false;
  59. $output = null;
  60. $extra = '';
  61. foreach ($params as $_key => $_val) {
  62. switch ($_key) {
  63. case 'name':
  64. case 'separator':
  65. $$_key = (string) $_val;
  66. break;
  67. case 'checked':
  68. case 'selected':
  69. if (is_array($_val)) {
  70. trigger_error('html_radios: the "' . $_key . '" attribute cannot be an array', E_USER_WARNING);
  71. } elseif (is_object($_val)) {
  72. if (method_exists($_val, "__toString")) {
  73. $selected = smarty_function_escape_special_chars((string) $_val->__toString());
  74. } else {
  75. trigger_error("html_radios: selected attribute is an object of class '" . get_class($_val) .
  76. "' without __toString() method", E_USER_NOTICE);
  77. }
  78. } else {
  79. $selected = (string) $_val;
  80. }
  81. break;
  82. case 'escape':
  83. case 'labels':
  84. case 'label_ids':
  85. $$_key = (bool) $_val;
  86. break;
  87. case 'options':
  88. $$_key = (array) $_val;
  89. break;
  90. case 'values':
  91. case 'output':
  92. $$_key = array_values((array) $_val);
  93. break;
  94. case 'radios':
  95. trigger_error('html_radios: the use of the "radios" attribute is deprecated, use "options" instead',
  96. E_USER_WARNING);
  97. $options = (array) $_val;
  98. break;
  99. case 'assign':
  100. break;
  101. case 'strict':
  102. break;
  103. case 'disabled':
  104. case 'readonly':
  105. if (!empty($params[ 'strict' ])) {
  106. if (!is_scalar($_val)) {
  107. trigger_error("html_options: $_key attribute must be a scalar, only boolean true or string '$_key' will actually add the attribute",
  108. E_USER_NOTICE);
  109. }
  110. if ($_val === true || $_val === $_key) {
  111. $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_key) . '"';
  112. }
  113. break;
  114. }
  115. // omit break; to fall through!
  116. default:
  117. if (!is_array($_val)) {
  118. $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
  119. } else {
  120. trigger_error("html_radios: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
  121. }
  122. break;
  123. }
  124. }
  125. if (!isset($options) && !isset($values)) {
  126. /* raise error here? */
  127. return '';
  128. }
  129. $_html_result = array();
  130. if (isset($options)) {
  131. foreach ($options as $_key => $_val) {
  132. $_html_result[] =
  133. smarty_function_html_radios_output($name, $_key, $_val, $selected, $extra, $separator, $labels,
  134. $label_ids, $escape);
  135. }
  136. } else {
  137. foreach ($values as $_i => $_key) {
  138. $_val = isset($output[ $_i ]) ? $output[ $_i ] : '';
  139. $_html_result[] =
  140. smarty_function_html_radios_output($name, $_key, $_val, $selected, $extra, $separator, $labels,
  141. $label_ids, $escape);
  142. }
  143. }
  144. if (!empty($params[ 'assign' ])) {
  145. $template->assign($params[ 'assign' ], $_html_result);
  146. } else {
  147. return implode("\n", $_html_result);
  148. }
  149. }
  150. function smarty_function_html_radios_output($name, $value, $output, $selected, $extra, $separator, $labels, $label_ids,
  151. $escape)
  152. {
  153. $_output = '';
  154. if (is_object($value)) {
  155. if (method_exists($value, "__toString")) {
  156. $value = (string) $value->__toString();
  157. } else {
  158. trigger_error("html_options: value is an object of class '" . get_class($value) .
  159. "' without __toString() method", E_USER_NOTICE);
  160. return '';
  161. }
  162. } else {
  163. $value = (string) $value;
  164. }
  165. if (is_object($output)) {
  166. if (method_exists($output, "__toString")) {
  167. $output = (string) $output->__toString();
  168. } else {
  169. trigger_error("html_options: output is an object of class '" . get_class($output) .
  170. "' without __toString() method", E_USER_NOTICE);
  171. return '';
  172. }
  173. } else {
  174. $output = (string) $output;
  175. }
  176. if ($labels) {
  177. if ($label_ids) {
  178. $_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!' . Smarty::$_UTF8_MODIFIER, '_',
  179. $name . '_' . $value));
  180. $_output .= '<label for="' . $_id . '">';
  181. } else {
  182. $_output .= '<label>';
  183. }
  184. }
  185. $name = smarty_function_escape_special_chars($name);
  186. $value = smarty_function_escape_special_chars($value);
  187. if ($escape) {
  188. $output = smarty_function_escape_special_chars($output);
  189. }
  190. $_output .= '<input type="radio" name="' . $name . '" value="' . $value . '"';
  191. if ($labels && $label_ids) {
  192. $_output .= ' id="' . $_id . '"';
  193. }
  194. if ($value === $selected) {
  195. $_output .= ' checked="checked"';
  196. }
  197. $_output .= $extra . ' />' . $output;
  198. if ($labels) {
  199. $_output .= '</label>';
  200. }
  201. $_output .= $separator;
  202. return $_output;
  203. }