function.html_options.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /**
  3. * Smarty plugin
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsFunction
  7. */
  8. /**
  9. * Smarty {html_options} function plugin
  10. * Type: function<br>
  11. * Name: html_options<br>
  12. * Purpose: Prints the list of <option> tags generated from
  13. * the passed parameters<br>
  14. * Params:
  15. * <pre>
  16. * - name (optional) - string default "select"
  17. * - values (required) - if no options supplied) - array
  18. * - options (required) - if no values supplied) - associative array
  19. * - selected (optional) - string default not set
  20. * - output (required) - if not options supplied) - array
  21. * - id (optional) - string default not set
  22. * - class (optional) - string default not set
  23. * </pre>
  24. *
  25. * @link http://www.smarty.net/manual/en/language.function.html.options.php {html_image}
  26. * (Smarty online manual)
  27. * @author Monte Ohrt <monte at ohrt dot com>
  28. * @author Ralf Strehle (minor optimization) <ralf dot strehle at yahoo dot de>
  29. *
  30. * @param array $params parameters
  31. *
  32. * @return string
  33. * @uses smarty_function_escape_special_chars()
  34. */
  35. function smarty_function_html_options($params)
  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 '" .
  69. get_class($_sel) . "' without __toString() method", E_USER_NOTICE);
  70. continue;
  71. }
  72. } else {
  73. $_sel = smarty_function_escape_special_chars((string) $_sel);
  74. }
  75. $selected[ $_sel ] = true;
  76. }
  77. } elseif (is_object($_val)) {
  78. if (method_exists($_val, "__toString")) {
  79. $selected = smarty_function_escape_special_chars((string) $_val->__toString());
  80. } else {
  81. trigger_error("html_options: selected attribute is an object of class '" . get_class($_val) .
  82. "' without __toString() method", E_USER_NOTICE);
  83. }
  84. } else {
  85. $selected = smarty_function_escape_special_chars((string) $_val);
  86. }
  87. break;
  88. case 'strict':
  89. break;
  90. case 'disabled':
  91. case 'readonly':
  92. if (!empty($params[ 'strict' ])) {
  93. if (!is_scalar($_val)) {
  94. trigger_error("html_options: $_key attribute must be a scalar, only boolean true or string '$_key' will actually add the attribute",
  95. E_USER_NOTICE);
  96. }
  97. if ($_val === true || $_val === $_key) {
  98. $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_key) . '"';
  99. }
  100. break;
  101. }
  102. // omit break; to fall through!
  103. default:
  104. if (!is_array($_val)) {
  105. $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
  106. } else {
  107. trigger_error("html_options: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
  108. }
  109. break;
  110. }
  111. }
  112. if (!isset($options) && !isset($values)) {
  113. /* raise error here? */
  114. return '';
  115. }
  116. $_html_result = '';
  117. $_idx = 0;
  118. if (isset($options)) {
  119. foreach ($options as $_key => $_val) {
  120. $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected, $id, $class, $_idx);
  121. }
  122. } else {
  123. foreach ($values as $_i => $_key) {
  124. $_val = isset($output[ $_i ]) ? $output[ $_i ] : '';
  125. $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected, $id, $class, $_idx);
  126. }
  127. }
  128. if (!empty($name)) {
  129. $_html_class = !empty($class) ? ' class="' . $class . '"' : '';
  130. $_html_id = !empty($id) ? ' id="' . $id . '"' : '';
  131. $_html_result =
  132. '<select name="' . $name . '"' . $_html_class . $_html_id . $extra . '>' . "\n" . $_html_result .
  133. '</select>' . "\n";
  134. }
  135. return $_html_result;
  136. }
  137. function smarty_function_html_options_optoutput($key, $value, $selected, $id, $class, &$idx)
  138. {
  139. if (!is_array($value)) {
  140. $_key = smarty_function_escape_special_chars($key);
  141. $_html_result = '<option value="' . $_key . '"';
  142. if (is_array($selected)) {
  143. if (isset($selected[ $_key ])) {
  144. $_html_result .= ' selected="selected"';
  145. }
  146. } elseif ($_key === $selected) {
  147. $_html_result .= ' selected="selected"';
  148. }
  149. $_html_class = !empty($class) ? ' class="' . $class . ' option"' : '';
  150. $_html_id = !empty($id) ? ' id="' . $id . '-' . $idx . '"' : '';
  151. if (is_object($value)) {
  152. if (method_exists($value, "__toString")) {
  153. $value = smarty_function_escape_special_chars((string) $value->__toString());
  154. } else {
  155. trigger_error("html_options: value is an object of class '" . get_class($value) .
  156. "' without __toString() method", E_USER_NOTICE);
  157. return '';
  158. }
  159. } else {
  160. $value = smarty_function_escape_special_chars((string) $value);
  161. }
  162. $_html_result .= $_html_class . $_html_id . '>' . $value . '</option>' . "\n";
  163. $idx ++;
  164. } else {
  165. $_idx = 0;
  166. $_html_result =
  167. smarty_function_html_options_optgroup($key, $value, $selected, !empty($id) ? ($id . '-' . $idx) : null,
  168. $class, $_idx);
  169. $idx ++;
  170. }
  171. return $_html_result;
  172. }
  173. function smarty_function_html_options_optgroup($key, $values, $selected, $id, $class, &$idx)
  174. {
  175. $optgroup_html = '<optgroup label="' . smarty_function_escape_special_chars($key) . '">' . "\n";
  176. foreach ($values as $key => $value) {
  177. $optgroup_html .= smarty_function_html_options_optoutput($key, $value, $selected, $id, $class, $idx);
  178. }
  179. $optgroup_html .= "</optgroup>\n";
  180. return $optgroup_html;
  181. }