| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* TLinkButton class file
|
|
|
4 |
*
|
|
|
5 |
* @author Qiang Xue <qiang.xue@gmail.com>
|
|
|
6 |
* @link http://www.pradosoft.com/
|
|
|
7 |
* @copyright Copyright © 2005-2008 PradoSoft
|
|
|
8 |
* @license http://www.pradosoft.com/license/
|
|
|
9 |
* @version $Id: TLinkButton.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
10 |
* @package System.Web.UI.WebControls
|
|
|
11 |
*/
|
|
|
12 |
|
|
|
13 |
/**
|
|
|
14 |
* TLinkButton class
|
|
|
15 |
*
|
|
|
16 |
* TLinkButton creates a hyperlink style button on the page.
|
|
|
17 |
* TLinkButton has the same appearance as a hyperlink. However, it is mainly
|
|
|
18 |
* used to submit data to a page. Like {@link TButton}, you can create either
|
|
|
19 |
* a <b>submit</b> button or a <b>command</b> button.
|
|
|
20 |
*
|
|
|
21 |
* A <b>command</b> button has a command name (specified by
|
|
|
22 |
* the {@link setCommandName CommandName} property) and and a command parameter
|
|
|
23 |
* (specified by {@link setCommandParameter CommandParameter} property)
|
|
|
24 |
* associated with the button. This allows you to create multiple TLinkButton
|
|
|
25 |
* components on a Web page and programmatically determine which one is clicked
|
|
|
26 |
* with what parameter. You can provide an event handler for
|
|
|
27 |
* {@link onCommand OnCommand} event to programmatically control the actions performed
|
|
|
28 |
* when the command button is clicked. In the event handler, you can determine
|
|
|
29 |
* the {@link setCommandName CommandName} property value and
|
|
|
30 |
* the {@link setCommandParameter CommandParameter} property value
|
|
|
31 |
* through the {@link TCommandParameter::getName Name} and
|
|
|
32 |
* {@link TCommandParameter::getParameter Parameter} properties of the event
|
|
|
33 |
* parameter which is of type {@link TCommandEventParameter}.
|
|
|
34 |
*
|
|
|
35 |
* A <b>submit</b> button does not have a command name associated with the button
|
|
|
36 |
* and clicking on it simply posts the Web page back to the server.
|
|
|
37 |
* By default, a TLinkButton component is a submit button.
|
|
|
38 |
* You can provide an event handler for the {@link onClick OnClick} event
|
|
|
39 |
* to programmatically control the actions performed when the submit button is clicked.
|
|
|
40 |
*
|
|
|
41 |
* Clicking on button can trigger form validation, if
|
|
|
42 |
* {@link setCausesValidation CausesValidation} is true.
|
|
|
43 |
* And the validation may be restricted within a certain group of validator
|
|
|
44 |
* controls by setting {@link setValidationGroup ValidationGroup} property.
|
|
|
45 |
* If validation is successful, the data will be post back to the same page.
|
|
|
46 |
*
|
|
|
47 |
* TLinkButton will display the {@link setText Text} property value
|
|
|
48 |
* as the hyperlink text. If {@link setText Text} is empty, the body content
|
|
|
49 |
* of TLinkButton will be displayed. Therefore, you can use TLinkButton
|
|
|
50 |
* as an image button by enclosing an <img> tag as the body of TLinkButton.
|
|
|
51 |
*
|
|
|
52 |
* @author Qiang Xue <qiang.xue@gmail.com>
|
|
|
53 |
* @version $Id: TLinkButton.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
54 |
* @package System.Web.UI.WebControls
|
|
|
55 |
* @since 3.0
|
|
|
56 |
*/
|
|
|
57 |
class TLinkButton extends TWebControl implements IPostBackEventHandler, IButtonControl, IDataRenderer
|
|
|
58 |
{
|
|
|
59 |
/**
|
|
|
60 |
* @return string tag name of the button
|
|
|
61 |
*/
|
|
|
62 |
protected function getTagName()
|
|
|
63 |
{
|
|
|
64 |
return 'a';
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* @return boolean whether to render javascript.
|
|
|
69 |
*/
|
|
|
70 |
public function getEnableClientScript()
|
|
|
71 |
{
|
|
|
72 |
return $this->getViewState('EnableClientScript',true);
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
/**
|
|
|
76 |
* @param boolean whether to render javascript.
|
|
|
77 |
*/
|
|
|
78 |
public function setEnableClientScript($value)
|
|
|
79 |
{
|
|
|
80 |
$this->setViewState('EnableClientScript',TPropertyValue::ensureBoolean($value),true);
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
/**
|
|
|
84 |
* Adds attribute name-value pairs to renderer.
|
|
|
85 |
* This overrides the parent implementation with additional button specific attributes.
|
|
|
86 |
* @param THtmlWriter the writer used for the rendering purpose
|
|
|
87 |
*/
|
|
|
88 |
protected function addAttributesToRender($writer)
|
|
|
89 |
{
|
|
|
90 |
$page=$this->getPage();
|
|
|
91 |
$page->ensureRenderInForm($this);
|
|
|
92 |
|
|
|
93 |
$writer->addAttribute('id',$this->getClientID());
|
|
|
94 |
|
|
|
95 |
// We call parent implementation here because some attributes
|
|
|
96 |
// may be overwritten in the following
|
|
|
97 |
parent::addAttributesToRender($writer);
|
|
|
98 |
|
|
|
99 |
if($this->getEnabled(true) && $this->getEnableClientScript())
|
|
|
100 |
{
|
|
|
101 |
$this->renderLinkButtonHref($writer);
|
|
|
102 |
$this->renderClientControlScript($writer);
|
|
|
103 |
}
|
|
|
104 |
else if($this->getEnabled()) // in this case, parent will not render 'disabled'
|
|
|
105 |
$writer->addAttribute('disabled','disabled');
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
/**
|
|
|
109 |
* Renders the client-script code.
|
|
|
110 |
* @param THtmlWriter renderer
|
|
|
111 |
*/
|
|
|
112 |
protected function renderClientControlScript($writer)
|
|
|
113 |
{
|
|
|
114 |
$cs = $this->getPage()->getClientScript();
|
|
|
115 |
$cs->registerPostBackControl($this->getClientClassName(),$this->getPostBackOptions());
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
/**
|
|
|
119 |
* @param boolean set by a panel to register this button as the default button for the panel.
|
|
|
120 |
*/
|
|
|
121 |
public function setIsDefaultButton($value)
|
|
|
122 |
{
|
|
|
123 |
$this->setViewState('IsDefaultButton', TPropertyValue::ensureBoolean($value),false);
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
/**
|
|
|
127 |
* @return boolean true if this button is registered as a default button for a panel.
|
|
|
128 |
*/
|
|
|
129 |
public function getIsDefaultButton()
|
|
|
130 |
{
|
|
|
131 |
return $this->getViewState('IsDefaultButton', false);
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
/**
|
|
|
135 |
* Renders the Href for link button.
|
|
|
136 |
* @param THtmlWriter renderer
|
|
|
137 |
*/
|
|
|
138 |
protected function renderLinkButtonHref($writer)
|
|
|
139 |
{
|
|
|
140 |
//create unique no-op url references
|
|
|
141 |
$nop = "javascript:;//".$this->getClientID();
|
|
|
142 |
$writer->addAttribute('href', $nop);
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
/**
|
|
|
146 |
* Gets the name of the javascript class responsible for performing postback for this control.
|
|
|
147 |
* This method overrides the parent implementation.
|
|
|
148 |
* @return string the javascript class name
|
|
|
149 |
*/
|
|
|
150 |
protected function getClientClassName()
|
|
|
151 |
{
|
|
|
152 |
return 'Prado.WebUI.TLinkButton';
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
/**
|
|
|
156 |
* Returns postback specifications for the button.
|
|
|
157 |
* This method is used by framework and control developers.
|
|
|
158 |
* @return array parameters about how the button defines its postback behavior.
|
|
|
159 |
*/
|
|
|
160 |
protected function getPostBackOptions()
|
|
|
161 |
{
|
|
|
162 |
$options['ID'] = $this->getClientID();
|
|
|
163 |
$options['EventTarget'] = $this->getUniqueID();
|
|
|
164 |
$options['CausesValidation'] = $this->getCausesValidation();
|
|
|
165 |
$options['ValidationGroup'] = $this->getValidationGroup();
|
|
|
166 |
$options['StopEvent'] = true;
|
|
|
167 |
|
|
|
168 |
return $options;
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
/**
|
|
|
172 |
* Renders the body content enclosed between the control tag.
|
|
|
173 |
* If {@link getText Text} is not empty, it will be rendered. Otherwise,
|
|
|
174 |
* the body content enclosed in the control tag will be rendered.
|
|
|
175 |
* @param THtmlWriter the writer used for the rendering purpose
|
|
|
176 |
*/
|
|
|
177 |
public function renderContents($writer)
|
|
|
178 |
{
|
|
|
179 |
if(($text=$this->getText())==='')
|
|
|
180 |
parent::renderContents($writer);
|
|
|
181 |
else
|
|
|
182 |
$writer->write($text);
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
/**
|
|
|
186 |
* @return string the text caption of the button
|
|
|
187 |
*/
|
|
|
188 |
public function getText()
|
|
|
189 |
{
|
|
|
190 |
return $this->getViewState('Text','');
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
/**
|
|
|
194 |
* @param string the text caption to be set
|
|
|
195 |
*/
|
|
|
196 |
public function setText($value)
|
|
|
197 |
{
|
|
|
198 |
$this->setViewState('Text',$value,'');
|
|
|
199 |
}
|
|
|
200 |
|
|
|
201 |
/**
|
|
|
202 |
* Returns the caption of the button.
|
|
|
203 |
* This method is required by {@link IDataRenderer}.
|
|
|
204 |
* It is the same as {@link getText()}.
|
|
|
205 |
* @return string caption of the button.
|
|
|
206 |
* @see getText
|
|
|
207 |
* @since 3.1.0
|
|
|
208 |
*/
|
|
|
209 |
public function getData()
|
|
|
210 |
{
|
|
|
211 |
return $this->getText();
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
/**
|
|
|
215 |
* Sets the caption of the button.
|
|
|
216 |
* This method is required by {@link IDataRenderer}.
|
|
|
217 |
* It is the same as {@link setText()}.
|
|
|
218 |
* @param string caption of the button
|
|
|
219 |
* @see setText
|
|
|
220 |
* @since 3.1.0
|
|
|
221 |
*/
|
|
|
222 |
public function setData($value)
|
|
|
223 |
{
|
|
|
224 |
$this->setText($value);
|
|
|
225 |
}
|
|
|
226 |
|
|
|
227 |
/**
|
|
|
228 |
* @return string the command name associated with the {@link onCommand OnCommand} event.
|
|
|
229 |
*/
|
|
|
230 |
public function getCommandName()
|
|
|
231 |
{
|
|
|
232 |
return $this->getViewState('CommandName','');
|
|
|
233 |
}
|
|
|
234 |
|
|
|
235 |
/**
|
|
|
236 |
* @param string the command name associated with the {@link onCommand OnCommand} event.
|
|
|
237 |
*/
|
|
|
238 |
public function setCommandName($value)
|
|
|
239 |
{
|
|
|
240 |
$this->setViewState('CommandName',$value,'');
|
|
|
241 |
}
|
|
|
242 |
|
|
|
243 |
/**
|
|
|
244 |
* @return string the parameter associated with the {@link onCommand OnCommand} event
|
|
|
245 |
*/
|
|
|
246 |
public function getCommandParameter()
|
|
|
247 |
{
|
|
|
248 |
return $this->getViewState('CommandParameter','');
|
|
|
249 |
}
|
|
|
250 |
|
|
|
251 |
/**
|
|
|
252 |
* @param string the parameter associated with the {@link onCommand OnCommand} event.
|
|
|
253 |
*/
|
|
|
254 |
public function setCommandParameter($value)
|
|
|
255 |
{
|
|
|
256 |
$this->setViewState('CommandParameter',$value,'');
|
|
|
257 |
}
|
|
|
258 |
|
|
|
259 |
/**
|
|
|
260 |
* @return boolean whether postback event trigger by this button will cause input validation
|
|
|
261 |
*/
|
|
|
262 |
public function getCausesValidation()
|
|
|
263 |
{
|
|
|
264 |
return $this->getViewState('CausesValidation',true);
|
|
|
265 |
}
|
|
|
266 |
|
|
|
267 |
/**
|
|
|
268 |
* Sets the value indicating whether postback event trigger by this button will cause input validation.
|
|
|
269 |
* @param string the text caption to be set
|
|
|
270 |
*/
|
|
|
271 |
public function setCausesValidation($value)
|
|
|
272 |
{
|
|
|
273 |
$this->setViewState('CausesValidation',TPropertyValue::ensureBoolean($value),true);
|
|
|
274 |
}
|
|
|
275 |
|
|
|
276 |
/**
|
|
|
277 |
* @return string the group of validators which the button causes validation upon postback
|
|
|
278 |
*/
|
|
|
279 |
public function getValidationGroup()
|
|
|
280 |
{
|
|
|
281 |
return $this->getViewState('ValidationGroup','');
|
|
|
282 |
}
|
|
|
283 |
|
|
|
284 |
/**
|
|
|
285 |
* @param string the group of validators which the button causes validation upon postback
|
|
|
286 |
*/
|
|
|
287 |
public function setValidationGroup($value)
|
|
|
288 |
{
|
|
|
289 |
$this->setViewState('ValidationGroup',$value,'');
|
|
|
290 |
}
|
|
|
291 |
|
|
|
292 |
/**
|
|
|
293 |
* Raises the postback event.
|
|
|
294 |
* This method is required by {@link IPostBackEventHandler} interface.
|
|
|
295 |
* If {@link getCausesValidation CausesValidation} is true, it will
|
|
|
296 |
* invoke the page's {@link TPage::validate validate} method first.
|
|
|
297 |
* It will raise {@link onClick OnClick} and {@link onCommand OnCommand} events.
|
|
|
298 |
* This method is mainly used by framework and control developers.
|
|
|
299 |
* @param TEventParameter the event parameter
|
|
|
300 |
*/
|
|
|
301 |
public function raisePostBackEvent($param)
|
|
|
302 |
{
|
|
|
303 |
if($this->getCausesValidation())
|
|
|
304 |
$this->getPage()->validate($this->getValidationGroup());
|
|
|
305 |
$this->onClick(null);
|
|
|
306 |
$this->onCommand(new TCommandEventParameter($this->getCommandName(),$this->getCommandParameter()));
|
|
|
307 |
}
|
|
|
308 |
|
|
|
309 |
/**
|
|
|
310 |
* This method is invoked when the button is clicked.
|
|
|
311 |
* The method raises 'OnClick' event to fire up the event handlers.
|
|
|
312 |
* If you override this method, be sure to call the parent implementation
|
|
|
313 |
* so that the event handler can be invoked.
|
|
|
314 |
* @param TEventParameter event parameter to be passed to the event handlers
|
|
|
315 |
*/
|
|
|
316 |
public function onClick($param)
|
|
|
317 |
{
|
|
|
318 |
$this->raiseEvent('OnClick',$this,$param);
|
|
|
319 |
}
|
|
|
320 |
|
|
|
321 |
/**
|
|
|
322 |
* This method is invoked when the button is clicked.
|
|
|
323 |
* The method raises 'OnCommand' event to fire up the event handlers.
|
|
|
324 |
* If you override this method, be sure to call the parent implementation
|
|
|
325 |
* so that the event handlers can be invoked.
|
|
|
326 |
* @param TCommandEventParameter event parameter to be passed to the event handlers
|
|
|
327 |
*/
|
|
|
328 |
public function onCommand($param)
|
|
|
329 |
{
|
|
|
330 |
$this->raiseEvent('OnCommand',$this,$param);
|
|
|
331 |
$this->raiseBubbleEvent($this,$param);
|
|
|
332 |
}
|
|
|
333 |
}
|
|
|
334 |
|