Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | 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
//
20
// $Id: advcheckbox.php,v 1.1 2005-03-30 08:50:33 jpm Exp $
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
            ));
200
            $html = parent::toHtml() . '<input type="hidden" name="' . $oldName .
201
                    '" value="' . $this->getValue() . '" />';
202
            // revert the name and JS, in case this method will be called once more
203
            $this->updateAttributes(array(
204
                'name'    => $oldName,
205
                'onclick' => $oldJs
206
            ));
207
            return $html;
208
        }
209
    } //end func toHtml
210
 
211
    // }}}
212
    // {{{ getFrozenHtml()
213
 
214
   /**
215
    * Unlike checkbox, this has to append a hidden input in both
216
    * checked and non-checked states
217
    */
218
    function getFrozenHtml()
219
    {
220
        return ($this->getChecked()? '<tt>[x]</tt>': '<tt>[ ]</tt>') .
221
               $this->_getPersistantData();
222
    }
223
 
224
    // }}}
225
    // {{{ onQuickFormEvent()
226
 
227
    /**
228
     * Called by HTML_QuickForm whenever form event is made on this element
229
     *
230
     * @param     string    $event  Name of event
231
     * @param     mixed     $arg    event arguments
232
     * @param     object    $caller calling object
233
     * @since     1.0
234
     * @access    public
235
     * @return    void
236
     */
237
    function onQuickFormEvent($event, $arg, &$caller)
238
    {
239
        switch ($event) {
240
            case 'updateValue':
241
                // constant values override both default and submitted ones
242
                // default values are overriden by submitted
243
                $value = $this->_findValue($caller->_constantValues);
244
                if (null === $value) {
245
                    $value = $this->_findValue($caller->_submitValues);
246
                    if (null === $value) {
247
                        $value = $this->_findValue($caller->_defaultValues);
248
                    }
249
                }
250
                if (null !== $value) {
251
                    $this->setValue($value);
252
                }
253
                break;
254
            default:
255
                parent::onQuickFormEvent($event, $arg, $caller);
256
        }
257
        return true;
258
    } // end func onQuickFormLoad
259
 
260
    // }}}
261
    // {{{ exportValue()
262
 
263
   /**
264
    * This element has a value even if it is not checked, thus we override
265
    * checkbox's behaviour here
266
    */
267
    function exportValue(&$submitValues, $assoc)
268
    {
269
        $value = $this->_findValue($submitValues);
270
        if (null === $value) {
271
            $value = $this->getValue();
272
        } elseif (is_array($this->_values) && ($value != $this->_values[0]) && ($value != $this->_values[1])) {
273
            $value = null;
274
        }
275
        return $this->_prepareValue($value, $assoc);
276
    }
277
    // }}}
278
} //end class HTML_QuickForm_advcheckbox
279
?>