function.html_options.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. * @param \Smarty_Internal_Template $template
  33. *
  34. * @return string
  35. * @uses smarty_function_escape_special_chars()
  36. */
  37. function smarty_function_html_options($params, Smarty_Internal_Template $template)
  38. {
  39. if (!isset($template->smarty->_cache[ '_required_sesc' ])) {
  40. require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
  41. $template->smarty->_cache[ '_required_sesc' ] = true;
  42. }
  43. $name = null;
  44. $values = null;
  45. $options = null;
  46. $selected = null;
  47. $output = null;
  48. $id = null;
  49. $class = null;
  50. $extra = '';
  51. foreach ($params as $_key => $_val) {
  52. switch ($_key) {
  53. case 'name':
  54. case 'class':
  55. case 'id':
  56. $$_key = (string) $_val;
  57. break;
  58. case 'options':
  59. $options = (array) $_val;
  60. break;
  61. case 'values':
  62. case 'output':
  63. $$_key = array_values((array) $_val);
  64. break;
  65. case 'selected':
  66. if (is_array($_val)) {
  67. $selected = array();
  68. foreach ($_val as $_sel) {
  69. if (is_object($_sel)) {
  70. if (method_exists($_sel, "__toString")) {
  71. $_sel = smarty_function_escape_special_chars((string) $_sel->__toString());
  72. } else {
  73. trigger_error("html_options: selected attribute contains an object of class '" .
  74. get_class($_sel) . "' without __toString() method", E_USER_NOTICE);
  75. continue;
  76. }
  77. } else {
  78. $_sel = smarty_function_escape_special_chars((string) $_sel);
  79. }
  80. $selected[ $_sel ] = true;
  81. }
  82. } elseif (is_object($_val)) {
  83. if (method_exists($_val, "__toString")) {
  84. $selected = smarty_function_escape_special_chars((string) $_val->__toString());
  85. } else {
  86. trigger_error("html_options: selected attribute is an object of class '" . get_class($_val) .
  87. "' without __toString() method", E_USER_NOTICE);
  88. }
  89. } else {
  90. $selected = smarty_function_escape_special_chars((string) $_val);
  91. }
  92. break;
  93. case 'strict':
  94. break;
  95. case 'disabled':
  96. case 'readonly':
  97. if (!empty($params[ 'strict' ])) {
  98. if (!is_scalar($_val)) {
  99. trigger_error("html_options: $_key attribute must be a scalar, only boolean true or string '$_key' will actually add the attribute",
  100. E_USER_NOTICE);
  101. }
  102. if ($_val === true || $_val === $_key) {
  103. $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_key) . '"';
  104. }
  105. break;
  106. }
  107. // omit break; to fall through!
  108. default:
  109. if (!is_array($_val)) {
  110. $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
  111. } else {
  112. trigger_error("html_options: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
  113. }
  114. break;
  115. }
  116. }
  117. if (!isset($options) && !isset($values)) {
  118. /* raise error here? */
  119. return '';
  120. }
  121. $_html_result = '';
  122. $_idx = 0;
  123. if (isset($options)) {
  124. foreach ($options as $_key => $_val) {
  125. $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected, $id, $class, $_idx);
  126. }
  127. } else {
  128. foreach ($values as $_i => $_key) {
  129. $_val = isset($output[ $_i ]) ? $output[ $_i ] : '';
  130. $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected, $id, $class, $_idx);
  131. }
  132. }
  133. if (!empty($name)) {
  134. $_html_class = !empty($class) ? ' class="' . $class . '"' : '';
  135. $_html_id = !empty($id) ? ' id="' . $id . '"' : '';
  136. $_html_result =
  137. '<select name="' . $name . '"' . $_html_class . $_html_id . $extra . '>' . "\n" . $_html_result .
  138. '</select>' . "\n";
  139. }
  140. return $_html_result;
  141. }
  142. function smarty_function_html_options_optoutput($key, $value, $selected, $id, $class, &$idx)
  143. {
  144. if (!is_array($value)) {
  145. $_key = smarty_function_escape_special_chars($key);
  146. $_html_result = '<option value="' . $_key . '"';
  147. if (is_array($selected)) {
  148. if (isset($selected[ $_key ])) {
  149. $_html_result .= ' selected="selected"';
  150. }
  151. } elseif ($_key === $selected) {
  152. $_html_result .= ' selected="selected"';
  153. }
  154. $_html_class = !empty($class) ? ' class="' . $class . ' option"' : '';
  155. $_html_id = !empty($id) ? ' id="' . $id . '-' . $idx . '"' : '';
  156. if (is_object($value)) {
  157. if (method_exists($value, "__toString")) {
  158. $value = smarty_function_escape_special_chars((string) $value->__toString());
  159. } else {
  160. trigger_error("html_options: value is an object of class '" . get_class($value) .
  161. "' without __toString() method", E_USER_NOTICE);
  162. return '';
  163. }
  164. } else {
  165. $value = smarty_function_escape_special_chars((string) $value);
  166. }
  167. $_html_result .= $_html_class . $_html_id . '>' . $value . '</option>' . "\n";
  168. $idx ++;
  169. } else {
  170. $_idx = 0;
  171. $_html_result =
  172. smarty_function_html_options_optgroup($key, $value, $selected, !empty($id) ? ($id . '-' . $idx) : null,
  173. $class, $_idx);
  174. $idx ++;
  175. }
  176. return $_html_result;
  177. }
  178. function smarty_function_html_options_optgroup($key, $values, $selected, $id, $class, &$idx)
  179. {
  180. $optgroup_html = '<optgroup label="' . smarty_function_escape_special_chars($key) . '">' . "\n";
  181. foreach ($values as $key => $value) {
  182. $optgroup_html .= smarty_function_html_options_optoutput($key, $value, $selected, $id, $class, $idx);
  183. }
  184. $optgroup_html .= "</optgroup>\n";
  185. return $optgroup_html;
  186. }