source: branches/version-2_12-dev/data/smarty_extends/function.html_radios_ex.php @ 22567

Revision 22567, 5.0 KB checked in by shutta, 11 years ago (diff)

#2043 (typo修正・ソース整形・ソースコメントの改善 for 2.12.4)
Zend Framework PHP 標準コーディング規約のコーディングスタイルへ準拠。
classおよびfunctionの開始波括弧「{」のスタイルを修正。

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
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{
134    $_output = '';
135
136    $_output .= '<input type="radio" name="'
137        . smarty_function_escape_special_chars($name) . '" value="'
138        . smarty_function_escape_special_chars($value) . '"';
139
140    if ($labels && $label_ids) {
141        $_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!', '_', $name . '_' . $value));
142        $_output .= ' id="' . $_id . '"';
143    }
144    if ((string)$value == $selected) {
145        $_output .= ' checked="checked"';
146    }
147
148    $_output .= $extra . ' />';
149
150    $_output .= $tags[0];
151
152    if ($labels) {
153        if ($label_ids) {
154        $_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!', '_', $name . '_' . $value));
155            $_output .= '<label for="' . $_id . '">';
156        } else {
157            $_output .= '<label>';
158        }
159    }
160
161    // 値を挿入
162    $_output .= $output;
163
164    $_output .= $tags[1];
165
166    if ($labels) $_output .= '</label>';
167    $_output .=  $separator;
168
169    return $_output;
170}
Note: See TracBrowser for help on using the repository browser.