PageRenderTime 27ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/core/model/smarty/plugins/function.html_options.php

http://github.com/modxcms/revolution
PHP | 230 lines | 170 code | 2 blank | 58 comment | 28 complexity | 892fc2687973811c74c4711a8df5f27d MD5 | raw file
Possible License(s): GPL-2.0, Apache-2.0, BSD-3-Clause, LGPL-2.1
  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. * @link http://www.smarty.net/manual/en/language.function.html.options.php {html_image}
  25. * (Smarty online manual)
  26. * @author Monte Ohrt <monte at ohrt dot com>
  27. * @author Ralf Strehle (minor optimization) <ralf dot strehle at yahoo dot de>
  28. *
  29. * @param array $params parameters
  30. *
  31. * @param \Smarty_Internal_Template $template
  32. *
  33. * @return string
  34. * @uses smarty_function_escape_special_chars()
  35. * @throws \SmartyException
  36. */
  37. function smarty_function_html_options($params, Smarty_Internal_Template $template)
  38. {
  39. $template->_checkPlugins(
  40. array(
  41. array(
  42. 'function' => 'smarty_function_escape_special_chars',
  43. 'file' => SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php'
  44. )
  45. )
  46. );
  47. $name = null;
  48. $values = null;
  49. $options = null;
  50. $selected = null;
  51. $output = null;
  52. $id = null;
  53. $class = null;
  54. $extra = '';
  55. foreach ($params as $_key => $_val) {
  56. switch ($_key) {
  57. case 'name':
  58. case 'class':
  59. case 'id':
  60. $$_key = (string)$_val;
  61. break;
  62. case 'options':
  63. $options = (array)$_val;
  64. break;
  65. case 'values':
  66. case 'output':
  67. $$_key = array_values((array)$_val);
  68. break;
  69. case 'selected':
  70. if (is_array($_val)) {
  71. $selected = array();
  72. foreach ($_val as $_sel) {
  73. if (is_object($_sel)) {
  74. if (method_exists($_sel, '__toString')) {
  75. $_sel = smarty_function_escape_special_chars((string)$_sel->__toString());
  76. } else {
  77. trigger_error(
  78. 'html_options: selected attribute contains an object of class \'' .
  79. get_class($_sel) . '\' without __toString() method',
  80. E_USER_NOTICE
  81. );
  82. continue;
  83. }
  84. } else {
  85. $_sel = smarty_function_escape_special_chars((string)$_sel);
  86. }
  87. $selected[ $_sel ] = true;
  88. }
  89. } elseif (is_object($_val)) {
  90. if (method_exists($_val, '__toString')) {
  91. $selected = smarty_function_escape_special_chars((string)$_val->__toString());
  92. } else {
  93. trigger_error(
  94. 'html_options: selected attribute is an object of class \'' . get_class($_val) .
  95. '\' without __toString() method',
  96. E_USER_NOTICE
  97. );
  98. }
  99. } else {
  100. $selected = smarty_function_escape_special_chars((string)$_val);
  101. }
  102. break;
  103. case 'strict':
  104. break;
  105. case 'disabled':
  106. case 'readonly':
  107. if (!empty($params[ 'strict' ])) {
  108. if (!is_scalar($_val)) {
  109. trigger_error(
  110. "html_options: {$_key} attribute must be a scalar, only boolean true or string '{$_key}' will actually add the attribute",
  111. E_USER_NOTICE
  112. );
  113. }
  114. if ($_val === true || $_val === $_key) {
  115. $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_key) . '"';
  116. }
  117. break;
  118. }
  119. // omit break; to fall through!
  120. // no break
  121. default:
  122. if (!is_array($_val)) {
  123. $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
  124. } else {
  125. trigger_error("html_options: extra attribute '{$_key}' cannot be an array", E_USER_NOTICE);
  126. }
  127. break;
  128. }
  129. }
  130. if (!isset($options) && !isset($values)) {
  131. /* raise error here? */
  132. return '';
  133. }
  134. $_html_result = '';
  135. $_idx = 0;
  136. if (isset($options)) {
  137. foreach ($options as $_key => $_val) {
  138. $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected, $id, $class, $_idx);
  139. }
  140. } else {
  141. foreach ($values as $_i => $_key) {
  142. $_val = isset($output[ $_i ]) ? $output[ $_i ] : '';
  143. $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected, $id, $class, $_idx);
  144. }
  145. }
  146. if (!empty($name)) {
  147. $_html_class = !empty($class) ? ' class="' . $class . '"' : '';
  148. $_html_id = !empty($id) ? ' id="' . $id . '"' : '';
  149. $_html_result =
  150. '<select name="' . $name . '"' . $_html_class . $_html_id . $extra . '>' . "\n" . $_html_result .
  151. '</select>' . "\n";
  152. }
  153. return $_html_result;
  154. }
  155. /**
  156. * @param $key
  157. * @param $value
  158. * @param $selected
  159. * @param $id
  160. * @param $class
  161. * @param $idx
  162. *
  163. * @return string
  164. */
  165. function smarty_function_html_options_optoutput($key, $value, $selected, $id, $class, &$idx)
  166. {
  167. if (!is_array($value)) {
  168. $_key = smarty_function_escape_special_chars($key);
  169. $_html_result = '<option value="' . $_key . '"';
  170. if (is_array($selected)) {
  171. if (isset($selected[ $_key ])) {
  172. $_html_result .= ' selected="selected"';
  173. }
  174. } elseif ($_key === $selected) {
  175. $_html_result .= ' selected="selected"';
  176. }
  177. $_html_class = !empty($class) ? ' class="' . $class . ' option"' : '';
  178. $_html_id = !empty($id) ? ' id="' . $id . '-' . $idx . '"' : '';
  179. if (is_object($value)) {
  180. if (method_exists($value, '__toString')) {
  181. $value = smarty_function_escape_special_chars((string)$value->__toString());
  182. } else {
  183. trigger_error(
  184. 'html_options: value is an object of class \'' . get_class($value) .
  185. '\' without __toString() method',
  186. E_USER_NOTICE
  187. );
  188. return '';
  189. }
  190. } else {
  191. $value = smarty_function_escape_special_chars((string)$value);
  192. }
  193. $_html_result .= $_html_class . $_html_id . '>' . $value . '</option>' . "\n";
  194. $idx++;
  195. } else {
  196. $_idx = 0;
  197. $_html_result =
  198. smarty_function_html_options_optgroup(
  199. $key,
  200. $value,
  201. $selected,
  202. !empty($id) ? ($id . '-' . $idx) : null,
  203. $class,
  204. $_idx
  205. );
  206. $idx++;
  207. }
  208. return $_html_result;
  209. }
  210. /**
  211. * @param $key
  212. * @param $values
  213. * @param $selected
  214. * @param $id
  215. * @param $class
  216. * @param $idx
  217. *
  218. * @return string
  219. */
  220. function smarty_function_html_options_optgroup($key, $values, $selected, $id, $class, &$idx)
  221. {
  222. $optgroup_html = '<optgroup label="' . smarty_function_escape_special_chars($key) . '">' . "\n";
  223. foreach ($values as $key => $value) {
  224. $optgroup_html .= smarty_function_html_options_optoutput($key, $value, $selected, $id, $class, $idx);
  225. }
  226. $optgroup_html .= "</optgroup>\n";
  227. return $optgroup_html;
  228. }