]>
src.bluestatic.org Git - isso.git/blob - PrinterTableElement.php
2 /*=====================================================================*
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright (c)2005-2008 Blue Static
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.
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
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 \*=====================================================================*/
23 * Printer Table Element (PrinterTableElement.php)
28 require_once(ISSO
. '/PrinterRootElementTable.php');
29 require_once(ISSO
. '/PrinterBaseElement.php');
32 * Printer Table Element
34 * This represents a table row that holds elements.
37 * @copyright Copyright (c)2005 - 2008, Blue Static
41 class BSPrinterTableElement
extends BSPrinterElement
44 * Array of child nodes
47 private $children = array();
50 * Number of columns to span
55 // ###################################################################
57 * Creates a table element; takes a variable number of arguments which
58 * are added as children in the order passed
60 * @param BSPrinterTableElement... Variable number (or no) children
62 public function __construct()
64 $childs = func_get_args();
65 if (is_array($childs))
67 $this->children
= $childs;
72 * Makes a new instance of the object in a static fashion
76 public static function make()
78 $obj = new ReflectionClass(__CLASS__
);
79 $args = func_get_args();
80 return $obj->newInstanceArgs($args);
83 // ###################################################################
85 * Prints a simple row of text and text
87 * @param string Label (left side)
88 * @param string Value (right side)
90 * @return BSPrinterTableElement Table row element
92 public static function row_label($label, $value)
94 $tr = new BSPrinterTableElement();
96 $tr->addChild(new BSPrinterLabelElement($label));
97 $tr->addChild(new BSPrinterLabelElement($value));
102 // ###################################################################
104 * Creates an input[text/password] field row
106 * @param string Label
107 * @param string Field name
108 * @param mixed Default value
109 * @param bool A password field?
111 * @return BPrinterTableElement Table row element
113 public static function row_text($label, $name, $value = null, $password = false)
115 $tr = new BSPrinterTableElement();
117 $tr->addChild(new BSPrinterLabelElement($label));
118 $tr->addChild(new BSPrinterBaseElement(($password ? 'password' : 'text'), $name, $value));
123 // ###################################################################
125 * Creates a submit row
127 * @param array Child elements to add before the buttons
128 * @param string Save button text
129 * @param string Reset button text
131 * @return BSPrinterTableElement Table row element
133 public static function row_submit(Array $children = null, $save = ':submit:', $reset = ':reset:')
136 if (sizeof($children) > 0)
138 foreach ($children AS $child)
140 $build .= "\n\t\t\t" . $child->paint();
144 $save = ($save == ':submit:' ? _('Submit') : $save);
145 $reset = ($reset == ':reset:' ? _('Reset') : $reset);
149 $elm = new BSPrinterBaseElement('submit', '__submit__', " $save ");
150 $elm->setAccessKey('s');
151 $build .= "\n\t\t\t" . $elm->paint();
154 if (!is_null($reset))
156 $elm = new BSPrinterBaseElement('reset', '__reset__', " $reset ");
157 $elm->setAccessKey('r');
158 $build .= "\n\t\t\t" . $elm->paint() . "\n\t\t";
161 $tr = new BSPrinterTableElement();
162 $tr->addChild(new BSPrinterLabelElement($build));
163 $tr->setCssClass('tfoot');
168 // ###################################################################
170 * Constructs a <select> row from an array of BSPrinterBaseElement's
172 * @param string Label
174 * @param array Array of BSPrinterBaseElement[option]'s
176 * @return BSPrinterTableElement Table row
178 public static function row_list($label, $name, Array $options)
181 foreach ($options AS $option)
183 if ($option->getType() == 'option')
185 $build .= $option->paint();
189 throw new Exception('Only BSPrinterBaseElement\'s of type "option" are allowed in BSPrinterTableElement::row_list()');
193 $tr = new BSPrinterTableElement();
195 $tr->addChild(new BSPrinterLabelElement($label));
196 $tr->addChild(new BSPrinterBaseElement('select', $name, $build));
201 // ###################################################################
203 * Constructs a row from an array of BSPrinterBaseElement's of checkboxes
205 * @param string Label
207 * @param array Array of BSPrinterBaseElement[checkbox]'s that follow array(box label => BSPrinterBaseElement)
209 * @return BSPrinterTableElement Table row
211 public static function row_checkbox($label, $name, Array $boxes)
214 foreach ($boxes AS $boxLabel => $box)
216 if ($box->getType() == 'checkbox')
218 if ($box->getName() == null)
220 $box->setName($name . '[]');
222 $build .= '<div>' . $box->paint() . ' ' . $boxLabel . '</div>';
226 throw new Exception('Only BSPrinterBaseElement\'s of type "checkbox" are allowed in BSPrinterTableElement::row_checkbox()');
230 $tr = new BSPrinterTableElement();
232 $tr->addChild(new BSPrinterLabelElement($label));
233 $tr->addChild(new BSPrinterLabelElement($build));
238 // ###################################################################
240 * Factory method to create an upload form element; requires that the
241 * form this is attached to have the upload flag set
243 * @param string Label for the element
244 * @param string Name of the <input>
246 * @return BSPrinterTableElement Upload form
248 public static function row_upload($label, $name)
250 $tr = new BSPrinterTableElement();
252 $tr->addChild(new BSPrinterLabelElement($label));
253 $tr->addChild(new BSPrinterBaseElement('upload', $name));
258 // ###################################################################
260 * Creates a row with a radio select option for yes/no
262 * @param string Row label
263 * @param string Name of the radio buttons
264 * @param bool Yes is selected? (if false, No is selected)
266 * @return BSPrinterTableElement Yes-No row
268 public static function row_yes_no($label, $name, $yes)
270 $elm = new BSPrinterBaseElement('radio', $name, 1);
271 $elm->setActive($yes);
273 $build = $elm->paint() . ' ' . _('Yes') . ' ';
275 $elm = new BSPrinterBaseElement('radio', $name, 0);
276 $elm->setActive(!$yes);
278 $build .= $elm->paint() . ' ' . _('No');
280 $tr = new BSPrinterTableElement();
281 $tr->addChild(new BSPrinterLabelElement($label));
282 $tr->addChild(new BSPrinterLabelElement($build));
286 // ###################################################################
288 * Prints a row with a textarea
290 * @param string Label
291 * @param string Textarea name
292 * @param string Value to fill with
294 * @return BSPrinterTableElement Table row
296 public static function row_textarea($label, $name, $value = null)
298 $tr = new BSPrinterTableElement();
300 $tr->addChild(new BSPrinterLabelElement($label));
301 $tr->addChild(new BSPrinterBaseElement('textarea', $name, $value));
306 // ###################################################################
308 * Returns the number of columns in this element
310 * @return integer Column count
312 public function numberOfColumns()
314 return sizeof($this->children
);
317 // ###################################################################
319 * Adds a child node to the element
321 * @param BSPrinterElement A child element
323 * @return fluent interface
325 public function addChild(BSPrinterElement
$child)
327 $this->children
[] = $child;
331 // ###################################################################
333 * Sets the number of columns this row should have and pads the <td>
334 * elements accordingly
336 * @param integer Column count
338 * @return fluent interface
340 public function setColumnNumber($cols)
342 if ($cols < $this->numberOfColumns())
344 throw new Exception('You need to have at least ' . $this->numberOfColumns() . ' columns');
346 $this->colspan
= $cols;
350 // ###################################################################
352 * Returns the HTML for all printed children elements
354 * @return string Printed HTML
356 protected function _paintChildren()
360 $numCols = $this->numberOfColumns();
363 if ($this->colspan %
$numCols == 0)
365 $cols = $this->colspan
/ $numCols;
370 $cols = intval($this->colspan
/ $numCols);
375 foreach ($this->children
AS $child)
378 $colspan = (($i == $numCols AND !$even) ? $cols +
1 : $cols);
379 $builder .= "\n\t\t<td" . ($colspan > 1 ? ' colspan="' . $colspan . '"' : '') . ">" . $child->paint() . "</td>";
385 // ###################################################################
387 * Paints the entire table row
389 * @return string Table row HTML
391 public function paint()
393 return "\t<tr" . $this->_prepareStyle() . ">" . $this->_paintChildren() . "\n\t</tr>";