function.html_radios.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * Smarty plugin
  4. * @package Smarty
  5. * @subpackage plugins
  6. */
  7. /**
  8. * Smarty {html_radios} function plugin
  9. *
  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. * Input:<br>
  16. * - name (optional) - string default "radio"
  17. * - values (required) - array
  18. * - options (optional) - associative array
  19. * - checked (optional) - array default not set
  20. * - separator (optional) - ie <br> or &nbsp;
  21. * - output (optional) - the output next to each radio button
  22. * - assign (optional) - assign the output as an array to this variable
  23. * Examples:
  24. * <pre>
  25. * {html_radios values=$ids output=$names}
  26. * {html_radios values=$ids name='box' separator='<br>' output=$names}
  27. * {html_radios values=$ids checked=$checked separator='<br>' output=$names}
  28. * </pre>
  29. * @link http://smarty.php.net/manual/en/language.function.html.radios.php {html_radios}
  30. * (Smarty online manual)
  31. * @author Christopher Kvarme <christopher.kvarme@flashjab.com>
  32. * @author credits to Monte Ohrt <monte@ispi.net>
  33. * @version 1.0
  34. * @param array
  35. * @param Smarty
  36. * @return string
  37. * @uses smarty_function_escape_special_chars()
  38. */
  39. function smarty_function_html_radios($params, &$smarty)
  40. {
  41. require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
  42. $name = 'radio';
  43. $values = null;
  44. $options = null;
  45. $selected = null;
  46. $separator = '';
  47. $labels = true;
  48. $output = null;
  49. $extra = '';
  50. foreach($params as $_key => $_val) {
  51. switch($_key) {
  52. case 'name':
  53. case 'separator':
  54. $$_key = (string)$_val;
  55. break;
  56. case 'checked':
  57. case 'selected':
  58. if(is_array($_val)) {
  59. $smarty->trigger_error('html_radios: the "' . $_key . '" attribute cannot be an array', E_USER_WARNING);
  60. } else {
  61. $selected = (string)$_val;
  62. }
  63. break;
  64. case 'labels':
  65. $$_key = (bool)$_val;
  66. break;
  67. case 'options':
  68. $$_key = (array)$_val;
  69. break;
  70. case 'values':
  71. case 'output':
  72. $$_key = array_values((array)$_val);
  73. break;
  74. case 'radios':
  75. $smarty->trigger_error('html_radios: the use of the "radios" attribute is deprecated, use "options" instead', E_USER_WARNING);
  76. $options = (array)$_val;
  77. break;
  78. case 'assign':
  79. break;
  80. default:
  81. if(!is_array($_val)) {
  82. $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
  83. } else {
  84. $smarty->trigger_error("html_radios: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
  85. }
  86. break;
  87. }
  88. }
  89. if (!isset($options) && !isset($values))
  90. return ''; /* raise error here? */
  91. $_html_result = array();
  92. if (isset($options) && is_array($options)) {
  93. foreach ((array)$options as $_key=>$_val)
  94. $_html_result[] = smarty_function_html_radios_output($name, $_key, $_val, $selected, $extra, $separator, $labels);
  95. } else {
  96. foreach ((array)$values as $_i=>$_key) {
  97. $_val = isset($output[$_i]) ? $output[$_i] : '';
  98. $_html_result[] = smarty_function_html_radios_output($name, $_key, $_val, $selected, $extra, $separator, $labels);
  99. }
  100. }
  101. if(!empty($params['assign'])) {
  102. $smarty->assign($params['assign'], $_html_result);
  103. } else {
  104. return implode("\n",$_html_result);
  105. }
  106. }
  107. function smarty_function_html_radios_output($name, $value, $output, $selected, $extra, $separator, $labels) {
  108. $_output = '';
  109. if ($labels) $_output .= '<label>';
  110. $_output .= '<input type="radio" name="'
  111. . smarty_function_escape_special_chars($name) . '" value="'
  112. . smarty_function_escape_special_chars($value) . '"';
  113. if ($value==$selected) {
  114. $_output .= ' checked="checked"';
  115. }
  116. $_output .= $extra . ' />' . $output;
  117. if ($labels) $_output .= '</label>';
  118. $_output .= $separator;
  119. return $_output;
  120. }
  121. ?>