PageRenderTime 65ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/include/SMARTY/plugins/function.html_options.php

https://github.com/MyITCRM/myitcrm1
PHP | 121 lines | 72 code | 23 blank | 26 comment | 10 complexity | 4f571f2ddb1e81edc5d8bf3d2baa2b2b MD5 | raw file
  1. <?php
  2. /**
  3. * Smarty plugin
  4. * @package Smarty
  5. * @subpackage plugins
  6. */
  7. /**
  8. * Smarty {html_options} function plugin
  9. *
  10. * Type: function<br>
  11. * Name: html_options<br>
  12. * Input:<br>
  13. * - name (optional) - string default "select"
  14. * - values (required if no options supplied) - array
  15. * - options (required if no values supplied) - associative array
  16. * - selected (optional) - string default not set
  17. * - output (required if not options supplied) - array
  18. * Purpose: Prints the list of <option> tags generated from
  19. * the passed parameters
  20. * @link http://smarty.php.net/manual/en/language.function.html.options.php {html_image}
  21. * (Smarty online manual)
  22. * @param array
  23. * @param Smarty
  24. * @return string
  25. * @uses smarty_function_escape_special_chars()
  26. */
  27. function smarty_function_html_options($params, &$smarty)
  28. {
  29. require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
  30. $name = null;
  31. $values = null;
  32. $options = null;
  33. $selected = array();
  34. $output = null;
  35. $extra = '';
  36. foreach($params as $_key => $_val) {
  37. switch($_key) {
  38. case 'name':
  39. $$_key = (string)$_val;
  40. break;
  41. case 'options':
  42. $$_key = (array)$_val;
  43. break;
  44. case 'values':
  45. case 'output':
  46. $$_key = array_values((array)$_val);
  47. break;
  48. case 'selected':
  49. $$_key = array_map('strval', array_values((array)$_val));
  50. break;
  51. default:
  52. if(!is_array($_val)) {
  53. $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
  54. } else {
  55. $smarty->trigger_error("html_options: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
  56. }
  57. break;
  58. }
  59. }
  60. if (!isset($options) && !isset($values))
  61. return ''; /* raise error here? */
  62. $_html_result = '';
  63. if (isset($options)) {
  64. foreach ($options as $_key=>$_val)
  65. $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected);
  66. } else {
  67. foreach ($values as $_i=>$_key) {
  68. $_val = isset($output[$_i]) ? $output[$_i] : '';
  69. $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected);
  70. }
  71. }
  72. if(!empty($name)) {
  73. $_html_result = '<select name="' . $name . '"' . $extra . '>' . "\n" . $_html_result . '</select>' . "\n";
  74. }
  75. return $_html_result;
  76. }
  77. function smarty_function_html_options_optoutput($key, $value, $selected) {
  78. if(!is_array($value)) {
  79. $_html_result = '<option label="' . smarty_function_escape_special_chars($value) . '" value="' .
  80. smarty_function_escape_special_chars($key) . '"';
  81. if (in_array((string)$key, $selected))
  82. $_html_result .= ' selected="selected"';
  83. $_html_result .= '>' . smarty_function_escape_special_chars($value) . '</option>' . "\n";
  84. } else {
  85. $_html_result = smarty_function_html_options_optgroup($key, $value, $selected);
  86. }
  87. return $_html_result;
  88. }
  89. function smarty_function_html_options_optgroup($key, $values, $selected) {
  90. $optgroup_html = '<optgroup label="' . smarty_function_escape_special_chars($key) . '">' . "\n";
  91. foreach ($values as $key => $value) {
  92. $optgroup_html .= smarty_function_html_options_optoutput($key, $value, $selected);
  93. }
  94. $optgroup_html .= "</optgroup>\n";
  95. return $optgroup_html;
  96. }
  97. /* vim: set expandtab: */
  98. ?>