diff options
author | vdigital <vdigital@piwigo.org> | 2008-05-23 21:05:41 +0000 |
---|---|---|
committer | vdigital <vdigital@piwigo.org> | 2008-05-23 21:05:41 +0000 |
commit | 77fd1f51a3c5f5a52f72ef8a299fe368228e2285 (patch) | |
tree | a67ede42904657ccf3349ecdaef1cec8b8e36ff8 /BSF/include/smarty/libs/plugins/function.html_options.php | |
parent | 553727dffacc48e8337c1d141f2a25af359e74b1 (diff) |
git-svn-id: http://piwigo.org/svn/trunk@2357 68402e56-0260-453c-a942-63ccdbb3a9ee
Diffstat (limited to 'BSF/include/smarty/libs/plugins/function.html_options.php')
-rw-r--r-- | BSF/include/smarty/libs/plugins/function.html_options.php | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/BSF/include/smarty/libs/plugins/function.html_options.php b/BSF/include/smarty/libs/plugins/function.html_options.php new file mode 100644 index 000000000..cebadde47 --- /dev/null +++ b/BSF/include/smarty/libs/plugins/function.html_options.php @@ -0,0 +1,122 @@ +<?php +/** + * Smarty plugin + * @package Smarty + * @subpackage plugins + */ + + +/** + * Smarty {html_options} function plugin + * + * Type: function<br> + * Name: html_options<br> + * Input:<br> + * - name (optional) - string default "select" + * - values (required if no options supplied) - array + * - options (required if no values supplied) - associative array + * - selected (optional) - string default not set + * - output (required if not options supplied) - array + * Purpose: Prints the list of <option> tags generated from + * the passed parameters + * @link http://smarty.php.net/manual/en/language.function.html.options.php {html_image} + * (Smarty online manual) + * @author Monte Ohrt <monte at ohrt dot com> + * @param array + * @param Smarty + * @return string + * @uses smarty_function_escape_special_chars() + */ +function smarty_function_html_options($params, &$smarty) +{ + require_once $smarty->_get_plugin_filepath('shared','escape_special_chars'); + + $name = null; + $values = null; + $options = null; + $selected = array(); + $output = null; + + $extra = ''; + + foreach($params as $_key => $_val) { + switch($_key) { + case 'name': + $$_key = (string)$_val; + break; + + case 'options': + $$_key = (array)$_val; + break; + + case 'values': + case 'output': + $$_key = array_values((array)$_val); + break; + + case 'selected': + $$_key = array_map('strval', array_values((array)$_val)); + break; + + default: + if(!is_array($_val)) { + $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"'; + } else { + $smarty->trigger_error("html_options: extra attribute '$_key' cannot be an array", E_USER_NOTICE); + } + break; + } + } + + if (!isset($options) && !isset($values)) + return ''; /* raise error here? */ + + $_html_result = ''; + + if (isset($options)) { + + foreach ($options as $_key=>$_val) + $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected); + + } else { + + foreach ($values as $_i=>$_key) { + $_val = isset($output[$_i]) ? $output[$_i] : ''; + $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected); + } + + } + + if(!empty($name)) { + $_html_result = '<select name="' . $name . '"' . $extra . '>' . "\n" . $_html_result . '</select>' . "\n"; + } + + return $_html_result; + +} + +function smarty_function_html_options_optoutput($key, $value, $selected) { + if(!is_array($value)) { + $_html_result = '<option label="' . smarty_function_escape_special_chars($value) . '" value="' . + smarty_function_escape_special_chars($key) . '"'; + if (in_array((string)$key, $selected)) + $_html_result .= ' selected="selected"'; + $_html_result .= '>' . smarty_function_escape_special_chars($value) . '</option>' . "\n"; + } else { + $_html_result = smarty_function_html_options_optgroup($key, $value, $selected); + } + return $_html_result; +} + +function smarty_function_html_options_optgroup($key, $values, $selected) { + $optgroup_html = '<optgroup label="' . smarty_function_escape_special_chars($key) . '">' . "\n"; + foreach ($values as $key => $value) { + $optgroup_html .= smarty_function_html_options_optoutput($key, $value, $selected); + } + $optgroup_html .= "</optgroup>\n"; + return $optgroup_html; +} + +/* vim: set expandtab: */ + +?> |