Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * TImageButton 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: TImageButton.php 2482 2008-07-30 02:07:13Z knut $
10
 * @package System.Web.UI.WebControls
11
 */
12
 
13
/**
14
 * Includes TImage class file
15
 */
16
Prado::using('System.Web.UI.WebControls.TImage');
17
 
18
/**
19
 * TImageButton class
20
 *
21
 * TImageButton creates an image button on the page. It is used to submit data to a page.
22
 * You can create either a <b>submit</b> button or a <b>command</b> button.
23
 *
24
 * A <b>command</b> button has a command name (specified by
25
 * the {@link setCommandName CommandName} property) and and a command parameter
26
 * (specified by {@link setCommandParameter CommandParameter} property)
27
 * associated with the button. This allows you to create multiple TLinkButton
28
 * components on a Web page and programmatically determine which one is clicked
29
 * with what parameter. You can provide an event handler for
30
 * {@link onCommand OnCommand} event to programmatically control the actions performed
31
 * when the command button is clicked. In the event handler, you can determine
32
 * the {@link setCommandName CommandName} property value and
33
 * the {@link setCommandParameter CommandParameter} property value
34
 * through the {@link TCommandParameter::getName Name} and
35
 * {@link TCommandParameter::getParameter Parameter} properties of the event
36
 * parameter which is of type {@link TCommandEventParameter}.
37
 *
38
 * A <b>submit</b> button does not have a command name associated with the button
39
 * and clicking on it simply posts the Web page back to the server.
40
 * By default, a TImageButton control is a submit button.
41
 * You can provide an event handler for the {@link onClick OnClick} event
42
 * to programmatically control the actions performed when the submit button is clicked.
43
 * The coordinates of the clicking point can be obtained from the {@link onClick OnClick}
44
 * event parameter, which is of type {@link TImageClickEventParameter}.
45
 *
46
 * Clicking on button can trigger form validation, if
47
 * {@link setCausesValidation CausesValidation} is true.
48
 * And the validation may be restricted within a certain group of validator
49
 * controls by setting {@link setValidationGroup ValidationGroup} property.
50
 * If validation is successful, the data will be post back to the same page.
51
 *
52
 * TImageButton displays the {@link setText Text} property as the hint text to the displayed image.
53
 *
54
 * @author Qiang Xue <qiang.xue@gmail.com>
55
 * @version $Id: TImageButton.php 2482 2008-07-30 02:07:13Z knut $
56
 * @package System.Web.UI.WebControls
57
 * @since 3.0
58
 */
59
class TImageButton extends TImage implements IPostBackDataHandler, IPostBackEventHandler, IButtonControl
60
{
61
	/**
62
	 * @var integer x coordinate that the image is being clicked at
63
	 */
64
	private $_x=0;
65
	/**
66
	 * @var integer y coordinate that the image is being clicked at
67
	 */
68
	private $_y=0;
69
	private $_dataChanged=false;
70
 
71
	/**
72
	 * @return string tag name of the button
73
	 */
74
	protected function getTagName()
75
	{
76
		return 'input';
77
	}
78
 
79
	/**
80
	 * @return boolean whether to render javascript.
81
	 */
82
	public function getEnableClientScript()
83
	{
84
		return $this->getViewState('EnableClientScript',true);
85
	}
86
 
87
	/**
88
	 * @param boolean whether to render javascript.
89
	 */
90
	public function setEnableClientScript($value)
91
	{
92
		$this->setViewState('EnableClientScript',TPropertyValue::ensureBoolean($value),true);
93
	}
94
 
95
	/**
96
	 * Adds attribute name-value pairs to renderer.
97
	 * This overrides the parent implementation with additional button specific attributes.
98
	 * @param THtmlWriter the writer used for the rendering purpose
99
	 */
100
	protected function addAttributesToRender($writer)
101
	{
102
		$page=$this->getPage();
103
		$page->ensureRenderInForm($this);
104
		$writer->addAttribute('type','image');
105
		if(($uniqueID=$this->getUniqueID())!=='')
106
			$writer->addAttribute('name',$uniqueID);
107
		if($this->getEnabled(true))
108
		{
109
			if($this->getEnableClientScript() && $this->needPostBackScript())
110
				$this->renderClientControlScript($writer);
111
		}
112
		else if($this->getEnabled()) // in this case, parent will not render 'disabled'
113
			$writer->addAttribute('disabled','disabled');
114
		parent::addAttributesToRender($writer);
115
	}
116
 
117
	/**
118
	 * Renders the client-script code.
119
	 */
120
	protected function renderClientControlScript($writer)
121
	{
122
		$writer->addAttribute('id',$this->getClientID());
123
		$cs = $this->getPage()->getClientScript();
124
		$cs->registerPostBackControl($this->getClientClassName(),$this->getPostBackOptions());
125
	}
126
 
127
	/**
128
	 * Gets the name of the javascript class responsible for performing postback for this control.
129
	 * This method overrides the parent implementation.
130
	 * @return string the javascript class name
131
	 */
132
	protected function getClientClassName()
133
	{
134
		return 'Prado.WebUI.TImageButton';
135
	}
136
 
137
	/**
138
	 * @return boolean whether to perform validation if the button is clicked
139
	 */
140
	protected function canCauseValidation()
141
	{
142
		if($this->getCausesValidation())
143
		{
144
			$group=$this->getValidationGroup();
145
			return $this->getPage()->getValidators($group)->getCount()>0;
146
		}
147
		else
148
			return false;
149
	}
150
 
151
	/**
152
	 * @param boolean set by a panel to register this button as the default button for the panel.
153
	 */
154
	public function setIsDefaultButton($value)
155
	{
156
		$this->setViewState('IsDefaultButton', TPropertyValue::ensureBoolean($value),false);
157
	}
158
 
159
	/**
160
	 * @return boolean true if this button is registered as a default button for a panel.
161
	 */
162
	public function getIsDefaultButton()
163
	{
164
		return $this->getViewState('IsDefaultButton', false);
165
	}
166
 
167
	/**
168
	 * @return boolean whether the button needs javascript to do postback
169
	 */
170
	protected function needPostBackScript()
171
	{
172
		return $this->canCauseValidation() || $this->getIsDefaultButton();
173
	}
174
 
175
	/**
176
	 * Returns postback specifications for the button.
177
	 * This method is used by framework and control developers.
178
	 * @return array parameters about how the button defines its postback behavior.
179
	 */
180
	protected function getPostBackOptions()
181
	{
182
		$options['ID'] = $this->getClientID();
183
		$options['CausesValidation'] = $this->getCausesValidation();
184
		$options['ValidationGroup'] = $this->getValidationGroup();
185
		$options['EventTarget'] = $this->getUniqueID();
186
 
187
		return $options;
188
	}
189
 
190
	/**
191
	 * This method checks if the TImageButton is clicked and loads the coordinates of the clicking position.
192
	 * This method is primarly used by framework developers.
193
	 * @param string the key that can be used to retrieve data from the input data collection
194
	 * @param array the input data collection
195
	 * @return boolean whether the data of the component has been changed
196
	 */
197
	public function loadPostData($key,$values)
198
	{
199
		$uid=$this->getUniqueID();
200
		if(isset($values["{$uid}_x"]) && isset($values["{$uid}_y"]))
201
		{
202
			$this->_x=intval($values["{$uid}_x"]);
203
			$this->_y=intval($values["{$uid}_y"]);
204
			if($this->getPage()->getPostBackEventTarget()===null)
205
				$this->getPage()->setPostBackEventTarget($this);
206
			$this->_dataChanged=true;
207
		}
208
		return false;
209
	}
210
 
211
	/**
212
	 * A dummy implementation for the IPostBackDataHandler interface.
213
	 */
214
	public function raisePostDataChangedEvent()
215
	{
216
		// no post data to handle
217
	}
218
 
219
	/**
220
	 * This method is invoked when the button is clicked.
221
	 * The method raises 'OnClick' event to fire up the event handlers.
222
	 * If you override this method, be sure to call the parent implementation
223
	 * so that the event handler can be invoked.
224
	 * @param TImageClickEventParameter event parameter to be passed to the event handlers
225
	 */
226
	public function onClick($param)
227
	{
228
		$this->raiseEvent('OnClick',$this,$param);
229
	}
230
 
231
	/**
232
	 * This method is invoked when the button is clicked.
233
	 * The method raises 'OnCommand' event to fire up the event handlers.
234
	 * If you override this method, be sure to call the parent implementation
235
	 * so that the event handlers can be invoked.
236
	 * @param TCommandEventParameter event parameter to be passed to the event handlers
237
	 */
238
	public function onCommand($param)
239
	{
240
		$this->raiseEvent('OnCommand',$this,$param);
241
		$this->raiseBubbleEvent($this,$param);
242
	}
243
 
244
	/**
245
	 * Raises the postback event.
246
	 * This method is required by {@link IPostBackEventHandler} interface.
247
	 * If {@link getCausesValidation CausesValidation} is true, it will
248
	 * invoke the page's {@link TPage::validate validate} method first.
249
	 * It will raise {@link onClick OnClick} and {@link onCommand OnCommand} events.
250
	 * This method is mainly used by framework and control developers.
251
	 * @param TEventParameter the event parameter
252
	 */
253
	public function raisePostBackEvent($param)
254
	{
255
		if($this->getCausesValidation())
256
			$this->getPage()->validate($this->getValidationGroup());
257
		$this->onClick(new TImageClickEventParameter($this->_x,$this->_y));
258
		$this->onCommand(new TCommandEventParameter($this->getCommandName(),$this->getCommandParameter()));
259
	}
260
 
261
	/**
262
	 * Returns a value indicating whether postback has caused the control data change.
263
	 * This method is required by the IPostBackDataHandler interface.
264
	 * @return boolean whether postback has caused the control data change. False if the page is not in postback mode.
265
	 */
266
	public function getDataChanged()
267
	{
268
		return $this->_dataChanged;
269
	}
270
 
271
	/**
272
	 * @return boolean whether postback event trigger by this button will cause input validation, default is true
273
	 */
274
	public function getCausesValidation()
275
	{
276
		return $this->getViewState('CausesValidation',true);
277
	}
278
 
279
	/**
280
	 * @param boolean whether postback event trigger by this button will cause input validation
281
	 */
282
	public function setCausesValidation($value)
283
	{
284
		$this->setViewState('CausesValidation',TPropertyValue::ensureBoolean($value),true);
285
	}
286
 
287
	/**
288
	 * @return string the command name associated with the {@link onCommand OnCommand} event.
289
	 */
290
	public function getCommandName()
291
	{
292
		return $this->getViewState('CommandName','');
293
	}
294
 
295
	/**
296
	 * @param string the command name associated with the {@link onCommand OnCommand} event.
297
	 */
298
	public function setCommandName($value)
299
	{
300
		$this->setViewState('CommandName',$value,'');
301
	}
302
 
303
	/**
304
	 * @return string the parameter associated with the {@link onCommand OnCommand} event
305
	 */
306
	public function getCommandParameter()
307
	{
308
		return $this->getViewState('CommandParameter','');
309
	}
310
 
311
	/**
312
	 * @param string the parameter associated with the {@link onCommand OnCommand} event.
313
	 */
314
	public function setCommandParameter($value)
315
	{
316
		$this->setViewState('CommandParameter',$value,'');
317
	}
318
 
319
	/**
320
	 * @return string the group of validators which the button causes validation upon postback
321
	 */
322
	public function getValidationGroup()
323
	{
324
		return $this->getViewState('ValidationGroup','');
325
	}
326
 
327
	/**
328
	 * @param string the group of validators which the button causes validation upon postback
329
	 */
330
	public function setValidationGroup($value)
331
	{
332
		$this->setViewState('ValidationGroup',$value,'');
333
	}
334
 
335
	/**
336
	 * @return string caption of the button
337
	 */
338
	public function getText()
339
	{
340
		return $this->getAlternateText();
341
	}
342
 
343
	/**
344
	 * @param string caption of the button
345
	 */
346
	public function setText($value)
347
	{
348
		$this->setAlternateText($value);
349
	}
350
 
351
	/**
352
	 * Registers the image button to receive postback data during postback.
353
	 * This is necessary because an image button, when postback, does not have
354
	 * direct mapping between post data and the image button name.
355
	 * This method overrides the parent implementation and is invoked before render.
356
	 * @param mixed event parameter
357
	 */
358
	public function onPreRender($param)
359
	{
360
		parent::onPreRender($param);
361
		$this->getPage()->registerRequiresPostData($this);
362
	}
363
 
364
	/**
365
	 * Renders the body content enclosed between the control tag.
366
	 * This overrides the parent implementation with nothing to be rendered.
367
	 * @param THtmlWriter the writer used for the rendering purpose
368
	 */
369
	public function renderContents($writer)
370
	{
371
	}
372
}
373
 
374
/**
375
 * TImageClickEventParameter class
376
 *
377
 * TImageClickEventParameter encapsulates the parameter data for
378
 * {@link TImageButton::onClick Click} event of {@link TImageButton} controls.
379
 *
380
 * @author Qiang Xue <qiang.xue@gmail.com>
381
 * @version $Id: TImageButton.php 2482 2008-07-30 02:07:13Z knut $
382
 * @package System.Web.UI.WebControls
383
 * @since 3.0
384
 */
385
class TImageClickEventParameter extends TEventParameter
386
{
387
	/**
388
	 * the X coordinate of the clicking point
389
	 * @var integer
390
	 */
391
	private $_x=0;
392
	/**
393
	 * the Y coordinate of the clicking point
394
	 * @var integer
395
	 */
396
	private $_y=0;
397
 
398
	/**
399
	 * Constructor.
400
	 * @param integer X coordinate of the clicking point
401
	 * @param integer Y coordinate of the clicking point
402
	 */
403
	public function __construct($x,$y)
404
	{
405
		$this->_x=$x;
406
		$this->_y=$y;
407
	}
408
 
409
	/**
410
	 * @return integer X coordinate of the clicking point, defaults to 0
411
	 */
412
	public function getX()
413
	{
414
		return $this->_x;
415
	}
416
 
417
	/**
418
	 * @param integer X coordinate of the clicking point
419
	 */
420
	public function setX($value)
421
	{
422
		$this->_x=TPropertyValue::ensureInteger($value);
423
	}
424
 
425
	/**
426
	 * @return integer Y coordinate of the clicking point, defaults to 0
427
	 */
428
	public function getY()
429
	{
430
		return $this->_y;
431
	}
432
 
433
	/**
434
	 * @param integer Y coordinate of the clicking point
435
	 */
436
	public function setY($value)
437
	{
438
		$this->_y=TPropertyValue::ensureInteger($value);
439
	}
440
}
441
 
442
?>