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