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.

132 lines
4.2 KiB

  1. <?php
  2. /**
  3. * Smarty plugin
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsFunction
  7. */
  8. /**
  9. * Smarty {html_options} function plugin
  10. *
  11. * Type: function<br>
  12. * Name: html_options<br>
  13. * Purpose: Prints the list of <option> tags generated from
  14. * the passed parameters
  15. *
  16. * @link http://smarty.php.net/manual/en/language.function.html.options.php {html_image}
  17. * (Smarty online manual)
  18. * @author Monte Ohrt <monte at ohrt dot com>
  19. * @param array $params parameters
  20. * Input:<br>
  21. * - name (optional) - string default "select"
  22. * - values (required if no options supplied) - array
  23. * - options (required if no values supplied) - associative array
  24. * - selected (optional) - string default not set
  25. * - output (required if not options supplied) - array
  26. * @param object $template template object
  27. * @return string
  28. * @uses smarty_function_escape_special_chars()
  29. */
  30. function smarty_function_html_options($params, $template)
  31. {
  32. require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
  33. $name = null;
  34. $values = null;
  35. $options = null;
  36. $selected = array();
  37. $output = null;
  38. $id = null;
  39. $class = null;
  40. $extra = '';
  41. $options_extra = '';
  42. foreach($params as $_key => $_val) {
  43. switch ($_key) {
  44. case 'name':
  45. case 'class':
  46. case 'id':
  47. $$_key = (string)$_val;
  48. break;
  49. case 'options':
  50. $$_key = (array)$_val;
  51. break;
  52. case 'values':
  53. case 'output':
  54. $$_key = array_values((array)$_val);
  55. break;
  56. case 'selected':
  57. $$_key = array_map('strval', array_values((array)$_val));
  58. break;
  59. default:
  60. if (!is_array($_val)) {
  61. $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
  62. } else {
  63. trigger_error("html_options: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
  64. }
  65. break;
  66. }
  67. }
  68. if (!isset($options) && !isset($values))
  69. return '';
  70. /* raise error here? */
  71. $_html_result = '';
  72. $_idx = 0;
  73. if (isset($options)) {
  74. foreach ($options as $_key => $_val) {
  75. $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected, $id, $class, $_idx);
  76. }
  77. } else {
  78. foreach ($values as $_i => $_key) {
  79. $_val = isset($output[$_i]) ? $output[$_i] : '';
  80. $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected, $id, $class, $_idx);
  81. }
  82. }
  83. if (!empty($name)) {
  84. $_html_class = !empty($class) ? ' class="'.$class.'"' : '';
  85. $_html_id = !empty($id) ? ' id="'.$id.'"' : '';
  86. $_html_result = '<select name="' . $name . '"' . $_html_class . $_html_id . $extra . '>' . "\n" . $_html_result . '</select>' . "\n";
  87. }
  88. return $_html_result;
  89. }
  90. function smarty_function_html_options_optoutput($key, $value, $selected, $id, $class, &$idx)
  91. {
  92. if (!is_array($value)) {
  93. $_html_result = '<option value="' .
  94. smarty_function_escape_special_chars($key) . '"';
  95. if (in_array((string)$key, $selected))
  96. $_html_result .= ' selected="selected"';
  97. $_html_class = !empty($class) ? ' class="'.$class.' option"' : '';
  98. $_html_id = !empty($id) ? ' id="'.$id.'-'.$idx.'"' : '';
  99. $_html_result .= $_html_class . $_html_id . '>' . smarty_function_escape_special_chars($value) . '</option>' . "\n";
  100. $idx++;
  101. } else {
  102. $_idx = 0;
  103. $_html_result = smarty_function_html_options_optgroup($key, $value, $selected, $id.'-'.$idx, $class, $_idx);
  104. $idx++;
  105. }
  106. return $_html_result;
  107. }
  108. function smarty_function_html_options_optgroup($key, $values, $selected, $id, $class, &$idx)
  109. {
  110. $optgroup_html = '<optgroup label="' . smarty_function_escape_special_chars($key) . '">' . "\n";
  111. foreach ($values as $key => $value) {
  112. $optgroup_html .= smarty_function_html_options_optoutput($key, $value, $selected, $id, $class, $idx);
  113. }
  114. $optgroup_html .= "</optgroup>\n";
  115. return $optgroup_html;
  116. }
  117. ?>