Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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