Automatically exported from code.google.com/p/planningalerts
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

function.html_options.php 3.7 KiB

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