Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * TCheckBox 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: TCheckBox.php 2522 2008-10-13 21:40:53Z mikl $
10
 * @package System.Web.UI.WebControls
11
 */
12
 
13
/**
14
 * TCheckBox class
15
 *
16
 * TCheckBox displays a check box on the page.
17
 * You can specify the caption to display beside the check box by setting
18
 * the {@link setText Text} property.  The caption can appear either on the right
19
 * or left of the check box, which is determined by the {@link setTextAlign TextAlign}
20
 * property.
21
 *
22
 * To determine whether the TCheckBox component is checked, test the {@link getChecked Checked}
23
 * property. The {@link onCheckedChanged OnCheckedChanged} event is raised when
24
 * the {@link getChecked Checked} state of the TCheckBox component changes
25
 * between posts to the server. You can provide an event handler for
26
 * the {@link onCheckedChanged OnCheckedChanged} event to  to programmatically
27
 * control the actions performed when the state of the TCheckBox component changes
28
 * between posts to the server.
29
 *
30
 * If {@link setAutoPostBack AutoPostBack} is set true, changing the check box state
31
 * will cause postback action. And if {@link setCausesValidation CausesValidation}
32
 * is true, validation will also be processed, which can be further restricted within
33
 * a {@link setValidationGroup ValidationGroup}.
34
 *
35
 * Note, {@link setText Text} is rendered as is. Make sure it does not contain unwanted characters
36
 * that may bring security vulnerabilities.
37
 *
38
 * @author Qiang Xue <qiang.xue@gmail.com>
39
 * @version $Id: TCheckBox.php 2522 2008-10-13 21:40:53Z mikl $
40
 * @package System.Web.UI.WebControls
41
 * @since 3.0
42
 */
43
class TCheckBox extends TWebControl implements IPostBackDataHandler, IValidatable, IDataRenderer, ISurroundable
44
{
45
	private $_dataChanged=false;
46
	private $_isValid=true;
47
 
48
	/**
49
	 * @return string tag name of the button
50
	 */
51
	protected function getTagName()
52
	{
53
		return 'input';
54
	}
55
 
56
	/**
57
	 * Loads user input data.
58
	 * This method is primarly used by framework developers.
59
	 * @param string the key that can be used to retrieve data from the input data collection
60
	 * @param array the input data collection
61
	 * @return boolean whether the data of the control has been changed
62
	 */
63
	public function loadPostData($key,$values)
64
	{
65
		$checked=$this->getChecked();
66
		if($newChecked=isset($values[$key]))
67
			$this->setValue($values[$key]);
68
		$this->setChecked($newChecked);
69
		return $this->_dataChanged=($newChecked!==$checked);
70
	}
71
 
72
	/**
73
	 * Raises postdata changed event.
74
	 * This method raises {@link onCheckedChanged OnCheckedChanged} event.
75
	 * This method is primarly used by framework developers.
76
	 */
77
	public function raisePostDataChangedEvent()
78
	{
79
		if($this->getAutoPostBack() && $this->getCausesValidation())
80
			$this->getPage()->validate($this->getValidationGroup());
81
		$this->onCheckedChanged(null);
82
	}
83
 
84
	/**
85
	 * Raises <b>OnCheckedChanged</b> event when {@link getChecked Checked} changes value during postback.
86
	 * If you override this method, be sure to call the parent implementation
87
	 * so that the event delegates can be invoked.
88
	 * @param TEventParameter event parameter to be passed to the event handlers
89
	 */
90
	public function onCheckedChanged($param)
91
	{
92
		$this->raiseEvent('OnCheckedChanged',$this,$param);
93
	}
94
 
95
	/**
96
	 * Registers the checkbox to receive postback data during postback.
97
	 * This is necessary because a checkbox if unchecked, when postback,
98
	 * does not have direct mapping between post data and the checkbox name.
99
	 *
100
	 * This method overrides the parent implementation and is invoked before render.
101
	 * @param mixed event parameter
102
	 */
103
	public function onPreRender($param)
104
	{
105
		parent::onPreRender($param);
106
		if($this->getEnabled(true))
107
			$this->getPage()->registerRequiresPostData($this);
108
	}
109
 
110
	/**
111
	 * Returns a value indicating whether postback has caused the control data change.
112
	 * This method is required by the IPostBackDataHandler interface.
113
	 * @return boolean whether postback has caused the control data change. False if the page is not in postback mode.
114
	 */
115
	public function getDataChanged()
116
	{
117
		return $this->_dataChanged;
118
	}
119
 
120
	/**
121
	 * Returns the value of the property that needs validation.
122
	 * @return mixed the property value to be validated
123
	 */
124
	public function getValidationPropertyValue()
125
	{
126
		return $this->getChecked();
127
	}
128
 
129
	/**
130
	 * Returns true if this control validated successfully.
131
	 * Defaults to true.
132
	 * @return bool wether this control validated successfully.
133
	 */
134
	public function getIsValid()
135
	{
136
	    return $this->_isValid;
137
	}
138
	/**
139
	 * @param bool wether this control is valid.
140
	 */
141
	public function setIsValid($value)
142
	{
143
	    $this->_isValid=TPropertyValue::ensureBoolean($value);
144
	}
145
 
146
	/**
147
	 * @return string the text caption of the checkbox
148
	 */
149
	public function getText()
150
	{
151
		return $this->getViewState('Text','');
152
	}
153
 
154
	/**
155
	 * Sets the text caption of the checkbox.
156
	 * @param string the text caption to be set
157
	 */
158
	public function setText($value)
159
	{
160
		$this->setViewState('Text',$value,'');
161
	}
162
 
163
	/**
164
	 * @return string the value of the checkbox. Defaults to empty.
165
	 */
166
	public function getValue()
167
	{
168
		return $this->getViewState('Value','');
169
	}
170
 
171
	/**
172
	 * @param string the value of the checkbox
173
	 */
174
	public function setValue($value)
175
	{
176
		$this->setViewState('Value',TPropertyValue::ensureString($value),'');
177
	}
178
 
179
	/**
180
	 * @return TTextAlign the alignment (Left or Right) of the text caption, defaults to TTextAlign::Right.
181
	 */
182
	public function getTextAlign()
183
	{
184
		return $this->getViewState('TextAlign',TTextAlign::Right);
185
	}
186
 
187
	/**
188
	 * @param TTextAlign the alignment of the text caption. Valid values include Left and Right.
189
	 */
190
	public function setTextAlign($value)
191
	{
192
		$this->setViewState('TextAlign',TPropertyValue::ensureEnum($value,'TTextAlign'),TTextAlign::Right);
193
	}
194
 
195
	/**
196
	 * @return boolean whether the checkbox is checked
197
	 */
198
	public function getChecked()
199
	{
200
		return $this->getViewState('Checked',false);
201
	}
202
 
203
	/**
204
	 * Sets a value indicating whether the checkbox is to be checked or not.
205
	 * @param boolean whether the checkbox is to be checked or not.
206
	 */
207
	public function setChecked($value)
208
	{
209
		$this->setViewState('Checked',TPropertyValue::ensureBoolean($value),false);
210
	}
211
 
212
	/**
213
	 * Returns the value indicating whether the checkbox is checked.
214
	 * This method is required by {@link IDataRenderer}.
215
	 * It is the same as {@link getChecked()}.
216
	 * @return boolean whether the checkbox is checked.
217
	 * @see getChecked
218
	 * @since 3.1.0
219
	 */
220
	public function getData()
221
	{
222
		return $this->getChecked();
223
	}
224
 
225
	/**
226
	 * Sets the value indicating whether the checkbox is to be checked or not.
227
	 * This method is required by {@link IDataRenderer}.
228
	 * It is the same as {@link setChecked()}.
229
	 * @param boolean whether the checkbox is to be checked
230
	 * @see setChecked
231
	 * @since 3.1.0
232
	 */
233
	public function setData($value)
234
	{
235
		$this->setChecked($value);
236
	}
237
 
238
	/**
239
	 * @return boolean whether clicking on the checkbox will post the page.
240
	 */
241
	public function getAutoPostBack()
242
	{
243
		return $this->getViewState('AutoPostBack',false);
244
	}
245
 
246
	/**
247
	 * Sets a value indicating whether clicking on the checkbox will post the page.
248
	 * @param boolean whether clicking on the checkbox will post the page.
249
	 */
250
	public function setAutoPostBack($value)
251
	{
252
		$this->setViewState('AutoPostBack',TPropertyValue::ensureBoolean($value),false);
253
	}
254
 
255
	/**
256
	 * @return boolean whether postback event triggered by this checkbox will cause input validation, default is true.
257
	 */
258
	public function getCausesValidation()
259
	{
260
		return $this->getViewState('CausesValidation',true);
261
	}
262
 
263
	/**
264
	 * Sets the value indicating whether postback event trigger by this checkbox will cause input validation.
265
	 * @param boolean whether postback event trigger by this checkbox will cause input validation.
266
	 */
267
	public function setCausesValidation($value)
268
	{
269
		$this->setViewState('CausesValidation',TPropertyValue::ensureBoolean($value),true);
270
	}
271
 
272
	/**
273
	 * @return string the group of validators which the checkbox causes validation upon postback
274
	 */
275
	public function getValidationGroup()
276
	{
277
		return $this->getViewState('ValidationGroup','');
278
	}
279
 
280
	/**
281
	 * @param string the group of validators which the checkbox causes validation upon postback
282
	 */
283
	public function setValidationGroup($value)
284
	{
285
		$this->setViewState('ValidationGroup',$value,'');
286
	}
287
 
288
	/**
289
	 * @return string the id of the surrounding tag or this clientID if no such tag needed
290
	 */
291
	public function getSurroundingTagID()
292
	{
293
        return $this->getSpanNeeded() ? $this->getClientID().'_parent' : $this->getClientID();
294
	}
295
 
296
	/**
297
	 * Renders the checkbox control.
298
	 * This method overrides the parent implementation by rendering a checkbox input element
299
	 * and a span element if needed.
300
	 * @param THtmlWriter the writer used for the rendering purpose
301
	 */
302
	public function render($writer)
303
	{
304
		$this->getPage()->ensureRenderInForm($this);
305
		if($this->getHasStyle())
306
			$this->getStyle()->addAttributesToRender($writer);
307
		if(($tooltip=$this->getToolTip())!=='')
308
			$writer->addAttribute('title',$tooltip);
309
		if($this->getHasAttributes())
310
		{
311
			$attributes=$this->getAttributes();
312
			$value=$attributes->remove('value');
313
			// onclick js should only be added to input tag
314
			if(($onclick=$attributes->remove('onclick'))===null)
315
				$onclick='';
316
			if($attributes->getCount())
317
				$writer->addAttributes($attributes);
318
			if($value!==null)
319
				$attributes->add('value',$value);
320
		}
321
		else
322
			$onclick='';
323
        if($needspan=$this->getSpanNeeded())
324
        {
325
            $writer->addAttribute('id',$this->getSurroundingTagID());
326
			$writer->renderBeginTag('span');
327
        }
328
		$clientID=$this->getClientID();
329
		if(($text=$this->getText())!=='')
330
		{
331
			if($this->getTextAlign()===TTextAlign::Left)
332
			{
333
				$this->renderLabel($writer,$clientID,$text);
334
				$this->renderInputTag($writer,$clientID,$onclick);
335
			}
336
			else
337
			{
338
				$this->renderInputTag($writer,$clientID,$onclick);
339
				$this->renderLabel($writer,$clientID,$text);
340
			}
341
		}
342
		else
343
			$this->renderInputTag($writer,$clientID,$onclick);
344
		if($needspan)
345
			$writer->renderEndTag();
346
	}
347
 
348
	/**
349
	 * @return TMap list of attributes to be rendered for label beside the checkbox
350
	 */
351
	public function getLabelAttributes()
352
	{
353
		if($attributes=$this->getViewState('LabelAttributes',null))
354
			return $attributes;
355
		else
356
		{
357
			$attributes=new TAttributeCollection;
358
			$this->setViewState('LabelAttributes',$attributes,null);
359
			return $attributes;
360
		}
361
	}
362
 
363
	/**
364
	 * @return TMap list of attributes to be rendered for the checkbox
365
	 */
366
	public function getInputAttributes()
367
	{
368
		if($attributes=$this->getViewState('InputAttributes',null))
369
			return $attributes;
370
		else
371
		{
372
			$attributes=new TAttributeCollection;
373
			$this->setViewState('InputAttributes',$attributes,null);
374
			return $attributes;
375
		}
376
	}
377
 
378
	/**
379
	 * @return string the value attribute to be rendered
380
	 */
381
	protected function getValueAttribute()
382
	{
383
		if(($value=$this->getValue())!=='')
384
			return $value;
385
		else
386
		{
387
			$attributes=$this->getViewState('InputAttributes',null);
388
			if($attributes && $attributes->contains('value'))
389
				return $attributes->itemAt('value');
390
			else if($this->hasAttribute('value'))
391
				return $this->getAttribute('value');
392
			else
393
				return '';
394
		}
395
	}
396
 
397
	/**
398
	 * @return boolean whether to render javascript.
399
	 */
400
	public function getEnableClientScript()
401
	{
402
		return $this->getViewState('EnableClientScript',true);
403
	}
404
 
405
	/**
406
	 * @param boolean whether to render javascript.
407
	 */
408
	public function setEnableClientScript($value)
409
	{
410
		$this->setViewState('EnableClientScript',TPropertyValue::ensureBoolean($value),true);
411
	}
412
 
413
    /**
414
     * Check if we need a span tag to surround this control. The span tag will be created if
415
     * the Text property is set for this control.
416
     *
417
     * @return bool wether this control needs a surrounding span tag
418
     */
419
    protected function getSpanNeeded() {
420
        return $this->getText()!=='';
421
    }
422
 
423
	/**
424
	 * Renders a label beside the checkbox.
425
	 * @param THtmlWriter the writer for the rendering purpose
426
	 * @param string checkbox id
427
	 * @param string label text
428
	 */
429
	protected function renderLabel($writer,$clientID,$text)
430
	{
431
		$writer->addAttribute('for',$clientID);
432
		if($attributes=$this->getViewState('LabelAttributes',null))
433
			$writer->addAttributes($attributes);
434
		$writer->renderBeginTag('label');
435
		$writer->write($text);
436
		$writer->renderEndTag();
437
	}
438
 
439
	/**
440
	 * Renders a checkbox input element.
441
	 * @param THtmlWriter the writer for the rendering purpose
442
	 * @param string checkbox id
443
	 * @param string onclick js
444
	 */
445
	protected function renderInputTag($writer,$clientID,$onclick)
446
	{
447
		if($clientID!=='')
448
			$writer->addAttribute('id',$clientID);
449
		$writer->addAttribute('type','checkbox');
450
		if(($value=$this->getValueAttribute())!=='')
451
			$writer->addAttribute('value',$value);
452
		if(!empty($onclick))
453
			$writer->addAttribute('onclick',$onclick);
454
		if(($uniqueID=$this->getUniqueID())!=='')
455
			$writer->addAttribute('name',$uniqueID);
456
		if($this->getChecked())
457
			$writer->addAttribute('checked','checked');
458
		if(!$this->getEnabled(true))
459
			$writer->addAttribute('disabled','disabled');
460
 
461
		$page=$this->getPage();
462
		if($this->getEnabled(true)
463
			&& $this->getEnableClientScript()
464
			&& $this->getAutoPostBack()
465
			&& $page->getClientSupportsJavaScript())
466
		{
467
			$this->renderClientControlScript($writer);
468
		}
469
 
470
		if(($accesskey=$this->getAccessKey())!=='')
471
			$writer->addAttribute('accesskey',$accesskey);
472
		if(($tabindex=$this->getTabIndex())>0)
473
			$writer->addAttribute('tabindex',"$tabindex");
474
		if($attributes=$this->getViewState('InputAttributes',null))
475
			$writer->addAttributes($attributes);
476
		$writer->renderBeginTag('input');
477
		$writer->renderEndTag();
478
	}
479
 
480
	/**
481
	 * Renders the client-script code.
482
	 */
483
	protected function renderClientControlScript($writer)
484
	{
485
		$cs = $this->getPage()->getClientScript();
486
		$cs->registerPostBackControl($this->getClientClassName(),$this->getPostBackOptions());
487
	}
488
 
489
	/**
490
	 * Gets the name of the javascript class responsible for performing postback for this control.
491
	 * This method overrides the parent implementation.
492
	 * @return string the javascript class name
493
	 */
494
	protected function getClientClassName()
495
	{
496
		return 'Prado.WebUI.TCheckBox';
497
	}
498
 
499
	/**
500
	 * Gets the post back options for this checkbox.
501
	 * @return array
502
	 */
503
	protected function getPostBackOptions()
504
	{
505
		$options['ID'] = $this->getClientID();
506
		$options['ValidationGroup'] = $this->getValidationGroup();
507
		$options['CausesValidation'] = $this->getCausesValidation();
508
		$options['EventTarget'] = $this->getUniqueID();
509
		return $options;
510
	}
511
}
512
 
513
/**
514
 * TTextAlign class.
515
 * TTextAlign defines the enumerable type for the possible text alignments
516
 *
517
 * The following enumerable values are defined:
518
 * - Left: left aligned
519
 * - Right: right aligned
520
 *
521
 * @author Qiang Xue <qiang.xue@gmail.com>
522
 * @version $Id: TCheckBox.php 2522 2008-10-13 21:40:53Z mikl $
523
 * @package System.Web.UI.WebControls
524
 * @since 3.0.4
525
 */
526
class TTextAlign extends TEnumerable
527
{
528
	const Left='Left';
529
	const Right='Right';
530
}
531
 
532
?>