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

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