3 * TRadioButton class file
5 * @author Qiang Xue <qiang.xue@gmail.com>
6 * @link https://github.com/pradosoft/prado
7 * @copyright Copyright © 2005-2016 The PRADO Group
8 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
9 * @package System.Web.UI.WebControls
13 * Using TCheckBox parent class
15 Prado::using('System.Web.UI.WebControls.TCheckBox');
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}
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.
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.
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}.
43 * Note, {@link setText Text} is rendered as is. Make sure it does not contain unwanted characters
44 * that may bring security vulnerabilities.
46 * @author Qiang Xue <qiang.xue@gmail.com>
47 * @package System.Web.UI.WebControls
50 class TRadioButton extends TCheckBox
53 * @param array list of radio buttons that are on the current page hierarchy
55 private static $_activeButtons=array();
57 * @var integer number of radio buttons created
59 private static $_buttonCount=0;
61 * @var integer global ID of this radiobutton
65 * @var string previous UniqueID (used to calculate UniqueGroup)
67 private $_previousUniqueID=null;
69 * @var string the name used to fetch radiobutton post data
71 private $_uniqueGroupName=null;
75 * Registers the radiobutton in a global radiobutton collection.
76 * If overridden, the parent implementation must be invoked first.
78 public function __construct()
80 parent::__construct();
81 $this->_globalID = self::$_buttonCount++;
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
89 public function onInit($param)
91 parent::onInit($param);
92 self::$_activeButtons[$this->_globalID]=$this;
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
100 public function onUnLoad($param)
102 unset(self::$_activeButtons[$this->_globalID]);
103 parent::onUnLoad($param);
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
113 public function loadPostData($key,$values)
115 $uniqueGroupName=$this->getUniqueGroupName();
116 $value=isset($values[$uniqueGroupName])?$values[$uniqueGroupName]:null;
117 if($value!==null && $value===$this->getValueAttribute())
119 if(!$this->getChecked())
121 $this->setChecked(true);
127 else if($this->getChecked())
128 $this->setChecked(false);
133 * @return string the name of the group that the radio button belongs to. Defaults to empty.
135 public function getGroupName()
137 return $this->getViewState('GroupName','');
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
146 public function setGroupName($value)
148 $this->setViewState('GroupName',$value,'');
149 $this->_uniqueGroupName=null;
153 * @return string the name used to fetch radiobutton post data
155 public function getUniqueGroupName()
157 if(($groupName=$this->getViewState('UniqueGroupName',''))!=='')
159 else if(($uniqueID=$this->getUniqueID())!==$this->_previousUniqueID || $this->_uniqueGroupName===null)
161 $groupName=$this->getGroupName();
162 $this->_previousUniqueID=$uniqueID;
165 if(($pos=strrpos($uniqueID,TControl::ID_SEPARATOR))!==false)
168 $groupName=substr($uniqueID,0,$pos+1).$groupName;
169 else if(is_a($this->getNamingContainer(), 'TRadioButtonList'))
170 $groupName=substr($uniqueID,0,$pos);
173 $groupName=$uniqueID;
175 $this->_uniqueGroupName=$groupName;
177 return $this->_uniqueGroupName;
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
196 public function setUniqueGroupName($value)
198 $this->setViewState('UniqueGroupName',$value,'');
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
207 public function getRadioButtonsInGroup()
209 $group = $this->getUniqueGroupName();
211 foreach(self::$_activeButtons as $control)
213 if($control->getUniqueGroupName() === $group)
214 $buttons[] = $control;
220 * @return string the value attribute to be rendered
222 protected function getValueAttribute()
224 if(($value=parent::getValueAttribute())==='')
225 return $this->getUniqueID();
231 * @return boolean whether to render javascript.
233 public function getEnableClientScript()
235 return $this->getViewState('EnableClientScript',true);
239 * @param boolean whether to render javascript.
241 public function setEnableClientScript($value)
243 $this->setViewState('EnableClientScript',TPropertyValue::ensureBoolean($value),true);
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
252 protected function renderInputTag($writer,$clientID,$onclick)
255 $writer->addAttribute('id',$clientID);
256 $writer->addAttribute('type','radio');
257 $writer->addAttribute('name',$this->getUniqueGroupName());
258 $writer->addAttribute('value',$this->getValueAttribute());
260 $writer->addAttribute('onclick',$onclick);
261 if($this->getChecked())
262 $writer->addAttribute('checked','checked');
263 if(!$this->getEnabled(true))
264 $writer->addAttribute('disabled','disabled');
266 $page=$this->getPage();
267 if($this->getEnabled(true)
268 && $this->getEnableClientScript()
269 && $this->getAutoPostBack()
270 && $page->getClientSupportsJavaScript())
272 $this->renderClientControlScript($writer);
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();
286 * Renders the client-script code.
288 protected function renderClientControlScript($writer)
290 $cs = $this->getPage()->getClientScript();
291 $cs->registerPostBackControl($this->getClientClassName(),$this->getPostBackOptions());
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
299 protected function getClientClassName()
301 return 'Prado.WebUI.TRadioButton';