Use (c) instead of the actual copyright symbol to avoid the really annoying character...
[isso.git] / PrinterTableElement.php
1 <?php
2 /*=====================================================================*
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright (c)2002-2007 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 Table Element (PrinterTableElement.php)
24 *
25 * @package ISSO
26 */
27
28 require_once('ISSO/PrinterRootElementTable.php');
29 require_once('ISSO/PrinterBaseElement.php');
30
31 /**
32 * Printer Table Element
33 *
34 * This represents a table row that holds elements.
35 *
36 * @author Blue Static
37 * @copyright Copyright (c)2002 - 2007, Blue Static
38 * @package ISSO
39 *
40 */
41 class BSPrinterTableElement extends BSPrinterElement
42 {
43 /**
44 * Array of child nodes
45 * @var array
46 */
47 private $children = array();
48
49 /**
50 * Number of columns to span
51 * @var integer
52 */
53 private $colspan = 0;
54
55 // ###################################################################
56 /**
57 * Creates a table element; takes a variable number of arguments which
58 * are added as children in the order passed
59 *
60 * @param BSPrinterTableElement... Variable number (or no) children
61 */
62 public function __construct()
63 {
64 $childs = func_get_args();
65 if (is_array($childs))
66 {
67 $this->children = $childs;
68 }
69 }
70
71 // ###################################################################
72 /**
73 * Prints a simple row of text and text
74 *
75 * @param string Label (left side)
76 * @param string Value (right side)
77 *
78 * @return BSPrinterTableElement Table row element
79 */
80 public static function RowLabel($label, $value)
81 {
82 $tr = new BSPrinterTableElement();
83
84 $tr->addChild(new BSPrinterLabelElement($label));
85 $tr->addChild(new BSPrinterLabelElement($value));
86
87 return $tr;
88 }
89
90 // ###################################################################
91 /**
92 * Creates an input[text/password] field row
93 *
94 * @param string Label
95 * @param string Field name
96 * @param mixed Default value
97 * @param bool A password field?
98 *
99 * @return BPrinterTableElement Table row element
100 */
101 public static function RowText($label, $name, $value = null, $password = false)
102 {
103 $tr = new BSPrinterTableElement();
104
105 $tr->addChild(new BSPrinterLabelElement($label));
106 $tr->addChild(new BSPrinterBaseElement(($password ? 'password' : 'text'), $name, $value));
107
108 return $tr;
109 }
110
111 // ###################################################################
112 /**
113 * Creates a submit row
114 *
115 * @param array Child elements to add before the buttons
116 * @param string Save button text
117 * @param string Reset button text
118 *
119 * @return BSPrinterTableElement Table row element
120 */
121 public static function RowSubmit(Array $children = null, $save = ':submit:', $reset = ':reset:')
122 {
123 $build = '';
124 if (sizeof($children) > 0)
125 {
126 foreach ($children AS $child)
127 {
128 $build .= "\n\t\t\t" . $child->paint();
129 }
130 }
131
132 $save = ($save == ':submit:' ? _('Submit') : $save);
133 $reset = ($reset == ':reset:' ? _('Reset') : $reset);
134
135 if (!is_null($save))
136 {
137 $elm = new BSPrinterBaseElement('submit', '__submit__', " $save ");
138 $elm->setAccessKey('s');
139 $build .= "\n\t\t\t" . $elm->paint();
140 }
141
142 if (!is_null($reset))
143 {
144 $elm = new BSPrinterBaseElement('reset', '__reset__', " $reset ");
145 $elm->setAccessKey('r');
146 $build .= "\n\t\t\t" . $elm->paint() . "\n\t\t";
147 }
148
149 $tr = new BSPrinterTableElement();
150 $tr->addChild(new BSPrinterLabelElement($build));
151 $tr->setCssClass('tfoot');
152
153 return $tr;
154 }
155
156 // ###################################################################
157 /**
158 * Constructs a <select> row from an array of BSPrinterBaseElement's
159 *
160 * @param string Label
161 * @param string Name
162 * @param array Array of BSPrinterBaseElement[option]'s
163 *
164 * @return BSPrinterTableElement Table row
165 */
166 public static function RowList($label, $name, Array $options)
167 {
168 $build = '';
169 foreach ($options AS $option)
170 {
171 if ($option->getType() == 'option')
172 {
173 $build .= $option->paint();
174 }
175 else
176 {
177 throw new Exception('Only BSPrinterBaseElement\'s of type "option" are allowed in BSPrinterTableElement::RowList()');
178 }
179 }
180
181 $tr = new BSPrinterTableElement();
182
183 $tr->addChild(new BSPrinterLabelElement($label));
184 $tr->addChild(new BSPrinterBaseElement('select', $name, $build));
185
186 return $tr;
187 }
188
189 // ###################################################################
190 /**
191 * Constructs a row from an array of BSPrinterBaseElement's of checkboxes
192 *
193 * @param string Label
194 * @param string Name
195 * @param array Array of BSPrinterBaseElement[checkbox]'s that follow array(box label => BSPrinterBaseElement)
196 *
197 * @return BSPrinterTableElement Table row
198 */
199 public static function RowCheckbox($label, $name, Array $boxes)
200 {
201 $build = '';
202 foreach ($boxes AS $boxLabel => $box)
203 {
204 if ($box->getType() == 'checkbox')
205 {
206 if ($box->getName() == null)
207 {
208 $box->setName($name . '[]');
209 }
210 $build .= '<div>' . $box->paint() . ' ' . $boxLabel . '</div>';
211 }
212 else
213 {
214 throw new Exception('Only BSPrinterBaseElement\'s of type "checkbox" are allowed in BSPrinterTableElement::RowCheckbox()');
215 }
216 }
217
218 $tr = new BSPrinterTableElement();
219
220 $tr->addChild(new BSPrinterLabelElement($label));
221 $tr->addChild(new BSPrinterLabelElement($build));
222
223 return $tr;
224 }
225
226 // ###################################################################
227 /**
228 * Factory method to create an upload form element; requires that the
229 * form this is attached to have the upload flag set
230 *
231 * @param string Label for the element
232 * @param string Name of the <input>
233 *
234 * @return BSPrinterTableElement Upload form
235 */
236 public static function RowUpload($label, $name)
237 {
238 $tr = new BSPrinterTableElement();
239
240 $tr->addChild(new BSPrinterLabelElement($label));
241 $tr->addChild(new BSPrinterBaseElement('upload', $name));
242
243 return $tr;
244 }
245
246 // ###################################################################
247 /**
248 * Creates a row with a radio select option for yes/no
249 *
250 * @param string Row label
251 * @param string Name of the radio buttons
252 * @param bool Yes is selected? (if false, No is selected)
253 *
254 * @return BSPrinterTableElement Yes-No row
255 */
256 public static function RowYesNo($label, $name, $yes)
257 {
258 $elm = new BSPrinterBaseElement('radio', $name, 1);
259 $elm->setActive($yes);
260
261 $build = $elm->paint() . ' ' . _('Yes') . ' ';
262
263 $elm = new BSPrinterBaseElement('radio', $name, 0);
264 $elm->setActive(!$yes);
265
266 $build .= $elm->paint() . ' ' . _('No');
267
268 $tr = new BSPrinterTableElement();
269 $tr->addChild(new BSPrinterLabelElement($label));
270 $tr->addChild(new BSPrinterLabelElement($build));
271 return $tr;
272 }
273
274 // ###################################################################
275 /**
276 * Prints a row with a textarea
277 *
278 * @param string Label
279 * @param string Textarea name
280 * @param string Value to fill with
281 *
282 * @return BSPrinterTableElement Table row
283 */
284 public static function RowTextarea($label, $name, $value = null)
285 {
286 $tr = new BSPrinterTableElement();
287
288 $tr->addChild(new BSPrinterLabelElement($label));
289 $tr->addChild(new BSPrinterBaseElement('textarea', $name, $value));
290
291 return $tr;
292 }
293
294 // ###################################################################
295 /**
296 * Returns the number of columns in this element
297 *
298 * @return integer Column count
299 */
300 public function numberOfColumns()
301 {
302 return sizeof($this->children);
303 }
304
305 // ###################################################################
306 /**
307 * Adds a child node to the element
308 *
309 * @param BSPrinterElement A child element
310 */
311 public function addChild(BSPrinterElement $child)
312 {
313 $this->children[] = $child;
314 }
315
316 // ###################################################################
317 /**
318 * Sets the number of columns this row should have and pads the <td>
319 * elements accordingly
320 *
321 * @param integer Column count
322 */
323 public function setColumnNumber($cols)
324 {
325 if ($cols < $this->numberOfColumns())
326 {
327 throw new Exception('You need to have at least ' . $this->numberOfColumns() . ' columns');
328 }
329 $this->colspan = $cols;
330 }
331
332 // ###################################################################
333 /**
334 * Returns the HTML for all printed children elements
335 *
336 * @return string Printed HTML
337 */
338 protected function _paintChildren()
339 {
340 $builder = '';
341
342 $numCols = $this->numberOfColumns();
343 $even = true;
344
345 if ($this->colspan % $numCols == 0)
346 {
347 $cols = $this->colspan / $numCols;
348 $even = true;
349 }
350 else
351 {
352 $cols = intval($this->colspan / $numCols);
353 $even = false;
354 }
355
356 $i = 0;
357 foreach ($this->children AS $child)
358 {
359 $i++;
360 $colspan = (($i == $numCols AND !$even) ? $cols + 1 : $cols);
361 $builder .= "\n\t\t<td" . ($colspan > 1 ? ' colspan="' . $colspan . '"' : '') . ">" . $child->paint() . "</td>";
362 }
363
364 return $builder;
365 }
366
367 // ###################################################################
368 /**
369 * Paints the entire table row
370 *
371 * @return string Table row HTML
372 */
373 public function paint()
374 {
375 return "\t<tr" . $this->_prepareStyle() . ">" . $this->_paintChildren() . "\n\t</tr>";
376 }
377 }
378
379 ?>