Chris PeBenito 696b41
Chris PeBenito 696b41
/**
Chris PeBenito 696b41
 * Smarty plugin
Chris PeBenito 696b41
 * @package Smarty
Chris PeBenito 696b41
 * @subpackage plugins
Chris PeBenito 696b41
 */
Chris PeBenito 696b41
Chris PeBenito 696b41
Chris PeBenito 696b41
/**
Chris PeBenito 696b41
 * Smarty {html_options} function plugin
Chris PeBenito 696b41
 *
Chris PeBenito 696b41
 * Type:     function
Chris PeBenito 696b41
 * Name:     html_options
Chris PeBenito 696b41
 * Input:
Chris PeBenito 696b41
 *           - name       (optional) - string default "select"
Chris PeBenito 696b41
 *           - values     (required if no options supplied) - array
Chris PeBenito 696b41
 *           - options    (required if no values supplied) - associative array
Chris PeBenito 696b41
 *           - selected   (optional) - string default not set
Chris PeBenito 696b41
 *           - output     (required if not options supplied) - array
Chris PeBenito 696b41
 * Purpose:  Prints the list of <option> tags generated from
Chris PeBenito 696b41
 *           the passed parameters
Chris PeBenito 696b41
 * @link http://smarty.php.net/manual/en/language.function.html.options.php {html_image}
Chris PeBenito 696b41
 *      (Smarty online manual)
Chris PeBenito 696b41
 * @param array
Chris PeBenito 696b41
 * @param Smarty
Chris PeBenito 696b41
 * @return string
Chris PeBenito 696b41
 * @uses smarty_function_escape_special_chars()
Chris PeBenito 696b41
 */
Chris PeBenito 696b41
function smarty_function_html_options($params, &$smarty)
Chris PeBenito 696b41
{
Chris PeBenito 696b41
    require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
Chris PeBenito 696b41
    
Chris PeBenito 696b41
    $name = null;
Chris PeBenito 696b41
    $values = null;
Chris PeBenito 696b41
    $options = null;
Chris PeBenito 696b41
    $selected = array();
Chris PeBenito 696b41
    $output = null;
Chris PeBenito 696b41
    
Chris PeBenito 696b41
    $extra = '';
Chris PeBenito 696b41
    
Chris PeBenito 696b41
    foreach($params as $_key => $_val) {
Chris PeBenito 696b41
        switch($_key) {
Chris PeBenito 696b41
            case 'name':
Chris PeBenito 696b41
                $$_key = (string)$_val;
Chris PeBenito 696b41
                break;
Chris PeBenito 696b41
            
Chris PeBenito 696b41
            case 'options':
Chris PeBenito 696b41
                $$_key = (array)$_val;
Chris PeBenito 696b41
                break;
Chris PeBenito 696b41
                
Chris PeBenito 696b41
            case 'values':
Chris PeBenito 696b41
            case 'output':
Chris PeBenito 696b41
                $$_key = array_values((array)$_val);
Chris PeBenito 696b41
                break;
Chris PeBenito 696b41
Chris PeBenito 696b41
            case 'selected':
Chris PeBenito 696b41
                $$_key = array_map('strval', array_values((array)$_val));
Chris PeBenito 696b41
                break;
Chris PeBenito 696b41
                
Chris PeBenito 696b41
            default:
Chris PeBenito 696b41
                if(!is_array($_val)) {
Chris PeBenito 696b41
                    $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
Chris PeBenito 696b41
                } else {
Chris PeBenito 696b41
                    $smarty->trigger_error("html_options: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
Chris PeBenito 696b41
                }
Chris PeBenito 696b41
                break;
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    if (!isset($options) && !isset($values))
Chris PeBenito 696b41
        return ''; /* raise error here? */
Chris PeBenito 696b41
Chris PeBenito 696b41
    $_html_result = '';
Chris PeBenito 696b41
Chris PeBenito 696b41
    if (isset($options)) {
Chris PeBenito 696b41
        
Chris PeBenito 696b41
        foreach ($options as $_key=>$_val)
Chris PeBenito 696b41
            $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected);
Chris PeBenito 696b41
Chris PeBenito 696b41
    } else {
Chris PeBenito 696b41
        
Chris PeBenito 696b41
        foreach ($values as $_i=>$_key) {
Chris PeBenito 696b41
            $_val = isset($output[$_i]) ? $output[$_i] : '';
Chris PeBenito 696b41
            $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected);
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    if(!empty($name)) {
Chris PeBenito 696b41
        $_html_result = '<select name="' . $name . '"' . $extra . '>' . "\n" . $_html_result . '</select>' . "\n";
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    return $_html_result;
Chris PeBenito 696b41
Chris PeBenito 696b41
}
Chris PeBenito 696b41
Chris PeBenito 696b41
function smarty_function_html_options_optoutput($key, $value, $selected) {
Chris PeBenito 696b41
    if(!is_array($value)) {
Chris PeBenito 696b41
        $_html_result = '
Chris PeBenito 696b41
            smarty_function_escape_special_chars($key) . '"';
Chris PeBenito 696b41
        if (in_array((string)$key, $selected))
Chris PeBenito 696b41
            $_html_result .= ' selected="selected"';
Chris PeBenito 696b41
        $_html_result .= '>' . smarty_function_escape_special_chars($value) . '</option>' . "\n";
Chris PeBenito 696b41
    } else {
Chris PeBenito 696b41
        $_html_result = smarty_function_html_options_optgroup($key, $value, $selected);
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
    return $_html_result;
Chris PeBenito 696b41
}
Chris PeBenito 696b41
Chris PeBenito 696b41
function smarty_function_html_options_optgroup($key, $values, $selected) {
Chris PeBenito 696b41
    $optgroup_html = '<optgroup label="' . smarty_function_escape_special_chars($key) . '">' . "\n";
Chris PeBenito 696b41
    foreach ($values as $key => $value) {
Chris PeBenito 696b41
        $optgroup_html .= smarty_function_html_options_optoutput($key, $value, $selected);
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
    $optgroup_html .= "</optgroup>\n";
Chris PeBenito 696b41
    return $optgroup_html;
Chris PeBenito 696b41
}
Chris PeBenito 696b41
Chris PeBenito 696b41
/* vim: set expandtab: */
Chris PeBenito 696b41
Chris PeBenito 696b41
?>