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