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