PostfixAdmin - web based virtual user administration interface for Postfix mail servers https://postfixadmin.github.io/postfixadmin/
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.

153 lines
4.8 KiB

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