Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * TCheckBoxList 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: TCheckBoxList.php 2541 2008-10-21 15:05:13Z qiang.xue $
10
 * @package System.Web.UI.WebControls
11
 */
12
 
13
/**
14
 * Includes TListControl class
15
 */
16
Prado::using('System.Web.UI.WebControls.TListControl');
17
/**
18
 * Includes TRepeatInfo class
19
 */
20
Prado::using('System.Web.UI.WebControls.TRepeatInfo');
21
/**
22
 * Includes TCheckBox class
23
 */
24
Prado::using('System.Web.UI.WebControls.TCheckBox');
25
 
26
/**
27
 * TCheckBoxList class
28
 *
29
 * TCheckBoxList displays a list of checkboxes on a Web page.
30
 *
31
 * The layout of the checkbox list is specified via {@link setRepeatLayout RepeatLayout},
32
 * which can be either 'Table' (default) or 'Flow'.
33
 * A table layout uses HTML table cells to organize the checkboxes while
34
 * a flow layout uses line breaks to organize the checkboxes.
35
 * When the layout is using 'Table', {@link setCellPadding CellPadding} and
36
 * {@link setCellSpacing CellSpacing} can be used to adjust the cellpadding and
37
 * cellpadding of the table.
38
 *
39
 * The number of columns used to display the checkboxes is specified via
40
 * {@link setRepeatColumns RepeatColumns} property, while the {@link setRepeatDirection RepeatDirection}
41
 * governs the order of the items being rendered.
42
 *
43
 * The alignment of the text besides each checkbox can be specified via {@link setTextAlign TextAlign}.
44
 *
45
 * @author Qiang Xue <qiang.xue@gmail.com>
46
 * @version $Id: TCheckBoxList.php 2541 2008-10-21 15:05:13Z qiang.xue $
47
 * @package System.Web.UI.WebControls
48
 * @since 3.0
49
 */
50
class TCheckBoxList extends TListControl implements IRepeatInfoUser, INamingContainer, IPostBackDataHandler,  IValidatable
51
{
52
	private $_repeatedControl;
53
	private $_isEnabled;
54
	private $_changedEventRaised=false;
55
	private $_dataChanged=false;
56
	private $_isValid=true;
57
 
58
	/**
59
	 * Constructor.
60
	 * Remember to call parent implementation if you override this method
61
	 */
62
	public function __construct()
63
	{
64
		parent::__construct();
65
		$this->_repeatedControl=$this->createRepeatedControl();
66
		$this->_repeatedControl->setEnableViewState(false);
67
		$this->_repeatedControl->setID('c0');
68
		$this->getControls()->add($this->_repeatedControl);
69
	}
70
 
71
	/**
72
	 * Creates a control used for repetition (used as a template).
73
	 * @return TControl the control to be repeated
74
	 */
75
	protected function createRepeatedControl()
76
	{
77
		return new TCheckBox;
78
	}
79
 
80
	/**
81
	 * Finds a control by ID.
82
	 * This method overrides the parent implementation so that it always returns
83
	 * the checkbox list itself (because the checkbox list does not have child controls.)
84
	 * @param string control ID
85
	 * @return TControl control being found
86
	 */
87
	public function findControl($id)
88
	{
89
		return $this;
90
	}
91
 
92
	/**
93
	 * @return boolean whether this control supports multiple selection. Always true for checkbox list.
94
	 */
95
	protected function getIsMultiSelect()
96
	{
97
		return true;
98
	}
99
 
100
	/**
101
	 * Creates a style object for the control.
102
	 * This method creates a {@link TTableStyle} to be used by checkbox list.
103
	 * @return TStyle control style to be used
104
	 */
105
	protected function createStyle()
106
	{
107
		return new TTableStyle;
108
	}
109
 
110
	/**
111
	 * @return TTextAlign the alignment of the text caption, defaults to TTextAlign::Right.
112
	 */
113
	public function getTextAlign()
114
	{
115
		return $this->getViewState('TextAlign',TTextAlign::Right);
116
	}
117
 
118
	/**
119
	 * @param TTextAlign the text alignment of the checkboxes
120
	 */
121
	public function setTextAlign($value)
122
	{
123
		$this->setViewState('TextAlign',TPropertyValue::ensureEnum($value,'TTextAlign'),TTextAlign::Right);
124
	}
125
 
126
 
127
	/**
128
	 * @return TRepeatInfo repeat information (primarily used by control developers)
129
	 */
130
	protected function getRepeatInfo()
131
	{
132
		if(($repeatInfo=$this->getViewState('RepeatInfo',null))===null)
133
		{
134
			$repeatInfo=new TRepeatInfo;
135
			$this->setViewState('RepeatInfo',$repeatInfo,null);
136
		}
137
		return $repeatInfo;
138
	}
139
 
140
	/**
141
	 * @return integer the number of columns that the list should be displayed with. Defaults to 0 meaning not set.
142
	 */
143
	public function getRepeatColumns()
144
	{
145
		return $this->getRepeatInfo()->getRepeatColumns();
146
	}
147
 
148
	/**
149
	 * @param integer the number of columns that the list should be displayed with.
150
	 */
151
	public function setRepeatColumns($value)
152
	{
153
		$this->getRepeatInfo()->setRepeatColumns($value);
154
	}
155
 
156
	/**
157
	 * @return string the direction of traversing the list, defaults to 'Vertical'
158
	 */
159
	public function getRepeatDirection()
160
	{
161
		return $this->getRepeatInfo()->getRepeatDirection();
162
	}
163
 
164
	/**
165
	 * @param string the direction (Vertical, Horizontal) of traversing the list
166
	 */
167
	public function setRepeatDirection($value)
168
	{
169
		$this->getRepeatInfo()->setRepeatDirection($value);
170
	}
171
 
172
	/**
173
	 * @return string how the list should be displayed, using table or using line breaks. Defaults to 'Table'.
174
	 */
175
	public function getRepeatLayout()
176
	{
177
		return $this->getRepeatInfo()->getRepeatLayout();
178
	}
179
 
180
	/**
181
	 * @param string how the list should be displayed, using table or using line breaks (Table, Flow)
182
	 */
183
	public function setRepeatLayout($value)
184
	{
185
		$this->getRepeatInfo()->setRepeatLayout($value);
186
	}
187
 
188
	/**
189
	 * @return integer the cellspacing for the table keeping the checkbox list. Defaults to -1, meaning not set.
190
	 */
191
	public function getCellSpacing()
192
	{
193
		if($this->getHasStyle())
194
			return $this->getStyle()->getCellSpacing();
195
		else
196
			return -1;
197
	}
198
 
199
	/**
200
	 * Sets the cellspacing for the table keeping the checkbox list.
201
	 * @param integer the cellspacing for the table keeping the checkbox list.
202
	 */
203
	public function setCellSpacing($value)
204
	{
205
		$this->getStyle()->setCellSpacing($value);
206
	}
207
 
208
	/**
209
	 * @return integer the cellpadding for the table keeping the checkbox list. Defaults to -1, meaning not set.
210
	 */
211
	public function getCellPadding()
212
	{
213
		if($this->getHasStyle())
214
			return $this->getStyle()->getCellPadding();
215
		else
216
			return -1;
217
	}
218
 
219
	/**
220
	 * Sets the cellpadding for the table keeping the checkbox list.
221
	 * @param integer the cellpadding for the table keeping the checkbox list.
222
	 */
223
	public function setCellPadding($value)
224
	{
225
		$this->getStyle()->setCellPadding($value);
226
	}
227
 
228
	/**
229
	 * Returns a value indicating whether this control contains header item.
230
	 * This method is required by {@link IRepeatInfoUser} interface.
231
	 * @return boolean always false.
232
	 */
233
	public function getHasHeader()
234
	{
235
		return false;
236
	}
237
 
238
	/**
239
	 * Returns a value indicating whether this control contains footer item.
240
	 * This method is required by {@link IRepeatInfoUser} interface.
241
	 * @return boolean always false.
242
	 */
243
	public function getHasFooter()
244
	{
245
		return false;
246
	}
247
 
248
	/**
249
	 * Returns a value indicating whether this control contains separator items.
250
	 * This method is required by {@link IRepeatInfoUser} interface.
251
	 * @return boolean always false.
252
	 */
253
	public function getHasSeparators()
254
	{
255
		return false;
256
	}
257
 
258
	/**
259
	 * Returns a style used for rendering items.
260
	 * This method is required by {@link IRepeatInfoUser} interface.
261
	 * @param string item type (Header,Footer,Item,AlternatingItem,SelectedItem,EditItem,Separator,Pager)
262
	 * @param integer index of the item being rendered
263
	 * @return null
264
	 */
265
	public function generateItemStyle($itemType,$index)
266
	{
267
		return null;
268
	}
269
 
270
	/**
271
	 * Renders an item in the list.
272
	 * This method is required by {@link IRepeatInfoUser} interface.
273
	 * @param THtmlWriter writer for rendering purpose
274
	 * @param TRepeatInfo repeat information
275
	 * @param string item type (Header,Footer,Item,AlternatingItem,SelectedItem,EditItem,Separator,Pager)
276
	 * @param integer zero-based index of the item in the item list
277
	 */
278
	public function renderItem($writer,$repeatInfo,$itemType,$index)
279
	{
280
		$repeatedControl=$this->_repeatedControl;
281
		$item=$this->getItems()->itemAt($index);
282
		if($item->getHasAttributes())
283
			$repeatedControl->getAttributes()->copyFrom($item->getAttributes());
284
		else if($repeatedControl->getHasAttributes())
285
			$repeatedControl->getAttributes()->clear();
286
		$repeatedControl->setID("c$index");
287
		$repeatedControl->setText($item->getText());
288
		$repeatedControl->setChecked($item->getSelected());
289
		$repeatedControl->setAttribute('value',$item->getValue());
290
		$repeatedControl->setEnabled($this->_isEnabled && $item->getEnabled());
291
		$repeatedControl->setEnableClientScript(false);
292
		$repeatedControl->renderControl($writer);
293
	}
294
 
295
	/**
296
	 * Loads user input data.
297
	 * This method is primarly used by framework developers.
298
	 * @param string the key that can be used to retrieve data from the input data collection
299
	 * @param array the input data collection
300
	 * @return boolean whether the data of the control has been changed
301
	 */
302
	public function loadPostData($key,$values)
303
	{
304
		if($this->getEnabled(true))
305
		{
306
			$index=(int)substr($key,strlen($this->getUniqueID())+2);
307
			$this->ensureDataBound();
308
			if($index>=0 && $index<$this->getItemCount())
309
			{
310
				$item=$this->getItems()->itemAt($index);
311
				if($item->getEnabled())
312
				{
313
					$checked=isset($values[$key]);
314
					if($item->getSelected()!==$checked)
315
					{
316
						$item->setSelected($checked);
317
						if(!$this->_changedEventRaised)
318
						{
319
							$this->_changedEventRaised=true;
320
							return $this->_dataChanged=true;
321
						}
322
					}
323
				}
324
			}
325
		}
326
		return false;
327
	}
328
 
329
	/**
330
	 * Raises postdata changed event.
331
	 * This method is required by {@link IPostBackDataHandler} interface.
332
	 * It is invoked by the framework when {@link getSelectedIndices SelectedIndices} property
333
	 * is changed on postback.
334
	 * This method is primarly used by framework developers.
335
	 */
336
	public function raisePostDataChangedEvent()
337
	{
338
		if($this->getAutoPostBack() && $this->getCausesValidation())
339
			$this->getPage()->validate($this->getValidationGroup());
340
		$this->onSelectedIndexChanged(null);
341
	}
342
 
343
	/**
344
	 * Registers for post data on postback.
345
	 * This method overrides the parent implementation.
346
	 * @param mixed event parameter
347
	 */
348
	public function onPreRender($param)
349
	{
350
		parent::onPreRender($param);
351
		$this->_repeatedControl->setAutoPostBack($this->getAutoPostBack());
352
		$this->_repeatedControl->setCausesValidation($this->getCausesValidation());
353
		$this->_repeatedControl->setValidationGroup($this->getValidationGroup());
354
		$page=$this->getPage();
355
		$n=$this->getItemCount();
356
		for($i=0;$i<$n;++$i)
357
		{
358
			$this->_repeatedControl->setID("c$i");
359
			$page->registerRequiresPostData($this->_repeatedControl);
360
		}
361
	}
362
 
363
	/**
364
	 * Wether the list should be rendered inside a span or not
365
	 *
366
	 *@return boolean true if we need a span
367
	 */
368
	protected function getSpanNeeded ()
369
	{
370
		return $this->getRepeatLayout()===TRepeatLayout::Raw;
371
	}
372
 
373
	/**
374
	 * Renders the checkbox list control.
375
	 * This method overrides the parent implementation.
376
	 * @param THtmlWriter writer for rendering purpose.
377
	 */
378
	public function render($writer)
379
	{
380
		if($this->getItemCount()>0)
381
		{
382
			if ($needSpan=$this->getSpanNeeded())
383
			{
384
				$writer->addAttribute('id', $this->getClientId());
385
				$writer->renderBeginTag('span');
386
			}
387
			$this->_isEnabled=$this->getEnabled(true);
388
			$repeatInfo=$this->getRepeatInfo();
389
			$accessKey=$this->getAccessKey();
390
			$tabIndex=$this->getTabIndex();
391
			$this->_repeatedControl->setTextAlign($this->getTextAlign());
392
			$this->_repeatedControl->setAccessKey($accessKey);
393
			$this->_repeatedControl->setTabIndex($tabIndex);
394
			$this->setAccessKey('');
395
			$this->setTabIndex(0);
396
			$repeatInfo->renderRepeater($writer,$this);
397
			$this->setAccessKey($accessKey);
398
			$this->setTabIndex($tabIndex);
399
			if ($needSpan)
400
				$writer->renderEndTag();
401
		}
402
		//checkbox skipped the client control script in addAttributesToRender
403
		if($this->getEnabled(true)
404
			&& $this->getEnableClientScript()
405
			&& $this->getAutoPostBack()
406
			&& $this->getPage()->getClientSupportsJavaScript())
407
		{
408
			$this->renderClientControlScript($writer);
409
		}
410
	}
411
 
412
	/**
413
	 * Returns a value indicating whether postback has caused the control data change.
414
	 * This method is required by the IPostBackDataHandler interface.
415
	 * @return boolean whether postback has caused the control data change. False if the page is not in postback mode.
416
	 */
417
	public function getDataChanged()
418
	{
419
		return $this->_dataChanged;
420
	}
421
 
422
	/**
423
	 * Returns the value to be validated.
424
	 * This methid is required by IValidatable interface.
425
	 * @return mixed the value of the property to be validated.
426
	 */
427
	public function getValidationPropertyValue()
428
	{
429
		return $this->getSelectedValue();
430
	}
431
 
432
	/**
433
	 * Returns true if this control validated successfully.
434
	 * Defaults to true.
435
	 * @return bool wether this control validated successfully.
436
	 */
437
	public function getIsValid()
438
	{
439
	    return $this->_isValid;
440
	}
441
	/**
442
	 * @param bool wether this control is valid.
443
	 */
444
	public function setIsValid($value)
445
	{
446
	    $this->_isValid=TPropertyValue::ensureBoolean($value);
447
	}
448
 
449
	/**
450
	 * Gets the name of the javascript class responsible for performing postback for this control.
451
	 * This method overrides the parent implementation.
452
	 * @return string the javascript class name
453
	 */
454
	protected function getClientClassName()
455
	{
456
		return 'Prado.WebUI.TCheckBoxList';
457
	}
458
 
459
	/**
460
	 * Gets the post back options for this checkbox.
461
	 * @return array
462
	 */
463
	protected function getPostBackOptions()
464
	{
465
		$options['ListID'] = $this->getClientID();
466
		$options['ValidationGroup'] = $this->getValidationGroup();
467
		$options['CausesValidation'] = $this->getCausesValidation();
468
		$options['ListName'] = $this->getUniqueID();
469
		$options['ItemCount'] = $this->getItemCount();
470
		return $options;
471
	}
472
 
473
}
474