PageRenderTime 13ms CodeModel.GetById 1ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/vendor/smarty3/lib/libs/plugins/function.html_options.php

http://zoop.googlecode.com/
PHP | 122 lines | 74 code | 17 blank | 31 comment | 11 complexity | 4108adff2e8b0114553b9849fe6db9cc MD5 | raw file
  1<?php
  2/**
  3* Smarty plugin
  4* 
  5* @package Smarty
  6* @subpackage PluginsFunction
  7*/
  8
  9/**
 10* Smarty {html_options} function plugin
 11* 
 12* Type:     function<br>
 13* Name:     html_options<br>
 14* Purpose:  Prints the list of <option> tags generated from
 15*            the passed parameters
 16* 
 17* @link http://smarty.php.net/manual/en/language.function.html.options.php {html_image}
 18      (Smarty online manual)
 19* @author Monte Ohrt <monte at ohrt dot com> 
 20* @param array $params parameters
 21* Input:<br>
 22*            - name       (optional) - string default "select"
 23*            - values     (required if no options supplied) - array
 24*            - options    (required if no values supplied) - associative array
 25*            - selected   (optional) - string default not set
 26*            - output     (required if not options supplied) - array
 27* @param object $smarty Smarty object
 28* @param object $template template object
 29* @return string 
 30* @uses smarty_function_escape_special_chars()
 31*/
 32
 33function smarty_function_html_options($params, $smarty, $template)
 34{
 35    require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
 36    //$smarty->loadPlugin('Smarty_shared_escape_special_chars');
 37
 38    $name = null;
 39    $values = null;
 40    $options = null;
 41    $selected = array();
 42    $output = null;
 43
 44    $extra = '';
 45
 46    foreach($params as $_key => $_val) {
 47        switch ($_key) {
 48            case 'name':
 49                $$_key = (string)$_val;
 50                break;
 51
 52            case 'options':
 53                $$_key = (array)$_val;
 54                break;
 55
 56            case 'values':
 57            case 'output':
 58                $$_key = array_values((array)$_val);
 59                break;
 60
 61            case 'selected':
 62                $$_key = array_map('strval', array_values((array)$_val));
 63                break;
 64
 65            default:
 66                if (!is_array($_val)) {
 67                    $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
 68                } else {
 69                    trigger_error("html_options: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
 70                } 
 71                break;
 72        } 
 73    } 
 74
 75    if (!isset($options) && !isset($values))
 76        return '';
 77    /* raise error here? */
 78
 79    $_html_result = '';
 80
 81    if (isset($options)) {
 82        foreach ($options as $_key => $_val)
 83        $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected);
 84    } else {
 85        foreach ($values as $_i => $_key) {
 86            $_val = isset($output[$_i]) ? $output[$_i] : '';
 87            $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected);
 88        } 
 89    } 
 90
 91    if (!empty($name)) {
 92        $_html_result = '<select name="' . $name . '"' . $extra . '>' . "\n" . $_html_result . '</select>' . "\n";
 93    } 
 94
 95    return $_html_result;
 96} 
 97
 98function smarty_function_html_options_optoutput($key, $value, $selected)
 99{
100    if (!is_array($value)) {
101        $_html_result = '<option label="' . smarty_function_escape_special_chars($value) . '" value="' .
102        smarty_function_escape_special_chars($key) . '"';
103        if (in_array((string)$key, $selected))
104            $_html_result .= ' selected="selected"';
105        $_html_result .= '>' . smarty_function_escape_special_chars($value) . '</option>' . "\n";
106    } else {
107        $_html_result = smarty_function_html_options_optgroup($key, $value, $selected);
108    } 
109    return $_html_result;
110} 
111
112function smarty_function_html_options_optgroup($key, $values, $selected)
113{
114    $optgroup_html = '<optgroup label="' . smarty_function_escape_special_chars($key) . '">' . "\n";
115    foreach ($values as $key => $value) {
116        $optgroup_html .= smarty_function_html_options_optoutput($key, $value, $selected);
117    } 
118    $optgroup_html .= "</optgroup>\n";
119    return $optgroup_html;
120} 
121
122?>