source: branches/version-2_12-dev/data/smarty_extends/function.html_checkboxes_ex.php @ 21479

Revision 21479, 4.9 KB checked in by Seasoft, 12 years ago (diff)

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

  • ソース整形(主に制御構造のスペース)
  • 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    require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
43
44    $name = 'checkbox';
45    $values = null;
46    $options = null;
47    $selected = null;
48    $separator = '';
49    $labels = true;
50    $label_ids = true;
51    $output = null;
52
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 = $_val;
63                break;
64
65            case 'labels':
66    case 'label_ids':
67                $$_key = (bool)$_val;
68                break;
69
70            case 'options':
71                $$_key = (array)$_val;
72                break;
73
74            case 'values':
75            case 'output':
76                $$_key = array_values((array)$_val);
77                break;
78
79            case 'checked':
80            case 'selected':
81                $selected = array_map('strval', array_values((array)$_val));
82                break;
83
84            case 'checkboxes':
85                $smarty->trigger_error('html_checkboxes: the use of the "checkboxes" attribute is deprecated, use "options" instead', E_USER_WARNING);
86                $options = (array)$_val;
87                break;
88
89            case 'assign':
90                break;
91
92            default:
93                if (!is_array($_val)) {
94                    $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
95                } else {
96                    $smarty->trigger_error("html_checkboxes: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
97                }
98                break;
99        }
100    }
101
102    if (!isset($options) && !isset($values))
103        return ''; /* raise error here? */
104
105    settype($selected, 'array');
106    $_html_result = array();
107
108    if (isset($options)) {
109
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    $_output = '';
131
132    $_output .= '<input type="checkbox" name="'
133        . smarty_function_escape_special_chars($name) . '[]" value="'
134        . smarty_function_escape_special_chars($value) . '"';
135
136    if ($labels && $label_ids) {
137    $_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!', '_', $name . '_' . $value));
138    $_output .= ' id="' . $_id . '"';
139    }
140
141    if (in_array((string)$value, $selected)) {
142        $_output .= ' checked="checked"';
143    }
144    $_output .= $extra . ' />';
145
146    $_output .= $tags[0];
147
148    if ($labels) {
149    if ($label_ids) {
150    $_id = smarty_function_escape_special_chars(preg_replace('![^\w\-\.]!', '_', $name . '_' . $value));
151    $_output .= '<label for="' . $_id . '">';
152    } else {
153    $_output .= '<label>';
154    }
155    }
156
157    // 値を挿入
158    $_output .= $output;
159
160    $_output .= $tags[1];
161
162    if ($labels) $_output .= '</label>';
163    $_output .=  $separator;
164
165    return $_output;
166}
Note: See TracBrowser for help on using the repository browser.