/[cvs]/nfo/php/libs/net.php.smarty/plugins/function.html_options.php
ViewVC logotype

Diff of /nfo/php/libs/net.php.smarty/plugins/function.html_options.php

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by cvsjoko, Wed Oct 9 00:53:36 2002 UTC revision 1.3 by joko, Wed Jun 16 21:58:16 2004 UTC
# Line 1  Line 1 
1  <?php  <?php
2    /**
 /*  
3   * Smarty plugin   * Smarty plugin
4   * -------------------------------------------------------------   * @package Smarty
5   * Type:     function   * @subpackage plugins
6   * Name:     html_options   */
7    
8    
9    /**
10     * Smarty {html_options} function plugin
11     *
12     * Type:     function<br>
13     * Name:     html_options<br>
14     * Input:<br>
15     *           - name       (optional) - string default "select"
16     *           - values     (required if no options supplied) - array
17     *           - options    (required if no values supplied) - associative array
18     *           - selected   (optional) - string default not set
19     *           - output     (required if not options supplied) - array
20   * Purpose:  Prints the list of <option> tags generated from   * Purpose:  Prints the list of <option> tags generated from
21   *           the passed parameters   *           the passed parameters
22   * -------------------------------------------------------------   * @link http://smarty.php.net/manual/en/language.function.html.options.php {html_image}
23     *      (Smarty online manual)
24     * @param array
25     * @param Smarty
26     * @return string
27     * @uses smarty_function_escape_special_chars()
28   */   */
29  function smarty_function_html_options($params, &$smarty)  function smarty_function_html_options($params, &$smarty)
30  {  {
31      $print_result = true;      require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
32        
33        $name = null;
34        $values = null;
35        $options = null;
36        $selected = array();
37        $output = null;
38        
39        $extra = '';
40        
41        foreach($params as $_key => $_val) {
42            switch($_key) {
43                case 'name':
44                    $$_key = (string)$_val;
45                    break;
46                
47                case 'options':
48                    $$_key = (array)$_val;
49                    break;
50                    
51                case 'values':
52                case 'output':
53                    $$_key = array_values((array)$_val);
54                    break;
55    
56                case 'selected':
57                    $$_key = array_map('strval', array_values((array)$_val));
58                    break;
59                    
60                default:
61                    if(!is_array($_val)) {
62                        $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
63                    } else {
64                        $smarty->trigger_error("html_options: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
65                    }
66                    break;
67            }
68        }
69    
70      extract($params);      if (!isset($options) && !isset($values))
71            return ''; /* raise error here? */
72    
73      $html_result = '';      $_html_result = '';
74    
75        if (is_array($options)) {
76            
77            foreach ($options as $_key=>$_val)
78                $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected);
79    
     settype($selected, 'array');  
     if (isset($options)) {  
         settype($options, 'array');  
         foreach ($options as $key => $value) {  
             $html_result .= "<option value=\"$key\"";  
             if (in_array($key, $selected))  
                 $html_result .= " selected=\"selected\"";  
             $html_result .= ">$value</option>\n";  
         }  
80      } else {      } else {
81          settype($output, 'array');          
82          settype($values, 'array');          foreach ((array)$values as $_i=>$_key) {
83          for ($i = 0, $for_max = count($output); $i < $for_max; $i++) {              $_val = isset($output[$_i]) ? $output[$_i] : '';
84              /* By default, check value against $selected */              $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected);
             $sel_check = $values[$i];  
             $html_result .= "<option";  
             if ($i < count($values))  
                 $html_result .= " value=\"".$values[$i]."\"";  
             else  
                 $sel_check = $output[$i];       /* if more outputs than values, then  
                                                    check output against $selected */  
             if (in_array($sel_check, $selected))  
                 $html_result .= " selected=\"selected\"";  
             $html_result .= ">".$output[$i]."</option>\n";  
85          }          }
86    
87      }      }
88    
89      if ($print_result)      if(!empty($name)) {
90          print $html_result;          $_html_result = '<select name="' . $name . '"' . $extra . '>' . "\n" . $_html_result . '</select>' . "\n";
91      else      }
92          return $html_result;  
93        return $_html_result;
94    
95    }
96    
97    function smarty_function_html_options_optoutput($key, $value, $selected) {
98        if(!is_array($value)) {
99            $_html_result = '<option label="' . smarty_function_escape_special_chars($value) . '" value="' .
100                smarty_function_escape_special_chars($key) . '"';
101            if (in_array((string)$key, $selected))
102                $_html_result .= ' selected="selected"';
103            $_html_result .= '>' . smarty_function_escape_special_chars($value) . '</option>' . "\n";
104        } else {
105            $_html_result = smarty_function_html_options_optgroup($key, $value, $selected);
106        }
107        return $_html_result;
108    }
109    
110    function smarty_function_html_options_optgroup($key, $values, $selected) {
111        $optgroup_html = '<optgroup label="' . smarty_function_escape_special_chars($key) . '">' . "\n";
112        foreach ($values as $key => $value) {
113            $optgroup_html .= smarty_function_html_options_optoutput($key, $value, $selected);
114        }
115        $optgroup_html .= "</optgroup>\n";
116        return $optgroup_html;
117  }  }
118    
119  /* vim: set expandtab: */  /* vim: set expandtab: */

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.3

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed