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