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_checkboxes.php (8053B)


      1 <?php
      2 /**
      3  * Smarty plugin
      4  *
      5  * @package    Smarty
      6  * @subpackage PluginsFunction
      7  */
      8 
      9 /**
     10  * Smarty {html_checkboxes} function plugin
     11  * File:       function.html_checkboxes.php<br>
     12  * Type:       function<br>
     13  * Name:       html_checkboxes<br>
     14  * Date:       24.Feb.2003<br>
     15  * Purpose:    Prints out a list of checkbox input types<br>
     16  * Examples:
     17  * <pre>
     18  * {html_checkboxes values=$ids output=$names}
     19  * {html_checkboxes values=$ids name='box' separator='<br>' output=$names}
     20  * {html_checkboxes values=$ids checked=$checked separator='<br>' output=$names}
     21  * </pre>
     22  * Params:
     23  * <pre>
     24  * - name       (optional) - string default "checkbox"
     25  * - values     (required) - array
     26  * - options    (optional) - associative array
     27  * - checked    (optional) - array default not set
     28  * - separator  (optional) - ie <br> or &nbsp;
     29  * - output     (optional) - the output next to each checkbox
     30  * - assign     (optional) - assign the output as an array to this variable
     31  * - escape     (optional) - escape the content (not value), defaults to true
     32  * </pre>
     33  *
     34  * @link       http://www.smarty.net/manual/en/language.function.html.checkboxes.php {html_checkboxes}
     35  *             (Smarty online manual)
     36  * @author     Christopher Kvarme <christopher.kvarme@flashjab.com>
     37  * @author     credits to Monte Ohrt <monte at ohrt dot com>
     38  * @version    1.0
     39  *
     40  * @param array  $params   parameters
     41  * @param object $template template object
     42  *
     43  * @return string
     44  * @uses       smarty_function_escape_special_chars()
     45  */
     46 function smarty_function_html_checkboxes($params, $template)
     47 {
     48     require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
     49 
     50     $name = 'checkbox';
     51     $values = null;
     52     $options = null;
     53     $selected = array();
     54     $separator = '';
     55     $escape = true;
     56     $labels = true;
     57     $label_ids = false;
     58     $output = null;
     59 
     60     $extra = '';
     61 
     62     foreach ($params as $_key => $_val) {
     63         switch ($_key) {
     64             case 'name':
     65             case 'separator':
     66                 $$_key = (string) $_val;
     67                 break;
     68 
     69             case 'escape':
     70             case 'labels':
     71             case 'label_ids':
     72                 $$_key = (bool) $_val;
     73                 break;
     74 
     75             case 'options':
     76                 $$_key = (array) $_val;
     77                 break;
     78 
     79             case 'values':
     80             case 'output':
     81                 $$_key = array_values((array) $_val);
     82                 break;
     83 
     84             case 'checked':
     85             case 'selected':
     86                 if (is_array($_val)) {
     87                     $selected = array();
     88                     foreach ($_val as $_sel) {
     89                         if (is_object($_sel)) {
     90                             if (method_exists($_sel, "__toString")) {
     91                                 $_sel = smarty_function_escape_special_chars((string) $_sel->__toString());
     92                             } else {
     93                                 trigger_error("html_checkboxes: selected attribute contains an object of class '" . get_class($_sel) . "' without __toString() method", E_USER_NOTICE);
     94                                 continue;
     95                             }
     96                         } else {
     97                             $_sel = smarty_function_escape_special_chars((string) $_sel);
     98                         }
     99                         $selected[$_sel] = true;
    100                     }
    101                 } elseif (is_object($_val)) {
    102                     if (method_exists($_val, "__toString")) {
    103                         $selected = smarty_function_escape_special_chars((string) $_val->__toString());
    104                     } else {
    105                         trigger_error("html_checkboxes: selected attribute is an object of class '" . get_class($_val) . "' without __toString() method", E_USER_NOTICE);
    106                     }
    107                 } else {
    108                     $selected = smarty_function_escape_special_chars((string) $_val);
    109                 }
    110                 break;
    111 
    112             case 'checkboxes':
    113                 trigger_error('html_checkboxes: the use of the "checkboxes" attribute is deprecated, use "options" instead', E_USER_WARNING);
    114                 $options = (array) $_val;
    115                 break;
    116 
    117             case 'assign':
    118                 break;
    119 
    120             case 'strict':
    121                 break;
    122 
    123             case 'disabled':
    124             case 'readonly':
    125                 if (!empty($params['strict'])) {
    126                     if (!is_scalar($_val)) {
    127                         trigger_error("html_options: $_key attribute must be a scalar, only boolean true or string '$_key' will actually add the attribute", E_USER_NOTICE);
    128                     }
    129 
    130                     if ($_val === true || $_val === $_key) {
    131                         $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_key) . '"';
    132                     }
    133 
    134                     break;
    135                 }
    136             // omit break; to fall through!
    137 
    138             default:
    139                 if (!is_array($_val)) {
    140                     $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
    141                 } else {
    142                     trigger_error("html_checkboxes: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
    143                 }
    144                 break;
    145         }
    146     }
    147 
    148     if (!isset($options) && !isset($values)) {
    149         return '';
    150     } /* raise error here? */
    151 
    152     $_html_result = array();
    153 
    154     if (isset($options)) {
    155         foreach ($options as $_key => $_val) {
    156             $_html_result[] = smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids, $escape);
    157         }
    158     } else {
    159         foreach ($values as $_i => $_key) {
    160             $_val = isset($output[$_i]) ? $output[$_i] : '';
    161             $_html_result[] = smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids, $escape);
    162         }
    163     }
    164 
    165     if (!empty($params['assign'])) {
    166         $template->assign($params['assign'], $_html_result);
    167     } else {
    168         return implode("\n", $_html_result);
    169     }
    170 }
    171 
    172 function smarty_function_html_checkboxes_output($name, $value, $output, $selected, $extra, $separator, $labels, $label_ids, $escape = true)
    173 {
    174     $_output = '';
    175 
    176     if (is_object($value)) {
    177         if (method_exists($value, "__toString")) {
    178             $value = (string) $value->__toString();
    179         } else {
    180             trigger_error("html_options: value is an object of class '" . get_class($value) . "' without __toString() method", E_USER_NOTICE);
    181 
    182             return '';
    183         }
    184     } else {
    185         $value = (string) $value;
    186     }
    187 
    188     if (is_object($output)) {
    189         if (method_exists($output, "__toString")) {
    190             $output = (string) $output->__toString();
    191         } else {
    192             trigger_error("html_options: output is an object of class '" . get_class($output) . "' without __toString() method", E_USER_NOTICE);
    193 
    194             return '';
    195         }
    196     } else {
    197         $output = (string) $output;
    198     }
    199 
    200     if ($labels) {
    201         if ($label_ids) {
    202             $_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!' . Smarty::$_UTF8_MODIFIER, '_', $name . '_' . $value));
    203             $_output .= '<label for="' . $_id . '">';
    204         } else {
    205             $_output .= '<label>';
    206         }
    207     }
    208 
    209     $name = smarty_function_escape_special_chars($name);
    210     $value = smarty_function_escape_special_chars($value);
    211     if ($escape) {
    212         $output = smarty_function_escape_special_chars($output);
    213     }
    214 
    215     $_output .= '<input type="checkbox" name="' . $name . '[]" value="' . $value . '"';
    216 
    217     if ($labels && $label_ids) {
    218         $_output .= ' id="' . $_id . '"';
    219     }
    220 
    221     if (is_array($selected)) {
    222         if (isset($selected[$value])) {
    223             $_output .= ' checked="checked"';
    224         }
    225     } elseif ($value === $selected) {
    226         $_output .= ' checked="checked"';
    227     }
    228 
    229     $_output .= $extra . ' />' . $output;
    230     if ($labels) {
    231         $_output .= '</label>';
    232     }
    233 
    234     $_output .= $separator;
    235 
    236     return $_output;
    237 }