Subversion Repositories Applications.papyrus

Rev

Rev 320 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 320 Rev 443
1
<?php
1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4: */
2
/* vim: set expandtab tabstop=4 shiftwidth=4: */
3
// +----------------------------------------------------------------------+
3
// +----------------------------------------------------------------------+
4
// | PHP version 4.0                                                      |
4
// | PHP version 4.0                                                      |
5
// +----------------------------------------------------------------------+
5
// +----------------------------------------------------------------------+
6
// | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group             |
6
// | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group             |
7
// +----------------------------------------------------------------------+
7
// +----------------------------------------------------------------------+
8
// | This source file is subject to version 2.0 of the PHP license,       |
8
// | This source file is subject to version 2.0 of the PHP license,       |
9
// | that is bundled with this package in the file LICENSE, and is        |
9
// | that is bundled with this package in the file LICENSE, and is        |
10
// | available at through the world-wide-web at                           |
10
// | available at through the world-wide-web at                           |
11
// | http://www.php.net/license/2_02.txt.                                 |
11
// | http://www.php.net/license/2_02.txt.                                 |
12
// | If you did not receive a copy of the PHP license and are unable to   |
12
// | If you did not receive a copy of the PHP license and are unable to   |
13
// | obtain it through the world-wide-web, please send a note to          |
13
// | obtain it through the world-wide-web, please send a note to          |
14
// | license@php.net so we can mail you a copy immediately.               |
14
// | license@php.net so we can mail you a copy immediately.               |
15
// +----------------------------------------------------------------------+
15
// +----------------------------------------------------------------------+
16
// | Authors: Adam Daniel <adaniel1@eesus.jnj.com>                        |
16
// | Authors: Adam Daniel <adaniel1@eesus.jnj.com>                        |
17
// |          Bertrand Mansion <bmansion@mamasam.com>                     |
17
// |          Bertrand Mansion <bmansion@mamasam.com>                     |
18
// +----------------------------------------------------------------------+
18
// +----------------------------------------------------------------------+
19
//
19
//
20
// $Id: checkbox.php,v 1.1 2005-03-30 08:50:33 jpm Exp $
20
// $Id: checkbox.php,v 1.2 2005-09-20 17:01:22 ddelon Exp $
21
 
21
 
22
require_once("HTML/QuickForm/input.php");
22
require_once("HTML/QuickForm/input.php");
23
 
23
 
24
/**
24
/**
25
 * HTML class for a checkbox type field
25
 * HTML class for a checkbox type field
26
 * 
26
 * 
27
 * @author       Adam Daniel <adaniel1@eesus.jnj.com>
27
 * @author       Adam Daniel <adaniel1@eesus.jnj.com>
28
 * @author       Bertrand Mansion <bmansion@mamasam.com>
28
 * @author       Bertrand Mansion <bmansion@mamasam.com>
29
 * @version      1.1
29
 * @version      1.1
30
 * @since        PHP4.04pl1
30
 * @since        PHP4.04pl1
31
 * @access       public
31
 * @access       public
32
 */
32
 */
33
class HTML_QuickForm_checkbox extends HTML_QuickForm_input
33
class HTML_QuickForm_checkbox extends HTML_QuickForm_input
34
{
34
{
35
    // {{{ properties
35
    // {{{ properties
36
 
36
 
37
    /**
37
    /**
38
     * Checkbox display text
38
     * Checkbox display text
39
     * @var       string
39
     * @var       string
40
     * @since     1.1
40
     * @since     1.1
41
     * @access    private
41
     * @access    private
42
     */
42
     */
43
    var $_text = '';
43
    var $_text = '';
44
 
44
 
45
    // }}}
45
    // }}}
46
    // {{{ constructor
46
    // {{{ constructor
47
 
47
 
48
    /**
48
    /**
49
     * Class constructor
49
     * Class constructor
50
     * 
50
     * 
51
     * @param     string    $elementName    (optional)Input field name attribute
51
     * @param     string    $elementName    (optional)Input field name attribute
52
     * @param     string    $elementLabel   (optional)Input field value
52
     * @param     string    $elementLabel   (optional)Input field value
53
     * @param     string    $text           (optional)Checkbox display text
53
     * @param     string    $text           (optional)Checkbox display text
54
     * @param     mixed     $attributes     (optional)Either a typical HTML attribute string 
54
     * @param     mixed     $attributes     (optional)Either a typical HTML attribute string 
55
     *                                      or an associative array
55
     *                                      or an associative array
56
     * @since     1.0
56
     * @since     1.0
57
     * @access    public
57
     * @access    public
58
     * @return    void
58
     * @return    void
59
     */
59
     */
60
    function HTML_QuickForm_checkbox($elementName=null, $elementLabel=null, $text='', $attributes=null)
60
    function HTML_QuickForm_checkbox($elementName=null, $elementLabel=null, $text='', $attributes=null)
61
    {
61
    {
62
        HTML_QuickForm_input::HTML_QuickForm_input($elementName, $elementLabel, $attributes);
62
        HTML_QuickForm_input::HTML_QuickForm_input($elementName, $elementLabel, $attributes);
63
        $this->_persistantFreeze = true;
63
        $this->_persistantFreeze = true;
64
        $this->_text = $text;
64
        $this->_text = $text;
65
        $this->setType('checkbox');
65
        $this->setType('checkbox');
66
        $this->updateAttributes(array('value'=>1));
66
        $this->updateAttributes(array('value'=>1));
67
        $this->_generateId();
67
        $this->_generateId();
68
    } //end constructor
68
    } //end constructor
69
    
69
    
70
    // }}}
70
    // }}}
71
    // {{{ setChecked()
71
    // {{{ setChecked()
72
 
72
 
73
    /**
73
    /**
74
     * Sets whether a checkbox is checked
74
     * Sets whether a checkbox is checked
75
     * 
75
     * 
76
     * @param     bool    $checked  Whether the field is checked or not
76
     * @param     bool    $checked  Whether the field is checked or not
77
     * @since     1.0
77
     * @since     1.0
78
     * @access    public
78
     * @access    public
79
     * @return    void
79
     * @return    void
80
     */
80
     */
81
    function setChecked($checked)
81
    function setChecked($checked)
82
    {
82
    {
83
        if (!$checked) {
83
        if (!$checked) {
84
            $this->removeAttribute('checked');
84
            $this->removeAttribute('checked');
85
        } else {
85
        } else {
86
            $this->updateAttributes(array('checked'=>'checked'));
86
            $this->updateAttributes(array('checked'=>'checked'));
87
        }
87
        }
88
    } //end func setChecked
88
    } //end func setChecked
89
 
89
 
90
    // }}}
90
    // }}}
91
    // {{{ getChecked()
91
    // {{{ getChecked()
92
 
92
 
93
    /**
93
    /**
94
     * Returns whether a checkbox is checked
94
     * Returns whether a checkbox is checked
95
     * 
95
     * 
96
     * @since     1.0
96
     * @since     1.0
97
     * @access    public
97
     * @access    public
98
     * @return    bool
98
     * @return    bool
99
     */
99
     */
100
    function getChecked()
100
    function getChecked()
101
    {
101
    {
102
        return (bool)$this->getAttribute('checked');
102
        return (bool)$this->getAttribute('checked');
103
    } //end func getChecked
103
    } //end func getChecked
104
    
104
    
105
    // }}}
105
    // }}}
106
    // {{{ toHtml()
106
    // {{{ toHtml()
107
 
107
 
108
    /**
108
    /**
109
     * Returns the checkbox element in HTML
109
     * Returns the checkbox element in HTML
110
     * 
110
     * 
111
     * @since     1.0
111
     * @since     1.0
112
     * @access    public
112
     * @access    public
113
     * @return    string
113
     * @return    string
114
     */
114
     */
115
    function toHtml()
115
    function toHtml()
116
    {
116
    {
117
        if (0 == strlen($this->_text)) {
117
        if (0 == strlen($this->_text)) {
118
            $label = '';
118
            $label = '';
119
        } elseif ($this->_flagFrozen) {
119
        } elseif ($this->_flagFrozen) {
120
            $label = $this->_text;
120
            $label = $this->_text;
121
        } else {
121
        } else {
122
            $label = '<label for="' . $this->getAttribute('id') . '">' . $this->_text . '</label>';
122
            $label = '<label for="' . $this->getAttribute('id') . '">' . $this->_text . '</label>';
123
        }
123
        }
124
        return HTML_QuickForm_input::toHtml() . $label;
124
        return HTML_QuickForm_input::toHtml() . $label;
125
    } //end func toHtml
125
    } //end func toHtml
126
    
126
    
127
    // }}}
127
    // }}}
128
    // {{{ getFrozenHtml()
128
    // {{{ getFrozenHtml()
129
 
129
 
130
    /**
130
    /**
131
     * Returns the value of field without HTML tags
131
     * Returns the value of field without HTML tags
132
     * 
132
     * 
133
     * @since     1.0
133
     * @since     1.0
134
     * @access    public
134
     * @access    public
135
     * @return    string
135
     * @return    string
136
     */
136
     */
137
    function getFrozenHtml()
137
    function getFrozenHtml()
138
    {
138
    {
139
        if ($this->getChecked()) {
139
        if ($this->getChecked()) {
140
            return '<tt>[x]</tt>' .
140
            return '<tt>[x]</tt>' .
141
                   $this->_getPersistantData();
141
                   $this->_getPersistantData();
142
        } else {
142
        } else {
143
            return '<tt>[ ]</tt>';
143
            return '<tt>[ ]</tt>';
144
        }
144
        }
145
    } //end func getFrozenHtml
145
    } //end func getFrozenHtml
146
 
146
 
147
    // }}}
147
    // }}}
148
    // {{{ setText()
148
    // {{{ setText()
149
 
149
 
150
    /**
150
    /**
151
     * Sets the checkbox text
151
     * Sets the checkbox text
152
     * 
152
     * 
153
     * @param     string    $text  
153
     * @param     string    $text  
154
     * @since     1.1
154
     * @since     1.1
155
     * @access    public
155
     * @access    public
156
     * @return    void
156
     * @return    void
157
     */
157
     */
158
    function setText($text)
158
    function setText($text)
159
    {
159
    {
160
        $this->_text = $text;
160
        $this->_text = $text;
161
    } //end func setText
161
    } //end func setText
162
 
162
 
163
    // }}}
163
    // }}}
164
    // {{{ getText()
164
    // {{{ getText()
165
 
165
 
166
    /**
166
    /**
167
     * Returns the checkbox text 
167
     * Returns the checkbox text 
168
     * 
168
     * 
169
     * @since     1.1
169
     * @since     1.1
170
     * @access    public
170
     * @access    public
171
     * @return    string
171
     * @return    string
172
     */
172
     */
173
    function getText()
173
    function getText()
174
    {
174
    {
175
        return $this->_text;
175
        return $this->_text;
176
    } //end func getText
176
    } //end func getText
177
 
177
 
178
    // }}}
178
    // }}}
179
    // {{{ setValue()
179
    // {{{ setValue()
180
 
180
 
181
    /**
181
    /**
182
     * Sets the value of the form element
182
     * Sets the value of the form element
183
     *
183
     *
184
     * @param     string    $value      Default value of the form element
184
     * @param     string    $value      Default value of the form element
185
     * @since     1.0
185
     * @since     1.0
186
     * @access    public
186
     * @access    public
187
     * @return    void
187
     * @return    void
188
     */
188
     */
189
    function setValue($value)
189
    function setValue($value)
190
    {
190
    {
191
        return $this->setChecked($value);
191
        return $this->setChecked($value);
192
    } // end func setValue
192
    } // end func setValue
193
 
193
 
194
    // }}}
194
    // }}}
195
    // {{{ getValue()
195
    // {{{ getValue()
196
 
196
 
197
    /**
197
    /**
198
     * Returns the value of the form element
198
     * Returns the value of the form element
199
     *
199
     *
200
     * @since     1.0
200
     * @since     1.0
201
     * @access    public
201
     * @access    public
202
     * @return    bool
202
     * @return    bool
203
     */
203
     */
204
    function getValue()
204
    function getValue()
205
    {
205
    {
206
        return $this->getChecked();
206
        return $this->getChecked();
207
    } // end func getValue
207
    } // end func getValue
208
 
208
 
209
    // }}}
209
    // }}}
210
    // {{{ onQuickFormEvent()
210
    // {{{ onQuickFormEvent()
211
 
211
 
212
    /**
212
    /**
213
     * Called by HTML_QuickForm whenever form event is made on this element
213
     * Called by HTML_QuickForm whenever form event is made on this element
214
     *
214
     *
215
     * @param     string    $event  Name of event
215
     * @param     string    $event  Name of event
216
     * @param     mixed     $arg    event arguments
216
     * @param     mixed     $arg    event arguments
217
     * @param     object    $caller calling object
217
     * @param     object    $caller calling object
218
     * @since     1.0
218
     * @since     1.0
219
     * @access    public
219
     * @access    public
220
     * @return    void
220
     * @return    void
221
     */
221
     */
222
    function onQuickFormEvent($event, $arg, &$caller)
222
    function onQuickFormEvent($event, $arg, &$caller)
223
    {
223
    {
224
        switch ($event) {
224
        switch ($event) {
225
            case 'updateValue':
225
            case 'updateValue':
226
                // constant values override both default and submitted ones
226
                // constant values override both default and submitted ones
227
                // default values are overriden by submitted
227
                // default values are overriden by submitted
228
                $value = $this->_findValue($caller->_constantValues);
228
                $value = $this->_findValue($caller->_constantValues);
229
                if (null === $value) {
229
                if (null === $value) {
230
                    // if no boxes were checked, then there is no value in the array
230
                    // if no boxes were checked, then there is no value in the array
231
                    // yet we don't want to display default value in this case
231
                    // yet we don't want to display default value in this case
232
                    if (isset($caller->_submitValues) && 0 < count($caller->_submitValues)) {
232
                    if ($caller->isSubmitted()) {
233
                        $value = $this->_findValue($caller->_submitValues);
233
                        $value = $this->_findValue($caller->_submitValues);
234
                    } else {
234
                    } else {
235
                        $value = $this->_findValue($caller->_defaultValues);
235
                        $value = $this->_findValue($caller->_defaultValues);
236
                    }
236
                    }
237
                }
237
                }
238
                if (null !== $value) {
238
                if (null !== $value) {
239
                    $this->setChecked($value);
239
                    $this->setChecked($value);
240
                }
240
                }
241
                break;
241
                break;
242
            case 'setGroupValue':
242
            case 'setGroupValue':
243
                $this->setChecked($arg);
243
                $this->setChecked($arg);
244
                break;
244
                break;
245
            default:
245
            default:
246
                parent::onQuickFormEvent($event, $arg, $caller);
246
                parent::onQuickFormEvent($event, $arg, $caller);
247
        }
247
        }
248
        return true;
248
        return true;
249
    } // end func onQuickFormEvent
249
    } // end func onQuickFormEvent
250
 
250
 
251
    // }}}
251
    // }}}
252
    // {{{ exportValue()
252
    // {{{ exportValue()
253
 
253
 
254
   /**
254
   /**
255
    * Return true if the checkbox is checked, null if it is not checked (getValue() returns false)
255
    * Return true if the checkbox is checked, null if it is not checked (getValue() returns false)
256
    */
256
    */
257
    function exportValue(&$submitValues, $assoc = false)
257
    function exportValue(&$submitValues, $assoc = false)
258
    {
258
    {
259
        $value = $this->_findValue($submitValues);
259
        $value = $this->_findValue($submitValues);
260
        if (null === $value) {
260
        if (null === $value) {
261
            $value = $this->getChecked()? true: null;
261
            $value = $this->getChecked()? true: null;
262
        }
262
        }
263
        return $this->_prepareValue($value, $assoc);
263
        return $this->_prepareValue($value, $assoc);
264
    }
264
    }
265
    
265
    
266
    // }}}
266
    // }}}
267
} //end class HTML_QuickForm_checkbox
267
} //end class HTML_QuickForm_checkbox
268
?>
268
?>