Subversion Repositories Applications.papyrus

Rev

Rev 1987 | 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: radio.php,v 1.2 2005-09-20 17:01:22 ddelon Exp $
320 jpm 21
 
22
require_once('HTML/QuickForm/input.php');
23
 
24
/**
25
 * HTML class for a radio type element
26
 *
27
 * @author       Adam Daniel <adaniel1@eesus.jnj.com>
28
 * @author       Bertrand Mansion <bmansion@mamasam.com>
29
 * @version      1.1
30
 * @since        PHP4.04pl1
31
 * @access       public
32
 */
33
class HTML_QuickForm_radio extends HTML_QuickForm_input
34
{
35
    // {{{ properties
36
 
37
    /**
38
     * Radio display text
39
     * @var       string
40
     * @since     1.1
41
     * @access    private
42
     */
43
    var $_text = '';
44
 
45
    // }}}
46
    // {{{ constructor
47
 
48
    /**
49
     * Class constructor
50
     *
51
     * @param     string    Input field name attribute
52
     * @param     mixed     Label(s) for a field
53
     * @param     string    Text to display near the radio
54
     * @param     string    Input field value
55
     * @param     mixed     Either a typical HTML attribute string or an associative array
56
     * @since     1.0
57
     * @access    public
58
     * @return    void
59
     */
60
    function HTML_QuickForm_radio($elementName=null, $elementLabel=null, $text=null, $value=null, $attributes=null)
61
    {
62
        $this->HTML_QuickForm_element($elementName, $elementLabel, $attributes);
63
        if (isset($value)) {
64
            $this->setValue($value);
65
        }
66
        $this->_persistantFreeze = true;
67
        $this->setType('radio');
68
        $this->_text = $text;
69
        $this->_generateId();
70
    } //end constructor
71
 
72
    // }}}
73
    // {{{ setChecked()
74
 
75
    /**
76
     * Sets whether radio button is checked
77
     *
78
     * @param     bool    $checked  Whether the field is checked or not
79
     * @since     1.0
80
     * @access    public
81
     * @return    void
82
     */
83
    function setChecked($checked)
84
    {
85
        if (!$checked) {
86
            $this->removeAttribute('checked');
87
        } else {
88
            $this->updateAttributes(array('checked'=>'checked'));
89
        }
90
    } //end func setChecked
91
 
92
    // }}}
93
    // {{{ getChecked()
94
 
95
    /**
96
     * Returns whether radio button is checked
97
     *
98
     * @since     1.0
99
     * @access    public
100
     * @return    string
101
     */
102
    function getChecked()
103
    {
104
        return $this->getAttribute('checked');
105
    } //end func getChecked
106
 
107
    // }}}
108
    // {{{ toHtml()
109
 
110
    /**
111
     * Returns the radio element in HTML
112
     *
113
     * @since     1.0
114
     * @access    public
115
     * @return    string
116
     */
117
    function toHtml()
118
    {
119
        if (0 == strlen($this->_text)) {
120
            $label = '';
121
        } elseif ($this->_flagFrozen) {
122
            $label = $this->_text;
123
        } else {
124
            $label = '<label for="' . $this->getAttribute('id') . '">' . $this->_text . '</label>';
125
        }
126
        return HTML_QuickForm_input::toHtml() . $label;
127
    } //end func toHtml
128
 
129
    // }}}
130
    // {{{ getFrozenHtml()
131
 
132
    /**
133
     * Returns the value of field without HTML tags
134
     *
135
     * @since     1.0
136
     * @access    public
137
     * @return    string
138
     */
139
    function getFrozenHtml()
140
    {
141
        if ($this->getChecked()) {
142
            return '<tt>(x)</tt>' .
143
                   $this->_getPersistantData();
144
        } else {
145
            return '<tt>( )</tt>';
146
        }
147
    } //end func getFrozenHtml
148
 
149
    // }}}
150
    // {{{ setText()
151
 
152
    /**
153
     * Sets the radio text
154
     *
155
     * @param     string    $text  Text to display near the radio button
156
     * @since     1.1
157
     * @access    public
158
     * @return    void
159
     */
160
    function setText($text)
161
    {
162
        $this->_text = $text;
163
    } //end func setText
164
 
165
    // }}}
166
    // {{{ getText()
167
 
168
    /**
169
     * Returns the radio text
170
     *
171
     * @since     1.1
172
     * @access    public
173
     * @return    string
174
     */
175
    function getText()
176
    {
177
        return $this->_text;
178
    } //end func getText
179
 
180
    // }}}
181
    // {{{ onQuickFormEvent()
182
 
183
    /**
184
     * Called by HTML_QuickForm whenever form event is made on this element
185
     *
186
     * @param     string    $event  Name of event
187
     * @param     mixed     $arg    event arguments
188
     * @param     object    $caller calling object
189
     * @since     1.0
190
     * @access    public
191
     * @return    void
192
     */
193
    function onQuickFormEvent($event, $arg, &$caller)
194
    {
195
        switch ($event) {
196
            case 'updateValue':
197
                // constant values override both default and submitted ones
198
                // default values are overriden by submitted
199
                $value = $this->_findValue($caller->_constantValues);
200
                if (null === $value) {
201
                    $value = $this->_findValue($caller->_submitValues);
202
                    if (null === $value) {
203
                        $value = $this->_findValue($caller->_defaultValues);
204
                    }
205
                }
206
                if ($value == $this->getValue()) {
207
                    $this->setChecked(true);
208
                } else {
209
                    $this->setChecked(false);
210
                }
211
                break;
212
            case 'setGroupValue':
213
                if ($arg == $this->getValue()) {
214
                    $this->setChecked(true);
215
                } else {
216
                    $this->setChecked(false);
217
                }
218
                break;
219
            default:
220
                parent::onQuickFormEvent($event, $arg, $caller);
221
        }
222
        return true;
223
    } // end func onQuickFormLoad
224
 
225
    // }}}
226
    // {{{ exportValue()
227
 
228
   /**
229
    * Returns the value attribute if the radio is checked, null if it is not
230
    */
231
    function exportValue(&$submitValues, $assoc = false)
232
    {
233
        $value = $this->_findValue($submitValues);
234
        if (null === $value) {
235
            $value = $this->getChecked()? $this->getValue(): null;
236
        } elseif ($value != $this->getValue()) {
237
            $value = null;
238
        }
239
        return $this->_prepareValue($value, $assoc);
240
    }
241
 
242
    // }}}
243
} //end class HTML_QuickForm_radio
244
?>