Completed
Push — master ( c7ab73...cc5395 )
by Fabio
19:04
created

TInPlaceTextBox::getEditTriggerControlID()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * TInPlaceTextBox class file.
4
 *
5
 * @author Wei Zhuo <weizhuo[at]gamil[dot]com>
6
 * @link https://github.com/pradosoft/prado
7
 * @copyright Copyright &copy; 2005-2015 The PRADO Group
8
 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
9
 * @package System.Web.UI.ActiveControls
10
 */
11
12
Prado::using('System.Web.UI.ActiveControls.TActiveTextBox');
13
14
/**
15
 * TInPlaceTextBox Class
16
 *
17
 * TInPlaceTextBox is a component rendered as a label and allows its
18
 * contents to be edited by changing the label to a textbox when
19
 * the label is clicked or when another control or html element with
20
 * ID given by {@link setEditTriggerControlID EditTriggerControlID} is clicked.
21
 *
22
 * If the {@link OnLoadingText} event is handled, a callback request is
23
 * made when the label is clicked, while the request is being made the
24
 * textbox is disabled from editing. The {@link OnLoadingText} event allows
25
 * you to update the content of the textbox before the client is allowed
26
 * to edit the content. After the callback request returns successfully,
27
 * the textbox is enabled and the contents is then allowed to be edited.
28
 *
29
 * Once the textbox loses focus, if {@link setAutoPostBack AutoPostBack}
30
 * is true and the textbox content has changed, a callback request is made and
31
 * the {@link OnTextChanged} event is raised like that of the TActiveTextBox.
32
 * During the request, the textbox is disabled.
33
 *
34
 * After the callback request returns sucessfully, the textbox is enabled.
35
 * If the {@link setAutoHideTextBox AutoHideTextBox} property is true, then
36
 * the textbox will be hidden and the label is then shown.
37
 *
38
 * Since 3.1.2, you can set the {@link setReadOnly ReadOnly} property to make
39
 * the control not editable. This property can be also changed on callback
40
 *
41
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
42
 * @package System.Web.UI.ActiveControls
43
 * @since 3.1
44
 */
45
class TInPlaceTextBox extends TActiveTextBox
46
{
47
	/**
48
	 * Sets the auto post back to true by default.
49
	 */
50
	public function __construct()
51
	{
52
		parent::__construct();
53
		$this->setAutoPostBack(true);
54
	}
55
56
	/**
57
	 * @param boolean true to hide the textbox after losing focus.
58
	 */
59
	public function setAutoHideTextBox($value)
60
	{
61
		$this->setViewState('AutoHide', TPropertyValue::ensureBoolean($value), true);
62
	}
63
64
	/**
65
	 * @return boolean true will hide the textbox after losing focus.
66
	 */
67
	public function getAutoHideTextBox()
68
	{
69
		return $this->getViewState('AutoHide', true);
70
	}
71
72
	/**
73
	 * @param boolean true to display the edit textbox
74
	 */
75
	public function setDisplayTextBox($value)
76
	{
77
		$value = TPropertyValue::ensureBoolean($value);
78
		$this->setViewState('DisplayTextBox', $value,false);
79
		if($this->getActiveControl()->canUpdateClientSide())
80
			$this->callClientFunction('setDisplayTextBox',$value);
81
	}
82
83
	/**
84
	 * @return boolean true to display the edit textbox
85
	 */
86
	public function getDisplayTextBox()
87
	{
88
		return $this->getViewState('DisplayTextBox', false);
89
	}
90
91
	/**
92
	 * Calls the client-side static method for this control class.
93
	 * @param string static method name
94
	 * @param mixed method parmaeter
95
	 */
96
	protected function callClientFunction($func,$value)
97
	{
98
		$client = $this->getPage()->getCallbackClient();
99
		$code = $this->getClientClassName().'.'.$func;
100
		$client->callClientFunction($code,array($this,$value));
101
	}
102
103
	/**
104
	 * @param string ID of the control that can trigger to edit the textbox
105
	 */
106
	public function setEditTriggerControlID($value)
107
	{
108
		$this->setViewState('EditTriggerControlID', $value);
109
	}
110
111
	/**
112
	 * @return string ID of the control that can trigger to edit the textbox
113
	 */
114
	public function getEditTriggerControlID()
115
	{
116
		return $this->getViewState('EditTriggerControlID');
117
	}
118
119
	/**
120
	 * @return string edit trigger control client ID.
121
	 */
122
	protected function getExternalControlID()
123
	{
124
		$extID = $this->getEditTriggerControlID();
125
		if($extID===null) return '';
126
		if(($control = $this->findControl($extID))!==null)
127
			return $control->getClientID();
128
		return $extID;
129
	}
130
131
	/**
132
	 * On callback response, the inner HTMl of the label and the
133
	 * value of the textbox is updated
134
	 * @param string the text value of the label
135
	 */
136
	public function setText($value)
137
	{
138
		if(TTextBox::getText() === $value)
139
			return;
140
141
		TTextBox::setText($value);
142
		if($this->getActiveControl()->canUpdateClientSide())
143
		{
144
			$client = $this->getPage()->getCallbackClient();
145
			$client->update($this->getLabelClientID(), $value);
146
			$client->setValue($this, $value);
147
		}
148
	}
149
150
	/**
151
	 * Update ClientSide Readonly property
152
	 * @param boolean value
153
	 * @since 3.1.2
154
	 */
155
	public function setReadOnly ($value)
156
	{
157
		$value=TPropertyValue::ensureBoolean($value);
158
		if(TTextBox::getReadOnly() === $value)
159
			return;
160
161
		TTextBox::setReadOnly($value);
162
		if ($this->getActiveControl()->canUpdateClientSide())
163
		{
164
			$this->callClientFunction('setReadOnly', $value);
165
		}
166
	}
167
168
	/**
169
	 * @return string tag name of the label.
170
	 */
171
	protected function getTagName()
172
	{
173
		return 'span';
174
	}
175
176
	/**
177
	 * Renders the body content of the label.
178
	 * @param THtmlWriter the writer for rendering
179
	 */
180
	public function renderContents($writer)
181
	{
182
		if(($text=$this->getText())==='')
183
			parent::renderContents($writer);
184
		else
185
			$writer->write($text);
186
	}
187
188
	/**
189
	 * @return string label client ID
190
	 */
191
	protected function getLabelClientID()
192
	{
193
		return $this->getClientID().'__label';
194
	}
195
196
	/**
197
	 * This method is invoked when a callback is requested. The method raises
198
	 * 'OnCallback' event to fire up the event handlers. If you override this
199
	 * method, be sure to call the parent implementation so that the event
200
	 * handler can be invoked.
201
	 * @param TCallbackEventParameter event parameter to be passed to the event handlers
202
	 */
203
	public function onCallback($param)
204
	{
205
		$action = $param->getCallbackParameter();
206
		if(is_array($action) && $action[0] === '__InlineEditor_loadExternalText__')
207
		{
208
			$parameter = new TCallbackEventParameter($this->getResponse(), $action[1]);
209
			$this->onLoadingText($parameter);
210
		}
211
		$this->raiseEvent('OnCallback', $this, $param);
212
	}
213
214
	/**
215
	 * @return array callback options.
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,string|boolean|integer>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
216
	 */
217
	protected function getPostBackOptions()
218
	{
219
		$options = parent::getPostBackOptions();
220
		$options['ID'] = $this->getLabelClientID();
221
		$options['TextBoxID'] = $this->getClientID();
222
		$options['ExternalControl'] = $this->getExternalControlID();
223
		$options['AutoHide'] = $this->getAutoHideTextBox() == false ? '' : true;
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
224
		$options['AutoPostBack'] = $this->getAutoPostBack() == false ? '' : true;
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
225
		$options['Columns'] = $this->getColumns();
226
		if($this->getTextMode()==='MultiLine')
227
		{
228
			$options['Rows'] = $this->getRows();
229
			$options['Wrap'] = $this->getWrap()== false ? '' : true;
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
230
		}
231
		else
232
		{
233
			$length = $this->getMaxLength();
234
			$options['MaxLength'] = $length > 0 ? $length : '';
235
		}
236
237
		if($this->hasEventHandler('OnLoadingText'))
238
			$options['LoadTextOnEdit'] = true;
239
240
		$options['ReadOnly']=$this->getReadOnly();
241
		return $options;
242
	}
243
244
	/**
245
	 * Raised when editing the content is requsted to be loaded from the
246
	 * server side.
247
	 * @param TCallbackEventParameter event parameter to be passed to the event handlers
248
	 */
249
	public function onLoadingText($param)
250
	{
251
		$this->raiseEvent('OnLoadingText',$this,$param);
252
	}
253
254
	/**
255
	 * @return string corresponding javascript class name for this TInPlaceTextBox
256
	 */
257
	protected function getClientClassName()
258
	{
259
		return 'Prado.WebUI.TInPlaceTextBox';
260
	}
261
262
	/**
263
	 * Ensure that the ID attribute is rendered and registers the javascript code
264
	 * for initializing the active control.
265
	 */
266
	protected function addAttributesToRender($writer)
267
	{
268
		//calls the TWebControl to avoid rendering other attribute normally render for a textbox.
269
		TWebControl::addAttributesToRender($writer);
270
		$writer->addAttribute('id',$this->getLabelClientID());
271
		$this->getActiveControl()->registerCallbackClientScript(
272
			$this->getClientClassName(), $this->getPostBackOptions());
273
	}
274
275
	/**
276
	 * Registers CSS and JS.
277
	 * This method is invoked right before the control rendering, if the control is visible.
278
	 * @param mixed event parameter
279
	 */
280
	public function onPreRender($param)
281
	{
282
		parent::onPreRender($param);
283
		$this->registerClientScript();
284
	}
285
286
	/**
287
	 * Registers the relevant JavaScript.
288
	 */
289
	protected function registerClientScript()
290
	{
291
		$cs=$this->getPage()->getClientScript();
292
		$cs->registerPradoScript('inlineeditor');
293
	}
294
}
295