Automatically exported from code.google.com/p/planningalerts
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

123 righe
3.7 KiB

  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. ?>