You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

function.html_options.php 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 '" . 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. case 'strict':
  87. break;
  88. case 'disabled':
  89. case 'readonly':
  90. if (!empty($params['strict'])) {
  91. if (!is_scalar($_val)) {
  92. trigger_error("html_options: $_key attribute must be a scalar, only boolean true or string '$_key' will actually add the attribute", E_USER_NOTICE);
  93. }
  94. if ($_val === true || $_val === $_key) {
  95. $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_key) . '"';
  96. }
  97. break;
  98. }
  99. // omit break; to fall through!
  100. default:
  101. if (!is_array($_val)) {
  102. $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
  103. } else {
  104. trigger_error("html_options: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
  105. }
  106. break;
  107. }
  108. }
  109. if (!isset($options) && !isset($values)) {
  110. /* raise error here? */
  111. return '';
  112. }
  113. $_html_result = '';
  114. $_idx = 0;
  115. if (isset($options)) {
  116. foreach ($options as $_key => $_val) {
  117. $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected, $id, $class, $_idx);
  118. }
  119. } else {
  120. foreach ($values as $_i => $_key) {
  121. $_val = isset($output[$_i]) ? $output[$_i] : '';
  122. $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected, $id, $class, $_idx);
  123. }
  124. }
  125. if (!empty($name)) {
  126. $_html_class = !empty($class) ? ' class="' . $class . '"' : '';
  127. $_html_id = !empty($id) ? ' id="' . $id . '"' : '';
  128. $_html_result = '<select name="' . $name . '"' . $_html_class . $_html_id . $extra . '>' . "\n" . $_html_result . '</select>' . "\n";
  129. }
  130. return $_html_result;
  131. }
  132. function smarty_function_html_options_optoutput($key, $value, $selected, $id, $class, &$idx)
  133. {
  134. if (!is_array($value)) {
  135. $_key = smarty_function_escape_special_chars($key);
  136. $_html_result = '<option value="' . $_key . '"';
  137. if (is_array($selected)) {
  138. if (isset($selected[$_key])) {
  139. $_html_result .= ' selected="selected"';
  140. }
  141. } elseif ($_key === $selected) {
  142. $_html_result .= ' selected="selected"';
  143. }
  144. $_html_class = !empty($class) ? ' class="' . $class . ' option"' : '';
  145. $_html_id = !empty($id) ? ' id="' . $id . '-' . $idx . '"' : '';
  146. if (is_object($value)) {
  147. if (method_exists($value, "__toString")) {
  148. $value = smarty_function_escape_special_chars((string) $value->__toString());
  149. } else {
  150. trigger_error("html_options: value is an object of class '" . get_class($value) . "' without __toString() method", E_USER_NOTICE);
  151. return '';
  152. }
  153. } else {
  154. $value = smarty_function_escape_special_chars((string) $value);
  155. }
  156. $_html_result .= $_html_class . $_html_id . '>' . $value . '</option>' . "\n";
  157. $idx ++;
  158. } else {
  159. $_idx = 0;
  160. $_html_result = smarty_function_html_options_optgroup($key, $value, $selected, !empty($id) ? ($id . '-' . $idx) : null, $class, $_idx);
  161. $idx ++;
  162. }
  163. return $_html_result;
  164. }
  165. function smarty_function_html_options_optgroup($key, $values, $selected, $id, $class, &$idx)
  166. {
  167. $optgroup_html = '<optgroup label="' . smarty_function_escape_special_chars($key) . '">' . "\n";
  168. foreach ($values as $key => $value) {
  169. $optgroup_html .= smarty_function_html_options_optoutput($key, $value, $selected, $id, $class, $idx);
  170. }
  171. $optgroup_html .= "</optgroup>\n";
  172. return $optgroup_html;
  173. }