Finished that file name swap.
[isso.git] / PrinterRootTable.php
1 <?php
2 /*=====================================================================*
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright (c)2005-2008 Blue Static
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version 2 of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 /**
23 * Printer Root Element: Table (PrinterRootElementTable.php)
24 *
25 * @package ISSO
26 */
27
28 require_once(ISSO . '/PrinterRootElement.php');
29
30 /**
31 * Printer Root Element: Table
32 *
33 * Represents a <table> object. This can have a parent or not: if it does,
34 * then it will be painted as a child, otherwise it will act as a root.
35 *
36 * @author Blue Static
37 * @copyright Copyright (c)2005 - 2008, Blue Static
38 * @package ISSO
39 *
40 */
41 class BSPrinterRootElementTable extends BSPrinterRootElement
42 {
43 /**
44 * Maximum number of columns
45 * @var integer
46 */
47 private $colspan = 0;
48
49 /**
50 * Heading child elements
51 * @var array
52 */
53 private $headers = array();
54
55 /**
56 * Width of the table
57 * @var string
58 */
59 private $width = '90%';
60
61 // ###################################################################
62 /**
63 * Constructor
64 */
65 public function __construct()
66 {
67 $this->setCssClass('tborder');
68 }
69
70 /**
71 * Makes a new instance of the object in a static fashion
72 *
73 * @return object
74 */
75 public static function make()
76 {
77 $obj = new ReflectionClass(__CLASS__);
78 $args = func_get_args();
79 return $obj->newInstanceArgs($args);
80 }
81
82 // ###################################################################
83 /**
84 * Adds a table row into the child list
85 *
86 * @param BSPrinterTableElement Table element
87 *
88 * @return fluent interface
89 */
90 public function addChild(BSPrinterElement $tr)
91 {
92 if (!$tr instanceof BSPrinterTableElement)
93 {
94 throw new Exception('BSPrinterRootElementTable::addChild() only accepts BSPrinterTableElement objects as children');
95 }
96 $this->children[] = $tr;
97 return $this;
98 }
99
100 // ###################################################################
101 /**
102 * Adds a table element to be a heading of the table. This is still
103 * considered a child, but it goes before all other child elemends
104 *
105 * @param BSPrinterTableElement Child element
106 *
107 * @return fluent interface
108 */
109 public function addHeadingChild(BSPrinterTableElement $tr)
110 {
111 $this->headers[] = $tr;
112 return $this;
113 }
114
115 // ###################################################################
116 /**
117 * Sets the width of the table
118 *
119 * @param string Width value
120 *
121 * @return fluent interface
122 */
123 public function setWidth($width)
124 {
125 $this->width = $width;
126 return $this;
127 }
128
129 // ###################################################################
130 /**
131 * Calculates the number of columns to display based on the colspan
132 * of children elements
133 */
134 private function _calculateColumnCount()
135 {
136 foreach ($this->children as $child)
137 {
138 if ($child->numberOfColumns() > $this->colspan)
139 {
140 $this->colspan = $child->numberOfColumns();
141 }
142 }
143 }
144
145 // ###################################################################
146 /**
147 * Returns the HTML for all printed children elements
148 *
149 * @return string Printed HTML
150 */
151 protected function _paintChildren()
152 {
153 $builder = '';
154
155 $this->children = array_merge($this->headers, $this->children);
156 $this->headers = array();
157
158 $this->_calculateColumnCount();
159
160 foreach ($this->children as $child)
161 {
162 $child->setColumnNumber($this->colspan);
163 $builder .= "\n" . $child->paint();
164 }
165
166 return $builder;
167 }
168
169 // ###################################################################
170 /**
171 * Paints the <table>
172 *
173 * @return string Table HTML code
174 */
175 public function paint()
176 {
177 return "<br />\n<table cellpadding=\"4\" cellspacing=\"1\" border=\"0\" align=\"center\" width=\"" . $this->width . "\"" . $this->_prepareStyle() . ">" . $this->_paintChildren() . "\n</table>";
178 }
179 }
180
181 ?>