3 * TCheckBoxList class file
5 * @author Qiang Xue <qiang.xue@gmail.com>
6 * @link http://www.pradosoft.com/
7 * @copyright Copyright © 2005-2014 PradoSoft
8 * @license http://www.pradosoft.com/license/
9 * @package System.Web.UI.WebControls
13 * Includes TListControl class
15 Prado::using('System.Web.UI.WebControls.TListControl');
17 * Includes TRepeatInfo class
19 Prado::using('System.Web.UI.WebControls.TRepeatInfo');
21 * Includes TCheckBox class
23 Prado::using('System.Web.UI.WebControls.TCheckBox');
28 * TCheckBoxList displays a list of checkboxes on a Web page.
30 * The layout of the checkbox list is specified via {@link setRepeatLayout RepeatLayout},
31 * which can be either 'Table' (default) or 'Flow'.
32 * A table layout uses HTML table cells to organize the checkboxes while
33 * a flow layout uses line breaks to organize the checkboxes.
34 * When the layout is using 'Table', {@link setCellPadding CellPadding} and
35 * {@link setCellSpacing CellSpacing} can be used to adjust the cellpadding and
36 * cellpadding of the table.
38 * The number of columns used to display the checkboxes is specified via
39 * {@link setRepeatColumns RepeatColumns} property, while the {@link setRepeatDirection RepeatDirection}
40 * governs the order of the items being rendered.
42 * The alignment of the text besides each checkbox can be specified via {@link setTextAlign TextAlign}.
44 * @author Qiang Xue <qiang.xue@gmail.com>
45 * @package System.Web.UI.WebControls
48 class TCheckBoxList extends TListControl implements IRepeatInfoUser, INamingContainer, IPostBackDataHandler, IValidatable
50 private $_repeatedControl;
52 private $_changedEventRaised=false;
53 private $_dataChanged=false;
54 private $_isValid=true;
58 * Remember to call parent implementation if you override this method
60 public function __construct()
62 parent::__construct();
63 $this->_repeatedControl=$this->createRepeatedControl();
64 $this->_repeatedControl->setEnableViewState(false);
65 $this->_repeatedControl->setID('c0');
66 $this->getControls()->add($this->_repeatedControl);
70 * Creates a control used for repetition (used as a template).
71 * @return TControl the control to be repeated
73 protected function createRepeatedControl()
79 * Finds a control by ID.
80 * This method overrides the parent implementation so that it always returns
81 * the checkbox list itself (because the checkbox list does not have child controls.)
82 * @param string control ID
83 * @return TControl control being found
85 public function findControl($id, $real=false)
88 return parent::findControl($id);
93 * @return boolean whether this control supports multiple selection. Always true for checkbox list.
95 protected function getIsMultiSelect()
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
105 protected function createStyle()
107 return new TTableStyle;
111 * @return TTextAlign the alignment of the text caption, defaults to TTextAlign::Right.
113 public function getTextAlign()
115 return $this->getViewState('TextAlign',TTextAlign::Right);
119 * @param TTextAlign the text alignment of the checkboxes
121 public function setTextAlign($value)
123 $this->setViewState('TextAlign',TPropertyValue::ensureEnum($value,'TTextAlign'),TTextAlign::Right);
128 * @return TRepeatInfo repeat information (primarily used by control developers)
130 protected function getRepeatInfo()
132 if(($repeatInfo=$this->getViewState('RepeatInfo',null))===null)
134 $repeatInfo=new TRepeatInfo;
135 $this->setViewState('RepeatInfo',$repeatInfo,null);
141 * @return integer the number of columns that the list should be displayed with. Defaults to 0 meaning not set.
143 public function getRepeatColumns()
145 return $this->getRepeatInfo()->getRepeatColumns();
149 * @param integer the number of columns that the list should be displayed with.
151 public function setRepeatColumns($value)
153 $this->getRepeatInfo()->setRepeatColumns($value);
157 * @return string the direction of traversing the list, defaults to 'Vertical'
159 public function getRepeatDirection()
161 return $this->getRepeatInfo()->getRepeatDirection();
165 * @param string the direction (Vertical, Horizontal) of traversing the list
167 public function setRepeatDirection($value)
169 $this->getRepeatInfo()->setRepeatDirection($value);
173 * @return string how the list should be displayed, using table or using line breaks. Defaults to 'Table'.
175 public function getRepeatLayout()
177 return $this->getRepeatInfo()->getRepeatLayout();
181 * @param string how the list should be displayed, using table or using line breaks (Table, Flow)
183 public function setRepeatLayout($value)
185 $this->getRepeatInfo()->setRepeatLayout($value);
189 * @return integer the cellspacing for the table keeping the checkbox list. Defaults to -1, meaning not set.
191 public function getCellSpacing()
193 if($this->getHasStyle())
194 return $this->getStyle()->getCellSpacing();
200 * Sets the cellspacing for the table keeping the checkbox list.
201 * @param integer the cellspacing for the table keeping the checkbox list.
203 public function setCellSpacing($value)
205 $this->getStyle()->setCellSpacing($value);
209 * @return integer the cellpadding for the table keeping the checkbox list. Defaults to -1, meaning not set.
211 public function getCellPadding()
213 if($this->getHasStyle())
214 return $this->getStyle()->getCellPadding();
220 * Sets the cellpadding for the table keeping the checkbox list.
221 * @param integer the cellpadding for the table keeping the checkbox list.
223 public function setCellPadding($value)
225 $this->getStyle()->setCellPadding($value);
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.
233 public function getHasHeader()
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.
243 public function getHasFooter()
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.
253 public function getHasSeparators()
259 * @param boolean whether the control is to be enabled.
261 public function setEnabled($value)
263 parent::setEnabled($value);
264 $value = !TPropertyValue::ensureBoolean($value);
265 // if this is an active control,
266 // and it's a callback,
267 // and we can update clientside,
268 // then update the 'disabled' attribute of the items.
269 if(($this instanceof IActiveControl) &&
270 $this->getPage()->getIsCallBack() &&
271 $this->getActiveControl()->canUpdateClientSide())
273 $items = $this->getItems();
274 $cs = $this->getPage()->getCallbackClient();
275 $baseClientID = $this->getClientID().'_c';
276 foreach($items as $index=>$item)
278 $cs->setAttribute($baseClientID.$index, 'disabled', $value);
284 * Returns a style used for rendering items.
285 * This method is required by {@link IRepeatInfoUser} interface.
286 * @param string item type (Header,Footer,Item,AlternatingItem,SelectedItem,EditItem,Separator,Pager)
287 * @param integer index of the item being rendered
290 public function generateItemStyle($itemType,$index)
296 * Renders an item in the list.
297 * This method is required by {@link IRepeatInfoUser} interface.
298 * @param THtmlWriter writer for rendering purpose
299 * @param TRepeatInfo repeat information
300 * @param string item type (Header,Footer,Item,AlternatingItem,SelectedItem,EditItem,Separator,Pager)
301 * @param integer zero-based index of the item in the item list
303 public function renderItem($writer,$repeatInfo,$itemType,$index)
305 $repeatedControl=$this->_repeatedControl;
306 $item=$this->getItems()->itemAt($index);
307 if($item->getHasAttributes())
308 $repeatedControl->getAttributes()->copyFrom($item->getAttributes());
309 else if($repeatedControl->getHasAttributes())
310 $repeatedControl->getAttributes()->clear();
311 $repeatedControl->setID("c$index");
312 $repeatedControl->setText($item->getText());
313 $repeatedControl->setChecked($item->getSelected());
314 $repeatedControl->setAttribute('value',$item->getValue());
315 $repeatedControl->setEnabled($this->_isEnabled && $item->getEnabled());
316 $repeatedControl->setEnableClientScript(false);
317 $repeatedControl->renderControl($writer);
321 * Loads user input data.
322 * This method is primarly used by framework developers.
323 * @param string the key that can be used to retrieve data from the input data collection
324 * @param array the input data collection
325 * @return boolean whether the data of the control has been changed
327 public function loadPostData($key,$values)
329 if($this->getEnabled(true))
331 $index=(int)substr($key,strlen($this->getUniqueID())+2);
332 $this->ensureDataBound();
333 if($index>=0 && $index<$this->getItemCount())
335 $item=$this->getItems()->itemAt($index);
336 if($item->getEnabled())
338 $checked=isset($values[$key]);
339 if($item->getSelected()!==$checked)
341 $item->setSelected($checked);
342 if(!$this->_changedEventRaised)
344 $this->_changedEventRaised=true;
345 return $this->_dataChanged=true;
355 * Raises postdata changed event.
356 * This method is required by {@link IPostBackDataHandler} interface.
357 * It is invoked by the framework when {@link getSelectedIndices SelectedIndices} property
358 * is changed on postback.
359 * This method is primarly used by framework developers.
361 public function raisePostDataChangedEvent()
363 if($this->getAutoPostBack() && $this->getCausesValidation())
364 $this->getPage()->validate($this->getValidationGroup());
365 $this->onSelectedIndexChanged(null);
369 * Registers for post data on postback.
370 * This method overrides the parent implementation.
371 * @param mixed event parameter
373 public function onPreRender($param)
375 parent::onPreRender($param);
376 $this->_repeatedControl->setAutoPostBack($this->getAutoPostBack());
377 $this->_repeatedControl->setCausesValidation($this->getCausesValidation());
378 $this->_repeatedControl->setValidationGroup($this->getValidationGroup());
379 $page=$this->getPage();
380 $n=$this->getItemCount();
383 $this->_repeatedControl->setID("c$i");
384 $page->registerRequiresPostData($this->_repeatedControl);
389 * Wether the list should be rendered inside a span or not
391 *@return boolean true if we need a span
393 protected function getSpanNeeded ()
395 return $this->getRepeatLayout()===TRepeatLayout::Raw;
399 * Renders the checkbox list control.
400 * This method overrides the parent implementation.
401 * @param THtmlWriter writer for rendering purpose.
403 public function render($writer)
405 if($this->getItemCount()>0)
407 if ($needSpan=$this->getSpanNeeded())
409 $writer->addAttribute('id', $this->getClientId());
410 $writer->renderBeginTag('span');
412 $this->_isEnabled=$this->getEnabled(true);
413 $repeatInfo=$this->getRepeatInfo();
414 $accessKey=$this->getAccessKey();
415 $tabIndex=$this->getTabIndex();
416 $this->_repeatedControl->setTextAlign($this->getTextAlign());
417 $this->_repeatedControl->setAccessKey($accessKey);
418 $this->_repeatedControl->setTabIndex($tabIndex);
419 $this->setAccessKey('');
420 $this->setTabIndex(0);
421 $this->addAttributesToRender($writer);
422 $repeatInfo->renderRepeater($writer,$this);
423 $this->setAccessKey($accessKey);
424 $this->setTabIndex($tabIndex);
426 $writer->renderEndTag();
429 //checkbox skipped the client control script in addAttributesToRender
430 if($this->getEnabled(true)
431 && $this->getEnableClientScript()
432 && $this->getAutoPostBack()
433 && $this->getPage()->getClientSupportsJavaScript())
435 $this->renderClientControlScript($writer);
440 * Returns a value indicating whether postback has caused the control data change.
441 * This method is required by the IPostBackDataHandler interface.
442 * @return boolean whether postback has caused the control data change. False if the page is not in postback mode.
444 public function getDataChanged()
446 return $this->_dataChanged;
450 * Returns the value to be validated.
451 * This methid is required by IValidatable interface.
452 * @return mixed the value of the property to be validated.
454 public function getValidationPropertyValue()
456 return $this->getSelectedValue();
460 * Returns true if this control validated successfully.
462 * @return bool wether this control validated successfully.
464 public function getIsValid()
466 return $this->_isValid;
469 * @param bool wether this control is valid.
471 public function setIsValid($value)
473 $this->_isValid=TPropertyValue::ensureBoolean($value);
477 * Gets the name of the javascript class responsible for performing postback for this control.
478 * This method overrides the parent implementation.
479 * @return string the javascript class name
481 protected function getClientClassName()
483 return 'Prado.WebUI.TCheckBoxList';
487 * Gets the post back options for this checkbox.
490 protected function getPostBackOptions()
492 $options['ListID'] = $this->getClientID();
493 $options['ValidationGroup'] = $this->getValidationGroup();
494 $options['CausesValidation'] = $this->getCausesValidation();
495 $options['ListName'] = $this->getUniqueID();
496 $options['ItemCount'] = $this->getItemCount();