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

Revision 21527, 5.0 KB checked in by Seasoft, 12 years ago (diff)

#1613 (ソース整形・ソースコメントの改善)

  • Zend Framework PHP 標準コーディング規約への準拠を高めた
  • 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    require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
43
44    $name = 'radio';
45    $values = null;
46    $options = null;
47    $selected = null;
48    $separator = '';
49    $labels = true;
50    $label_ids = true;
51    $output = null;
52    $extra = '';
53
54    foreach ($params as $_key => $_val) {
55        switch ($_key) {
56    case 'tags':
57    $$_key = split("\|", $_val);
58    break;
59            case 'name':
60            case 'separator':
61                $$_key = (string)$_val;
62                break;
63
64            case 'checked':
65            case 'selected':
66                if (is_array($_val)) {
67                    $smarty->trigger_error('html_radios: the "' . $_key . '" attribute cannot be an array', E_USER_WARNING);
68                } else {
69                    $selected = (string)$_val;
70                }
71                break;
72
73            case 'labels':
74            case 'label_ids':
75                $$_key = (bool)$_val;
76                break;
77
78            case 'options':
79                $$_key = (array)$_val;
80                break;
81            case 'values':
82            case 'output':
83                $$_key = array_values((array)$_val);
84                break;
85
86            case 'radios':
87                $smarty->trigger_error('html_radios: the use of the "radios" attribute is deprecated, use "options" instead', E_USER_WARNING);
88                $options = (array)$_val;
89                break;
90
91            case 'assign':
92                break;
93
94            default:
95                if (!is_array($_val)) {
96                    $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
97                } else {
98                    $smarty->trigger_error("html_radios: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
99                }
100                break;
101        }
102    }
103
104    if (!isset($options) && !isset($values))
105        return ''; /* raise error here? */
106
107    $_html_result = array();
108
109    if (isset($options)) {
110
111        foreach ($options as $_key=>$_val)
112            $_html_result[] = smarty_function_html_radios_output_ex($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids, $tags);
113
114    } else {
115
116        foreach ($values as $_i=>$_key) {
117            $_val = isset($output[$_i]) ? $output[$_i] : '';
118            $_html_result[] = smarty_function_html_radios_output_ex($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids, $tags);
119        }
120
121    }
122
123    if (!empty($params['assign'])) {
124        $smarty->assign($params['assign'], $_html_result);
125    } else {
126        return implode("\n",$_html_result);
127    }
128
129}
130
131function smarty_function_html_radios_output_ex($name, $value, $output, $selected, $extra, $separator, $labels, $label_ids, $tags) {
132    $_output = '';
133
134    $_output .= '<input type="radio" name="'
135        . smarty_function_escape_special_chars($name) . '" value="'
136        . smarty_function_escape_special_chars($value) . '"';
137
138    if ($labels && $label_ids) {
139        $_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!', '_', $name . '_' . $value));
140        $_output .= ' id="' . $_id . '"';
141    }
142    if ((string)$value == $selected) {
143        $_output .= ' checked="checked"';
144    }
145
146    $_output .= $extra . ' />';
147
148    $_output .= $tags[0];
149
150    if ($labels) {
151        if ($label_ids) {
152        $_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!', '_', $name . '_' . $value));
153            $_output .= '<label for="' . $_id . '">';
154        } else {
155            $_output .= '<label>';
156        }
157    }
158
159    // 値を挿入
160    $_output .= $output;
161
162    $_output .= $tags[1];
163
164    if ($labels) $_output .= '</label>';
165    $_output .=  $separator;
166
167    return $_output;
168}
Note: See TracBrowser for help on using the repository browser.