Completed
Push — master ( 8d9a9b...cd572c )
by Richard
11s
created

XoopsFormSelect::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 35 and the first side effect is on line 20.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * select form element
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @copyright       (c) 2000-2017 XOOPS Project (www.xoops.org)
13
 * @license             GNU GPL 2 (http://www.gnu.org/licenses/gpl-2.0.html)
14
 * @package             kernel
15
 * @subpackage          form
16
 * @since               2.0.0
17
 * @author              Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/
18
 * @author              Taiwen Jiang <[email protected]>
19
 */
20
defined('XOOPS_ROOT_PATH') || exit('Restricted access');
21
22
xoops_load('XoopsFormElement');
23
24
/**
25
 * A select field
26
 *
27
 * @author              Kazumi Ono <[email protected]>
28
 * @author              Taiwen Jiang <[email protected]>
29
 * @author              John Neill <[email protected]>
30
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
31
 * @package             kernel
32
 * @subpackage          form
33
 * @access              public
34
 */
35
class XoopsFormSelect extends XoopsFormElement
36
{
37
    /**
38
     * Options
39
     *
40
     * @var array
41
     * @access private
42
     */
43
    public $_options = array();
44
45
    /**
46
     * Allow multiple selections?
47
     *
48
     * @var bool
49
     * @access private
50
     */
51
    public $_multiple = false;
52
53
    /**
54
     * Number of rows. "1" makes a dropdown list.
55
     *
56
     * @var int
57
     * @access private
58
     */
59
    public $_size;
60
61
    /**
62
     * Pre-selcted values
63
     *
64
     * @var array
65
     * @access private
66
     */
67
    public $_value = array();
68
69
    /**
70
     * Constructor
71
     *
72
     * @param string $caption  Caption
73
     * @param string $name     "name" attribute
74
     * @param mixed  $value    Pre-selected value (or array of them).
75
     * @param int    $size     Number or rows. "1" makes a drop-down-list
76
     * @param bool   $multiple Allow multiple selections?
77
     */
78 View Code Duplication
    public function __construct($caption, $name, $value = null, $size = 1, $multiple = false)
79
    {
80
        $this->setCaption($caption);
81
        $this->setName($name);
82
        $this->_multiple = $multiple;
83
        $this->_size     = (int)$size;
84
        if (isset($value)) {
85
            $this->setValue($value);
86
        }
87
    }
88
89
    /**
90
     * Are multiple selections allowed?
91
     *
92
     * @return bool
93
     */
94
    public function isMultiple()
95
    {
96
        return $this->_multiple;
97
    }
98
99
    /**
100
     * Get the size
101
     *
102
     * @return int
103
     */
104
    public function getSize()
105
    {
106
        return $this->_size;
107
    }
108
109
    /**
110
     * Get an array of pre-selected values
111
     *
112
     * @param  bool $encode To sanitizer the text?
113
     * @return array
114
     */
115 View Code Duplication
    public function getValue($encode = false)
116
    {
117
        if (!$encode) {
118
            return $this->_value;
119
        }
120
        $value = array();
121
        foreach ($this->_value as $val) {
122
            $value[] = $val ? htmlspecialchars($val, ENT_QUOTES) : $val;
123
        }
124
125
        return $value;
126
    }
127
128
    /**
129
     * Set pre-selected values
130
     *
131
     * @param  $value mixed
132
     */
133 View Code Duplication
    public function setValue($value)
134
    {
135
        if (is_array($value)) {
136
            foreach ($value as $v) {
137
                $this->_value[] = $v;
138
            }
139
        } elseif (isset($value)) {
140
            $this->_value[] = $value;
141
        }
142
    }
143
144
    /**
145
     * Add an option
146
     *
147
     * @param string $value "value" attribute
148
     * @param string $name  "name" attribute
149
     */
150 View Code Duplication
    public function addOption($value, $name = '')
151
    {
152
        if ($name != '') {
153
            $this->_options[$value] = $name;
154
        } else {
155
            $this->_options[$value] = $value;
156
        }
157
    }
158
159
    /**
160
     * Add multiple options
161
     *
162
     * @param array $options Associative array of value->name pairs
163
     */
164
    public function addOptionArray($options)
165
    {
166
        if (is_array($options)) {
167
            foreach ($options as $k => $v) {
168
                $this->addOption($k, $v);
169
            }
170
        }
171
    }
172
173
    /**
174
     * Get an array with all the options
175
     *
176
     * Note: both name and value should be sanitized. However for backward compatibility, only value is sanitized for now.
177
     *
178
     * @param bool|int $encode To sanitizer the text? potential values: 0 - skip; 1 - only for value; 2 - for both value and name
179
     *
180
     * @return array Associative array of value->name pairs
181
     */
182 View Code Duplication
    public function getOptions($encode = false)
183
    {
184
        if (!$encode) {
185
            return $this->_options;
186
        }
187
        $value = array();
188
        foreach ($this->_options as $val => $name) {
189
            $value[$encode ? htmlspecialchars($val, ENT_QUOTES) : $val] = ($encode > 1) ? htmlspecialchars($name, ENT_QUOTES) : $name;
190
        }
191
192
        return $value;
193
    }
194
195
    /**
196
     * Prepare HTML for output
197
     *
198
     * @return string HTML
199
     */
200
    public function render()
201
    {
202
        return XoopsFormRenderer::getInstance()->get()->renderFormSelect($this);
203
    }
204
205
    /**
206
     * Render custom javascript validation code
207
     *
208
     * @seealso XoopsForm::renderValidationJS
209
     */
210
    public function renderValidationJS()
211
    {
212
        // render custom validation code if any
213
        if (!empty($this->customValidationCode)) {
214
            return implode("\n", $this->customValidationCode);
215
            // generate validation code if required
216 View Code Duplication
        } elseif ($this->isRequired()) {
217
            $eltname    = $this->getName();
218
            $eltcaption = $this->getCaption();
219
            $eltmsg     = empty($eltcaption) ? sprintf(_FORM_ENTER, $eltname) : sprintf(_FORM_ENTER, $eltcaption);
220
            $eltmsg     = str_replace('"', '\"', stripslashes($eltmsg));
221
222
            return "\nvar hasSelected = false; var selectBox = myform.{$eltname};" . "for (i = 0; i < selectBox.options.length; i++) { if (selectBox.options[i].selected == true && selectBox.options[i].value != '') { hasSelected = true; break; } }" . "if (!hasSelected) { window.alert(\"{$eltmsg}\"); selectBox.focus(); return false; }";
223
        }
224
225
        return '';
226
    }
227
}
228