library/smarty/libs/plugins/function.html_options.php
changeset 0 4869aea77e21
equal deleted inserted replaced
-1:000000000000 0:4869aea77e21
       
     1 <?php
       
     2 /**
       
     3  * Smarty plugin
       
     4  *
       
     5  * @package    Smarty
       
     6  * @subpackage PluginsFunction
       
     7  */
       
     8 
       
     9 /**
       
    10  * Smarty {html_options} function plugin
       
    11  * Type:     function<br>
       
    12  * Name:     html_options<br>
       
    13  * Purpose:  Prints the list of <option> tags generated from
       
    14  *           the passed parameters<br>
       
    15  * Params:
       
    16  * <pre>
       
    17  * - name       (optional) - string default "select"
       
    18  * - values     (required) - if no options supplied) - array
       
    19  * - options    (required) - if no values supplied) - associative array
       
    20  * - selected   (optional) - string default not set
       
    21  * - output     (required) - if not options supplied) - array
       
    22  * - id         (optional) - string default not set
       
    23  * - class      (optional) - string default not set
       
    24  * </pre>
       
    25  *
       
    26  * @link     http://www.smarty.net/manual/en/language.function.html.options.php {html_image}
       
    27  *           (Smarty online manual)
       
    28  * @author   Monte Ohrt <monte at ohrt dot com>
       
    29  * @author   Ralf Strehle (minor optimization) <ralf dot strehle at yahoo dot de>
       
    30  *
       
    31  * @param array $params parameters
       
    32  *
       
    33  * @return string
       
    34  * @uses     smarty_function_escape_special_chars()
       
    35  */
       
    36 function smarty_function_html_options($params) {
       
    37     require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
       
    38 
       
    39     $name = null;
       
    40     $values = null;
       
    41     $options = null;
       
    42     $selected = null;
       
    43     $output = null;
       
    44     $id = null;
       
    45     $class = null;
       
    46 
       
    47     $extra = '';
       
    48 
       
    49     foreach ($params as $_key => $_val) {
       
    50         switch ($_key) {
       
    51             case 'name':
       
    52             case 'class':
       
    53             case 'id':
       
    54                 $$_key = (string)$_val;
       
    55                 break;
       
    56 
       
    57             case 'options':
       
    58                 $options = (array)$_val;
       
    59                 break;
       
    60 
       
    61             case 'values':
       
    62             case 'output':
       
    63                 $$_key = array_values((array)$_val);
       
    64                 break;
       
    65 
       
    66             case 'selected':
       
    67                 if (is_array($_val)) {
       
    68                     $selected = array();
       
    69                     foreach ($_val as $_sel) {
       
    70                         if (is_object($_sel)) {
       
    71                             if (method_exists($_sel, "__toString")) {
       
    72                                 $_sel = smarty_function_escape_special_chars((string)$_sel->__toString());
       
    73                             } else {
       
    74                                 trigger_error("html_options: selected attribute contains an object of class '" . get_class($_sel) . "' without __toString() method", E_USER_NOTICE);
       
    75                                 continue;
       
    76                             }
       
    77                         } else {
       
    78                             $_sel = smarty_function_escape_special_chars((string)$_sel);
       
    79                         }
       
    80                         $selected[$_sel] = true;
       
    81                     }
       
    82                 } elseif (is_object($_val)) {
       
    83                     if (method_exists($_val, "__toString")) {
       
    84                         $selected = smarty_function_escape_special_chars((string)$_val->__toString());
       
    85                     } else {
       
    86                         trigger_error("html_options: selected attribute is an object of class '" . get_class($_val) . "' without __toString() method", E_USER_NOTICE);
       
    87                     }
       
    88                 } else {
       
    89                     $selected = smarty_function_escape_special_chars((string)$_val);
       
    90                 }
       
    91                 break;
       
    92 
       
    93             case 'strict':
       
    94                 break;
       
    95 
       
    96             case 'disabled':
       
    97             case 'readonly':
       
    98                 if (!empty($params['strict'])) {
       
    99                     if (!is_scalar($_val)) {
       
   100                         trigger_error("html_options: $_key attribute must be a scalar, only boolean true or string '$_key' will actually add the attribute", E_USER_NOTICE);
       
   101                     }
       
   102 
       
   103                     if ($_val === true || $_val === $_key) {
       
   104                         $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_key) . '"';
       
   105                     }
       
   106 
       
   107                     break;
       
   108                 }
       
   109             // omit break; to fall through!
       
   110 
       
   111             default:
       
   112                 if (!is_array($_val)) {
       
   113                     $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
       
   114                 } else {
       
   115                     trigger_error("html_options: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
       
   116                 }
       
   117                 break;
       
   118         }
       
   119     }
       
   120 
       
   121     if (!isset($options) && !isset($values)) {
       
   122         /* raise error here? */
       
   123 
       
   124         return '';
       
   125     }
       
   126 
       
   127     $_html_result = '';
       
   128     $_idx = 0;
       
   129 
       
   130     if (isset($options)) {
       
   131         foreach ($options as $_key => $_val) {
       
   132             $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected, $id, $class, $_idx);
       
   133         }
       
   134     } else {
       
   135         foreach ($values as $_i => $_key) {
       
   136             $_val = isset($output[$_i]) ? $output[$_i] : '';
       
   137             $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected, $id, $class, $_idx);
       
   138         }
       
   139     }
       
   140 
       
   141     if (!empty($name)) {
       
   142         $_html_class = !empty($class) ? ' class="' . $class . '"' : '';
       
   143         $_html_id = !empty($id) ? ' id="' . $id . '"' : '';
       
   144         $_html_result = '<select name="' . $name . '"' . $_html_class . $_html_id . $extra . '>' . "\n" . $_html_result . '</select>' . "\n";
       
   145     }
       
   146 
       
   147     return $_html_result;
       
   148 }
       
   149 
       
   150 function smarty_function_html_options_optoutput($key, $value, $selected, $id, $class, &$idx) {
       
   151     if (!is_array($value)) {
       
   152         $_key = smarty_function_escape_special_chars($key);
       
   153         $_html_result = '<option value="' . $_key . '"';
       
   154         if (is_array($selected)) {
       
   155             if (isset($selected[$_key])) {
       
   156                 $_html_result .= ' selected="selected"';
       
   157             }
       
   158         } elseif ($_key === $selected) {
       
   159             $_html_result .= ' selected="selected"';
       
   160         }
       
   161         $_html_class = !empty($class) ? ' class="' . $class . ' option"' : '';
       
   162         $_html_id = !empty($id) ? ' id="' . $id . '-' . $idx . '"' : '';
       
   163         if (is_object($value)) {
       
   164             if (method_exists($value, "__toString")) {
       
   165                 $value = smarty_function_escape_special_chars((string)$value->__toString());
       
   166             } else {
       
   167                 trigger_error("html_options: value is an object of class '" . get_class($value) . "' without __toString() method", E_USER_NOTICE);
       
   168 
       
   169                 return '';
       
   170             }
       
   171         } else {
       
   172             $value = smarty_function_escape_special_chars((string)$value);
       
   173         }
       
   174         $_html_result .= $_html_class . $_html_id . '>' . $value . '</option>' . "\n";
       
   175         $idx++;
       
   176     } else {
       
   177         $_idx = 0;
       
   178         $_html_result = smarty_function_html_options_optgroup($key, $value, $selected, !empty($id) ? ($id . '-' . $idx) : null, $class, $_idx);
       
   179         $idx++;
       
   180     }
       
   181 
       
   182     return $_html_result;
       
   183 }
       
   184 
       
   185 function smarty_function_html_options_optgroup($key, $values, $selected, $id, $class, &$idx) {
       
   186     $optgroup_html = '<optgroup label="' . smarty_function_escape_special_chars($key) . '">' . "\n";
       
   187     foreach ($values as $key => $value) {
       
   188         $optgroup_html .= smarty_function_html_options_optoutput($key, $value, $selected, $id, $class, $idx);
       
   189     }
       
   190     $optgroup_html .= "</optgroup>\n";
       
   191 
       
   192     return $optgroup_html;
       
   193 }