Automatically exported from code.google.com/p/planningalerts
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.
 
 
 
 
 
 

157 lines
4.7 KiB

  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 at ohrt dot com>
  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. $label_ids = false;
  49. $output = null;
  50. $extra = '';
  51. foreach($params as $_key => $_val) {
  52. switch($_key) {
  53. case 'name':
  54. case 'separator':
  55. $$_key = (string)$_val;
  56. break;
  57. case 'checked':
  58. case 'selected':
  59. if(is_array($_val)) {
  60. $smarty->trigger_error('html_radios: the "' . $_key . '" attribute cannot be an array', E_USER_WARNING);
  61. } else {
  62. $selected = (string)$_val;
  63. }
  64. break;
  65. case 'labels':
  66. case 'label_ids':
  67. $$_key = (bool)$_val;
  68. break;
  69. case 'options':
  70. $$_key = (array)$_val;
  71. break;
  72. case 'values':
  73. case 'output':
  74. $$_key = array_values((array)$_val);
  75. break;
  76. case 'radios':
  77. $smarty->trigger_error('html_radios: the use of the "radios" attribute is deprecated, use "options" instead', E_USER_WARNING);
  78. $options = (array)$_val;
  79. break;
  80. case 'assign':
  81. break;
  82. default:
  83. if(!is_array($_val)) {
  84. $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
  85. } else {
  86. $smarty->trigger_error("html_radios: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
  87. }
  88. break;
  89. }
  90. }
  91. if (!isset($options) && !isset($values))
  92. return ''; /* raise error here? */
  93. $_html_result = array();
  94. if (isset($options)) {
  95. foreach ($options as $_key=>$_val)
  96. $_html_result[] = smarty_function_html_radios_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids);
  97. } else {
  98. foreach ($values as $_i=>$_key) {
  99. $_val = isset($output[$_i]) ? $output[$_i] : '';
  100. $_html_result[] = smarty_function_html_radios_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids);
  101. }
  102. }
  103. if(!empty($params['assign'])) {
  104. $smarty->assign($params['assign'], $_html_result);
  105. } else {
  106. return implode("\n",$_html_result);
  107. }
  108. }
  109. function smarty_function_html_radios_output($name, $value, $output, $selected, $extra, $separator, $labels, $label_ids) {
  110. $_output = '';
  111. if ($labels) {
  112. if($label_ids) {
  113. $_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!', '_', $name . '_' . $value));
  114. $_output .= '<label for="' . $_id . '">';
  115. } else {
  116. $_output .= '<label>';
  117. }
  118. }
  119. $_output .= '<input type="radio" name="'
  120. . smarty_function_escape_special_chars($name) . '" value="'
  121. . smarty_function_escape_special_chars($value) . '"';
  122. if ($labels && $label_ids) $_output .= ' id="' . $_id . '"';
  123. if ((string)$value==$selected) {
  124. $_output .= ' checked="checked"';
  125. }
  126. $_output .= $extra . ' />' . $output;
  127. if ($labels) $_output .= '</label>';
  128. $_output .= $separator;
  129. return $_output;
  130. }
  131. ?>