]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/framework/Web/UI/WebControls/TBoundColumn.php
baculum: New Baculum API and Baculum Web
[bacula/bacula] / gui / baculum / framework / Web / UI / WebControls / TBoundColumn.php
1 <?php
2 /**
3  * TBoundColumn class file
4  *
5  * @author Qiang Xue <qiang.xue@gmail.com>
6  * @link https://github.com/pradosoft/prado
7  * @copyright Copyright &copy; 2005-2016 The PRADO Group
8  * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
9  * @package System.Web.UI.WebControls
10  */
11
12 /**
13  * TDataGridColumn class file
14  */
15 Prado::using('System.Web.UI.WebControls.TDataGridColumn');
16
17 /**
18  * TBoundColumn class
19  *
20  * TBoundColumn represents a column that is bound to a field in a data source.
21  * The cells in the column will be displayed using the data indexed by
22  * {@link setDataField DataField}. You can customize the display by
23  * setting {@link setDataFormatString DataFormatString}.
24  *
25  * If {@link setReadOnly ReadOnly} is false, TBoundColumn will display cells in edit mode
26  * with textboxes. Otherwise, a static text is displayed.
27  *
28  * When a datagrid row is in edit mode, the textbox control in the TBoundColumn
29  * can be accessed by one of the following two methods:
30  * <code>
31  * $datagridItem->BoundColumnID->TextBox
32  * $datagridItem->BoundColumnID->Controls[0]
33  * </code>
34  * The second method is possible because the textbox control created within the
35  * datagrid cell is the first child.
36  *
37  * Since v3.1.0, TBoundColumn has introduced two new properties {@link setItemRenderer ItemRenderer}
38  * and {@link setEditItemRenderer EditItemRenderer} which can be used to specify
39  * the layout of the datagrid cells in browsing and editing mode.
40  * A renderer refers to a control class that is to be instantiated as a control.
41  * For more details, see {@link TRepeater} and {@link TDataList}.
42  *
43  * @author Qiang Xue <qiang.xue@gmail.com>
44  * @package System.Web.UI.WebControls
45  * @since 3.0
46  */
47 class TBoundColumn extends TDataGridColumn
48 {
49         /**
50          * @return string the class name for the item cell renderer. Defaults to empty, meaning not set.
51          * @since 3.1.0
52          */
53         public function getItemRenderer()
54         {
55                 return $this->getViewState('ItemRenderer','');
56         }
57
58         /**
59          * Sets the item cell renderer class.
60          *
61          * If not empty, the class will be used to instantiate as a child control in the item cells of the column.
62          *
63          * If the class implements {@link IDataRenderer}, the <b>Data</b> property
64          * will be set as the data associated with the datagrid cell during databinding.
65          * The data can be either the whole data row or a field of the row if
66          * {@link getDataField DataField} is not empty. If {@link getDataFormatString DataFormatString}
67          * is not empty, the data will be formatted first before passing to the renderer.
68          *
69          * @param string the renderer class name in namespace format.
70          * @since 3.1.0
71          */
72         public function setItemRenderer($value)
73         {
74                 $this->setViewState('ItemRenderer',$value,'');
75         }
76
77         /**
78          * @return string the class name for the edit item cell renderer. Defaults to empty, meaning not set.
79          * @since 3.1.0
80          */
81         public function getEditItemRenderer()
82         {
83                 return $this->getViewState('EditItemRenderer','');
84         }
85
86         /**
87          * Sets the edit item cell renderer class.
88          *
89          * If not empty, the class will be used to instantiate as a child control in the item cell that is in edit mode.
90          *
91          * If the class implements {@link IDataRenderer}, the <b>Data</b> property
92          * will be set as the data associated with the datagrid cell during databinding.
93          * The data can be either the whole data row or a field of the row if
94          * {@link getDataField DataField} is not empty. If {@link getDataFormatString DataFormatString}
95          * is not empty, the data will be formatted first before passing to the renderer.
96          *
97          * @param string the renderer class name in namespace format.
98          * @since 3.1.0
99          */
100         public function setEditItemRenderer($value)
101         {
102                 $this->setViewState('EditItemRenderer',$value,'');
103         }
104
105         /**
106          * @return string the field name from the data source to bind to the column
107          */
108         public function getDataField()
109         {
110                 return $this->getViewState('DataField','');
111         }
112
113         /**
114          * @param string the field name from the data source to bind to the column
115          */
116         public function setDataField($value)
117         {
118                 $this->setViewState('DataField',$value,'');
119         }
120
121         /**
122          * @return string the formatting string used to control how the bound data will be displayed.
123          */
124         public function getDataFormatString()
125         {
126                 return $this->getViewState('DataFormatString','');
127         }
128
129         /**
130          * @param string the formatting string used to control how the bound data will be displayed.
131          */
132         public function setDataFormatString($value)
133         {
134                 $this->setViewState('DataFormatString',$value,'');
135         }
136
137         /**
138          * @return boolean whether the items in the column can be edited. Defaults to false.
139          */
140         public function getReadOnly()
141         {
142                 return $this->getViewState('ReadOnly',false);
143         }
144
145         /**
146          * @param boolean whether the items in the column can be edited
147          */
148         public function setReadOnly($value)
149         {
150                 $this->setViewState('ReadOnly',TPropertyValue::ensureBoolean($value),false);
151         }
152
153         /**
154          * Initializes the specified cell to its initial values.
155          * This method overrides the parent implementation.
156          * It creates a textbox for item in edit mode and the column is not read-only.
157          * Otherwise it displays a static text.
158          * The caption of the button and the static text are retrieved
159          * from the datasource.
160          * @param TTableCell the cell to be initialized.
161          * @param integer the index to the Columns property that the cell resides in.
162          * @param string the type of cell (Header,Footer,Item,AlternatingItem,EditItem,SelectedItem)
163          */
164         public function initializeCell($cell,$columnIndex,$itemType)
165         {
166                 $item=$cell->getParent();
167                 switch($itemType)
168                 {
169                         case TListItemType::Item:
170                         case TListItemType::AlternatingItem:
171                         case TListItemType::SelectedItem:
172                                 if(($classPath=$this->getItemRenderer())!=='')
173                                 {
174                                         $control=Prado::createComponent($classPath);
175                                         if($control instanceof IItemDataRenderer)
176                                         {
177                                                 $control->setItemIndex($item->getItemIndex());
178                                                 $control->setItemType($item->getItemType());
179                                         }
180                                         $cell->getControls()->add($control);
181                                 }
182                                 else
183                                         $control=$cell;
184                                 $control->attachEventHandler('OnDataBinding',array($this,'dataBindColumn'));
185                                 break;
186                         case TListItemType::EditItem:
187                                 if(!$this->getReadOnly())
188                                 {
189                                         if(($classPath=$this->getEditItemRenderer())!=='')
190                                         {
191                                                 $control=Prado::createComponent($classPath);
192                                                 if($control instanceof IItemDataRenderer)
193                                                 {
194                                                         $control->setItemIndex($item->getItemIndex());
195                                                         $control->setItemType($item->getItemType());
196                                                 }
197                                                 $cell->getControls()->add($control);
198                                                 $cell->registerObject('EditControl',$control);
199                                         }
200                                         else
201                                         {
202                                                 $control=Prado::createComponent('System.Web.UI.WebControls.TTextBox');
203                                                 $cell->getControls()->add($control);
204                                                 $cell->registerObject('TextBox',$control);
205                                         }
206                                 }
207                                 else
208                                 {
209                                         if(($classPath=$this->getItemRenderer())!=='')
210                                         {
211                                                 $control=Prado::createComponent($classPath);
212                                                 if($control instanceof IItemDataRenderer)
213                                                 {
214                                                         $control->setItemIndex($item->getItemIndex());
215                                                         $control->setItemType($item->getItemType());
216                                                 }
217                                                 $cell->getControls()->add($control);
218                                         }
219                                         else
220                                                 $control=$cell;
221                                 }
222                                 $control->attachEventHandler('OnDataBinding',array($this,'dataBindColumn'));
223                                 break;
224                         default:
225                                 parent::initializeCell($cell,$columnIndex,$itemType);
226                                 break;
227                 }
228         }
229
230         /**
231          * Databinds a cell in the column.
232          * This method is invoked when datagrid performs databinding.
233          * It populates the content of the cell with the relevant data from data source.
234          */
235         public function dataBindColumn($sender,$param)
236         {
237                 $item=$sender->getNamingContainer();
238                 $data=$item->getData();
239                 $formatString=$this->getDataFormatString();
240                 if(($field=$this->getDataField())!=='')
241                         $value=$this->formatDataValue($formatString,$this->getDataFieldValue($data,$field));
242                 else
243                         $value=$this->formatDataValue($formatString,$data);
244                 $sender->setData($value);
245         }
246 }
247