function.html_options.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 $smarty Smarty object
  27. * @param object $template template object
  28. * @return string
  29. * @uses smarty_function_escape_special_chars()
  30. */
  31. function smarty_function_html_options($params, $smarty, $template)
  32. {
  33. require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
  34. //$smarty->loadPlugin('Smarty_shared_escape_special_chars');
  35. $name = null;
  36. $values = null;
  37. $options = null;
  38. $selected = array();
  39. $output = null;
  40. $extra = '';
  41. foreach($params as $_key => $_val) {
  42. switch ($_key) {
  43. case 'name':
  44. $$_key = (string)$_val;
  45. break;
  46. case 'options':
  47. $$_key = (array)$_val;
  48. break;
  49. case 'values':
  50. case 'output':
  51. $$_key = array_values((array)$_val);
  52. break;
  53. case 'selected':
  54. $$_key = array_map('strval', array_values((array)$_val));
  55. break;
  56. default:
  57. if (!is_array($_val)) {
  58. $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
  59. } else {
  60. trigger_error("html_options: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
  61. }
  62. break;
  63. }
  64. }
  65. if (!isset($options) && !isset($values))
  66. return '';
  67. /* raise error here? */
  68. $_html_result = '';
  69. if (isset($options)) {
  70. foreach ($options as $_key => $_val)
  71. $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected);
  72. } else {
  73. foreach ($values as $_i => $_key) {
  74. $_val = isset($output[$_i]) ? $output[$_i] : '';
  75. $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected);
  76. }
  77. }
  78. if (!empty($name)) {
  79. $_html_result = '<select name="' . $name . '"' . $extra . '>' . "\n" . $_html_result . '</select>' . "\n";
  80. }
  81. return $_html_result;
  82. }
  83. function smarty_function_html_options_optoutput($key, $value, $selected)
  84. {
  85. if (!is_array($value)) {
  86. $_html_result = '<option value="' .
  87. smarty_function_escape_special_chars($key) . '"';
  88. if (in_array((string)$key, $selected))
  89. $_html_result .= ' selected="selected"';
  90. $_html_result .= '>' . smarty_function_escape_special_chars($value) . '</option>' . "\n";
  91. } else {
  92. $_html_result = smarty_function_html_options_optgroup($key, $value, $selected);
  93. }
  94. return $_html_result;
  95. }
  96. function smarty_function_html_options_optgroup($key, $values, $selected)
  97. {
  98. $optgroup_html = '<optgroup label="' . smarty_function_escape_special_chars($key) . '">' . "\n";
  99. foreach ($values as $key => $value) {
  100. $optgroup_html .= smarty_function_html_options_optoutput($key, $value, $selected);
  101. }
  102. $optgroup_html .= "</optgroup>\n";
  103. return $optgroup_html;
  104. }
  105. ?>