source: branches/version-2_13-dev/data/smarty_extends/function.html_checkboxes_ex.php @ 22856

Revision 22856, 4.9 KB checked in by Seasoft, 11 years ago (diff)

#2043 (typo修正・ソース整形・ソースコメントの改善 for 2.13.0)

  • 主に空白・空白行の調整。もう少し整えたいが、一旦現状コミット。
  • 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_checkboxes} function plugin
11 *
12 * File:       function.html_checkboxes.php<br>
13 * Type:       function<br>
14 * Name:       html_checkboxes<br>
15 * Date:       24.Feb.2003<br>
16 * Purpose:    Prints out a list of checkbox input types<br>
17 * Input:<br>
18 *           - name       (optional) - string default "checkbox"
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 checkbox
24 *           - assign     (optional) - assign the output as an array to this variable
25 * Examples:
26 * <pre>
27 * {html_checkboxes values=$ids output=$names}
28 * {html_checkboxes values=$ids name='box' separator='<br>' output=$names}
29 * {html_checkboxes values=$ids checked=$checked separator='<br>' output=$names}
30 * </pre>
31 * @link http://smarty.php.net/manual/en/language.function.html.checkboxes.php {html_checkboxes}
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_checkboxes_ex($params, &$smarty)
42{
43    require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
44
45    $name = 'checkbox';
46    $values = null;
47    $options = null;
48    $selected = null;
49    $separator = '';
50    $labels = true;
51    $label_ids = true;
52    $output = null;
53
54    $extra = '';
55
56    foreach ($params as $_key => $_val) {
57        switch ($_key) {
58    case 'tags':
59    $$_key = split("\|", $_val);
60    break;
61            case 'name':
62            case 'separator':
63                $$_key = $_val;
64                break;
65
66            case 'labels':
67    case 'label_ids':
68                $$_key = (bool)$_val;
69                break;
70
71            case 'options':
72                $$_key = (array)$_val;
73                break;
74
75            case 'values':
76            case 'output':
77                $$_key = array_values((array)$_val);
78                break;
79
80            case 'checked':
81            case 'selected':
82                $selected = array_map('strval', array_values((array)$_val));
83                break;
84
85            case 'checkboxes':
86                $smarty->trigger_error('html_checkboxes: the use of the "checkboxes" attribute is deprecated, use "options" instead', E_USER_WARNING);
87                $options = (array)$_val;
88                break;
89
90            case 'assign':
91                break;
92
93            default:
94                if (!is_array($_val)) {
95                    $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
96                } else {
97                    $smarty->trigger_error("html_checkboxes: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
98                }
99                break;
100        }
101    }
102
103    if (!isset($options) && !isset($values))
104        return ''; /* raise error here? */
105
106    settype($selected, 'array');
107    $_html_result = array();
108
109    if (isset($options)) {
110        foreach ($options as $_key=>$_val)
111            $_html_result[] = smarty_function_html_checkboxes_output_ex($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids, $tags);
112
113
114    } else {
115        foreach ($values as $_i=>$_key) {
116            $_val = isset($output[$_i]) ? $output[$_i] : '';
117            $_html_result[] = smarty_function_html_checkboxes_output_ex($name, $_key, $_val, $selected, $extra, $separator, $labels, $label_ids, $tags);
118        }
119
120    }
121
122    if (!empty($params['assign'])) {
123        $smarty->assign($params['assign'], $_html_result);
124    } else {
125        return implode("\n",$_html_result);
126    }
127}
128
129function smarty_function_html_checkboxes_output_ex($name, $value, $output, $selected, $extra, $separator, $labels, $label_ids, $tags)
130{
131    $_output = '';
132
133    $_output .= '<input type="checkbox" name="'
134        . smarty_function_escape_special_chars($name) . '[]" value="'
135        . smarty_function_escape_special_chars($value) . '"';
136
137    if ($labels && $label_ids) {
138    $_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!', '_', $name . '_' . $value));
139    $_output .= ' id="' . $_id . '"';
140    }
141
142    if (in_array((string)$value, $selected)) {
143        $_output .= ' checked="checked"';
144    }
145    $_output .= $extra . ' />';
146
147    $_output .= $tags[0];
148
149    if ($labels) {
150    if ($label_ids) {
151    $_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!', '_', $name . '_' . $value));
152    $_output .= '<label for="' . $_id . '">';
153    } else {
154    $_output .= '<label>';
155    }
156    }
157
158    // 値を挿入
159    $_output .= $output;
160
161    $_output .= $tags[1];
162
163    if ($labels) $_output .= '</label>';
164    $_output .=  $separator;
165
166    return $_output;
167}
Note: See TracBrowser for help on using the repository browser.