library/smarty/libs/plugins/function.html_checkboxes.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_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     require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php');
       
    48 
       
    49     $name = 'checkbox';
       
    50     $values = null;
       
    51     $options = null;
       
    52     $selected = array();
       
    53     $separator = '';
       
    54     $escape = true;
       
    55     $labels = true;
       
    56     $label_ids = false;
       
    57     $output = null;
       
    58 
       
    59     $extra = '';
       
    60 
       
    61     foreach ($params as $_key => $_val) {
       
    62         switch ($_key) {
       
    63             case 'name':
       
    64             case 'separator':
       
    65                 $$_key = (string)$_val;
       
    66                 break;
       
    67 
       
    68             case 'escape':
       
    69             case 'labels':
       
    70             case 'label_ids':
       
    71                 $$_key = (bool)$_val;
       
    72                 break;
       
    73 
       
    74             case 'options':
       
    75                 $$_key = (array)$_val;
       
    76                 break;
       
    77 
       
    78             case 'values':
       
    79             case 'output':
       
    80                 $$_key = array_values((array)$_val);
       
    81                 break;
       
    82 
       
    83             case 'checked':
       
    84             case 'selected':
       
    85                 if (is_array($_val)) {
       
    86                     $selected = array();
       
    87                     foreach ($_val as $_sel) {
       
    88                         if (is_object($_sel)) {
       
    89                             if (method_exists($_sel, "__toString")) {
       
    90                                 $_sel = smarty_function_escape_special_chars((string)$_sel->__toString());
       
    91                             } else {
       
    92                                 trigger_error("html_checkboxes: selected attribute contains an object of class '" . get_class($_sel) . "' without __toString() method", E_USER_NOTICE);
       
    93                                 continue;
       
    94                             }
       
    95                         } else {
       
    96                             $_sel = smarty_function_escape_special_chars((string)$_sel);
       
    97                         }
       
    98                         $selected[$_sel] = true;
       
    99                     }
       
   100                 } elseif (is_object($_val)) {
       
   101                     if (method_exists($_val, "__toString")) {
       
   102                         $selected = smarty_function_escape_special_chars((string)$_val->__toString());
       
   103                     } else {
       
   104                         trigger_error("html_checkboxes: selected attribute is an object of class '" . get_class($_val) . "' without __toString() method", E_USER_NOTICE);
       
   105                     }
       
   106                 } else {
       
   107                     $selected = smarty_function_escape_special_chars((string)$_val);
       
   108                 }
       
   109                 break;
       
   110 
       
   111             case 'checkboxes':
       
   112                 trigger_error('html_checkboxes: the use of the "checkboxes" attribute is deprecated, use "options" instead', E_USER_WARNING);
       
   113                 $options = (array)$_val;
       
   114                 break;
       
   115 
       
   116             case 'assign':
       
   117                 break;
       
   118 
       
   119             case 'strict':
       
   120                 break;
       
   121 
       
   122             case 'disabled':
       
   123             case 'readonly':
       
   124                 if (!empty($params['strict'])) {
       
   125                     if (!is_scalar($_val)) {
       
   126                         trigger_error("html_options: $_key attribute must be a scalar, only boolean true or string '$_key' will actually add the attribute", E_USER_NOTICE);
       
   127                     }
       
   128 
       
   129                     if ($_val === true || $_val === $_key) {
       
   130                         $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_key) . '"';
       
   131                     }
       
   132 
       
   133                     break;
       
   134                 }
       
   135             // omit break; to fall through!
       
   136 
       
   137             default:
       
   138                 if (!is_array($_val)) {
       
   139                     $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
       
   140                 } else {
       
   141                     trigger_error("html_checkboxes: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
       
   142                 }
       
   143                 break;
       
   144         }
       
   145     }
       
   146 
       
   147     if (!isset($options) && !isset($values)) {
       
   148         return '';
       
   149     } /* raise error here? */
       
   150 
       
   151     $_html_result = array();
       
   152 
       
   153     if (isset($options)) {
       
   154         foreach ($options as $_key => $_val) {
       
   155             $_html_result[] = smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids, $escape);
       
   156         }
       
   157     } else {
       
   158         foreach ($values as $_i => $_key) {
       
   159             $_val = isset($output[$_i]) ? $output[$_i] : '';
       
   160             $_html_result[] = smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids, $escape);
       
   161         }
       
   162     }
       
   163 
       
   164     if (!empty($params['assign'])) {
       
   165         $template->assign($params['assign'], $_html_result);
       
   166     } else {
       
   167         return implode("\n", $_html_result);
       
   168     }
       
   169 }
       
   170 
       
   171 function smarty_function_html_checkboxes_output($name, $value, $output, $selected, $extra, $separator, $labels, $label_ids, $escape = true) {
       
   172     $_output = '';
       
   173 
       
   174     if (is_object($value)) {
       
   175         if (method_exists($value, "__toString")) {
       
   176             $value = (string)$value->__toString();
       
   177         } else {
       
   178             trigger_error("html_options: value is an object of class '" . get_class($value) . "' without __toString() method", E_USER_NOTICE);
       
   179 
       
   180             return '';
       
   181         }
       
   182     } else {
       
   183         $value = (string)$value;
       
   184     }
       
   185 
       
   186     if (is_object($output)) {
       
   187         if (method_exists($output, "__toString")) {
       
   188             $output = (string)$output->__toString();
       
   189         } else {
       
   190             trigger_error("html_options: output is an object of class '" . get_class($output) . "' without __toString() method", E_USER_NOTICE);
       
   191 
       
   192             return '';
       
   193         }
       
   194     } else {
       
   195         $output = (string)$output;
       
   196     }
       
   197 
       
   198     if ($labels) {
       
   199         if ($label_ids) {
       
   200             $_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!' . Smarty::$_UTF8_MODIFIER, '_', $name . '_' . $value));
       
   201             $_output .= '<label for="' . $_id . '">';
       
   202         } else {
       
   203             $_output .= '<label>';
       
   204         }
       
   205     }
       
   206 
       
   207     $name = smarty_function_escape_special_chars($name);
       
   208     $value = smarty_function_escape_special_chars($value);
       
   209     if ($escape) {
       
   210         $output = smarty_function_escape_special_chars($output);
       
   211     }
       
   212 
       
   213     $_output .= '<input type="checkbox" name="' . $name . '[]" value="' . $value . '"';
       
   214 
       
   215     if ($labels && $label_ids) {
       
   216         $_output .= ' id="' . $_id . '"';
       
   217     }
       
   218 
       
   219     if (is_array($selected)) {
       
   220         if (isset($selected[$value])) {
       
   221             $_output .= ' checked="checked"';
       
   222         }
       
   223     } elseif ($value === $selected) {
       
   224         $_output .= ' checked="checked"';
       
   225     }
       
   226 
       
   227     $_output .= $extra . ' />' . $output;
       
   228     if ($labels) {
       
   229         $_output .= '</label>';
       
   230     }
       
   231 
       
   232     $_output .= $separator;
       
   233 
       
   234     return $_output;
       
   235 }