mirror of
https://github.com/smarty-php/smarty.git
synced 2025-08-06 11:24:27 +02:00
- improvement html_checkboxes and html_radios to accept null- and object values, and label_ids attribute
This commit is contained in:
@@ -4,6 +4,7 @@
|
|||||||
- bugfix testInstall() would not show path to internal plugins_dir (Forum Post 74627)
|
- 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
|
- 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)
|
- 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
|
06.10.2011
|
||||||
- bugfix switch lexer internals depending on mbstring.func_overload
|
- bugfix switch lexer internals depending on mbstring.func_overload
|
||||||
|
@@ -48,9 +48,10 @@ function smarty_function_html_checkboxes($params, $template)
|
|||||||
$name = 'checkbox';
|
$name = 'checkbox';
|
||||||
$values = null;
|
$values = null;
|
||||||
$options = null;
|
$options = null;
|
||||||
$selected = null;
|
$selected = array();
|
||||||
$separator = '';
|
$separator = '';
|
||||||
$labels = true;
|
$labels = true;
|
||||||
|
$label_ids = false;
|
||||||
$output = null;
|
$output = null;
|
||||||
|
|
||||||
$extra = '';
|
$extra = '';
|
||||||
@@ -59,30 +60,54 @@ function smarty_function_html_checkboxes($params, $template)
|
|||||||
switch($_key) {
|
switch($_key) {
|
||||||
case 'name':
|
case 'name':
|
||||||
case 'separator':
|
case 'separator':
|
||||||
$$_key = $_val;
|
$$_key = (string) $_val;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'labels':
|
case 'labels':
|
||||||
$$_key = (bool)$_val;
|
case 'label_ids':
|
||||||
|
$$_key = (bool) $_val;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'options':
|
case 'options':
|
||||||
$$_key = (array)$_val;
|
$$_key = (array) $_val;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'values':
|
case 'values':
|
||||||
case 'output':
|
case 'output':
|
||||||
$$_key = array_values((array)$_val);
|
$$_key = array_values((array) $_val);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'checked':
|
case 'checked':
|
||||||
case 'selected':
|
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;
|
break;
|
||||||
|
|
||||||
case 'checkboxes':
|
case 'checkboxes':
|
||||||
trigger_error('html_checkboxes: the use of the "checkboxes" attribute is deprecated, use "options" instead', E_USER_WARNING);
|
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;
|
break;
|
||||||
|
|
||||||
case 'assign':
|
case 'assign':
|
||||||
@@ -101,45 +126,85 @@ function smarty_function_html_checkboxes($params, $template)
|
|||||||
if (!isset($options) && !isset($values))
|
if (!isset($options) && !isset($values))
|
||||||
return ''; /* raise error here? */
|
return ''; /* raise error here? */
|
||||||
|
|
||||||
settype($selected, 'array');
|
|
||||||
$_html_result = array();
|
$_html_result = array();
|
||||||
|
|
||||||
if (isset($options)) {
|
if (isset($options)) {
|
||||||
|
foreach ($options as $_key=>$_val) {
|
||||||
foreach ($options as $_key=>$_val)
|
$_html_result[] = smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids);
|
||||||
$_html_result[] = smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels);
|
}
|
||||||
|
|
||||||
|
|
||||||
} 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_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'])) {
|
if(!empty($params['assign'])) {
|
||||||
$template->assign($params['assign'], $_html_result);
|
$template->assign($params['assign'], $_html_result);
|
||||||
} else {
|
} 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 = '';
|
$_output = '';
|
||||||
if ($labels) $_output .= '<label>';
|
|
||||||
$_output .= '<input type="checkbox" name="'
|
if (is_object($value)) {
|
||||||
. smarty_function_escape_special_chars($name) . '[]" value="'
|
if (method_exists($value, "__toString")) {
|
||||||
. smarty_function_escape_special_chars($value) . '"';
|
$value = (string) $value->__toString();
|
||||||
|
} else {
|
||||||
if (in_array((string)$value, $selected)) {
|
trigger_error("html_options: value is an object of class '". get_class($value) ."' without __toString() method", E_USER_NOTICE);
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$value = (string) $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_object($output)) {
|
||||||
|
if (method_exists($output, "__toString")) {
|
||||||
|
$output = (string) $output->__toString();
|
||||||
|
} else {
|
||||||
|
trigger_error("html_options: output is an object of class '". get_class($output) ."' without __toString() method", E_USER_NOTICE);
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$output = (string) $output;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($labels) {
|
||||||
|
if ($label_ids) {
|
||||||
|
$_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!u', '_', $name . '_' . $value));
|
||||||
|
$_output .= '<label for="' . $_id . '">';
|
||||||
|
} else {
|
||||||
|
$_output .= '<label>';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$name = smarty_function_escape_special_chars($name);
|
||||||
|
$value = smarty_function_escape_special_chars($value);
|
||||||
|
$output = smarty_function_escape_special_chars($output);
|
||||||
|
|
||||||
|
$_output .= '<input type="checkbox" name="' . $name . '[]" value="' . $value . '"';
|
||||||
|
|
||||||
|
if ($labels && $label_ids) {
|
||||||
|
$_output .= ' id="' . $_id . '"';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_array($selected)) {
|
||||||
|
if (isset($selected[$value])) {
|
||||||
|
$_output .= ' checked="checked"';
|
||||||
|
}
|
||||||
|
} elseif ($value === $selected) {
|
||||||
$_output .= ' checked="checked"';
|
$_output .= ' checked="checked"';
|
||||||
}
|
}
|
||||||
|
|
||||||
$_output .= $extra . ' />' . $output;
|
$_output .= $extra . ' />' . $output;
|
||||||
if ($labels) $_output .= '</label>';
|
if ($labels) {
|
||||||
|
$_output .= '</label>';
|
||||||
|
}
|
||||||
|
|
||||||
$_output .= $separator;
|
$_output .= $separator;
|
||||||
|
|
||||||
return $_output;
|
return $_output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -52,16 +52,16 @@ function smarty_function_html_options($params, $template)
|
|||||||
case 'name':
|
case 'name':
|
||||||
case 'class':
|
case 'class':
|
||||||
case 'id':
|
case 'id':
|
||||||
$$_key = (string)$_val;
|
$$_key = (string) $_val;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'options':
|
case 'options':
|
||||||
$options = (array)$_val;
|
$options = (array) $_val;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'values':
|
case 'values':
|
||||||
case 'output':
|
case 'output':
|
||||||
$$_key = array_values((array)$_val);
|
$$_key = array_values((array) $_val);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'selected':
|
case 'selected':
|
||||||
|
@@ -59,35 +59,41 @@ function smarty_function_html_radios($params, $template)
|
|||||||
switch ($_key) {
|
switch ($_key) {
|
||||||
case 'name':
|
case 'name':
|
||||||
case 'separator':
|
case 'separator':
|
||||||
$$_key = (string)$_val;
|
$$_key = (string) $_val;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'checked':
|
case 'checked':
|
||||||
case 'selected':
|
case 'selected':
|
||||||
if (is_array($_val)) {
|
if (is_array($_val)) {
|
||||||
trigger_error('html_radios: the "' . $_key . '" attribute cannot be an array', E_USER_WARNING);
|
trigger_error('html_radios: the "' . $_key . '" attribute cannot be an array', E_USER_WARNING);
|
||||||
|
} elseif (is_object($_val)) {
|
||||||
|
if (method_exists($_val, "__toString")) {
|
||||||
|
$selected = smarty_function_escape_special_chars((string) $_val->__toString());
|
||||||
|
} else {
|
||||||
|
trigger_error("html_radios: selected attribute is an object of class '". get_class($_val) ."' without __toString() method", E_USER_NOTICE);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
$selected = (string)$_val;
|
$selected = (string) $_val;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'labels':
|
case 'labels':
|
||||||
case 'label_ids':
|
case 'label_ids':
|
||||||
$$_key = (bool)$_val;
|
$$_key = (bool) $_val;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'options':
|
case 'options':
|
||||||
$$_key = (array)$_val;
|
$$_key = (array) $_val;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'values':
|
case 'values':
|
||||||
case 'output':
|
case 'output':
|
||||||
$$_key = array_values((array)$_val);
|
$$_key = array_values((array) $_val);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'radios':
|
case 'radios':
|
||||||
trigger_error('html_radios: the use of the "radios" attribute is deprecated, use "options" instead', E_USER_WARNING);
|
trigger_error('html_radios: the use of the "radios" attribute is deprecated, use "options" instead', E_USER_WARNING);
|
||||||
$options = (array)$_val;
|
$options = (array) $_val;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'assign':
|
case 'assign':
|
||||||
@@ -103,15 +109,17 @@ function smarty_function_html_radios($params, $template)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isset($options) && !isset($values))
|
if (!isset($options) && !isset($values)) {
|
||||||
|
/* raise error here? */
|
||||||
return '';
|
return '';
|
||||||
/* raise error here? */
|
}
|
||||||
|
|
||||||
$_html_result = array();
|
$_html_result = array();
|
||||||
|
|
||||||
if (isset($options)) {
|
if (isset($options)) {
|
||||||
foreach ($options as $_key => $_val)
|
foreach ($options as $_key => $_val) {
|
||||||
$_html_result[] = smarty_function_html_radios_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids);
|
$_html_result[] = smarty_function_html_radios_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
foreach ($values as $_i => $_key) {
|
foreach ($values as $_i => $_key) {
|
||||||
$_val = isset($output[$_i]) ? $output[$_i] : '';
|
$_val = isset($output[$_i]) ? $output[$_i] : '';
|
||||||
@@ -129,27 +137,58 @@ function smarty_function_html_radios($params, $template)
|
|||||||
function smarty_function_html_radios_output($name, $value, $output, $selected, $extra, $separator, $labels, $label_ids)
|
function smarty_function_html_radios_output($name, $value, $output, $selected, $extra, $separator, $labels, $label_ids)
|
||||||
{
|
{
|
||||||
$_output = '';
|
$_output = '';
|
||||||
|
|
||||||
|
if (is_object($value)) {
|
||||||
|
if (method_exists($value, "__toString")) {
|
||||||
|
$value = (string) $value->__toString();
|
||||||
|
} else {
|
||||||
|
trigger_error("html_options: value is an object of class '". get_class($value) ."' without __toString() method", E_USER_NOTICE);
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$value = (string) $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_object($output)) {
|
||||||
|
if (method_exists($output, "__toString")) {
|
||||||
|
$output = (string) $output->__toString();
|
||||||
|
} else {
|
||||||
|
trigger_error("html_options: output is an object of class '". get_class($output) ."' without __toString() method", E_USER_NOTICE);
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$output = (string) $output;
|
||||||
|
}
|
||||||
|
|
||||||
if ($labels) {
|
if ($labels) {
|
||||||
if ($label_ids) {
|
if ($label_ids) {
|
||||||
$_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!', '_', $name . '_' . $value));
|
$_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!u', '_', $name . '_' . $value));
|
||||||
$_output .= '<label for="' . $_id . '">';
|
$_output .= '<label for="' . $_id . '">';
|
||||||
} else {
|
} else {
|
||||||
$_output .= '<label>';
|
$_output .= '<label>';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$_output .= '<input type="radio" name="'
|
|
||||||
. smarty_function_escape_special_chars($name) . '" value="'
|
$name = smarty_function_escape_special_chars($name);
|
||||||
. smarty_function_escape_special_chars($value) . '"';
|
$value = smarty_function_escape_special_chars($value);
|
||||||
|
$output = smarty_function_escape_special_chars($output);
|
||||||
|
|
||||||
|
$_output .= '<input type="radio" name="' . $name . '" value="' . $value . '"';
|
||||||
|
|
||||||
if ($labels && $label_ids) $_output .= ' id="' . $_id . '"';
|
if ($labels && $label_ids) {
|
||||||
|
$_output .= ' id="' . $_id . '"';
|
||||||
|
}
|
||||||
|
|
||||||
if ((string)$value == $selected) {
|
if ($value === $selected) {
|
||||||
$_output .= ' checked="checked"';
|
$_output .= ' checked="checked"';
|
||||||
}
|
}
|
||||||
|
|
||||||
$_output .= $extra . ' />' . $output;
|
$_output .= $extra . ' />' . $output;
|
||||||
if ($labels) $_output .= '</label>';
|
if ($labels) {
|
||||||
|
$_output .= '</label>';
|
||||||
|
}
|
||||||
|
|
||||||
$_output .= $separator;
|
$_output .= $separator;
|
||||||
|
|
||||||
return $_output;
|
return $_output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user