gluon-web-remote

Web remote Administration for big size of gluon routers
git clone git://archive.git.mtrnord.blog/MTRNord/gluon-web-remote.git
Log | Files | Refs | README

function.html_options.php (7146B)


      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 {
     38     require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
     39 
     40     $name = null;
     41     $values = null;
     42     $options = null;
     43     $selected = null;
     44     $output = null;
     45     $id = null;
     46     $class = null;
     47 
     48     $extra = '';
     49 
     50     foreach ($params as $_key => $_val) {
     51         switch ($_key) {
     52             case 'name':
     53             case 'class':
     54             case 'id':
     55                 $$_key = (string) $_val;
     56                 break;
     57 
     58             case 'options':
     59                 $options = (array) $_val;
     60                 break;
     61 
     62             case 'values':
     63             case 'output':
     64                 $$_key = array_values((array) $_val);
     65                 break;
     66 
     67             case 'selected':
     68                 if (is_array($_val)) {
     69                     $selected = array();
     70                     foreach ($_val as $_sel) {
     71                         if (is_object($_sel)) {
     72                             if (method_exists($_sel, "__toString")) {
     73                                 $_sel = smarty_function_escape_special_chars((string) $_sel->__toString());
     74                             } else {
     75                                 trigger_error("html_options: selected attribute contains an object of class '" . get_class($_sel) . "' without __toString() method", E_USER_NOTICE);
     76                                 continue;
     77                             }
     78                         } else {
     79                             $_sel = smarty_function_escape_special_chars((string) $_sel);
     80                         }
     81                         $selected[$_sel] = true;
     82                     }
     83                 } elseif (is_object($_val)) {
     84                     if (method_exists($_val, "__toString")) {
     85                         $selected = smarty_function_escape_special_chars((string) $_val->__toString());
     86                     } else {
     87                         trigger_error("html_options: selected attribute is an object of class '" . get_class($_val) . "' without __toString() method", E_USER_NOTICE);
     88                     }
     89                 } else {
     90                     $selected = smarty_function_escape_special_chars((string) $_val);
     91                 }
     92                 break;
     93 
     94             case 'strict':
     95                 break;
     96 
     97             case 'disabled':
     98             case 'readonly':
     99                 if (!empty($params['strict'])) {
    100                     if (!is_scalar($_val)) {
    101                         trigger_error("html_options: $_key attribute must be a scalar, only boolean true or string '$_key' will actually add the attribute", E_USER_NOTICE);
    102                     }
    103 
    104                     if ($_val === true || $_val === $_key) {
    105                         $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_key) . '"';
    106                     }
    107 
    108                     break;
    109                 }
    110             // omit break; to fall through!
    111 
    112             default:
    113                 if (!is_array($_val)) {
    114                     $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
    115                 } else {
    116                     trigger_error("html_options: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
    117                 }
    118                 break;
    119         }
    120     }
    121 
    122     if (!isset($options) && !isset($values)) {
    123         /* raise error here? */
    124 
    125         return '';
    126     }
    127 
    128     $_html_result = '';
    129     $_idx = 0;
    130 
    131     if (isset($options)) {
    132         foreach ($options as $_key => $_val) {
    133             $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected, $id, $class, $_idx);
    134         }
    135     } else {
    136         foreach ($values as $_i => $_key) {
    137             $_val = isset($output[$_i]) ? $output[$_i] : '';
    138             $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected, $id, $class, $_idx);
    139         }
    140     }
    141 
    142     if (!empty($name)) {
    143         $_html_class = !empty($class) ? ' class="' . $class . '"' : '';
    144         $_html_id = !empty($id) ? ' id="' . $id . '"' : '';
    145         $_html_result = '<select name="' . $name . '"' . $_html_class . $_html_id . $extra . '>' . "\n" . $_html_result . '</select>' . "\n";
    146     }
    147 
    148     return $_html_result;
    149 }
    150 
    151 function smarty_function_html_options_optoutput($key, $value, $selected, $id, $class, &$idx)
    152 {
    153     if (!is_array($value)) {
    154         $_key = smarty_function_escape_special_chars($key);
    155         $_html_result = '<option value="' . $_key . '"';
    156         if (is_array($selected)) {
    157             if (isset($selected[$_key])) {
    158                 $_html_result .= ' selected="selected"';
    159             }
    160         } elseif ($_key === $selected) {
    161             $_html_result .= ' selected="selected"';
    162         }
    163         $_html_class = !empty($class) ? ' class="' . $class . ' option"' : '';
    164         $_html_id = !empty($id) ? ' id="' . $id . '-' . $idx . '"' : '';
    165         if (is_object($value)) {
    166             if (method_exists($value, "__toString")) {
    167                 $value = smarty_function_escape_special_chars((string) $value->__toString());
    168             } else {
    169                 trigger_error("html_options: value is an object of class '" . get_class($value) . "' without __toString() method", E_USER_NOTICE);
    170 
    171                 return '';
    172             }
    173         } else {
    174             $value = smarty_function_escape_special_chars((string) $value);
    175         }
    176         $_html_result .= $_html_class . $_html_id . '>' . $value . '</option>' . "\n";
    177         $idx ++;
    178     } else {
    179         $_idx = 0;
    180         $_html_result = smarty_function_html_options_optgroup($key, $value, $selected, !empty($id) ? ($id . '-' . $idx) : null, $class, $_idx);
    181         $idx ++;
    182     }
    183 
    184     return $_html_result;
    185 }
    186 
    187 function smarty_function_html_options_optgroup($key, $values, $selected, $id, $class, &$idx)
    188 {
    189     $optgroup_html = '<optgroup label="' . smarty_function_escape_special_chars($key) . '">' . "\n";
    190     foreach ($values as $key => $value) {
    191         $optgroup_html .= smarty_function_html_options_optoutput($key, $value, $selected, $id, $class, $idx);
    192     }
    193     $optgroup_html .= "</optgroup>\n";
    194 
    195     return $optgroup_html;
    196 }