]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/framework/Web/UI/WebControls/TCheckBoxList.php
af59545c7eab153367f9420e96597afcbb2d630f
[bacula/bacula] / gui / baculum / framework / Web / UI / WebControls / TCheckBoxList.php
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-2014 PradoSoft
8  * @license http://www.pradosoft.com/license/
9  * @package System.Web.UI.WebControls
10  */
11
12 /**
13  * Includes TListControl class
14  */
15 Prado::using('System.Web.UI.WebControls.TListControl');
16 /**
17  * Includes TRepeatInfo class
18  */
19 Prado::using('System.Web.UI.WebControls.TRepeatInfo');
20 /**
21  * Includes TCheckBox class
22  */
23 Prado::using('System.Web.UI.WebControls.TCheckBox');
24
25 /**
26  * TCheckBoxList class
27  *
28  * TCheckBoxList displays a list of checkboxes on a Web page.
29  *
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.
37  *
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.
41  *
42  * The alignment of the text besides each checkbox can be specified via {@link setTextAlign TextAlign}.
43  *
44  * @author Qiang Xue <qiang.xue@gmail.com>
45  * @package System.Web.UI.WebControls
46  * @since 3.0
47  */
48 class TCheckBoxList extends TListControl implements IRepeatInfoUser, INamingContainer, IPostBackDataHandler,  IValidatable
49 {
50         private $_repeatedControl;
51         private $_isEnabled;
52         private $_changedEventRaised=false;
53         private $_dataChanged=false;
54         private $_isValid=true;
55
56         /**
57          * Constructor.
58          * Remember to call parent implementation if you override this method
59          */
60         public function __construct()
61         {
62                 parent::__construct();
63                 $this->_repeatedControl=$this->createRepeatedControl();
64                 $this->_repeatedControl->setEnableViewState(false);
65                 $this->_repeatedControl->setID('c0');
66                 $this->getControls()->add($this->_repeatedControl);
67         }
68
69         /**
70          * Creates a control used for repetition (used as a template).
71          * @return TControl the control to be repeated
72          */
73         protected function createRepeatedControl()
74         {
75                 return new TCheckBox;
76         }
77
78         /**
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
84          */
85         public function findControl($id, $real=false)
86         {
87                 if ($real===true)
88                         return parent::findControl($id);
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          * @param boolean whether the control is to be enabled.
260          */
261         public function setEnabled($value)
262         {
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())
272                 {
273                         $items = $this->getItems();
274                         $cs = $this->getPage()->getCallbackClient();
275                         $baseClientID = $this->getClientID().'_c';
276                         foreach($items as $index=>$item)
277                         {
278                                 $cs->setAttribute($baseClientID.$index, 'disabled', $value);
279                         }
280                 }
281         }
282
283         /**
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
288          * @return null
289          */
290         public function generateItemStyle($itemType,$index)
291         {
292                 return null;
293         }
294
295         /**
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
302          */
303         public function renderItem($writer,$repeatInfo,$itemType,$index)
304         {
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);
318         }
319
320         /**
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
326          */
327         public function loadPostData($key,$values)
328         {
329                 if($this->getEnabled(true))
330                 {
331                         $index=(int)substr($key,strlen($this->getUniqueID())+2);
332                         $this->ensureDataBound();
333                         if($index>=0 && $index<$this->getItemCount())
334                         {
335                                 $item=$this->getItems()->itemAt($index);
336                                 if($item->getEnabled())
337                                 {
338                                         $checked=isset($values[$key]);
339                                         if($item->getSelected()!==$checked)
340                                         {
341                                                 $item->setSelected($checked);
342                                                 if(!$this->_changedEventRaised)
343                                                 {
344                                                         $this->_changedEventRaised=true;
345                                                         return $this->_dataChanged=true;
346                                                 }
347                                         }
348                                 }
349                         }
350                 }
351                 return false;
352         }
353
354         /**
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.
360          */
361         public function raisePostDataChangedEvent()
362         {
363                 if($this->getAutoPostBack() && $this->getCausesValidation())
364                         $this->getPage()->validate($this->getValidationGroup());
365                 $this->onSelectedIndexChanged(null);
366         }
367
368         /**
369          * Registers for post data on postback.
370          * This method overrides the parent implementation.
371          * @param mixed event parameter
372          */
373         public function onPreRender($param)
374         {
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();
381                 for($i=0;$i<$n;++$i)
382                 {
383                         $this->_repeatedControl->setID("c$i");
384                         $page->registerRequiresPostData($this->_repeatedControl);
385                 }
386         }
387
388         /**
389          * Wether the list should be rendered inside a span or not
390          *
391          *@return boolean true if we need a span
392          */
393         protected function getSpanNeeded ()
394         {
395                 return $this->getRepeatLayout()===TRepeatLayout::Raw;
396         }
397
398         /**
399          * Renders the checkbox list control.
400          * This method overrides the parent implementation.
401          * @param THtmlWriter writer for rendering purpose.
402          */
403         public function render($writer)
404         {
405                 if($this->getItemCount()>0)
406                 {
407                         if ($needSpan=$this->getSpanNeeded())
408                         {
409                                 $writer->addAttribute('id', $this->getClientId());
410                                 $writer->renderBeginTag('span');
411                         }
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);
425                         if ($needSpan)
426                                 $writer->renderEndTag();
427                 }
428
429                 //checkbox skipped the client control script in addAttributesToRender
430                 if($this->getEnabled(true)
431                         && $this->getEnableClientScript()
432                         && $this->getAutoPostBack()
433                         && $this->getPage()->getClientSupportsJavaScript())
434                 {
435                         $this->renderClientControlScript($writer);
436                 }
437         }
438
439         /**
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.
443          */
444         public function getDataChanged()
445         {
446                 return $this->_dataChanged;
447         }
448
449         /**
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.
453          */
454         public function getValidationPropertyValue()
455         {
456                 return $this->getSelectedValue();
457         }
458
459         /**
460          * Returns true if this control validated successfully.
461          * Defaults to true.
462          * @return bool wether this control validated successfully.
463          */
464         public function getIsValid()
465         {
466             return $this->_isValid;
467         }
468         /**
469          * @param bool wether this control is valid.
470          */
471         public function setIsValid($value)
472         {
473             $this->_isValid=TPropertyValue::ensureBoolean($value);
474         }
475
476         /**
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
480          */
481         protected function getClientClassName()
482         {
483                 return 'Prado.WebUI.TCheckBoxList';
484         }
485
486         /**
487          * Gets the post back options for this checkbox.
488          * @return array
489          */
490         protected function getPostBackOptions()
491         {
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();
497                 return $options;
498         }
499
500 }
501