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

XoopsFormCheckBox::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 4
dl 10
loc 10
rs 9.4285
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 27 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
 * XOOPS form checkbox compo
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
 * @since               2.0
16
 * @author              Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/
17
 * @author              Skalpa Keo <[email protected]>
18
 * @author              Taiwen Jiang <[email protected]>
19
 */
20
defined('XOOPS_ROOT_PATH') || exit('Restricted access');
21
22
xoops_load('XoopsFormElement');
23
24
/**
25
 * Class XoopsFormCheckBox
26
 */
27
class XoopsFormCheckBox extends XoopsFormElement
28
{
29
    /**
30
     * Availlable options
31
     *
32
     * @var array
33
     * @access private
34
     */
35
    public $_options = array();
36
37
    /**
38
     * pre-selected values in array
39
     *
40
     * @var array
41
     * @access private
42
     */
43
    public $_value = array();
44
45
    /**
46
     * HTML to seperate the elements
47
     *
48
     * @var string
49
     * @access private
50
     */
51
    public $_delimeter;
52
53
    /**
54
     * Column number for rendering
55
     *
56
     * @var int
57
     * @access public
58
     */
59
    public $columns;
60
61
    /**
62
     * Constructor
63
     *
64
     * @param string $caption
65
     * @param string $name
66
     * @param mixed  $value Either one value as a string or an array of them.
67
     * @param string $delimeter
68
     */
69 View Code Duplication
    public function __construct($caption, $name, $value = null, $delimeter = '&nbsp;')
70
    {
71
        $this->setCaption($caption);
72
        $this->setName($name);
73
        if (isset($value)) {
74
            $this->setValue($value);
75
        }
76
        $this->_delimeter = $delimeter;
77
        $this->setFormType('checkbox');
0 ignored issues
show
Deprecated Code introduced by
The method XoopsFormElement::setFormType() has been deprecated with message: PLEASE AVOID USING THIS METHOD

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
78
    }
79
80
    /**
81
     * Get the "value"
82
     *
83
     * @param  bool $encode To sanitizer the text?
84
     * @return array
85
     */
86 View Code Duplication
    public function getValue($encode = false)
87
    {
88
        if (!$encode) {
89
            return $this->_value;
90
        }
91
        $value = array();
92
        foreach ($this->_value as $val) {
93
            $value[] = $val ? htmlspecialchars($val, ENT_QUOTES) : $val;
94
        }
95
96
        return $value;
97
    }
98
99
    /**
100
     * Set the "value"
101
     *
102
     * @param array $value
103
     *
104
     */
105
    public function setValue($value)
106
    {
107
        $this->_value = array();
108
        if (is_array($value)) {
109
            foreach ($value as $v) {
110
                $this->_value[] = $v;
111
            }
112
        } else {
113
            $this->_value[] = $value;
114
        }
115
    }
116
117
    /**
118
     * Add an option
119
     *
120
     * @param string $value
121
     * @param string $name
122
     */
123 View Code Duplication
    public function addOption($value, $name = '')
124
    {
125
        if ($name != '') {
126
            $this->_options[$value] = $name;
127
        } else {
128
            $this->_options[$value] = $value;
129
        }
130
    }
131
132
    /**
133
     * Add multiple Options at once
134
     *
135
     * @param array $options Associative array of value->name pairs
136
     */
137
    public function addOptionArray($options)
138
    {
139
        if (is_array($options)) {
140
            foreach ($options as $k => $v) {
141
                $this->addOption($k, $v);
142
            }
143
        }
144
    }
145
146
    /**
147
     * Get an array with all the options
148
     *
149
     * @param  bool|int $encode To sanitizer the text? potential values: 0 - skip; 1 - only for value; 2 - for both value and name
150
     * @return array    Associative array of value->name pairs
151
     */
152 View Code Duplication
    public function getOptions($encode = false)
153
    {
154
        if (!$encode) {
155
            return $this->_options;
156
        }
157
        $value = array();
158
        foreach ($this->_options as $val => $name) {
159
            $value[$encode ? htmlspecialchars($val, ENT_QUOTES) : $val] = ($encode > 1) ? htmlspecialchars($name, ENT_QUOTES) : $name;
160
        }
161
162
        return $value;
163
    }
164
165
    /**
166
     * Get the delimiter of this group
167
     *
168
     * @param  bool $encode To sanitizer the text?
169
     * @return string The delimiter
170
     */
171
    public function getDelimeter($encode = false)
172
    {
173
        return $encode ? htmlspecialchars(str_replace('&nbsp;', ' ', $this->_delimeter)) : $this->_delimeter;
174
    }
175
176
    /**
177
     * prepare HTML for output
178
     *
179
     * @return string
180
     */
181
    public function render()
182
    {
183
        return XoopsFormRenderer::getInstance()->get()->renderFormCheckBox($this);
184
    }
185
186
    /**
187
     * Render custom javascript validation code
188
     *
189
     * @seealso XoopsForm::renderValidationJS
190
     */
191
    public function renderValidationJS()
192
    {
193
        // render custom validation code if any
194
        if (!empty($this->customValidationCode)) {
195
            return implode(NWLINE, $this->customValidationCode);
196
            // generate validation code if required
197 View Code Duplication
        } elseif ($this->isRequired()) {
198
            $eltname    = $this->getName();
199
            $eltcaption = $this->getCaption();
200
            $eltmsg     = empty($eltcaption) ? sprintf(_FORM_ENTER, $eltname) : sprintf(_FORM_ENTER, $eltcaption);
201
            $eltmsg     = str_replace('"', '\"', stripslashes($eltmsg));
202
203
            return NWLINE . "var hasChecked = false; var checkBox = myform.elements['{$eltname}']; if (checkBox.length) {for (var i = 0; i < checkBox.length; i++) {if (checkBox[i].checked == true) {hasChecked = true; break;}}} else {if (checkBox.checked == true) {hasChecked = true;}}if (!hasChecked) {window.alert(\"{$eltmsg}\");if (checkBox.length) {checkBox[0].focus();} else {checkBox.focus();}return false;}";
204
        }
205
206
        return '';
207
    }
208
}
209