]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/framework/Web/UI/WebControls/TTable.php
4102cd4bccc6126c25b3c1ee8010930929e85909
[bacula/bacula] / gui / baculum / framework / Web / UI / WebControls / TTable.php
1 <?php
2 /**
3  * TTable and TTableRowCollection 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 TTableRow class
14  */
15 Prado::using('System.Web.UI.WebControls.TTableRow');
16
17 /**
18  * TTable class
19  *
20  * TTable displays an HTML table on a Web page.
21  *
22  * A table may have {@link setCaption Caption}, whose alignment is specified
23  * via {@link setCaptionAlign CaptionAlign}. The table cellpadding and cellspacing
24  * are specified via {@link setCellPadding CellPadding} and {@link setCellSpacing CellSpacing}
25  * properties, respectively. The {@link setGridLines GridLines} specifies how
26  * the table should display its borders. The horizontal alignment of the table
27  * content can be specified via {@link setHorizontalAlign HorizontalAlign},
28  * and {@link setBackImageUrl BackImageUrl} can assign a background image to the table.
29  *
30  * A TTable maintains a list of {@link TTableRow} controls in its
31  * {@link getRows Rows} property. Each {@link TTableRow} represents
32  * an HTML table row.
33  *
34  * To populate the table {@link getRows Rows}, you may either use control template
35  * or dynamically create {@link TTableRow} in code.
36  * In template, do as follows to create the table rows and cells,
37  * <code>
38  *   <com:TTable>
39  *     <com:TTableRow>
40  *       <com:TTableCell Text="content" />
41  *       <com:TTableCell Text="content" />
42  *     </com:TTableRow>
43  *     <com:TTableRow>
44  *       <com:TTableCell Text="content" />
45  *       <com:TTableCell Text="content" />
46  *     </com:TTableRow>
47  *   </com:TTable>
48  * </code>
49  * The above can also be accomplished in code as follows,
50  * <code>
51  *   $table=new TTable;
52  *   $row=new TTableRow;
53  *   $cell=new TTableCell; $cell->Text="content"; $row->Cells->add($cell);
54  *   $cell=new TTableCell; $cell->Text="content"; $row->Cells->add($cell);
55  *   $table->Rows->add($row);
56  *   $row=new TTableRow;
57  *   $cell=new TTableCell; $cell->Text="content"; $row->Cells->add($cell);
58  *   $cell=new TTableCell; $cell->Text="content"; $row->Cells->add($cell);
59  *   $table->Rows->add($row);
60  * </code>
61  *
62  * @author Qiang Xue <qiang.xue@gmail.com>
63  * @package System.Web.UI.WebControls
64  * @since 3.0
65  */
66 class TTable extends TWebControl
67 {
68         /**
69          * @return string tag name for the table
70          */
71         protected function getTagName()
72         {
73                 return 'table';
74         }
75
76         /**
77          * Adds object parsed from template to the control.
78          * This method adds only {@link TTableRow} objects into the {@link getRows Rows} collection.
79          * All other objects are ignored.
80          * @param mixed object parsed from template
81          */
82         public function addParsedObject($object)
83         {
84                 if($object instanceof TTableRow)
85                         $this->getRows()->add($object);
86         }
87
88         /**
89          * Creates a style object for the control.
90          * This method creates a {@link TTableStyle} to be used by the table.
91          * @return TTableStyle control style to be used
92          */
93         protected function createStyle()
94         {
95                 return new TTableStyle;
96         }
97
98         /**
99          * Adds attributes to renderer.
100          * @param THtmlWriter the renderer
101          */
102         protected function addAttributesToRender($writer)
103         {
104                 parent::addAttributesToRender($writer);
105                 $border=0;
106                 if($this->getHasStyle())
107                 {
108                         if($this->getGridLines()!==TTableGridLines::None)
109                         {
110                                 if(($border=$this->getBorderWidth())==='')
111                                         $border=1;
112                                 else
113                                         $border=(int)$border;
114                         }
115                 }
116                 $writer->addAttribute('border',"$border");
117         }
118
119         /**
120          * Creates a control collection object that is to be used to hold child controls
121          * @return TTableRowCollection control collection
122          * @see getControls
123          */
124         protected function createControlCollection()
125         {
126                 return new TTableRowCollection($this);
127         }
128
129         /**
130          * @return TTableRowCollection list of {@link TTableRow} controls
131          */
132         public function getRows()
133         {
134                 return $this->getControls();
135         }
136
137         /**
138          * @return string table caption
139          */
140         public function getCaption()
141         {
142                 return $this->getViewState('Caption','');
143         }
144
145         /**
146          * @param string table caption
147          */
148         public function setCaption($value)
149         {
150                 $this->setViewState('Caption',$value,'');
151         }
152
153         /**
154          * @return TTableCaptionAlign table caption alignment. Defaults to TTableCaptionAlign::NotSet.
155          */
156         public function getCaptionAlign()
157         {
158                 return $this->getViewState('CaptionAlign',TTableCaptionAlign::NotSet);
159         }
160
161         /**
162          * @param TTableCaptionAlign table caption alignment.
163          */
164         public function setCaptionAlign($value)
165         {
166                 $this->setViewState('CaptionAlign',TPropertyValue::ensureEnum($value,'TTableCaptionAlign'),TTableCaptionAlign::NotSet);
167         }
168
169         /**
170          * @return integer the cellspacing for the table. Defaults to -1, meaning not set.
171          */
172         public function getCellSpacing()
173         {
174                 if($this->getHasStyle())
175                         return $this->getStyle()->getCellSpacing();
176                 else
177                         return -1;
178         }
179
180         /**
181          * @param integer the cellspacing for the table. Defaults to -1, meaning not set.
182          */
183         public function setCellSpacing($value)
184         {
185                 $this->getStyle()->setCellSpacing($value);
186         }
187
188         /**
189          * @return integer the cellpadding for the table. Defaults to -1, meaning not set.
190          */
191         public function getCellPadding()
192         {
193                 if($this->getHasStyle())
194                         return $this->getStyle()->getCellPadding();
195                 else
196                         return -1;
197         }
198
199         /**
200          * @param integer the cellpadding for the table. Defaults to -1, meaning not set.
201          */
202         public function setCellPadding($value)
203         {
204                 $this->getStyle()->setCellPadding($value);
205         }
206
207         /**
208          * @return THorizontalAlign the horizontal alignment of the table content. Defaults to THorizontalAlign::NotSet.
209          */
210         public function getHorizontalAlign()
211         {
212                 if($this->getHasStyle())
213                         return $this->getStyle()->getHorizontalAlign();
214                 else
215                         return THorizontalAlign::NotSet;
216         }
217
218         /**
219          * @param THorizontalAlign the horizontal alignment of the table content.
220          */
221         public function setHorizontalAlign($value)
222         {
223                 $this->getStyle()->setHorizontalAlign($value);
224         }
225
226         /**
227          * @return TTableGridLines the grid line setting of the table. Defaults to TTableGridLines::None.
228          */
229         public function getGridLines()
230         {
231                 if($this->getHasStyle())
232                         return $this->getStyle()->getGridLines();
233                 else
234                         return TTableGridLines::None;
235         }
236
237         /**
238          * @param TTableGridLines the grid line setting of the table
239          */
240         public function setGridLines($value)
241         {
242                 $this->getStyle()->setGridLines($value);
243         }
244
245         /**
246          * @return string the URL of the background image for the table
247          */
248         public function getBackImageUrl()
249         {
250                 if($this->getHasStyle())
251                         return $this->getStyle()->getBackImageUrl();
252                 else
253                         return '';
254         }
255
256         /**
257          * Sets the URL of the background image for the table
258          * @param string the URL
259          */
260         public function setBackImageUrl($value)
261         {
262                 $this->getStyle()->setBackImageUrl($value);
263         }
264
265         /**
266          * Renders the openning tag for the table control which will render table caption if present.
267          * @param THtmlWriter the writer used for the rendering purpose
268          */
269         public function renderBeginTag($writer)
270         {
271                 parent::renderBeginTag($writer);
272                 if(($caption=$this->getCaption())!=='')
273                 {
274                         if(($align=$this->getCaptionAlign())!==TTableCaptionAlign::NotSet)
275                                 $writer->addAttribute('align',strtolower($align));
276                         $writer->renderBeginTag('caption');
277                         $writer->write($caption);
278                         $writer->renderEndTag();
279                 }
280         }
281
282         /**
283          * Renders body contents of the table.
284          * @param THtmlWriter the writer used for the rendering purpose.
285          */
286         public function renderContents($writer)
287         {
288                 if($this->getHasControls())
289                 {
290                         $renderTableSection=false;
291                         foreach($this->getControls() as $row)
292                         {
293                                 if($row->getTableSection()!==TTableRowSection::Body)
294                                 {
295                                         $renderTableSection=true;
296                                         break;
297                                 }
298                         }
299                         if($renderTableSection)
300                         {
301                                 $currentSection=TTableRowSection::Header;
302                                 $writer->writeLine();
303                                 foreach($this->getControls() as $index=>$row)
304                                 {
305                                         if(($section=$row->getTableSection())===$currentSection)
306                                         {
307                                                 if($index===0 && $currentSection===TTableRowSection::Header)
308                                                         $writer->renderBeginTag('thead');
309                                         }
310                                         else
311                                         {
312                                                 if($currentSection===TTableRowSection::Header)
313                                                 {
314                                                         if($index>0)
315                                                                 $writer->renderEndTag();
316                                                         if($section===TTableRowSection::Body)
317                                                                 $writer->renderBeginTag('tbody');
318                                                         else
319                                                                 $writer->renderBeginTag('tfoot');
320                                                         $currentSection=$section;
321                                                 }
322                                                 else if($currentSection===TTableRowSection::Body)
323                                                 {
324                                                         $writer->renderEndTag();
325                                                         if($section===TTableRowSection::Footer)
326                                                                 $writer->renderBeginTag('tfoot');
327                                                         else
328                                                                 throw new TConfigurationException('table_tablesection_outoforder');
329                                                         $currentSection=$section;
330                                                 }
331                                                 else // Footer
332                                                         throw new TConfigurationException('table_tablesection_outoforder');
333                                         }
334                                         $row->renderControl($writer);
335                                         $writer->writeLine();
336                                 }
337                                 $writer->renderEndTag();
338                         }
339                         else
340                         {
341                                 $writer->writeLine();
342                                 foreach($this->getControls() as $row)
343                                 {
344                                         $row->renderControl($writer);
345                                         $writer->writeLine();
346                                 }
347                         }
348                 }
349         }
350 }
351
352
353 /**
354  * TTableRowCollection class.
355  *
356  * TTableRowCollection is used to maintain a list of rows belong to a table.
357  *
358  * @author Qiang Xue <qiang.xue@gmail.com>
359  * @package System.Web.UI.WebControls
360  * @since 3.0
361  */
362 class TTableRowCollection extends TControlCollection
363 {
364         /**
365          * Inserts an item at the specified position.
366          * This overrides the parent implementation by performing additional
367          * operations for each newly added table row.
368          * @param integer the speicified position.
369          * @param mixed new item
370          * @throws TInvalidDataTypeException if the item to be inserted is not a TTableRow object.
371          */
372         public function insertAt($index,$item)
373         {
374                 if($item instanceof TTableRow)
375                         parent::insertAt($index,$item);
376                 else
377                         throw new TInvalidDataTypeException('tablerowcollection_tablerow_required');
378         }
379 }
380
381
382 /**
383  * TTableCaptionAlign class.
384  * TTableCaptionAlign defines the enumerable type for the possible alignments
385  * that a table caption can take.
386  *
387  * The following enumerable values are defined:
388  * - NotSet: alignment not specified
389  * - Top: top aligned
390  * - Bottom: bottom aligned
391  * - Left: left aligned
392  * - Right: right aligned
393  *
394  * @author Qiang Xue <qiang.xue@gmail.com>
395  * @package System.Web.UI.WebControls
396  * @since 3.0.4
397  */
398 class TTableCaptionAlign extends TEnumerable
399 {
400         const NotSet='NotSet';
401         const Top='Top';
402         const Bottom='Bottom';
403         const Left='Left';
404         const Right='Right';
405 }
406