3 * TImageButton 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 * Includes TImage class file
15 Prado::using('System.Web.UI.WebControls.TImage');
20 * TImageButton creates an image button on the page. It is used to submit data to a page.
21 * You can create either a <b>submit</b> button or a <b>command</b> button.
23 * A <b>command</b> button has a command name (specified by
24 * the {@link setCommandName CommandName} property) and and a command parameter
25 * (specified by {@link setCommandParameter CommandParameter} property)
26 * associated with the button. This allows you to create multiple TLinkButton
27 * components on a Web page and programmatically determine which one is clicked
28 * with what parameter. You can provide an event handler for
29 * {@link onCommand OnCommand} event to programmatically control the actions performed
30 * when the command button is clicked. In the event handler, you can determine
31 * the {@link setCommandName CommandName} property value and
32 * the {@link setCommandParameter CommandParameter} property value
33 * through the {@link TCommandParameter::getName Name} and
34 * {@link TCommandParameter::getParameter Parameter} properties of the event
35 * parameter which is of type {@link TCommandEventParameter}.
37 * A <b>submit</b> button does not have a command name associated with the button
38 * and clicking on it simply posts the Web page back to the server.
39 * By default, a TImageButton control is a submit button.
40 * You can provide an event handler for the {@link onClick OnClick} event
41 * to programmatically control the actions performed when the submit button is clicked.
42 * The coordinates of the clicking point can be obtained from the {@link onClick OnClick}
43 * event parameter, which is of type {@link TImageClickEventParameter}.
45 * Clicking on button can trigger form validation, if
46 * {@link setCausesValidation CausesValidation} is true.
47 * And the validation may be restricted within a certain group of validator
48 * controls by setting {@link setValidationGroup ValidationGroup} property.
49 * If validation is successful, the data will be post back to the same page.
51 * TImageButton displays the {@link setText Text} property as the hint text to the displayed image.
53 * @author Qiang Xue <qiang.xue@gmail.com>
54 * @package System.Web.UI.WebControls
57 class TImageButton extends TImage implements IPostBackDataHandler, IPostBackEventHandler, IButtonControl
60 * @var integer x coordinate that the image is being clicked at
64 * @var integer y coordinate that the image is being clicked at
67 private $_dataChanged=false;
70 * @return string tag name of the button
72 protected function getTagName()
78 * @return boolean whether to render javascript.
80 public function getEnableClientScript()
82 return $this->getViewState('EnableClientScript',true);
86 * @param boolean whether to render javascript.
88 public function setEnableClientScript($value)
90 $this->setViewState('EnableClientScript',TPropertyValue::ensureBoolean($value),true);
94 * Adds attribute name-value pairs to renderer.
95 * This overrides the parent implementation with additional button specific attributes.
96 * @param THtmlWriter the writer used for the rendering purpose
98 protected function addAttributesToRender($writer)
100 $page=$this->getPage();
101 $page->ensureRenderInForm($this);
102 $writer->addAttribute('type','image');
103 if(($uniqueID=$this->getUniqueID())!=='')
104 $writer->addAttribute('name',$uniqueID);
105 if($this->getEnabled(true))
107 if($this->getEnableClientScript() && $this->needPostBackScript())
108 $this->renderClientControlScript($writer);
110 else if($this->getEnabled()) // in this case, parent will not render 'disabled'
111 $writer->addAttribute('disabled','disabled');
112 parent::addAttributesToRender($writer);
116 * Renders the client-script code.
118 protected function renderClientControlScript($writer)
120 $writer->addAttribute('id',$this->getClientID());
121 $cs = $this->getPage()->getClientScript();
122 $cs->registerPostBackControl($this->getClientClassName(),$this->getPostBackOptions());
126 * Gets the name of the javascript class responsible for performing postback for this control.
127 * This method overrides the parent implementation.
128 * @return string the javascript class name
130 protected function getClientClassName()
132 return 'Prado.WebUI.TImageButton';
136 * @return boolean whether to perform validation if the button is clicked
138 protected function canCauseValidation()
140 if($this->getCausesValidation())
142 $group=$this->getValidationGroup();
143 return $this->getPage()->getValidators($group)->getCount()>0;
150 * @param boolean set by a panel to register this button as the default button for the panel.
152 public function setIsDefaultButton($value)
154 $this->setViewState('IsDefaultButton', TPropertyValue::ensureBoolean($value),false);
158 * @return boolean true if this button is registered as a default button for a panel.
160 public function getIsDefaultButton()
162 return $this->getViewState('IsDefaultButton', false);
166 * @return boolean whether the button needs javascript to do postback
168 protected function needPostBackScript()
170 return $this->canCauseValidation() || $this->getIsDefaultButton();
174 * Returns postback specifications for the button.
175 * This method is used by framework and control developers.
176 * @return array parameters about how the button defines its postback behavior.
178 protected function getPostBackOptions()
180 $options['ID'] = $this->getClientID();
181 $options['CausesValidation'] = $this->getCausesValidation();
182 $options['ValidationGroup'] = $this->getValidationGroup();
183 $options['EventTarget'] = $this->getUniqueID();
189 * This method checks if the TImageButton is clicked and loads the coordinates of the clicking position.
190 * This method is primarly used by framework developers.
191 * @param string the key that can be used to retrieve data from the input data collection
192 * @param array the input data collection
193 * @return boolean whether the data of the component has been changed
195 public function loadPostData($key,$values)
197 $uid=$this->getUniqueID();
198 if(isset($values["{$uid}_x"]) && isset($values["{$uid}_y"]))
200 $this->_x=intval($values["{$uid}_x"]);
201 $this->_y=intval($values["{$uid}_y"]);
202 if($this->getPage()->getPostBackEventTarget()===null)
203 $this->getPage()->setPostBackEventTarget($this);
204 $this->_dataChanged=true;
210 * A dummy implementation for the IPostBackDataHandler interface.
212 public function raisePostDataChangedEvent()
214 // no post data to handle
218 * This method is invoked when the button is clicked.
219 * The method raises 'OnClick' event to fire up the event handlers.
220 * If you override this method, be sure to call the parent implementation
221 * so that the event handler can be invoked.
222 * @param TImageClickEventParameter event parameter to be passed to the event handlers
224 public function onClick($param)
226 $this->raiseEvent('OnClick',$this,$param);
230 * This method is invoked when the button is clicked.
231 * The method raises 'OnCommand' event to fire up the event handlers.
232 * If you override this method, be sure to call the parent implementation
233 * so that the event handlers can be invoked.
234 * @param TCommandEventParameter event parameter to be passed to the event handlers
236 public function onCommand($param)
238 $this->raiseEvent('OnCommand',$this,$param);
239 $this->raiseBubbleEvent($this,$param);
243 * Raises the postback event.
244 * This method is required by {@link IPostBackEventHandler} interface.
245 * If {@link getCausesValidation CausesValidation} is true, it will
246 * invoke the page's {@link TPage::validate validate} method first.
247 * It will raise {@link onClick OnClick} and {@link onCommand OnCommand} events.
248 * This method is mainly used by framework and control developers.
249 * @param TEventParameter the event parameter
251 public function raisePostBackEvent($param)
253 if($this->getCausesValidation())
254 $this->getPage()->validate($this->getValidationGroup());
255 $this->onClick(new TImageClickEventParameter($this->_x,$this->_y));
256 $this->onCommand(new TCommandEventParameter($this->getCommandName(),$this->getCommandParameter()));
260 * Returns a value indicating whether postback has caused the control data change.
261 * This method is required by the IPostBackDataHandler interface.
262 * @return boolean whether postback has caused the control data change. False if the page is not in postback mode.
264 public function getDataChanged()
266 return $this->_dataChanged;
270 * @return boolean whether postback event trigger by this button will cause input validation, default is true
272 public function getCausesValidation()
274 return $this->getViewState('CausesValidation',true);
278 * @param boolean whether postback event trigger by this button will cause input validation
280 public function setCausesValidation($value)
282 $this->setViewState('CausesValidation',TPropertyValue::ensureBoolean($value),true);
286 * @return string the command name associated with the {@link onCommand OnCommand} event.
288 public function getCommandName()
290 return $this->getViewState('CommandName','');
294 * @param string the command name associated with the {@link onCommand OnCommand} event.
296 public function setCommandName($value)
298 $this->setViewState('CommandName',$value,'');
302 * @return string the parameter associated with the {@link onCommand OnCommand} event
304 public function getCommandParameter()
306 return $this->getViewState('CommandParameter','');
310 * @param string the parameter associated with the {@link onCommand OnCommand} event.
312 public function setCommandParameter($value)
314 $this->setViewState('CommandParameter',$value,'');
318 * @return string the group of validators which the button causes validation upon postback
320 public function getValidationGroup()
322 return $this->getViewState('ValidationGroup','');
326 * @param string the group of validators which the button causes validation upon postback
328 public function setValidationGroup($value)
330 $this->setViewState('ValidationGroup',$value,'');
334 * @return string caption of the button
336 public function getText()
338 return $this->getAlternateText();
342 * @param string caption of the button
344 public function setText($value)
346 $this->setAlternateText($value);
350 * Registers the image button to receive postback data during postback.
351 * This is necessary because an image button, when postback, does not have
352 * direct mapping between post data and the image button name.
353 * This method overrides the parent implementation and is invoked before render.
354 * @param mixed event parameter
356 public function onPreRender($param)
358 parent::onPreRender($param);
359 $this->getPage()->registerRequiresPostData($this);
363 * Renders the body content enclosed between the control tag.
364 * This overrides the parent implementation with nothing to be rendered.
365 * @param THtmlWriter the writer used for the rendering purpose
367 public function renderContents($writer)
373 * TImageClickEventParameter class
375 * TImageClickEventParameter encapsulates the parameter data for
376 * {@link TImageButton::onClick Click} event of {@link TImageButton} controls.
378 * @author Qiang Xue <qiang.xue@gmail.com>
379 * @package System.Web.UI.WebControls
382 class TImageClickEventParameter extends TEventParameter
385 * the X coordinate of the clicking point
390 * the Y coordinate of the clicking point
397 * @param integer X coordinate of the clicking point
398 * @param integer Y coordinate of the clicking point
400 public function __construct($x,$y)
407 * @return integer X coordinate of the clicking point, defaults to 0
409 public function getX()
415 * @param integer X coordinate of the clicking point
417 public function setX($value)
419 $this->_x=TPropertyValue::ensureInteger($value);
423 * @return integer Y coordinate of the clicking point, defaults to 0
425 public function getY()
431 * @param integer Y coordinate of the clicking point
433 public function setY($value)
435 $this->_y=TPropertyValue::ensureInteger($value);