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 7.7KB

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