update html_options to support class and id attrs

This commit is contained in:
monte.ohrt
2010-12-21 15:59:25 +00:00
parent b9477b0deb
commit c0b6f29077
2 changed files with 28 additions and 10 deletions

View File

@@ -1,4 +1,8 @@
===== SVN trunk ===== ===== SVN trunk =====
21/12/2010
- update html_options to support class and id attrs
17/12/2010 17/12/2010
- bugfix added missing support of $cache_attrs for registered plugins - bugfix added missing support of $cache_attrs for registered plugins

View File

@@ -37,12 +37,17 @@ function smarty_function_html_options($params, $template)
$options = null; $options = null;
$selected = array(); $selected = array();
$output = null; $output = null;
$id = null;
$class = null;
$extra = ''; $extra = '';
$options_extra = '';
foreach($params as $_key => $_val) { foreach($params as $_key => $_val) {
switch ($_key) { switch ($_key) {
case 'name': case 'name':
case 'class':
case 'id':
$$_key = (string)$_val; $$_key = (string)$_val;
break; break;
@@ -74,43 +79,52 @@ function smarty_function_html_options($params, $template)
/* raise error here? */ /* raise error here? */
$_html_result = ''; $_html_result = '';
$_idx = 0;
if (isset($options)) { if (isset($options)) {
foreach ($options as $_key => $_val) foreach ($options as $_key => $_val) {
$_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected); $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected, $id, $class, $_idx);
}
} else { } else {
foreach ($values as $_i => $_key) { foreach ($values as $_i => $_key) {
$_val = isset($output[$_i]) ? $output[$_i] : ''; $_val = isset($output[$_i]) ? $output[$_i] : '';
$_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected); $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected, $id, $class, $_idx);
} }
} }
if (!empty($name)) { if (!empty($name)) {
$_html_result = '<select name="' . $name . '"' . $extra . '>' . "\n" . $_html_result . '</select>' . "\n"; $_html_class = !empty($class) ? ' class="'.$class.'"' : '';
$_html_id = !empty($id) ? ' id="'.$id.'"' : '';
$_html_result = '<select name="' . $name . '"' . $_html_class . $_html_id . $extra . '>' . "\n" . $_html_result . '</select>' . "\n";
} }
return $_html_result; return $_html_result;
} }
function smarty_function_html_options_optoutput($key, $value, $selected) function smarty_function_html_options_optoutput($key, $value, $selected, $id, $class, &$idx)
{ {
if (!is_array($value)) { if (!is_array($value)) {
$_html_result = '<option value="' . $_html_result = '<option value="' .
smarty_function_escape_special_chars($key) . '"'; smarty_function_escape_special_chars($key) . '"';
if (in_array((string)$key, $selected)) if (in_array((string)$key, $selected))
$_html_result .= ' selected="selected"'; $_html_result .= ' selected="selected"';
$_html_result .= '>' . smarty_function_escape_special_chars($value) . '</option>' . "\n"; $_html_class = !empty($class) ? ' class="'.$class.' option"' : '';
$_html_id = !empty($id) ? ' id="'.$id.'-'.$idx.'"' : '';
$_html_result .= $_html_class . $_html_id . '>' . smarty_function_escape_special_chars($value) . '</option>' . "\n";
$idx++;
} else { } else {
$_html_result = smarty_function_html_options_optgroup($key, $value, $selected); $_idx = 0;
$_html_result = smarty_function_html_options_optgroup($key, $value, $selected, $id.'-'.$idx, $class, $_idx);
$idx++;
} }
return $_html_result; return $_html_result;
} }
function smarty_function_html_options_optgroup($key, $values, $selected) function smarty_function_html_options_optgroup($key, $values, $selected, $id, $class, &$idx)
{ {
$optgroup_html = '<optgroup label="' . smarty_function_escape_special_chars($key) . '">' . "\n"; $optgroup_html = '<optgroup label="' . smarty_function_escape_special_chars($key) . '">' . "\n";
foreach ($values as $key => $value) { foreach ($values as $key => $value) {
$optgroup_html .= smarty_function_html_options_optoutput($key, $value, $selected); $optgroup_html .= smarty_function_html_options_optoutput($key, $value, $selected, $id, $class, $idx);
} }
$optgroup_html .= "</optgroup>\n"; $optgroup_html .= "</optgroup>\n";
return $optgroup_html; return $optgroup_html;