Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * TButton 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: TButton.php 2541 2008-10-21 15:05:13Z qiang.xue $
10
 * @package System.Web.UI.WebControls
11
 */
12
 
13
/**
14
 * TButton class
15
 *
16
 * TButton creates a click button on the page. It is mainly used to submit data to a page.
17
 *
18
 * TButton raises two server-side events, {@link onClick OnClick} and {@link onCommand OnCommand},
19
 * when it is clicked on the client-side. The difference between these two events
20
 * is that the event {@link onCommand OnCommand} is bubbled up to the button's ancestor controls.
21
 * And within the event parameter for {@link onCommand OnCommand} contains the reference
22
 * to the {@link setCommandName CommandName} and {@link setCommandParameter CommandParameter}
23
 * property values that are set for the button object. This allows you to create multiple TButton
24
 * components on a Web page and programmatically determine which one is clicked
25
 * with what parameter.
26
 *
27
 * Clicking on button can also trigger form validation, if
28
 * {@link setCausesValidation CausesValidation} is true.
29
 * The validation may be restricted within a certain group of validator
30
 * controls by setting {@link setValidationGroup ValidationGroup} property.
31
 * If validation is successful, the data will be post back to the same page.
32
 *
33
 * TButton displays the {@link setText Text} property as the button caption.
34
 *
35
 * TButton can be one of three {@link setButtonType ButtonType}: Submit, Button and Reset.
36
 * By default, it is a Submit button and the form submission uses the browser's
37
 * default submission capability. If it is Button or Reset, postback may occur
38
 * if one of the following conditions is met:
39
 * - an event handler is attached to {@link onClick OnClick} event;
40
 * - an event handler is attached to {@link onCommand OnCommand} event;
41
 * - the button is in a non-empty validation group.
42
 * In addition, clicking on a Reset button will clear up all input fields
43
 * if the button does not cause a postback.
44
 *
45
 * @author Qiang Xue <qiang.xue@gmail.com>
46
 * @version $Id: TButton.php 2541 2008-10-21 15:05:13Z qiang.xue $
47
 * @package System.Web.UI.WebControls
48
 * @since 3.0
49
 */
50
class TButton extends TWebControl implements IPostBackEventHandler, IButtonControl, IDataRenderer
51
{
52
	/**
53
	 * @return string tag name of the button
54
	 */
55
	protected function getTagName()
56
	{
57
		return 'input';
58
	}
59
 
60
	/**
61
	 * @return boolean whether to render javascript.
62
	 */
63
	public function getEnableClientScript()
64
	{
65
		return $this->getViewState('EnableClientScript',true);
66
	}
67
 
68
	/**
69
	 * @param boolean whether to render javascript.
70
	 */
71
	public function setEnableClientScript($value)
72
	{
73
		$this->setViewState('EnableClientScript',TPropertyValue::ensureBoolean($value),true);
74
	}
75
 
76
	/**
77
	 * Adds attribute name-value pairs to renderer.
78
	 * This overrides the parent implementation with additional button specific attributes.
79
	 * @param THtmlWriter the writer used for the rendering purpose
80
	 */
81
	protected function addAttributesToRender($writer)
82
	{
83
		$page=$this->getPage();
84
		$page->ensureRenderInForm($this);
85
		$writer->addAttribute('type',strtolower($this->getButtonType()));
86
		if(($uniqueID=$this->getUniqueID())!=='')
87
			$writer->addAttribute('name',$uniqueID);
88
		$writer->addAttribute('value',$this->getText());
89
		if($this->getEnabled(true))
90
		{
91
			if($this->getEnableClientScript() && $this->needPostBackScript())
92
				$this->renderClientControlScript($writer);
93
		}
94
		else if($this->getEnabled()) // in this case, parent will not render 'disabled'
95
			$writer->addAttribute('disabled','disabled');
96
 
97
		parent::addAttributesToRender($writer);
98
	}
99
 
100
	/**
101
	 * Renders the client-script code.
102
	 */
103
	protected function renderClientControlScript($writer)
104
	{
105
		$writer->addAttribute('id',$this->getClientID());
106
		$this->getPage()->getClientScript()->registerPostBackControl($this->getClientClassName(),$this->getPostBackOptions());
107
	}
108
 
109
	/**
110
	 * Gets the name of the javascript class responsible for performing postback for this control.
111
	 * This method overrides the parent implementation.
112
	 * @return string the javascript class name
113
	 */
114
	protected function getClientClassName()
115
	{
116
		return 'Prado.WebUI.TButton';
117
	}
118
 
119
	/**
120
	 * @return boolean whether to perform validation if the button is clicked
121
	 */
122
	protected function canCauseValidation()
123
	{
124
		if($this->getCausesValidation())
125
		{
126
			$group=$this->getValidationGroup();
127
			return $this->getPage()->getValidators($group)->getCount()>0;
128
		}
129
		else
130
			return false;
131
	}
132
 
133
	/**
134
	 * @param boolean set by a panel to register this button as the default button for the panel.
135
	 */
136
	public function setIsDefaultButton($value)
137
	{
138
		$this->setViewState('IsDefaultButton', TPropertyValue::ensureBoolean($value),false);
139
	}
140
 
141
	/**
142
	 * @return boolean true if this button is registered as a default button for a panel.
143
	 */
144
	public function getIsDefaultButton()
145
	{
146
		return $this->getViewState('IsDefaultButton', false);
147
	}
148
 
149
	/**
150
	 * @return boolean whether the button needs javascript to do postback
151
	 */
152
	protected function needPostBackScript()
153
	{
154
		return $this->canCauseValidation() || ($this->getButtonType()!==TButtonType::Submit &&
155
			($this->hasEventHandler('OnClick') || $this->hasEventHandler('OnCommand')))
156
			|| $this->getIsDefaultButton();
157
	}
158
 
159
	/**
160
	 * Returns postback specifications for the button.
161
	 * This method is used by framework and control developers.
162
	 * @return array parameters about how the button defines its postback behavior.
163
	 */
164
	protected function getPostBackOptions()
165
	{
166
		$options['ID']=$this->getClientID();
167
		$options['CausesValidation']=$this->getCausesValidation();
168
		$options['EventTarget'] = $this->getUniqueID();
169
		$options['ValidationGroup']=$this->getValidationGroup();
170
 
171
		return $options;
172
	}
173
 
174
	/**
175
	 * Renders the body content enclosed between the control tag.
176
	 * This overrides the parent implementation with nothing to be rendered.
177
	 * @param THtmlWriter the writer used for the rendering purpose
178
	 */
179
	public function renderContents($writer)
180
	{
181
	}
182
 
183
	/**
184
	 * This method is invoked when the button is clicked.
185
	 * The method raises 'OnClick' event to fire up the event handlers.
186
	 * If you override this method, be sure to call the parent implementation
187
	 * so that the event handler can be invoked.
188
	 * @param TEventParameter event parameter to be passed to the event handlers
189
	 */
190
	public function onClick($param)
191
	{
192
		$this->raiseEvent('OnClick',$this,$param);
193
	}
194
 
195
	/**
196
	 * This method is invoked when the button is clicked.
197
	 * The method raises 'OnCommand' event to fire up the event handlers.
198
	 * If you override this method, be sure to call the parent implementation
199
	 * so that the event handlers can be invoked.
200
	 * @param TCommandEventParameter event parameter to be passed to the event handlers
201
	 */
202
	public function onCommand($param)
203
	{
204
		$this->raiseEvent('OnCommand',$this,$param);
205
		$this->raiseBubbleEvent($this,$param);
206
	}
207
 
208
	/**
209
	 * Raises the postback event.
210
	 * This method is required by {@link IPostBackEventHandler} interface.
211
	 * If {@link getCausesValidation CausesValidation} is true, it will
212
	 * invoke the page's {@link TPage::validate validate} method first.
213
	 * It will raise {@link onClick OnClick} and {@link onCommand OnCommand} events.
214
	 * This method is mainly used by framework and control developers.
215
	 * @param TEventParameter the event parameter
216
	 */
217
	public function raisePostBackEvent($param)
218
	{
219
		if($this->getCausesValidation())
220
			$this->getPage()->validate($this->getValidationGroup());
221
		$this->onClick(null);
222
		$this->onCommand(new TCommandEventParameter($this->getCommandName(),$this->getCommandParameter()));
223
	}
224
 
225
	/**
226
	 * @return string caption of the button
227
	 */
228
	public function getText()
229
	{
230
		return $this->getViewState('Text','');
231
	}
232
 
233
	/**
234
	 * @param string caption of the button
235
	 */
236
	public function setText($value)
237
	{
238
		$this->setViewState('Text',$value,'');
239
	}
240
 
241
	/**
242
	 * Returns the caption of the button.
243
	 * This method is required by {@link IDataRenderer}.
244
	 * It is the same as {@link getText()}.
245
	 * @return string caption of the button.
246
	 * @see getText
247
	 * @since 3.1.0
248
	 */
249
	public function getData()
250
	{
251
		return $this->getText();
252
	}
253
 
254
	/**
255
	 * Sets the caption of the button.
256
	 * This method is required by {@link IDataRenderer}.
257
	 * It is the same as {@link setText()}.
258
	 * @param string caption of the button
259
	 * @see setText
260
	 * @since 3.1.0
261
	 */
262
	public function setData($value)
263
	{
264
		$this->setText($value);
265
	}
266
 
267
	/**
268
	 * @return boolean whether postback event trigger by this button will cause input validation, default is true
269
	 */
270
	public function getCausesValidation()
271
	{
272
		return $this->getViewState('CausesValidation',true);
273
	}
274
 
275
	/**
276
	 * @param boolean whether postback event trigger by this button will cause input validation
277
	 */
278
	public function setCausesValidation($value)
279
	{
280
		$this->setViewState('CausesValidation',TPropertyValue::ensureBoolean($value),true);
281
	}
282
 
283
	/**
284
	 * @return string the command name associated with the {@link onCommand OnCommand} event.
285
	 */
286
	public function getCommandName()
287
	{
288
		return $this->getViewState('CommandName','');
289
	}
290
 
291
	/**
292
	 * @param string the command name associated with the {@link onCommand OnCommand} event.
293
	 */
294
	public function setCommandName($value)
295
	{
296
		$this->setViewState('CommandName',$value,'');
297
	}
298
 
299
	/**
300
	 * @return string the parameter associated with the {@link onCommand OnCommand} event
301
	 */
302
	public function getCommandParameter()
303
	{
304
		return $this->getViewState('CommandParameter','');
305
	}
306
 
307
	/**
308
	 * @param string the parameter associated with the {@link onCommand OnCommand} event.
309
	 */
310
	public function setCommandParameter($value)
311
	{
312
		$this->setViewState('CommandParameter',$value,'');
313
	}
314
 
315
	/**
316
	 * @return string the group of validators which the button causes validation upon postback
317
	 */
318
	public function getValidationGroup()
319
	{
320
		return $this->getViewState('ValidationGroup','');
321
	}
322
 
323
	/**
324
	 * @param string the group of validators which the button causes validation upon postback
325
	 */
326
	public function setValidationGroup($value)
327
	{
328
		$this->setViewState('ValidationGroup',$value,'');
329
	}
330
 
331
	/**
332
	 * @return TButtonType the type of the button. Defaults to TButtonType::Submit.
333
	 */
334
	public function getButtonType()
335
	{
336
		return $this->getViewState('ButtonType',TButtonType::Submit);
337
	}
338
 
339
	/**
340
	 * @param TButtonType the type of the button.
341
	 */
342
	public function setButtonType($value)
343
	{
344
		$this->setViewState('ButtonType',TPropertyValue::ensureEnum($value,'TButtonType'),TButtonType::Submit);
345
	}
346
}
347
 
348
/**
349
 * TButtonType class.
350
 * TButtonType defines the enumerable type for the possible types that a {@link TButton} can take.
351
 *
352
 * The following enumerable values are defined:
353
 * - Submit: a normal submit button
354
 * - Reset: a reset button
355
 * - Button: a client button (normally does not perform form submission)
356
 *
357
 * @author Qiang Xue <qiang.xue@gmail.com>
358
 * @version $Id: TButton.php 2541 2008-10-21 15:05:13Z qiang.xue $
359
 * @package System.Web.UI.WebControls
360
 * @since 3.0.4
361
 */
362
class TButtonType extends TEnumerable
363
{
364
	const Submit='Submit';
365
	const Reset='Reset';
366
	const Button='Button';
367
}
368