function.html_radios.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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
  11. * Type: function
  12. * Name: html_radios
  13. * Date: 24.Feb.2003
  14. * Purpose: Prints out a list of radio input types
  15. * Params:
  16. *
  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. *
  26. * Examples:
  27. *
  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. *
  32. * @link http://smarty.php.net/manual/en/language.function.html.radios.php {html_radios}
  33. * (Smarty online manual)
  34. * @author Christopher Kvarme <christopher.kvarme@flashjab.com>
  35. * @author credits to Monte Ohrt <monte at ohrt dot com>
  36. * @version 1.0
  37. *
  38. * @param array $params parameters
  39. * @param Smarty_Internal_Template $template template object
  40. *
  41. * @return string
  42. * @uses smarty_function_escape_special_chars()
  43. * @throws \SmartyException
  44. */
  45. function smarty_function_html_radios($params, Smarty_Internal_Template $template)
  46. {
  47. $template->_checkPlugins(
  48. array(
  49. array(
  50. 'function' => 'smarty_function_escape_special_chars',
  51. 'file' => SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php'
  52. )
  53. )
  54. );
  55. $name = 'radio';
  56. $values = null;
  57. $options = null;
  58. $selected = null;
  59. $separator = '';
  60. $escape = true;
  61. $labels = true;
  62. $label_ids = false;
  63. $output = null;
  64. $extra = '';
  65. foreach ($params as $_key => $_val) {
  66. switch ($_key) {
  67. case 'name':
  68. case 'separator':
  69. $$_key = (string)$_val;
  70. break;
  71. case 'checked':
  72. case 'selected':
  73. if (is_array($_val)) {
  74. trigger_error('html_radios: the "' . $_key . '" attribute cannot be an array', E_USER_WARNING);
  75. } elseif (is_object($_val)) {
  76. if (method_exists($_val, '__toString')) {
  77. $selected = smarty_function_escape_special_chars((string)$_val->__toString());
  78. } else {
  79. trigger_error(
  80. 'html_radios: selected attribute is an object of class \'' . get_class($_val) .
  81. '\' without __toString() method',
  82. E_USER_NOTICE
  83. );
  84. }
  85. } else {
  86. $selected = (string)$_val;
  87. }
  88. break;
  89. case 'escape':
  90. case 'labels':
  91. case 'label_ids':
  92. $$_key = (bool)$_val;
  93. break;
  94. case 'options':
  95. $$_key = (array)$_val;
  96. break;
  97. case 'values':
  98. case 'output':
  99. $$_key = array_values((array)$_val);
  100. break;
  101. case 'radios':
  102. trigger_error(
  103. 'html_radios: the use of the "radios" attribute is deprecated, use "options" instead',
  104. E_USER_WARNING
  105. );
  106. $options = (array)$_val;
  107. break;
  108. case 'assign':
  109. break;
  110. case 'strict':
  111. break;
  112. case 'disabled':
  113. case 'readonly':
  114. if (!empty($params[ 'strict' ])) {
  115. if (!is_scalar($_val)) {
  116. trigger_error(
  117. "html_options: {$_key} attribute must be a scalar, only boolean true or string '$_key' will actually add the attribute",
  118. E_USER_NOTICE
  119. );
  120. }
  121. if ($_val === true || $_val === $_key) {
  122. $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_key) . '"';
  123. }
  124. break;
  125. }
  126. // omit break; to fall through!
  127. // no break
  128. default:
  129. if (!is_array($_val)) {
  130. $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
  131. } else {
  132. trigger_error("html_radios: extra attribute '{$_key}' cannot be an array", E_USER_NOTICE);
  133. }
  134. break;
  135. }
  136. }
  137. if (!isset($options) && !isset($values)) {
  138. /* raise error here? */
  139. return '';
  140. }
  141. $_html_result = array();
  142. if (isset($options)) {
  143. foreach ($options as $_key => $_val) {
  144. $_html_result[] =
  145. smarty_function_html_radios_output(
  146. $name,
  147. $_key,
  148. $_val,
  149. $selected,
  150. $extra,
  151. $separator,
  152. $labels,
  153. $label_ids,
  154. $escape
  155. );
  156. }
  157. } else {
  158. foreach ($values as $_i => $_key) {
  159. $_val = isset($output[ $_i ]) ? $output[ $_i ] : '';
  160. $_html_result[] =
  161. smarty_function_html_radios_output(
  162. $name,
  163. $_key,
  164. $_val,
  165. $selected,
  166. $extra,
  167. $separator,
  168. $labels,
  169. $label_ids,
  170. $escape
  171. );
  172. }
  173. }
  174. if (!empty($params[ 'assign' ])) {
  175. $template->assign($params[ 'assign' ], $_html_result);
  176. } else {
  177. return implode("\n", $_html_result);
  178. }
  179. }
  180. /**
  181. * @param $name
  182. * @param $value
  183. * @param $output
  184. * @param $selected
  185. * @param $extra
  186. * @param $separator
  187. * @param $labels
  188. * @param $label_ids
  189. * @param $escape
  190. *
  191. * @return string
  192. */
  193. function smarty_function_html_radios_output(
  194. $name,
  195. $value,
  196. $output,
  197. $selected,
  198. $extra,
  199. $separator,
  200. $labels,
  201. $label_ids,
  202. $escape
  203. ) {
  204. $_output = '';
  205. if (is_object($value)) {
  206. if (method_exists($value, '__toString')) {
  207. $value = (string)$value->__toString();
  208. } else {
  209. trigger_error(
  210. 'html_options: value is an object of class \'' . get_class($value) .
  211. '\' without __toString() method',
  212. E_USER_NOTICE
  213. );
  214. return '';
  215. }
  216. } else {
  217. $value = (string)$value;
  218. }
  219. if (is_object($output)) {
  220. if (method_exists($output, '__toString')) {
  221. $output = (string)$output->__toString();
  222. } else {
  223. trigger_error(
  224. 'html_options: output is an object of class \'' . get_class($output) .
  225. '\' without __toString() method',
  226. E_USER_NOTICE
  227. );
  228. return '';
  229. }
  230. } else {
  231. $output = (string)$output;
  232. }
  233. if ($labels) {
  234. if ($label_ids) {
  235. $_id = smarty_function_escape_special_chars(
  236. preg_replace(
  237. '![^\w\-\.]!' . Smarty::$_UTF8_MODIFIER,
  238. '_',
  239. $name . '_' . $value
  240. )
  241. );
  242. $_output .= '<label for="' . $_id . '">';
  243. } else {
  244. $_output .= '<label>';
  245. }
  246. }
  247. $name = smarty_function_escape_special_chars($name);
  248. $value = smarty_function_escape_special_chars($value);
  249. if ($escape) {
  250. $output = smarty_function_escape_special_chars($output);
  251. }
  252. $_output .= '<input type="radio" name="' . $name . '" value="' . $value . '"';
  253. if ($labels && $label_ids) {
  254. $_output .= ' id="' . $_id . '"';
  255. }
  256. if ($value === $selected) {
  257. $_output .= ' checked="checked"';
  258. }
  259. $_output .= $extra . ' />' . $output;
  260. if ($labels) {
  261. $_output .= '</label>';
  262. }
  263. $_output .= $separator;
  264. return $_output;
  265. }