/libs/plugins/function.html_radios.php

https://github.com/cabenitez/factuweb · PHP · 219 lines · 143 code · 32 blank · 44 comment · 33 complexity · b64a0be1261d0ed0c5c40f76323f5500 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. case 'strict': break;
  96. case 'disabled':
  97. case 'readonly':
  98. if (!empty($params['strict'])) {
  99. if (!is_scalar($_val)) {
  100. trigger_error("html_options: $_key attribute must be a scalar, only boolean true or string '$_key' will actually add the attribute", E_USER_NOTICE);
  101. }
  102. if ($_val === true || $_val === $_key) {
  103. $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_key) . '"';
  104. }
  105. break;
  106. }
  107. // omit break; to fall through!
  108. default:
  109. if (!is_array($_val)) {
  110. $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
  111. } else {
  112. trigger_error("html_radios: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
  113. }
  114. break;
  115. }
  116. }
  117. if (!isset($options) && !isset($values)) {
  118. /* raise error here? */
  119. return '';
  120. }
  121. $_html_result = array();
  122. if (isset($options)) {
  123. foreach ($options as $_key => $_val) {
  124. $_html_result[] = smarty_function_html_radios_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids, $escape);
  125. }
  126. } else {
  127. foreach ($values as $_i => $_key) {
  128. $_val = isset($output[$_i]) ? $output[$_i] : '';
  129. $_html_result[] = smarty_function_html_radios_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids, $escape);
  130. }
  131. }
  132. if (!empty($params['assign'])) {
  133. $template->assign($params['assign'], $_html_result);
  134. } else {
  135. return implode("\n", $_html_result);
  136. }
  137. }
  138. function smarty_function_html_radios_output($name, $value, $output, $selected, $extra, $separator, $labels, $label_ids, $escape)
  139. {
  140. $_output = '';
  141. if (is_object($value)) {
  142. if (method_exists($value, "__toString")) {
  143. $value = (string) $value->__toString();
  144. } else {
  145. trigger_error("html_options: value is an object of class '". get_class($value) ."' without __toString() method", E_USER_NOTICE);
  146. return '';
  147. }
  148. } else {
  149. $value = (string) $value;
  150. }
  151. if (is_object($output)) {
  152. if (method_exists($output, "__toString")) {
  153. $output = (string) $output->__toString();
  154. } else {
  155. trigger_error("html_options: output is an object of class '". get_class($output) ."' without __toString() method", E_USER_NOTICE);
  156. return '';
  157. }
  158. } else {
  159. $output = (string) $output;
  160. }
  161. if ($labels) {
  162. if ($label_ids) {
  163. $_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!' . Smarty::$_UTF8_MODIFIER, '_', $name . '_' . $value));
  164. $_output .= '<label for="' . $_id . '">';
  165. } else {
  166. $_output .= '<label>';
  167. }
  168. }
  169. $name = smarty_function_escape_special_chars($name);
  170. $value = smarty_function_escape_special_chars($value);
  171. if ($escape) {
  172. $output = smarty_function_escape_special_chars($output);
  173. }
  174. $_output .= '<input type="radio" name="' . $name . '" value="' . $value . '"';
  175. if ($labels && $label_ids) {
  176. $_output .= ' id="' . $_id . '"';
  177. }
  178. if ($value === $selected) {
  179. $_output .= ' checked="checked"';
  180. }
  181. $_output .= $extra . ' />' . $output;
  182. if ($labels) {
  183. $_output .= '</label>';
  184. }
  185. $_output .= $separator;
  186. return $_output;
  187. }