function.html_options.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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<br>
  15. * Params:
  16. * <pre>
  17. * - name (optional) - string default "select"
  18. * - values (required) - if no options supplied) - array
  19. * - options (required) - if no values supplied) - associative array
  20. * - selected (optional) - string default not set
  21. * - output (required) - if not options supplied) - array
  22. * - id (optional) - string default not set
  23. * - class (optional) - string default not set
  24. * </pre>
  25. *
  26. * @link http://www.smarty.net/manual/en/language.function.html.options.php {html_image}
  27. * (Smarty online manual)
  28. * @author Monte Ohrt <monte at ohrt dot com>
  29. * @author Ralf Strehle (minor optimization) <ralf dot strehle at yahoo dot de>
  30. * @param array $params parameters
  31. * @param Smarty_Internal_Template $template template object
  32. * @return string
  33. * @uses smarty_function_escape_special_chars()
  34. */
  35. function smarty_function_html_options($params, $template)
  36. {
  37. require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
  38. $name = null;
  39. $values = null;
  40. $options = null;
  41. $selected = null;
  42. $output = null;
  43. $id = null;
  44. $class = null;
  45. $extra = '';
  46. foreach ($params as $_key => $_val) {
  47. switch ($_key) {
  48. case 'name':
  49. case 'class':
  50. case 'id':
  51. $$_key = (string) $_val;
  52. break;
  53. case 'options':
  54. $options = (array) $_val;
  55. break;
  56. case 'values':
  57. case 'output':
  58. $$_key = array_values((array) $_val);
  59. break;
  60. case 'selected':
  61. if (is_array($_val)) {
  62. $selected = array();
  63. foreach ($_val as $_sel) {
  64. if (is_object($_sel)) {
  65. if (method_exists($_sel, "__toString")) {
  66. $_sel = smarty_function_escape_special_chars((string) $_sel->__toString());
  67. } else {
  68. trigger_error("html_options: selected attribute contains an object of class '". get_class($_sel) ."' without __toString() method", E_USER_NOTICE);
  69. continue;
  70. }
  71. } else {
  72. $_sel = smarty_function_escape_special_chars((string) $_sel);
  73. }
  74. $selected[$_sel] = true;
  75. }
  76. } elseif (is_object($_val)) {
  77. if (method_exists($_val, "__toString")) {
  78. $selected = smarty_function_escape_special_chars((string) $_val->__toString());
  79. } else {
  80. trigger_error("html_options: selected attribute is an object of class '". get_class($_val) ."' without __toString() method", E_USER_NOTICE);
  81. }
  82. } else {
  83. $selected = smarty_function_escape_special_chars((string) $_val);
  84. }
  85. break;
  86. default:
  87. if (!is_array($_val)) {
  88. $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
  89. } else {
  90. trigger_error("html_options: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
  91. }
  92. break;
  93. }
  94. }
  95. if (!isset($options) && !isset($values)) {
  96. /* raise error here? */
  97. return '';
  98. }
  99. $_html_result = '';
  100. $_idx = 0;
  101. if (isset($options)) {
  102. foreach ($options as $_key => $_val) {
  103. $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected, $id, $class, $_idx);
  104. }
  105. } else {
  106. foreach ($values as $_i => $_key) {
  107. $_val = isset($output[$_i]) ? $output[$_i] : '';
  108. $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected, $id, $class, $_idx);
  109. }
  110. }
  111. if (!empty($name)) {
  112. $_html_class = !empty($class) ? ' class="'.$class.'"' : '';
  113. $_html_id = !empty($id) ? ' id="'.$id.'"' : '';
  114. $_html_result = '<select name="' . $name . '"' . $_html_class . $_html_id . $extra . '>' . "\n" . $_html_result . '</select>' . "\n";
  115. }
  116. return $_html_result;
  117. }
  118. function smarty_function_html_options_optoutput($key, $value, $selected, $id, $class, &$idx)
  119. {
  120. if (!is_array($value)) {
  121. $_key = smarty_function_escape_special_chars($key);
  122. $_html_result = '<option value="' . $_key . '"';
  123. if (is_array($selected)) {
  124. if (isset($selected[$_key])) {
  125. $_html_result .= ' selected="selected"';
  126. }
  127. } elseif ($_key === $selected) {
  128. $_html_result .= ' selected="selected"';
  129. }
  130. $_html_class = !empty($class) ? ' class="'.$class.' option"' : '';
  131. $_html_id = !empty($id) ? ' id="'.$id.'-'.$idx.'"' : '';
  132. if (is_object($value)) {
  133. if (method_exists($value, "__toString")) {
  134. $value = smarty_function_escape_special_chars((string) $value->__toString());
  135. } else {
  136. trigger_error("html_options: value is an object of class '". get_class($value) ."' without __toString() method", E_USER_NOTICE);
  137. return '';
  138. }
  139. }
  140. $_html_result .= $_html_class . $_html_id . '>' . $value . '</option>' . "\n";
  141. $idx++;
  142. } else {
  143. $_idx = 0;
  144. $_html_result = smarty_function_html_options_optgroup($key, $value, $selected, !empty($id) ? ($id.'-'.$idx) : null, $class, $_idx);
  145. $idx++;
  146. }
  147. return $_html_result;
  148. }
  149. function smarty_function_html_options_optgroup($key, $values, $selected, $id, $class, &$idx)
  150. {
  151. $optgroup_html = '<optgroup label="' . smarty_function_escape_special_chars($key) . '">' . "\n";
  152. foreach ($values as $key => $value) {
  153. $optgroup_html .= smarty_function_html_options_optoutput($key, $value, $selected, $id, $class, $idx);
  154. }
  155. $optgroup_html .= "</optgroup>\n";
  156. return $optgroup_html;
  157. }
  158. ?>