Subversion Repositories Applications.papyrus

Rev

Rev 320 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
320 jpm 1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4: */
3
// +----------------------------------------------------------------------+
4
// | PHP version 4.0                                                      |
5
// +----------------------------------------------------------------------+
6
// | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group             |
7
// +----------------------------------------------------------------------+
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        |
10
// | available at through the world-wide-web at                           |
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   |
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.               |
15
// +----------------------------------------------------------------------+
16
// | Authors: Adam Daniel <adaniel1@eesus.jnj.com>                        |
17
// |          Bertrand Mansion <bmansion@mamasam.com>                     |
18
// +----------------------------------------------------------------------+
19
//
443 ddelon 20
// $Id: advcheckbox.php,v 1.2 2005-09-20 17:01:22 ddelon Exp $
320 jpm 21
 
22
require_once('HTML/QuickForm/checkbox.php');
23
 
24
/**
25
 * HTML class for an advanced checkbox type field
26
 *
27
 * Basically this fixes a problem that HTML has had
28
 * where checkboxes can only pass a single value (the
29
 * value of the checkbox when checked).  A value for when
30
 * the checkbox is not checked cannot be passed, and
31
 * furthermore the checkbox variable doesn't even exist if
32
 * the checkbox was submitted unchecked.
33
 *
34
 * It works by creating a hidden field with the passed-in name
35
 * and creating the checkbox with no name, but with a javascript
36
 * onclick which sets the value of the hidden field.
37
 *
38
 * @author       Jason Rust <jrust@php.net>
39
 * @since        2.0
40
 * @access       public
41
 */
42
class HTML_QuickForm_advcheckbox extends HTML_QuickForm_checkbox
43
{
44
    // {{{ properties
45
 
46
    /**
47
     * The values passed by the hidden elment
48
     *
49
     * @var array
50
     * @access private
51
     */
52
    var $_values = null;
53
 
54
    /**
55
     * The default value
56
     *
57
     * @var boolean
58
     * @access private
59
     */
60
    var $_currentValue = null;
61
 
62
    // }}}
63
    // {{{ constructor
64
 
65
    /**
66
     * Class constructor
67
     *
68
     * @param     string    $elementName    (optional)Input field name attribute
69
     * @param     string    $elementLabel   (optional)Input field label
70
     * @param     string    $text           (optional)Text to put after the checkbox
71
     * @param     mixed     $attributes     (optional)Either a typical HTML attribute string
72
     *                                      or an associative array
73
     * @param     mixed     $values         (optional)Values to pass if checked or not checked
74
     *
75
     * @since     1.0
76
     * @access    public
77
     * @return    void
78
     */
79
    function HTML_QuickForm_advcheckbox($elementName=null, $elementLabel=null, $text=null, $attributes=null, $values=null)
80
    {
81
        $this->HTML_QuickForm_checkbox($elementName, $elementLabel, $text, $attributes);
82
        $this->setValues($values);
83
    } //end constructor
84
 
85
    // }}}
86
    // {{{ getPrivateName()
87
 
88
    /**
89
     * Gets the pribate name for the element
90
     *
91
     * @param   string  $elementName The element name to make private
92
     *
93
     * @access public
94
     * @return string
95
     */
96
    function getPrivateName($elementName)
97
    {
98
        return '__'.$elementName;
99
    }
100
 
101
    // }}}
102
    // {{{ getOnclickJs()
103
 
104
    /**
105
     * Create the javascript for the onclick event which will
106
     * set the value of the hidden field
107
     *
108
     * @param     string    $elementName    The element name
109
     *
110
     * @access public
111
     * @return string
112
     */
113
    function getOnclickJs($elementName)
114
    {
115
        $onclickJs = 'if (this.checked) { this.form[\''.$elementName.'\'].value=\''.addcslashes($this->_values[1], '\'').'\'; }';
116
        $onclickJs .= 'else { this.form[\''.$elementName.'\'].value=\''.addcslashes($this->_values[0], '\'').'\'; }';
117
        return $onclickJs;
118
    }
119
 
120
    // }}}
121
    // {{{ setValues()
122
 
123
    /**
124
     * Sets the values used by the hidden element
125
     *
126
     * @param   mixed   $values The values, either a string or an array
127
     *
128
     * @access public
129
     * @return void
130
     */
131
    function setValues($values)
132
    {
133
        if (empty($values)) {
134
            // give it default checkbox behavior
135
            $this->_values = array('', 1);
136
        } elseif (is_scalar($values)) {
137
            // if it's string, then assume the value to
138
            // be passed is for when the element is checked
139
            $this->_values = array('', $values);
140
        } else {
141
            $this->_values = $values;
142
        }
143
        $this->setChecked($this->_currentValue == $this->_values[1]);
144
    }
145
 
146
    // }}}
147
    // {{{ setValue()
148
 
149
   /**
150
    * Sets the element's value
151
    *
152
    * @param    mixed   Element's value
153
    * @access   public
154
    */
155
    function setValue($value)
156
    {
157
        $this->setChecked(isset($this->_values[1]) && $value == $this->_values[1]);
158
        $this->_currentValue = $value;
159
    }
160
 
161
    // }}}
162
    // {{{ getValue()
163
 
164
   /**
165
    * Returns the element's value
166
    *
167
    * @access   public
168
    * @return   mixed
169
    */
170
    function getValue()
171
    {
172
        if (is_array($this->_values)) {
173
            return $this->_values[$this->getChecked()? 1: 0];
174
        } else {
175
            return null;
176
        }
177
    }
178
 
179
    // }}}
180
    // {{{ toHtml()
181
 
182
    /**
183
     * Returns the checkbox element in HTML
184
     * and the additional hidden element in HTML
185
     *
186
     * @access    public
187
     * @return    string
188
     */
189
    function toHtml()
190
    {
191
        if ($this->_flagFrozen) {
192
            return parent::toHtml();
193
        } else {
194
            $oldName = $this->getName();
195
            $oldJs   = $this->getAttribute('onclick');
196
            $this->updateAttributes(array(
197
                'name'    => $this->getPrivateName($oldName),
198
                'onclick' => $this->getOnclickJs($oldName) . ' ' . $oldJs
199
            ));
443 ddelon 200
            $html = parent::toHtml() . '<input' .
201
                    $this->_getAttrString(array(
202
                        'type'  => 'hidden',
203
                        'name'  => $oldName,
204
                        'value' => $this->getValue()
205
                    )) . ' />';
320 jpm 206
            // revert the name and JS, in case this method will be called once more
207
            $this->updateAttributes(array(
208
                'name'    => $oldName,
209
                'onclick' => $oldJs
210
            ));
211
            return $html;
212
        }
213
    } //end func toHtml
214
 
215
    // }}}
216
    // {{{ getFrozenHtml()
217
 
218
   /**
219
    * Unlike checkbox, this has to append a hidden input in both
220
    * checked and non-checked states
221
    */
222
    function getFrozenHtml()
223
    {
224
        return ($this->getChecked()? '<tt>[x]</tt>': '<tt>[ ]</tt>') .
225
               $this->_getPersistantData();
226
    }
227
 
228
    // }}}
229
    // {{{ onQuickFormEvent()
230
 
231
    /**
232
     * Called by HTML_QuickForm whenever form event is made on this element
233
     *
234
     * @param     string    $event  Name of event
235
     * @param     mixed     $arg    event arguments
236
     * @param     object    $caller calling object
237
     * @since     1.0
238
     * @access    public
239
     * @return    void
240
     */
241
    function onQuickFormEvent($event, $arg, &$caller)
242
    {
243
        switch ($event) {
244
            case 'updateValue':
245
                // constant values override both default and submitted ones
246
                // default values are overriden by submitted
247
                $value = $this->_findValue($caller->_constantValues);
248
                if (null === $value) {
249
                    $value = $this->_findValue($caller->_submitValues);
250
                    if (null === $value) {
251
                        $value = $this->_findValue($caller->_defaultValues);
252
                    }
253
                }
254
                if (null !== $value) {
255
                    $this->setValue($value);
256
                }
257
                break;
258
            default:
259
                parent::onQuickFormEvent($event, $arg, $caller);
260
        }
261
        return true;
262
    } // end func onQuickFormLoad
263
 
264
    // }}}
265
    // {{{ exportValue()
266
 
267
   /**
268
    * This element has a value even if it is not checked, thus we override
269
    * checkbox's behaviour here
270
    */
271
    function exportValue(&$submitValues, $assoc)
272
    {
273
        $value = $this->_findValue($submitValues);
274
        if (null === $value) {
275
            $value = $this->getValue();
276
        } elseif (is_array($this->_values) && ($value != $this->_values[0]) && ($value != $this->_values[1])) {
277
            $value = null;
278
        }
279
        return $this->_prepareValue($value, $assoc);
280
    }
281
    // }}}
282
} //end class HTML_QuickForm_advcheckbox
283
?>