Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * TTextBox 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: TTextBox.php 2541 2008-10-21 15:05:13Z qiang.xue $
10
 * @package System.Web.UI.WebControls
11
 */
12
 
13
/**
14
 * TTextBox class
15
 *
16
 * TTextBox displays a text box on the Web page for user input.
17
 * The text displayed in the TTextBox control is determined by the
18
 * {@link setText Text} property. You can create a <b>SingleLine</b>,
19
 * a <b>MultiLine</b>, or a <b>Password</b> text box by setting
20
 * the {@link setTextMode TextMode} property. If the TTextBox control
21
 * is a multiline text box, the number of rows it displays is determined
22
 * by the {@link setRows Rows} property, and the {@link setWrap Wrap} property
23
 * can be used to determine whether to wrap the text in the component.
24
 *
25
 * To specify the display width of the text box, in characters, set
26
 * the {@link setColumns Columns} property. To prevent the text displayed
27
 * in the component from being modified, set the {@link setReadOnly ReadOnly}
28
 * property to true. If you want to limit the user input to a specified number
29
 * of characters, set the {@link setMaxLength MaxLength} property.
30
 * To use AutoComplete feature, set the {@link setAutoCompleteType AutoCompleteType} property.
31
 *
32
 * If {@link setAutoPostBack AutoPostBack} is set true, updating the text box
33
 * and then changing the focus out of it will cause postback action.
34
 * And if {@link setCausesValidation CausesValidation} is true, validation will
35
 * also be processed, which can be further restricted within
36
 * a {@link setValidationGroup ValidationGroup}.
37
 *
38
 * WARNING: Be careful if you want to display the text collected via TTextBox.
39
 * Malicious cross-site script may be injected in. You may use {@link getSafeText SafeText}
40
 * to prevent this problem.
41
 *
42
 * NOTE: If you set {@link setWrap Wrap} to false or use {@link setAutoCompleteType AutoCompleteType},
43
 * the generated HTML output for the textbox will not be XHTML-compatible.
44
 * Currently, no alternatives are available.
45
 *
46
 * @author Qiang Xue <qiang.xue@gmail.com>
47
 * @version $Id: TTextBox.php 2541 2008-10-21 15:05:13Z qiang.xue $
48
 * @package System.Web.UI.WebControls
49
 * @since 3.0
50
 */
51
class TTextBox extends TWebControl implements IPostBackDataHandler, IValidatable, IDataRenderer
52
{
53
	/**
54
	 * Default number of rows (for MultiLine text box)
55
	 */
56
	const DEFAULT_ROWS=4;
57
	/**
58
	 * Default number of columns (for MultiLine text box)
59
	 */
60
	const DEFAULT_COLUMNS=20;
61
	/**
62
	 * @var mixed safe text parser
63
	 */
64
	private static $_safeTextParser=null;
65
	/**
66
	 * @var string safe textbox content with javascript stripped off
67
	 */
68
	private $_safeText;
69
	private $_dataChanged=false;
70
	private $_isValid=true;
71
 
72
	/**
73
	 * @return string tag name of the textbox
74
	 */
75
	protected function getTagName()
76
	{
77
		return ($this->getTextMode()==='MultiLine')?'textarea':'input';
78
	}
79
 
80
	/**
81
	 * @return boolean whether to render javascript.
82
	 */
83
	public function getEnableClientScript()
84
	{
85
		return $this->getViewState('EnableClientScript',true);
86
	}
87
 
88
	/**
89
	 * @param boolean whether to render javascript.
90
	 */
91
	public function setEnableClientScript($value)
92
	{
93
		$this->setViewState('EnableClientScript',TPropertyValue::ensureBoolean($value),true);
94
	}
95
 
96
	/**
97
	 * Adds attribute name-value pairs to renderer.
98
	 * This method overrides the parent implementation with additional textbox specific attributes.
99
	 * @param THtmlWriter the writer used for the rendering purpose
100
	 */
101
	protected function addAttributesToRender($writer)
102
	{
103
		$page=$this->getPage();
104
		$page->ensureRenderInForm($this);
105
		if(($uid=$this->getUniqueID())!=='')
106
			$writer->addAttribute('name',$uid);
107
		if(($textMode=$this->getTextMode())===TTextBoxMode::MultiLine)
108
		{
109
			if(($rows=$this->getRows())<=0)
110
				$rows=self::DEFAULT_ROWS;
111
			if(($cols=$this->getColumns())<=0)
112
				$cols=self::DEFAULT_COLUMNS;
113
			$writer->addAttribute('rows',"$rows");
114
			$writer->addAttribute('cols',"$cols");
115
			if(!$this->getWrap())
116
				$writer->addAttribute('wrap','off');
117
		}
118
		else
119
		{
120
			if($textMode===TTextBoxMode::SingleLine)
121
			{
122
				$writer->addAttribute('type','text');
123
				if(($text=$this->getText())!=='')
124
					$writer->addAttribute('value',$text);
125
				if(($act=$this->getAutoCompleteType())!=='None')
126
				{
127
					if($act==='Disabled')
128
						$writer->addAttribute('autocomplete','off');
129
					else if($act==='Search')
130
						$writer->addAttribute('vcard_name','search');
131
					else if($act==='HomeCountryRegion')
132
						$writer->addAttribute('vcard_name','HomeCountry');
133
					else if($act==='BusinessCountryRegion')
134
						$writer->addAttribute('vcard_name','BusinessCountry');
135
					else
136
					{
137
						if(strpos($act,'Business')===0)
138
							$act='Business'.'.'.substr($act,8);
139
						else if(strpos($act,'Home')===0)
140
							$act='Home'.'.'.substr($act,4);
141
						$writer->addAttribute('vcard_name','vCard.'.$act);
142
					}
143
				}
144
			}
145
			else
146
			{
147
				if($this->getPersistPassword() && ($text=$this->getText())!=='')
148
					$writer->addAttribute('value',$text);
149
				$writer->addAttribute('type','password');
150
			}
151
			if(($cols=$this->getColumns())>0)
152
				$writer->addAttribute('size',"$cols");
153
			if(($maxLength=$this->getMaxLength())>0)
154
				$writer->addAttribute('maxlength',"$maxLength");
155
		}
156
		if($this->getReadOnly())
157
			$writer->addAttribute('readonly','readonly');
158
		$isEnabled=$this->getEnabled(true);
159
		if(!$isEnabled && $this->getEnabled())  // in this case parent will not render 'disabled'
160
			$writer->addAttribute('disabled','disabled');
161
		if($isEnabled
162
			&& $this->getEnableClientScript()
163
			&& ( $this->getAutoPostBack() || $textMode===TTextBoxMode::SingleLine)
164
			&& $page->getClientSupportsJavaScript())
165
		{
166
			$this->renderClientControlScript($writer);
167
		}
168
		parent::addAttributesToRender($writer);
169
	}
170
 
171
	/**
172
	 * Renders the javascript for textbox.
173
	 */
174
	protected function renderClientControlScript($writer)
175
	{
176
		$writer->addAttribute('id',$this->getClientID());
177
		$cs = $this->getPage()->getClientScript();
178
		$cs->registerPostBackControl($this->getClientClassName(),$this->getPostBackOptions());
179
	}
180
 
181
	/**
182
	 * Gets the name of the javascript class responsible for performing postback for this control.
183
	 * This method overrides the parent implementation.
184
	 * @return string the javascript class name
185
	 */
186
	protected function getClientClassName()
187
	{
188
		return 'Prado.WebUI.TTextBox';
189
	}
190
 
191
	/**
192
	 * Gets the post back options for this textbox.
193
	 * @return array
194
	 */
195
	protected function getPostBackOptions()
196
	{
197
		$options['ID'] = $this->getClientID();
198
		$options['EventTarget'] = $this->getUniqueID();
199
		$options['AutoPostBack'] = $this->getAutoPostBack();
200
		$options['CausesValidation'] = $this->getCausesValidation();
201
		$options['ValidationGroup'] = $this->getValidationGroup();
202
		$options['TextMode'] = $this->getTextMode();
203
		return $options;
204
	}
205
 
206
	/**
207
	 * Loads user input data.
208
	 * This method is primarly used by framework developers.
209
	 * @param string the key that can be used to retrieve data from the input data collection
210
	 * @param array the input data collection
211
	 * @return boolean whether the data of the component has been changed
212
	 */
213
	public function loadPostData($key,$values)
214
	{
215
		$value=$values[$key];
216
		if($this->getAutoTrim())
217
			$value=trim($value);
218
		if(!$this->getReadOnly() && $this->getText()!==$value)
219
		{
220
			$this->setText($value);
221
			return $this->_dataChanged=true;
222
		}
223
		else
224
			return false;
225
	}
226
 
227
	/**
228
	 * Returns a value indicating whether postback has caused the control data change.
229
	 * This method is required by the IPostBackDataHandler interface.
230
	 * @return boolean whether postback has caused the control data change. False if the page is not in postback mode.
231
	 */
232
	public function getDataChanged()
233
	{
234
		return $this->_dataChanged;
235
	}
236
 
237
	/**
238
	 * Returns the value to be validated.
239
	 * This methid is required by IValidatable interface.
240
	 * @return mixed the value of the property to be validated.
241
	 */
242
	public function getValidationPropertyValue()
243
	{
244
		return $this->getText();
245
	}
246
 
247
	/**
248
	 * Returns true if this control validated successfully.
249
	 * Defaults to true.
250
	 * @return bool wether this control validated successfully.
251
	 */
252
	public function getIsValid()
253
	{
254
	    return $this->_isValid;
255
	}
256
	/**
257
	 * @param bool wether this control is valid.
258
	 */
259
	public function setIsValid($value)
260
	{
261
	    $this->_isValid=TPropertyValue::ensureBoolean($value);
262
	}
263
 
264
	/**
265
	 * Raises <b>OnTextChanged</b> event.
266
	 * This method is invoked when the value of the {@link getText Text}
267
	 * property changes on postback.
268
	 * If you override this method, be sure to call the parent implementation to ensure
269
	 * the invocation of the attached event handlers.
270
	 * @param TEventParameter event parameter to be passed to the event handlers
271
	 */
272
	public function onTextChanged($param)
273
	{
274
		$this->raiseEvent('OnTextChanged',$this,$param);
275
	}
276
 
277
	/**
278
	 * Raises postdata changed event.
279
	 * This method is required by {@link IPostBackDataHandler} interface.
280
	 * It is invoked by the framework when {@link getText Text} property
281
	 * is changed on postback.
282
	 * This method is primarly used by framework developers.
283
	 */
284
	public function raisePostDataChangedEvent()
285
	{
286
		if($this->getAutoPostBack() && $this->getCausesValidation())
287
			$this->getPage()->validate($this->getValidationGroup());
288
		$this->onTextChanged(null);
289
	}
290
 
291
	/**
292
	 * Renders the body content of the textbox when it is in MultiLine text mode.
293
	 * @param THtmlWriter the writer for rendering
294
	 */
295
	public function renderContents($writer)
296
	{
297
		if($this->getTextMode()==='MultiLine')
298
			$writer->write(THttpUtility::htmlEncode($this->getText()));
299
	}
300
 
301
	/**
302
	 * @return TTextBoxAutoCompleteType the AutoComplete type of the textbox
303
	 */
304
	public function getAutoCompleteType()
305
	{
306
		return $this->getViewState('AutoCompleteType',TTextBoxAutoCompleteType::None);
307
	}
308
 
309
	/**
310
	 * @param TTextBoxAutoCompleteType the AutoComplete type of the textbox, default value is TTextBoxAutoCompleteType::None.
311
	 * @throws TInvalidDataValueException if the input parameter is not a valid AutoComplete type
312
	 */
313
	public function setAutoCompleteType($value)
314
	{
315
		$this->setViewState('AutoCompleteType',TPropertyValue::ensureEnum($value,'TTextBoxAutoCompleteType'),TTextBoxAutoCompleteType::None);
316
	}
317
 
318
	/**
319
	 * @return boolean a value indicating whether an automatic postback to the server
320
     * will occur whenever the user modifies the text in the TTextBox control and
321
     * then tabs out of the component. Defaults to false.
322
	 */
323
	public function getAutoPostBack()
324
	{
325
		return $this->getViewState('AutoPostBack',false);
326
	}
327
 
328
	/**
329
	 * Sets the value indicating if postback automatically.
330
	 * An automatic postback to the server will occur whenever the user
331
	 * modifies the text in the TTextBox control and then tabs out of the component.
332
	 * @param boolean the value indicating if postback automatically
333
	 */
334
	public function setAutoPostBack($value)
335
	{
336
		$this->setViewState('AutoPostBack',TPropertyValue::ensureBoolean($value),false);
337
	}
338
 
339
	/**
340
	 * @return boolean a value indicating whether the input text should be trimmed spaces. Defaults to false.
341
	 */
342
	public function getAutoTrim()
343
	{
344
		return $this->getViewState('AutoTrim',false);
345
	}
346
 
347
	/**
348
	 * Sets the value indicating if the input text should be trimmed spaces
349
	 * @param boolean the value indicating if the input text should be trimmed spaces
350
	 */
351
	public function setAutoTrim($value)
352
	{
353
		$this->setViewState('AutoTrim',TPropertyValue::ensureBoolean($value),false);
354
	}
355
 
356
	/**
357
	 * @return boolean whether postback event trigger by this text box will cause input validation, default is true.
358
	 */
359
	public function getCausesValidation()
360
	{
361
		return $this->getViewState('CausesValidation',true);
362
	}
363
 
364
	/**
365
	 * @param boolean whether postback event trigger by this text box will cause input validation.
366
	 */
367
	public function setCausesValidation($value)
368
	{
369
		$this->setViewState('CausesValidation',TPropertyValue::ensureBoolean($value),true);
370
	}
371
 
372
	/**
373
	 * @return integer the display width of the text box in characters, default is 0 meaning not set.
374
	 */
375
	public function getColumns()
376
	{
377
		return $this->getViewState('Columns',0);
378
	}
379
 
380
	/**
381
	 * Sets the display width of the text box in characters.
382
	 * @param integer the display width, set it 0 to clear the setting
383
	 */
384
	public function setColumns($value)
385
	{
386
		$this->setViewState('Columns',TPropertyValue::ensureInteger($value),0);
387
	}
388
 
389
	/**
390
	 * @return integer the maximum number of characters allowed in the text box, default is 0 meaning not set.
391
	 */
392
	public function getMaxLength()
393
	{
394
		return $this->getViewState('MaxLength',0);
395
	}
396
 
397
	/**
398
	 * Sets the maximum number of characters allowed in the text box.
399
	 * @param integer the maximum length,  set it 0 to clear the setting
400
	 */
401
	public function setMaxLength($value)
402
	{
403
		$this->setViewState('MaxLength',TPropertyValue::ensureInteger($value),0);
404
	}
405
 
406
	/**
407
	 * @return boolean whether the textbox is read only, default is false.
408
	 */
409
	public function getReadOnly()
410
	{
411
		return $this->getViewState('ReadOnly',false);
412
	}
413
 
414
	/**
415
	 * @param boolean whether the textbox is read only
416
	 */
417
	public function setReadOnly($value)
418
	{
419
		$this->setViewState('ReadOnly',TPropertyValue::ensureBoolean($value),false);
420
	}
421
 
422
	/**
423
	 * @return integer the number of rows displayed in a multiline text box, default is 4
424
	 */
425
	public function getRows()
426
	{
427
		return $this->getViewState('Rows',self::DEFAULT_ROWS);
428
	}
429
 
430
	/**
431
	 * Sets the number of rows displayed in a multiline text box.
432
	 * @param integer the number of rows
433
	 */
434
	public function setRows($value)
435
	{
436
		$this->setViewState('Rows',TPropertyValue::ensureInteger($value),self::DEFAULT_ROWS);
437
	}
438
 
439
	/**
440
	 * @return boolean whether password should be displayed in the textbox during postback. Defaults to false. This property only applies when TextMode='Password'.
441
	 */
442
	public function getPersistPassword()
443
	{
444
		return $this->getViewState('PersistPassword',false);
445
	}
446
 
447
	/**
448
	 * @param boolean whether password should be displayed in the textbox during postback. This property only applies when TextMode='Password'.
449
	 */
450
	public function setPersistPassword($value)
451
	{
452
		$this->setViewState('PersistPassword',TPropertyValue::ensureBoolean($value),false);
453
	}
454
 
455
	/**
456
	 * @return string the text content of the TTextBox control.
457
	 */
458
	public function getText()
459
	{
460
		return $this->getViewState('Text','');
461
	}
462
 
463
	/**
464
	 * Sets the text content of the TTextBox control.
465
	 * @param string the text content
466
	 */
467
	public function setText($value)
468
	{
469
		$this->setViewState('Text',$value,'');
470
		$this->_safeText = null;
471
	}
472
 
473
	/**
474
	 * Returns the text content of the TTextBox control.
475
	 * This method is required by {@link IDataRenderer}.
476
	 * It is the same as {@link getText()}.
477
	 * @return string the text content of the TTextBox control.
478
	 * @see getText
479
	 * @since 3.1.0
480
	 */
481
	public function getData()
482
	{
483
		return $this->getText();
484
	}
485
 
486
	/**
487
	 * Sets the text content of the TTextBox control.
488
	 * This method is required by {@link IDataRenderer}.
489
	 * It is the same as {@link setText()}.
490
	 * @param string the text content of the TTextBox control.
491
	 * @see setText
492
	 * @since 3.1.0
493
	 */
494
	public function setData($value)
495
	{
496
		$this->setText($value);
497
	}
498
 
499
	/**
500
	 * @return string safe text content with javascript stripped off
501
	 */
502
	public function getSafeText()
503
	{
504
		if($this->_safeText===null)
505
			$this->_safeText=$this->getSafeTextParser()->parse($this->getText());
506
		return $this->_safeText;
507
	}
508
 
509
	/**
510
	 * @return mixed safe text parser
511
	 */
512
	protected function getSafeTextParser()
513
	{
514
		if(!self::$_safeTextParser)
515
			self::$_safeTextParser=Prado::createComponent('System.3rdParty.SafeHtml.TSafeHtmlParser');
516
		return self::$_safeTextParser;
517
	}
518
 
519
	/**
520
	 * @return TTextBoxMode the behavior mode of the TTextBox component. Defaults to TTextBoxMode::SingleLine.
521
	 */
522
	public function getTextMode()
523
	{
524
		return $this->getViewState('TextMode',TTextBoxMode::SingleLine);
525
	}
526
 
527
	/**
528
	 * Sets the behavior mode of the TTextBox component.
529
	 * @param TTextBoxMode the text mode
530
	 * @throws TInvalidDataValueException if the input value is not a valid text mode.
531
	 */
532
	public function setTextMode($value)
533
	{
534
		$this->setViewState('TextMode',TPropertyValue::ensureEnum($value,'TTextBoxMode'),TTextBoxMode::SingleLine);
535
	}
536
 
537
	/**
538
	 * @return string the group of validators which the text box causes validation upon postback
539
	 */
540
	public function getValidationGroup()
541
	{
542
		return $this->getViewState('ValidationGroup','');
543
	}
544
 
545
	/**
546
	 * @param string the group of validators which the text box causes validation upon postback
547
	 */
548
	public function setValidationGroup($value)
549
	{
550
		$this->setViewState('ValidationGroup',$value,'');
551
	}
552
 
553
	/**
554
	 * @return boolean whether the text content wraps within a multiline text box. Defaults to true.
555
	 */
556
	public function getWrap()
557
	{
558
		return $this->getViewState('Wrap',true);
559
	}
560
 
561
	/**
562
	 * Sets the value indicating whether the text content wraps within a multiline text box.
563
	 * @param boolean whether the text content wraps within a multiline text box.
564
	 */
565
	public function setWrap($value)
566
	{
567
		$this->setViewState('Wrap',TPropertyValue::ensureBoolean($value),true);
568
	}
569
}
570
 
571
/**
572
 * TTextBoxMode class.
573
 * TTextBoxMode defines the enumerable type for the possible mode
574
 * that a {@link TTextBox} control could be at.
575
 *
576
 * The following enumerable values are defined:
577
 * - SingleLine: the textbox will be a regular single line input
578
 * - MultiLine: the textbox will be a textarea allowing multiple line input
579
 * - Password: the textbox will hide user input like a password input box
580
 *
581
 * @author Qiang Xue <qiang.xue@gmail.com>
582
 * @version $Id: TTextBox.php 2541 2008-10-21 15:05:13Z qiang.xue $
583
 * @package System.Web.UI.WebControls
584
 * @since 3.0.4
585
 */
586
class TTextBoxMode extends TEnumerable
587
{
588
	const SingleLine='SingleLine';
589
	const MultiLine='MultiLine';
590
	const Password='Password';
591
}
592
 
593
/**
594
 * TTextBoxAutoCompleteType class.
595
 * TTextBoxAutoCompleteType defines the possible AutoComplete type that is supported
596
 * by a {@link TTextBox} control.
597
 *
598
 * @author Qiang Xue <qiang.xue@gmail.com>
599
 * @version $Id: TTextBox.php 2541 2008-10-21 15:05:13Z qiang.xue $
600
 * @package System.Web.UI.WebControls
601
 * @since 3.0.4
602
 */
603
class TTextBoxAutoCompleteType extends TEnumerable
604
{
605
	const BusinessCity='BusinessCity';
606
	const BusinessCountryRegion='BusinessCountryRegion';
607
	const BusinessFax='BusinessFax';
608
	const BusinessPhone='BusinessPhone';
609
	const BusinessState='BusinessState';
610
	const BusinessStreetAddress='BusinessStreetAddress';
611
	const BusinessUrl='BusinessUrl';
612
	const BusinessZipCode='BusinessZipCode';
613
	const Cellular='Cellular';
614
	const Company='Company';
615
	const Department='Department';
616
	const Disabled='Disabled';
617
	const DisplayName='DisplayName';
618
	const Email='Email';
619
	const FirstName='FirstName';
620
	const Gender='Gender';
621
	const HomeCity='HomeCity';
622
	const HomeCountryRegion='HomeCountryRegion';
623
	const HomeFax='HomeFax';
624
	const Homepage='Homepage';
625
	const HomePhone='HomePhone';
626
	const HomeState='HomeState';
627
	const HomeStreetAddress='HomeStreetAddress';
628
	const HomeZipCode='HomeZipCode';
629
	const JobTitle='JobTitle';
630
	const LastName='LastName';
631
	const MiddleName='MiddleName';
632
	const None='None';
633
	const Notes='Notes';
634
	const Office='Office';
635
	const Pager='Pager';
636
	const Search='Search';
637
}
638