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