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 (PrinterElementTable.php)
28 require_once(ISSO
. '/PrinterRootTable.php');
29 require_once(ISSO
. '/PrinterElement.php');
32 * Printer Table Element
34 * This represents a table row that holds elements.
37 * @copyright Copyright (c)2005 - 2008, Blue Static
41 class BSPrinterElementTable
extends BSPrinterAbstract
44 * Array of child nodes
47 private $children = array();
50 * Number of columns to span
56 * Creates a table element; takes a variable number of arguments which
57 * are added as children in the order passed
59 * @param BSPrinterElementTable... Variable number (or no) children
61 public function __construct()
63 $childs = func_get_args();
64 if (is_array($childs))
66 $this->children
= $childs;
71 * Makes a new instance of the object in a static fashion
75 public static function make()
77 $obj = new ReflectionClass(__CLASS__
);
78 $args = func_get_args();
79 return $obj->newInstanceArgs($args);
83 * Prints a simple row of text and text
85 * @param string Label (left side)
86 * @param string Value (right side)
88 * @return BSPrinterElementTable Table row element
90 public static function row_label($label, $value)
92 $tr = new BSPrinterElementTable();
94 $tr->addChild(new BSPrinterElementLabel($label));
95 $tr->addChild(new BSPrinterElementLabel($value));
101 * Creates an input[text/password] field row
103 * @param string Label
104 * @param string Field name
105 * @param mixed Default value
106 * @param bool A password field?
108 * @return BPrinterElementTable Table row element
110 public static function row_text($label, $name, $value = null
, $password = false
)
112 $tr = new BSPrinterElementTable();
114 $tr->addChild(new BSPrinterElementLabel($label));
115 $tr->addChild(new BSPrinterElement(($password ?
'password' : 'text'), $name, $value));
121 * Creates a submit row
123 * @param array Child elements to add before the buttons
124 * @param string Save button text
125 * @param string Reset button text
127 * @return BSPrinterElementTable Table row element
129 public static function row_submit(Array $children = null
, $save = ':submit:', $reset = ':reset:')
132 if (sizeof($children) > 0)
134 foreach ($children as $child)
136 $build .= "\n\t\t\t" . $child->paint();
140 $save = ($save == ':submit:' ?
_('Submit') : $save);
141 $reset = ($reset == ':reset:' ?
_('Reset') : $reset);
145 $elm = new BSPrinterElement('submit', '__submit__', " $save ");
146 $elm->setAccessKey('s');
147 $build .= "\n\t\t\t" . $elm->paint();
150 if (!is_null($reset))
152 $elm = new BSPrinterElement('reset', '__reset__', " $reset ");
153 $elm->setAccessKey('r');
154 $build .= "\n\t\t\t" . $elm->paint() . "\n\t\t";
157 $tr = new BSPrinterElementTable();
158 $tr->addChild(new BSPrinterElementLabel($build));
159 $tr->setCssClass('tfoot');
165 * Constructs a <select> row from an array of BSPrinterElement's
167 * @param string Label
169 * @param array Array of BSPrinterElement[option]'s
171 * @return BSPrinterElementTable Table row
173 public static function row_list($label, $name, Array $options)
176 foreach ($options as $option)
178 if ($option->getType() == 'option')
180 $build .= $option->paint();
184 throw new Exception('Only BSPrinterElement\'s of type "option" are allowed in BSPrinterElementTable::row_list()');
188 $tr = new BSPrinterElementTable();
190 $tr->addChild(new BSPrinterElementLabel($label));
191 $tr->addChild(new BSPrinterElement('select', $name, $build));
197 * Constructs a row from an array of BSPrinterElement's of checkboxes
199 * @param string Label
201 * @param array Array of BSPrinterElement[checkbox]'s that follow array(box label => BSPrinterElement)
203 * @return BSPrinterElementTable Table row
205 public static function row_checkbox($label, $name, Array $boxes)
208 foreach ($boxes as $boxLabel => $box)
210 if ($box->getType() == 'checkbox')
212 if ($box->getName() == null
)
214 $box->setName($name . '[]');
216 $build .= '<div>' . $box->paint() . ' ' . $boxLabel . '</div>';
220 throw new Exception('Only BSPrinterElement\'s of type "checkbox" are allowed in BSPrinterElementTable::row_checkbox()');
224 $tr = new BSPrinterElementTable();
226 $tr->addChild(new BSPrinterElementLabel($label));
227 $tr->addChild(new BSPrinterElementLabel($build));
233 * Factory method to create an upload form element; requires that the
234 * form this is attached to have the upload flag set
236 * @param string Label for the element
237 * @param string Name of the <input>
239 * @return BSPrinterElementTable Upload form
241 public static function row_upload($label, $name)
243 $tr = new BSPrinterElementTable();
245 $tr->addChild(new BSPrinterElementLabel($label));
246 $tr->addChild(new BSPrinterElement('upload', $name));
252 * Creates a row with a radio select option for yes/no
254 * @param string Row label
255 * @param string Name of the radio buttons
256 * @param bool Yes is selected? (if false, No is selected)
258 * @return BSPrinterElementTable Yes-No row
260 public static function row_yes_no($label, $name, $yes)
262 $elm = new BSPrinterElement('radio', $name, 1);
263 $elm->setActive($yes);
265 $build = $elm->paint() . ' ' . _('Yes') . ' ';
267 $elm = new BSPrinterElement('radio', $name, 0);
268 $elm->setActive(!$yes);
270 $build .= $elm->paint() . ' ' . _('No');
272 $tr = new BSPrinterElementTable();
273 $tr->addChild(new BSPrinterElementLabel($label));
274 $tr->addChild(new BSPrinterElementLabel($build));
279 * Prints a row with a textarea
281 * @param string Label
282 * @param string Textarea name
283 * @param string Value to fill with
285 * @return BSPrinterElementTable Table row
287 public static function row_textarea($label, $name, $value = null
)
289 $tr = new BSPrinterElementTable();
291 $tr->addChild(new BSPrinterElementLabel($label));
292 $tr->addChild(new BSPrinterElement('textarea', $name, $value));
298 * Returns the number of columns in this element
300 * @return integer Column count
302 public function numberOfColumns()
304 return sizeof($this->children
);
308 * Adds a child node to the element
310 * @param BSPrinterAbstract A child element
312 * @return fluent interface
314 public function addChild(BSPrinterAbstract
$child)
316 $this->children
[] = $child;
321 * Sets the number of columns this row should have and pads the <td>
322 * elements accordingly
324 * @param integer Column count
326 * @return fluent interface
328 public function setColumnNumber($cols)
330 if ($cols < $this->numberOfColumns())
332 throw new Exception('You need to have at least ' . $this->numberOfColumns() . ' columns');
334 $this->colspan
= $cols;
339 * Returns the HTML for all printed children elements
341 * @return string Printed HTML
343 protected function _paintChildren()
347 $numCols = $this->numberOfColumns();
350 if ($this->colspan %
$numCols == 0)
352 $cols = $this->colspan
/ $numCols;
357 $cols = intval($this->colspan
/ $numCols);
362 foreach ($this->children
as $child)
365 $colspan = (($i == $numCols && !$even) ?
$cols +
1 : $cols);
366 $builder .= "\n\t\t<td" . ($colspan > 1 ?
' colspan="' . $colspan . '"' : '') . ">" . $child->paint() . "</td>";
373 * Paints the entire table row
375 * @return string Table row HTML
377 public function paint()
379 return "\t<tr" . $this->_prepareStyle() . ">" . $this->_paintChildren() . "\n\t</tr>";