Passed
Push — 1.10.x ( 0e6d9a...8465ef )
by
unknown
96:49 queued 46:21
created

HTML_QuickForm_checkbox::onQuickFormEvent()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 28
rs 6.7272
cc 7
eloc 18
nc 8
nop 3
1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
4
/**
5
 * HTML class for a checkbox type field
6
 *
7
 * PHP versions 4 and 5
8
 *
9
 * LICENSE: This source file is subject to version 3.01 of the PHP license
10
 * that is available through the world-wide-web at the following URI:
11
 * http://www.php.net/license/3_01.txt If you did not receive a copy of
12
 * the PHP License and are unable to obtain it through the web, please
13
 * send a note to [email protected] so we can mail you a copy immediately.
14
 *
15
 * @category    HTML
16
 * @package     HTML_QuickForm
17
 * @author      Adam Daniel <[email protected]>
18
 * @author      Bertrand Mansion <[email protected]>
19
 * @author      Alexey Borzov <[email protected]>
20
 * @copyright   2001-2009 The PHP Group
21
 * @license     http://www.php.net/license/3_01.txt PHP License 3.01
22
 * @version     CVS: $Id: checkbox.php,v 1.23 2009/04/04 21:34:02 avb Exp $
23
 * @link        http://pear.php.net/package/HTML_QuickForm
24
 */
25
26
27
/**
28
 * HTML class for a checkbox type field
29
 *
30
 * @category    HTML
31
 * @package     HTML_QuickForm
32
 * @author      Adam Daniel <[email protected]>
33
 * @author      Bertrand Mansion <[email protected]>
34
 * @author      Alexey Borzov <[email protected]>
35
 * @version     Release: 3.2.11
36
 * @since       1.0
37
 */
38
class HTML_QuickForm_checkbox extends HTML_QuickForm_input
39
{
40
    // {{{ properties
41
42
    /**
43
     * Checkbox display text
44
     * @var       string
45
     * @since     1.1
46
     * @access    private
47
     */
48
    public $_text = '';
49
    public $labelClass;
50
    public $checkboxClass;
51
52
    // }}}
53
    // {{{ constructor
54
55
    /**
56
     * Class constructor
57
     *
58
     * @param     string    $elementName    (optional)Input field name attribute
59
     * @param     string    $elementLabel   (optional)Input field value
60
     * @param     string    $text           (optional)Checkbox display text
61
     * @param     mixed     $attributes     (optional)Either a typical HTML attribute string
62
     *                                      or an associative array
63
     * @since     1.0
64
     * @access    public
65
     * @return    void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
66
     */
67
    public function __construct(
68
        $elementName = null,
69
        $elementLabel = null,
70
        $text = '',
71
        $attributes = null
72
    ) {
73
        $this->labelClass = isset($attributes['label-class']) ? $attributes['label-class'] : '';
74
        $this->checkboxClass = isset($attributes['checkbox-class']) ? $attributes['checkbox-class'] : 'checkbox';
75
76
        if (isset($attributes['label-class'])) {
77
            unset($attributes['label-class']);
78
        }
79
80
        if (isset($attributes['checkbox-class'])) {
81
            unset($attributes['checkbox-class']);
82
        }
83
84
        parent::__construct($elementName, $elementLabel, $attributes);
85
        $this->_persistantFreeze = true;
86
        $this->_text = $text;
87
        $this->setType('checkbox');
88
89
        if (!isset($attributes['value'])) {
90
            $this->updateAttributes(array('value' => 1));
91
        } else {
92
            $this->updateAttributes(array('value' => $attributes['value']));
93
        }
94
95
        $this->_generateId();
96
    }
97
98
    /**
99
     * Sets whether a checkbox is checked
100
     *
101
     * @param     bool    $checked  Whether the field is checked or not
102
     * @since     1.0
103
     * @access    public
104
     * @return    void
105
     */
106
    function setChecked($checked)
107
    {
108
        if (!$checked) {
109
            $this->removeAttribute('checked');
110
        } else {
111
            $this->updateAttributes(array('checked'=>'checked'));
112
        }
113
    } //end func setChecked
114
115
    // }}}
116
    // {{{ getChecked()
117
118
    /**
119
     * Returns whether a checkbox is checked
120
     *
121
     * @since     1.0
122
     * @access    public
123
     * @return    bool
124
     */
125
    function getChecked()
126
    {
127
        return (bool)$this->getAttribute('checked');
128
    } //end func getChecked
129
130
    // }}}
131
    // {{{ toHtml()
132
133
    /**
134
     * Returns the checkbox element in HTML
135
     *
136
     * @since     1.0
137
     * @access    public
138
     * @return    string
139
     */
140 View Code Duplication
    public function toHtml()
141
    {
142
        if (0 == strlen($this->_text)) {
143
            $label = '';
144
        } elseif ($this->_flagFrozen) {
145
            $label = $this->_text;
146
        } else {
147
            $labelClass = $this->labelClass;
148
            $checkboxClass = $this->checkboxClass;
149
150
            $label ='
151
                <div class="'.$checkboxClass.'">
152
                <label class="'.$labelClass.'">' .
153
                    HTML_QuickForm_input::toHtml().$this->_text.
154
                '</label>
155
                </div>
156
            ';
157
158
            return $label;
159
        }
160
161
        return HTML_QuickForm_input::toHtml() . $label;
162
    } //end func toHtml
163
164
    // }}}
165
    // {{{ getFrozenHtml()
166
167
    /**
168
     * Returns the value of field without HTML tags
169
     *
170
     * @since     1.0
171
     * @access    public
172
     * @return    string
173
     */
174
    function getFrozenHtml()
175
    {
176
        if ($this->getChecked()) {
177
            return '<code>[x]</code>' .
178
                   $this->_getPersistantData();
179
        } else {
180
            return '<code>[ ]</code>';
181
        }
182
    } //end func getFrozenHtml
183
184
    // }}}
185
    // {{{ setText()
186
187
    /**
188
     * Sets the checkbox text
189
     *
190
     * @param     string    $text
191
     * @since     1.1
192
     * @access    public
193
     * @return    void
194
     */
195
    function setText($text)
196
    {
197
        $this->_text = $text;
198
    } //end func setText
199
200
    // }}}
201
    // {{{ getText()
202
203
    /**
204
     * Returns the checkbox text
205
     *
206
     * @since     1.1
207
     * @access    public
208
     * @return    string
209
     */
210
    function getText()
211
    {
212
        return $this->_text;
213
    } //end func getText
214
215
    // }}}
216
    // {{{ setValue()
217
218
    /**
219
     * Sets the value of the form element
220
     *
221
     * @param     string    $value      Default value of the form element
222
     * @since     1.0
223
     * @access    public
224
     * @return    void
225
     */
226
    function setValue($value)
227
    {
228
        return $this->setChecked($value);
229
    } // end func setValue
230
231
    // }}}
232
    // {{{ getValue()
233
234
    /**
235
     * Returns the value of the form element
236
     *
237
     * @since     1.0
238
     * @access    public
239
     * @return    bool
240
     */
241
    function getValue()
242
    {
243
        return $this->getChecked();
244
    } // end func getValue
245
246
    // }}}
247
    // {{{ onQuickFormEvent()
248
249
    /**
250
     * Called by HTML_QuickForm whenever form event is made on this element
251
     *
252
     * @param     string    $event  Name of event
253
     * @param     mixed     $arg    event arguments
254
     * @param     object    &$caller calling object
255
     * @since     1.0
256
     * @access    public
257
     * @return    void
258
     */
259
    function onQuickFormEvent($event, $arg, &$caller)
260
    {
261
        switch ($event) {
262
            case 'updateValue':
263
                // constant values override both default and submitted ones
264
                // default values are overriden by submitted
265
                $value = $this->_findValue($caller->_constantValues);
266
                if (null === $value) {
267
                    // if no boxes were checked, then there is no value in the array
268
                    // yet we don't want to display default value in this case
269
                    if ($caller->isSubmitted()) {
270
                        $value = $this->_findValue($caller->_submitValues);
271
                    } else {
272
                        $value = $this->_findValue($caller->_defaultValues);
273
                    }
274
                }
275
                if (null !== $value || $caller->isSubmitted()) {
276
                    $this->setChecked($value);
277
                }
278
                break;
279
            case 'setGroupValue':
280
                $this->setChecked($arg);
281
                break;
282
            default:
283
                parent::onQuickFormEvent($event, $arg, $caller);
284
        }
285
        return true;
286
    } // end func onQuickFormEvent
287
288
    // }}}
289
    // {{{ exportValue()
290
291
   /**
292
    * Return true if the checkbox is checked, null if it is not checked (getValue() returns false)
293
    */
294 View Code Duplication
    function exportValue(&$submitValues, $assoc = false)
295
    {
296
        $value = $this->_findValue($submitValues);
297
        if (null === $value) {
298
            $value = $this->getChecked()? true: null;
299
        }
300
        return $this->_prepareValue($value, $assoc);
301
    }
302
}
303