diff --git a/change_log.txt b/change_log.txt index fba8e245..21d9c6e0 100644 --- a/change_log.txt +++ b/change_log.txt @@ -4,6 +4,7 @@ - bugfix testInstall() would not show path to internal plugins_dir (Forum Post 74627) - improvement testInstall() now showing resolved paths and checking the include_path if necessary - bugfix html_options plugin did not handle object values properly (Issue #49, Forum Topic 20049) +- improvement html_checkboxes and html_radios to accept null- and object values, and label_ids attribute 06.10.2011 - bugfix switch lexer internals depending on mbstring.func_overload diff --git a/libs/plugins/function.html_checkboxes.php b/libs/plugins/function.html_checkboxes.php index 40e4a8fc..d620621b 100644 --- a/libs/plugins/function.html_checkboxes.php +++ b/libs/plugins/function.html_checkboxes.php @@ -48,9 +48,10 @@ function smarty_function_html_checkboxes($params, $template) $name = 'checkbox'; $values = null; $options = null; - $selected = null; + $selected = array(); $separator = ''; $labels = true; + $label_ids = false; $output = null; $extra = ''; @@ -59,30 +60,54 @@ function smarty_function_html_checkboxes($params, $template) switch($_key) { case 'name': case 'separator': - $$_key = $_val; + $$_key = (string) $_val; break; case 'labels': - $$_key = (bool)$_val; + case 'label_ids': + $$_key = (bool) $_val; break; case 'options': - $$_key = (array)$_val; + $$_key = (array) $_val; break; case 'values': case 'output': - $$_key = array_values((array)$_val); + $$_key = array_values((array) $_val); break; case 'checked': case 'selected': - $selected = array_map('strval', array_values((array)$_val)); + if (is_array($_val)) { + $selected = array(); + foreach ($_val as $_sel) { + if (is_object($_sel)) { + if (method_exists($_sel, "__toString")) { + $selected = smarty_function_escape_special_chars((string) $_sel->__toString()); + } else { + trigger_error("html_checkboxes: selected attribute contains an object of class '". get_class($_sel) ."' without __toString() method", E_USER_NOTICE); + continue; + } + } else { + $_sel = smarty_function_escape_special_chars((string) $_sel); + } + $selected[$_sel] = true; + } + } elseif (is_object($_val)) { + if (method_exists($_val, "__toString")) { + $selected = smarty_function_escape_special_chars((string) $_val->__toString()); + } else { + trigger_error("html_checkboxes: selected attribute is an object of class '". get_class($_val) ."' without __toString() method", E_USER_NOTICE); + } + } else { + $selected = smarty_function_escape_special_chars((string) $_val); + } break; case 'checkboxes': trigger_error('html_checkboxes: the use of the "checkboxes" attribute is deprecated, use "options" instead', E_USER_WARNING); - $options = (array)$_val; + $options = (array) $_val; break; case 'assign': @@ -101,45 +126,85 @@ function smarty_function_html_checkboxes($params, $template) if (!isset($options) && !isset($values)) return ''; /* raise error here? */ - settype($selected, 'array'); $_html_result = array(); if (isset($options)) { - - foreach ($options as $_key=>$_val) - $_html_result[] = smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels); - - + foreach ($options as $_key=>$_val) { + $_html_result[] = smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids); + } } else { foreach ($values as $_i=>$_key) { $_val = isset($output[$_i]) ? $output[$_i] : ''; - $_html_result[] = smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels); + $_html_result[] = smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids); } - } if(!empty($params['assign'])) { $template->assign($params['assign'], $_html_result); } else { - return implode("\n",$_html_result); + return implode("\n", $_html_result); } } -function smarty_function_html_checkboxes_output($name, $value, $output, $selected, $extra, $separator, $labels) { +function smarty_function_html_checkboxes_output($name, $value, $output, $selected, $extra, $separator, $labels, $label_ids) { $_output = ''; - if ($labels) $_output .= '