function.html_radios.php 4.9 KB

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