source: branches/feature-module-paygent/data/smarty_extends/function.html_radios_ex.php @ 8

Revision 8, 5.0 KB checked in by root, 17 years ago (diff)

new import

Line 
1<?php
2/**
3 * Smarty plugin
4 * @package Smarty
5 * @subpackage plugins
6 */
7
8
9/**
10 * Smarty {html_radios} function plugin
11 *
12 * File:       function.html_radios.php<br>
13 * Type:       function<br>
14 * Name:       html_radios<br>
15 * Date:       24.Feb.2003<br>
16 * Purpose:    Prints out a list of radio input types<br>
17 * Input:<br>
18 *           - name       (optional) - string default "radio"
19 *           - values     (required) - array
20 *           - options    (optional) - 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 * Examples:
26 * <pre>
27 * {html_radios values=$ids output=$names}
28 * {html_radios values=$ids name='box' separator='<br>' output=$names}
29 * {html_radios values=$ids checked=$checked separator='<br>' output=$names}
30 * </pre>
31 * @link http://smarty.php.net/manual/en/language.function.html.radios.php {html_radios}
32 *      (Smarty online manual)
33 * @author     Christopher Kvarme <christopher.kvarme@flashjab.com>
34 * @author credits to Monte Ohrt <monte at ohrt dot com>
35 * @version    1.0
36 * @param array
37 * @param Smarty
38 * @return string
39 * @uses smarty_function_escape_special_chars()
40 */
41function smarty_function_html_radios_ex($params, &$smarty)
42{
43    require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
44   
45    $name = 'radio';
46    $values = null;
47    $options = null;
48    $selected = null;
49    $separator = '';
50    $labels = true;
51    $label_ids = true;
52    $output = null;
53    $extra = '';
54
55    foreach($params as $_key => $_val) {
56        switch($_key) {
57            case 'tags':
58                $$_key = split("\|", $_val);
59                break;
60            case 'name':
61            case 'separator':
62                $$_key = (string)$_val;
63                break;
64
65            case 'checked':
66            case 'selected':
67                if(is_array($_val)) {
68                    $smarty->trigger_error('html_radios: the "' . $_key . '" attribute cannot be an array', E_USER_WARNING);
69                } else {
70                    $selected = (string)$_val;
71                }
72                break;
73
74            case 'labels':
75            case 'label_ids':
76                $$_key = (bool)$_val;
77                break;
78
79            case 'options':
80                $$_key = (array)$_val;
81                break;
82            case 'values':
83            case 'output':
84                $$_key = array_values((array)$_val);
85                break;
86
87            case 'radios':
88                $smarty->trigger_error('html_radios: the use of the "radios" attribute is deprecated, use "options" instead', E_USER_WARNING);
89                $options = (array)$_val;
90                break;
91
92            case 'assign':
93                break;
94           
95            default:
96                if(!is_array($_val)) {
97                    $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
98                } else {
99                    $smarty->trigger_error("html_radios: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
100                }
101                break;
102        }
103    }
104
105    if (!isset($options) && !isset($values))
106        return ''; /* raise error here? */
107
108    $_html_result = array();
109
110    if (isset($options)) {
111
112        foreach ($options as $_key=>$_val)
113            $_html_result[] = smarty_function_html_radios_output_ex($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids, $tags);
114
115    } else {
116
117        foreach ($values as $_i=>$_key) {
118            $_val = isset($output[$_i]) ? $output[$_i] : '';
119            $_html_result[] = smarty_function_html_radios_output_ex($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids, $tags);
120        }
121
122    }
123
124    if(!empty($params['assign'])) {
125        $smarty->assign($params['assign'], $_html_result);
126    } else {
127        return implode("\n",$_html_result);
128    }
129
130}
131
132function smarty_function_html_radios_output_ex($name, $value, $output, $selected, $extra, $separator, $labels, $label_ids, $tags) {
133   $_output = '';
134 
135   $_output .= '<input type="radio" name="'
136        . smarty_function_escape_special_chars($name) . '" value="'
137        . smarty_function_escape_special_chars($value) . '"';
138   
139   if ($labels && $label_ids) {
140       $_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!', '_', $name . '_' . $value));
141       $_output .= ' id="' . $_id . '"';
142   }
143    if ((string)$value == $selected) {
144        $_output .= ' checked="checked"';
145    }
146   
147    $_output .= $extra . ' />';
148   
149    $_output .= $tags[0];
150       
151    if ($labels) {
152      if($label_ids) {
153          $_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!', '_', $name . '_' . $value));
154          $_output .= '<label for="' . $_id . '">';
155      } else {
156          $_output .= '<label>';           
157      }
158    }
159   
160    // ÃͤòÁÞÆþ
161    $_output .= $output;
162   
163    $_output .= $tags[1];
164   
165    if ($labels) $_output .= '</label>';
166    $_output .=  $separator;
167
168    return $_output;
169}
170
171?>
Note: See TracBrowser for help on using the repository browser.