Subversion Repositories ALCASAR

Rev

Details | Last modification | View Log

Rev Author Line No. Line
2809 rexy 1
<?php
2
/**
3
 * Smarty plugin
4
 *
5
 * @package    Smarty
6
 * @subpackage PluginsFunction
7
 */
8
/**
9
 * Smarty {html_options} function plugin
10
 * Type:     function
11
 * Name:     html_options
12
 * Purpose:  Prints the list of <option> tags generated from
13
 *           the passed parameters
14
 * Params:
15
 *
16
 * - name       (optional) - string default "select"
17
 * - values     (required) - if no options supplied) - array
18
 * - options    (required) - if no values supplied) - associative array
19
 * - selected   (optional) - string default not set
20
 * - output     (required) - if not options supplied) - array
21
 * - id         (optional) - string default not set
22
 * - class      (optional) - string default not set
23
 *
24
 * @link   http://www.smarty.net/manual/en/language.function.html.options.php {html_image}
25
 *           (Smarty online manual)
26
 * @author Monte Ohrt <monte at ohrt dot com>
27
 * @author Ralf Strehle (minor optimization) <ralf dot strehle at yahoo dot de>
28
 *
29
 * @param array                     $params parameters
30
 *
31
 * @param \Smarty_Internal_Template $template
32
 *
33
 * @return string
34
 * @uses   smarty_function_escape_special_chars()
35
 * @throws \SmartyException
36
 */
37
function smarty_function_html_options($params, Smarty_Internal_Template $template)
38
{
39
    $template->_checkPlugins(
40
        array(
41
            array(
42
                'function' => 'smarty_function_escape_special_chars',
43
                'file'     => SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php'
44
            )
45
        )
46
    );
47
    $name = null;
48
    $values = null;
49
    $options = null;
50
    $selected = null;
51
    $output = null;
52
    $id = null;
53
    $class = null;
54
    $extra = '';
55
    foreach ($params as $_key => $_val) {
56
        switch ($_key) {
57
            case 'name':
58
            case 'class':
59
            case 'id':
60
                $$_key = (string)$_val;
61
                break;
62
            case 'options':
63
                $options = (array)$_val;
64
                break;
65
            case 'values':
66
            case 'output':
67
                $$_key = array_values((array)$_val);
68
                break;
69
            case 'selected':
70
                if (is_array($_val)) {
71
                    $selected = array();
72
                    foreach ($_val as $_sel) {
73
                        if (is_object($_sel)) {
74
                            if (method_exists($_sel, '__toString')) {
75
                                $_sel = smarty_function_escape_special_chars((string)$_sel->__toString());
76
                            } else {
77
                                trigger_error(
78
                                    'html_options: selected attribute contains an object of class \'' .
79
                                    get_class($_sel) . '\' without __toString() method',
80
                                    E_USER_NOTICE
81
                                );
82
                                continue;
83
                            }
84
                        } else {
85
                            $_sel = smarty_function_escape_special_chars((string)$_sel);
86
                        }
87
                        $selected[ $_sel ] = true;
88
                    }
89
                } elseif (is_object($_val)) {
90
                    if (method_exists($_val, '__toString')) {
91
                        $selected = smarty_function_escape_special_chars((string)$_val->__toString());
92
                    } else {
93
                        trigger_error(
94
                            'html_options: selected attribute is an object of class \'' . get_class($_val) .
95
                            '\' without __toString() method',
96
                            E_USER_NOTICE
97
                        );
98
                    }
99
                } else {
100
                    $selected = smarty_function_escape_special_chars((string)$_val);
101
                }
102
                break;
103
            case 'strict':
104
                break;
105
            case 'disabled':
106
            case 'readonly':
107
                if (!empty($params[ 'strict' ])) {
108
                    if (!is_scalar($_val)) {
109
                        trigger_error(
110
                            "html_options: {$_key} attribute must be a scalar, only boolean true or string '{$_key}' will actually add the attribute",
111
                            E_USER_NOTICE
112
                        );
113
                    }
114
                    if ($_val === true || $_val === $_key) {
115
                        $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_key) . '"';
116
                    }
117
                    break;
118
                }
119
            // omit break; to fall through!
120
            // no break
121
            default:
122
                if (!is_array($_val)) {
123
                    $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
124
                } else {
125
                    trigger_error("html_options: extra attribute '{$_key}' cannot be an array", E_USER_NOTICE);
126
                }
127
                break;
128
        }
129
    }
130
    if (!isset($options) && !isset($values)) {
131
        /* raise error here? */
132
        return '';
133
    }
134
    $_html_result = '';
135
    $_idx = 0;
136
    if (isset($options)) {
137
        foreach ($options as $_key => $_val) {
138
            $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected, $id, $class, $_idx);
139
        }
140
    } else {
141
        foreach ($values as $_i => $_key) {
142
            $_val = isset($output[ $_i ]) ? $output[ $_i ] : '';
143
            $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected, $id, $class, $_idx);
144
        }
145
    }
146
    if (!empty($name)) {
147
        $_html_class = !empty($class) ? ' class="' . $class . '"' : '';
148
        $_html_id = !empty($id) ? ' id="' . $id . '"' : '';
149
        $_html_result =
150
            '<select name="' . $name . '"' . $_html_class . $_html_id . $extra . '>' . "\n" . $_html_result .
151
            '</select>' . "\n";
152
    }
153
    return $_html_result;
154
}
155
 
156
/**
157
 * @param $key
158
 * @param $value
159
 * @param $selected
160
 * @param $id
161
 * @param $class
162
 * @param $idx
163
 *
164
 * @return string
165
 */
166
function smarty_function_html_options_optoutput($key, $value, $selected, $id, $class, &$idx)
167
{
168
    if (!is_array($value)) {
169
        $_key = smarty_function_escape_special_chars($key);
170
        $_html_result = '<option value="' . $_key . '"';
171
        if (is_array($selected)) {
172
            if (isset($selected[ $_key ])) {
173
                $_html_result .= ' selected="selected"';
174
            }
175
        } elseif ($_key === $selected) {
176
            $_html_result .= ' selected="selected"';
177
        }
178
        $_html_class = !empty($class) ? ' class="' . $class . ' option"' : '';
179
        $_html_id = !empty($id) ? ' id="' . $id . '-' . $idx . '"' : '';
180
        if (is_object($value)) {
181
            if (method_exists($value, '__toString')) {
182
                $value = smarty_function_escape_special_chars((string)$value->__toString());
183
            } else {
184
                trigger_error(
185
                    'html_options: value is an object of class \'' . get_class($value) .
186
                    '\' without __toString() method',
187
                    E_USER_NOTICE
188
                );
189
                return '';
190
            }
191
        } else {
192
            $value = smarty_function_escape_special_chars((string)$value);
193
        }
194
        $_html_result .= $_html_class . $_html_id . '>' . $value . '</option>' . "\n";
195
        $idx++;
196
    } else {
197
        $_idx = 0;
198
        $_html_result =
199
            smarty_function_html_options_optgroup(
200
                $key,
201
                $value,
202
                $selected,
203
                !empty($id) ? ($id . '-' . $idx) : null,
204
                $class,
205
                $_idx
206
            );
207
        $idx++;
208
    }
209
    return $_html_result;
210
}
211
 
212
/**
213
 * @param $key
214
 * @param $values
215
 * @param $selected
216
 * @param $id
217
 * @param $class
218
 * @param $idx
219
 *
220
 * @return string
221
 */
222
function smarty_function_html_options_optgroup($key, $values, $selected, $id, $class, &$idx)
223
{
224
    $optgroup_html = '<optgroup label="' . smarty_function_escape_special_chars($key) . '">' . "\n";
225
    foreach ($values as $key => $value) {
226
        $optgroup_html .= smarty_function_html_options_optoutput($key, $value, $selected, $id, $class, $idx);
227
    }
228
    $optgroup_html .= "</optgroup>\n";
229
    return $optgroup_html;
230
}