]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/framework/Web/UI/WebControls/TRadioButton.php
baculum: New Baculum API and Baculum Web
[bacula/bacula] / gui / baculum / framework / Web / UI / WebControls / TRadioButton.php
1 <?php
2 /**
3  * TRadioButton class file
4  *
5  * @author Qiang Xue <qiang.xue@gmail.com>
6  * @link https://github.com/pradosoft/prado
7  * @copyright Copyright &copy; 2005-2016 The PRADO Group
8  * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
9  * @package System.Web.UI.WebControls
10  */
11
12 /**
13  * Using TCheckBox parent class
14  */
15 Prado::using('System.Web.UI.WebControls.TCheckBox');
16
17 /**
18  * TRadioButton class
19  *
20  * TRadioButton displays a radio button on the page.
21  * You can specify the caption to display beside the radio buttonby setting
22  * the {@link setText Text} property.  The caption can appear either on the right
23  * or left of the radio button, which is determined by the {@link setTextAlign TextAlign}
24  * property.
25  *
26  * To determine whether the TRadioButton component is checked, test the {@link getChecked Checked}
27  * property. The {@link onCheckedChanged OnCheckedChanged} event is raised when
28  * the {@link getChecked Checked} state of the TRadioButton component changes
29  * between posts to the server. You can provide an event handler for
30  * the {@link onCheckedChanged OnCheckedChanged} event to  to programmatically
31  * control the actions performed when the state of the TRadioButton component changes
32  * between posts to the server.
33  *
34  * TRadioButton uses {@link setGroupName GroupName} to group together a set of radio buttons.
35  * Once the {@link setGroupName GroupName} is set, you can use the {@link getRadioButtonsInGroup}
36  * method to get an array of TRadioButtons having the same group name.
37  *
38  * If {@link setAutoPostBack AutoPostBack} is set true, changing the radio button state
39  * will cause postback action. And if {@link setCausesValidation CausesValidation}
40  * is true, validation will also be processed, which can be further restricted within
41  * a {@link setValidationGroup ValidationGroup}.
42  *
43  * Note, {@link setText Text} is rendered as is. Make sure it does not contain unwanted characters
44  * that may bring security vulnerabilities.
45  *
46  * @author Qiang Xue <qiang.xue@gmail.com>
47  * @package System.Web.UI.WebControls
48  * @since 3.0
49  */
50 class TRadioButton extends TCheckBox
51 {
52         /**
53          * @param array list of radio buttons that are on the current page hierarchy
54          */
55         private static $_activeButtons=array();
56         /**
57          * @var integer number of radio buttons created
58          */
59         private static $_buttonCount=0;
60         /**
61          * @var integer global ID of this radiobutton
62          */
63         private $_globalID;
64         /**
65          * @var string previous UniqueID (used to calculate UniqueGroup)
66          */
67         private $_previousUniqueID=null;
68         /**
69          * @var string the name used to fetch radiobutton post data
70          */
71         private $_uniqueGroupName=null;
72
73         /**
74          * Constructor.
75          * Registers the radiobutton in a global radiobutton collection.
76          * If overridden, the parent implementation must be invoked first.
77          */
78         public function __construct()
79         {
80                 parent::__construct();
81                 $this->_globalID = self::$_buttonCount++;
82         }
83
84         /**
85          * Registers the radio button groupings. If overriding onInit method,
86          * ensure to call parent implemenation.
87          * @param TEventParameter event parameter to be passed to the event handlers
88          */
89         public function onInit($param)
90         {
91                 parent::onInit($param);
92                 self::$_activeButtons[$this->_globalID]=$this;
93         }
94
95         /**
96          * Unregisters the radio button groupings. If overriding onInit method,
97          * ensure to call parent implemenation.
98          * @param TEventParameter event parameter to be passed to the event handlers
99          */
100         public function onUnLoad($param)
101         {
102                 unset(self::$_activeButtons[$this->_globalID]);
103                 parent::onUnLoad($param);
104         }
105
106         /**
107          * Loads user input data.
108          * This method is primarly used by framework developers.
109          * @param string the key that can be used to retrieve data from the input data collection
110          * @param array the input data collection
111          * @return boolean whether the data of the control has been changed
112          */
113         public function loadPostData($key,$values)
114         {
115                 $uniqueGroupName=$this->getUniqueGroupName();
116                 $value=isset($values[$uniqueGroupName])?$values[$uniqueGroupName]:null;
117                 if($value!==null && $value===$this->getValueAttribute())
118                 {
119                         if(!$this->getChecked())
120                         {
121                                 $this->setChecked(true);
122                                 return true;
123                         }
124                         else
125                                 return false;
126                 }
127                 else if($this->getChecked())
128                         $this->setChecked(false);
129                 return false;
130         }
131
132         /**
133          * @return string the name of the group that the radio button belongs to. Defaults to empty.
134          */
135         public function getGroupName()
136         {
137                 return $this->getViewState('GroupName','');
138         }
139
140         /**
141          * Sets the name of the group that the radio button belongs to.
142          * The group is unique among the control's naming container.
143          * @param string the group name
144          * @see setUniqueGroupName
145          */
146         public function setGroupName($value)
147         {
148                 $this->setViewState('GroupName',$value,'');
149                 $this->_uniqueGroupName=null;
150         }
151
152         /**
153          * @return string the name used to fetch radiobutton post data
154          */
155         public function getUniqueGroupName()
156         {
157                 if(($groupName=$this->getViewState('UniqueGroupName',''))!=='')
158                         return $groupName;
159                 else if(($uniqueID=$this->getUniqueID())!==$this->_previousUniqueID || $this->_uniqueGroupName===null)
160                 {
161                         $groupName=$this->getGroupName();
162                         $this->_previousUniqueID=$uniqueID;
163                         if($uniqueID!=='')
164                         {
165                                 if(($pos=strrpos($uniqueID,TControl::ID_SEPARATOR))!==false)
166                                 {
167                                         if($groupName!=='')
168                                                 $groupName=substr($uniqueID,0,$pos+1).$groupName;
169                                         else if(is_a($this->getNamingContainer(), 'TRadioButtonList'))
170                                                 $groupName=substr($uniqueID,0,$pos);
171                                 }
172                                 if($groupName==='')
173                                         $groupName=$uniqueID;
174                         }
175                         $this->_uniqueGroupName=$groupName;
176                 }
177                 return $this->_uniqueGroupName;
178         }
179
180         /**
181          * Sets the unique group name that the radio button belongs to.
182          * A unique group is a radiobutton group unique among the whole page hierarchy,
183          * while the {@link setGroupName GroupName} specifies a group that is unique
184          * among the control's naming container only.
185          * For example, each cell of a {@link TDataGrid} is a naming container.
186          * If you specify {@link setGroupName GroupName} for a radiobutton in a cell,
187          * it groups together radiobutton within a cell, but not the other, even though
188          * they have the same {@link setGroupName GroupName}.
189          * On the contratry, if {@link setUniqueGroupName UniqueGroupName} is used instead,
190          * it will group all appropriate radio buttons on the whole page hierarchy.
191          * Note, when both {@link setUniqueGroupName UniqueGroupName} and
192          * {@link setGroupName GroupName}, the former takes precedence.
193          * @param string the group name
194          * @see setGroupName
195          */
196         public function setUniqueGroupName($value)
197         {
198                 $this->setViewState('UniqueGroupName',$value,'');
199         }
200
201         /**
202          * Gets an array of radiobuttons whose group name is the same as this radiobutton's.
203          * Note, only those radiobuttons that are on the current page hierarchy may be
204          * returned in the result.
205          * @return array list of TRadioButton with the same group
206          */
207         public function getRadioButtonsInGroup()
208         {
209                 $group = $this->getUniqueGroupName();
210                 $buttons = array();
211                 foreach(self::$_activeButtons as $control)
212                 {
213                         if($control->getUniqueGroupName() === $group)
214                                 $buttons[] = $control;
215                 }
216                 return $buttons;
217         }
218
219         /**
220          * @return string the value attribute to be rendered
221          */
222         protected function getValueAttribute()
223         {
224                 if(($value=parent::getValueAttribute())==='')
225                         return $this->getUniqueID();
226                 else
227                         return $value;
228         }
229
230         /**
231          * @return boolean whether to render javascript.
232          */
233         public function getEnableClientScript()
234         {
235                 return $this->getViewState('EnableClientScript',true);
236         }
237
238         /**
239          * @param boolean whether to render javascript.
240          */
241         public function setEnableClientScript($value)
242         {
243                 $this->setViewState('EnableClientScript',TPropertyValue::ensureBoolean($value),true);
244         }
245
246         /**
247          * Renders a radiobutton input element.
248          * @param THtmlWriter the writer for the rendering purpose
249          * @param string checkbox id
250          * @param string onclick js
251          */
252         protected function renderInputTag($writer,$clientID,$onclick)
253         {
254                 if($clientID!=='')
255                         $writer->addAttribute('id',$clientID);
256                 $writer->addAttribute('type','radio');
257                 $writer->addAttribute('name',$this->getUniqueGroupName());
258                 $writer->addAttribute('value',$this->getValueAttribute());
259                 if(!empty($onclick))
260                         $writer->addAttribute('onclick',$onclick);
261                 if($this->getChecked())
262                         $writer->addAttribute('checked','checked');
263                 if(!$this->getEnabled(true))
264                         $writer->addAttribute('disabled','disabled');
265
266                 $page=$this->getPage();
267                 if($this->getEnabled(true)
268                         && $this->getEnableClientScript()
269                         && $this->getAutoPostBack()
270                         && $page->getClientSupportsJavaScript())
271                 {
272                         $this->renderClientControlScript($writer);
273                 }
274
275                 if(($accesskey=$this->getAccessKey())!=='')
276                         $writer->addAttribute('accesskey',$accesskey);
277                 if(($tabindex=$this->getTabIndex())>0)
278                         $writer->addAttribute('tabindex',"$tabindex");
279                 if($attributes=$this->getViewState('InputAttributes',null))
280                         $writer->addAttributes($attributes);
281                 $writer->renderBeginTag('input');
282                 $writer->renderEndTag();
283         }
284
285         /**
286          * Renders the client-script code.
287          */
288         protected function renderClientControlScript($writer)
289         {
290                 $cs = $this->getPage()->getClientScript();
291                 $cs->registerPostBackControl($this->getClientClassName(),$this->getPostBackOptions());
292         }
293
294         /**
295          * Gets the name of the javascript class responsible for performing postback for this control.
296          * This method overrides the parent implementation.
297          * @return string the javascript class name
298          */
299         protected function getClientClassName()
300         {
301                 return 'Prado.WebUI.TRadioButton';
302         }
303 }
304