PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/php/extlib/smarty/libs/plugins/function.html_radios.php

http://github.com/movabletype/movabletype
PHP | 232 lines | 155 code | 32 blank | 45 comment | 33 complexity | 4375dfa14280dc01d26b96bdfb278267 MD5 | raw file
Possible License(s): LGPL-2.1, MIT, BSD-3-Clause
  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) .
  73. "' without __toString() method", E_USER_NOTICE);
  74. }
  75. } else {
  76. $selected = (string) $_val;
  77. }
  78. break;
  79. case 'escape':
  80. case 'labels':
  81. case 'label_ids':
  82. $$_key = (bool) $_val;
  83. break;
  84. case 'options':
  85. $$_key = (array) $_val;
  86. break;
  87. case 'values':
  88. case 'output':
  89. $$_key = array_values((array) $_val);
  90. break;
  91. case 'radios':
  92. trigger_error('html_radios: the use of the "radios" attribute is deprecated, use "options" instead',
  93. E_USER_WARNING);
  94. $options = (array) $_val;
  95. break;
  96. case 'assign':
  97. break;
  98. case 'strict':
  99. break;
  100. case 'disabled':
  101. case 'readonly':
  102. if (!empty($params[ 'strict' ])) {
  103. if (!is_scalar($_val)) {
  104. trigger_error("html_options: $_key attribute must be a scalar, only boolean true or string '$_key' will actually add the attribute",
  105. E_USER_NOTICE);
  106. }
  107. if ($_val === true || $_val === $_key) {
  108. $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_key) . '"';
  109. }
  110. break;
  111. }
  112. // omit break; to fall through!
  113. default:
  114. if (!is_array($_val)) {
  115. $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
  116. } else {
  117. trigger_error("html_radios: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
  118. }
  119. break;
  120. }
  121. }
  122. if (!isset($options) && !isset($values)) {
  123. /* raise error here? */
  124. return '';
  125. }
  126. $_html_result = array();
  127. if (isset($options)) {
  128. foreach ($options as $_key => $_val) {
  129. $_html_result[] =
  130. smarty_function_html_radios_output($name, $_key, $_val, $selected, $extra, $separator, $labels,
  131. $label_ids, $escape);
  132. }
  133. } else {
  134. foreach ($values as $_i => $_key) {
  135. $_val = isset($output[ $_i ]) ? $output[ $_i ] : '';
  136. $_html_result[] =
  137. smarty_function_html_radios_output($name, $_key, $_val, $selected, $extra, $separator, $labels,
  138. $label_ids, $escape);
  139. }
  140. }
  141. if (!empty($params[ 'assign' ])) {
  142. $template->assign($params[ 'assign' ], $_html_result);
  143. } else {
  144. return implode("\n", $_html_result);
  145. }
  146. }
  147. function smarty_function_html_radios_output($name, $value, $output, $selected, $extra, $separator, $labels, $label_ids,
  148. $escape)
  149. {
  150. $_output = '';
  151. if (is_object($value)) {
  152. if (method_exists($value, "__toString")) {
  153. $value = (string) $value->__toString();
  154. } else {
  155. trigger_error("html_options: value is an object of class '" . get_class($value) .
  156. "' without __toString() method", E_USER_NOTICE);
  157. return '';
  158. }
  159. } else {
  160. $value = (string) $value;
  161. }
  162. if (is_object($output)) {
  163. if (method_exists($output, "__toString")) {
  164. $output = (string) $output->__toString();
  165. } else {
  166. trigger_error("html_options: output is an object of class '" . get_class($output) .
  167. "' without __toString() method", E_USER_NOTICE);
  168. return '';
  169. }
  170. } else {
  171. $output = (string) $output;
  172. }
  173. if ($labels) {
  174. if ($label_ids) {
  175. $_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!' . Smarty::$_UTF8_MODIFIER, '_',
  176. $name . '_' . $value));
  177. $_output .= '<label for="' . $_id . '">';
  178. } else {
  179. $_output .= '<label>';
  180. }
  181. }
  182. $name = smarty_function_escape_special_chars($name);
  183. $value = smarty_function_escape_special_chars($value);
  184. if ($escape) {
  185. $output = smarty_function_escape_special_chars($output);
  186. }
  187. $_output .= '<input type="radio" name="' . $name . '" value="' . $value . '"';
  188. if ($labels && $label_ids) {
  189. $_output .= ' id="' . $_id . '"';
  190. }
  191. if ($value === $selected) {
  192. $_output .= ' checked="checked"';
  193. }
  194. $_output .= $extra . ' />' . $output;
  195. if ($labels) {
  196. $_output .= '</label>';
  197. }
  198. $_output .= $separator;
  199. return $_output;
  200. }