]>
src.bluestatic.org Git - isso.git/blob - PrinterElementTable.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 (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 foreach ($childs as $key => $value)
68 if (!($value instanceof BSPrinterAbstract
))
70 $childs[$key] = BSPrinterElementLabel
::make($value);
73 $this->children
= $childs;
78 * Makes a new instance of the object in a static fashion
82 public static function make()
84 $obj = new ReflectionClass(__CLASS__
);
85 $args = func_get_args();
86 return $obj->newInstanceArgs($args);
90 * Prints a simple row of text and text
92 * @param string Label (left side)
93 * @param string Value (right side)
95 * @return BSPrinterElementTable Table row element
97 public static function row_label($label, $value)
99 $tr = new BSPrinterElementTable();
101 $tr->addChild(new BSPrinterElementLabel($label));
102 $tr->addChild(new BSPrinterElementLabel($value));
108 * Creates an input[text/password] field row
110 * @param string Label
111 * @param string Field name
112 * @param mixed Default value
113 * @param bool A password field?
115 * @return BPrinterElementTable Table row element
117 public static function row_text($label, $name, $value = null, $password = false)
119 $tr = new BSPrinterElementTable();
121 $tr->addChild(new BSPrinterElementLabel($label));
122 $tr->addChild(new BSPrinterElement(($password ? 'password' : 'text'), $name, $value));
128 * Creates a submit row
130 * @param array Child elements to add before the buttons
131 * @param string Save button text
132 * @param string Reset button text
134 * @return BSPrinterElementTable Table row element
136 public static function row_submit(Array $children = null, $save = ':submit:', $reset = ':reset:')
139 if (sizeof($children) > 0)
141 foreach ($children as $child)
143 $build .= "\n\t\t\t" . $child->paint();
147 $save = ($save == ':submit:' ? _('Submit') : $save);
148 $reset = ($reset == ':reset:' ? _('Reset') : $reset);
152 $elm = new BSPrinterElement('submit', '__submit__', " $save ");
153 $elm->setAccessKey('s');
154 $build .= "\n\t\t\t" . $elm->paint();
157 if (!is_null($reset))
159 $elm = new BSPrinterElement('reset', '__reset__', " $reset ");
160 $elm->setAccessKey('r');
161 $build .= "\n\t\t\t" . $elm->paint() . "\n\t\t";
164 $tr = new BSPrinterElementTable();
165 $tr->addChild(new BSPrinterElementLabel($build));
166 $tr->setCssClass('tfoot');
172 * Constructs a <select> row from an array of BSPrinterElement's
174 * @param string Label
176 * @param array Array of BSPrinterElement[option]'s
178 * @return BSPrinterElementTable Table row
180 public static function row_list($label, $name, Array $options)
183 foreach ($options as $option)
185 if ($option->getType() == 'option')
187 $build .= $option->paint();
191 throw new Exception('Only BSPrinterElement\'s of type "option" are allowed in BSPrinterElementTable::row_list()');
195 $tr = new BSPrinterElementTable();
197 $tr->addChild(new BSPrinterElementLabel($label));
198 $tr->addChild(new BSPrinterElement('select', $name, $build));
204 * Constructs a row from an array of BSPrinterElement's of checkboxes
206 * @param string Label
208 * @param array Array of BSPrinterElement[checkbox]'s that follow array(box label => BSPrinterElement)
210 * @return BSPrinterElementTable Table row
212 public static function row_checkbox($label, $name, Array $boxes)
215 foreach ($boxes as $boxLabel => $box)
217 if ($box->getType() == 'checkbox')
219 if ($box->getName() == null)
221 $box->setName($name . '[]');
223 $build .= '<div>' . $box->paint() . ' ' . $boxLabel . '</div>';
227 throw new Exception('Only BSPrinterElement\'s of type "checkbox" are allowed in BSPrinterElementTable::row_checkbox()');
231 $tr = new BSPrinterElementTable();
233 $tr->addChild(new BSPrinterElementLabel($label));
234 $tr->addChild(new BSPrinterElementLabel($build));
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 BSPrinterElementTable Upload form
248 public static function row_upload($label, $name)
250 $tr = new BSPrinterElementTable();
252 $tr->addChild(new BSPrinterElementLabel($label));
253 $tr->addChild(new BSPrinterElement('upload', $name));
259 * Creates a row with a radio select option for yes/no
261 * @param string Row label
262 * @param string Name of the radio buttons
263 * @param bool Yes is selected? (if false, No is selected)
265 * @return BSPrinterElementTable Yes-No row
267 public static function row_yes_no($label, $name, $yes)
269 $elm = new BSPrinterElement('radio', $name, 1);
270 $elm->setActive($yes);
272 $build = $elm->paint() . ' ' . _('Yes') . ' ';
274 $elm = new BSPrinterElement('radio', $name, 0);
275 $elm->setActive(!$yes);
277 $build .= $elm->paint() . ' ' . _('No');
279 $tr = new BSPrinterElementTable();
280 $tr->addChild(new BSPrinterElementLabel($label));
281 $tr->addChild(new BSPrinterElementLabel($build));
286 * Prints a row with a textarea
288 * @param string Label
289 * @param string Textarea name
290 * @param string Value to fill with
292 * @return BSPrinterElementTable Table row
294 public static function row_textarea($label, $name, $value = null)
296 $tr = new BSPrinterElementTable();
298 $tr->addChild(new BSPrinterElementLabel($label));
299 $tr->addChild(new BSPrinterElement('textarea', $name, $value));
305 * Returns the number of columns in this element
307 * @return integer Column count
309 public function numberOfColumns()
311 return sizeof($this->children
);
315 * Adds a child node to the element
317 * @param BSPrinterAbstract A child element
319 * @return fluent interface
321 public function addChild(BSPrinterAbstract
$child)
323 $this->children
[] = $child;
328 * Sets the number of columns this row should have and pads the <td>
329 * elements accordingly
331 * @param integer Column count
333 * @return fluent interface
335 public function setColumnNumber($cols)
337 if ($cols < $this->numberOfColumns())
339 throw new Exception('You need to have at least ' . $this->numberOfColumns() . ' columns');
341 $this->colspan
= $cols;
346 * Returns the HTML for all printed children elements
348 * @return string Printed HTML
350 protected function _paintChildren()
354 $numCols = $this->numberOfColumns();
357 if ($this->colspan %
$numCols == 0)
359 $cols = $this->colspan
/ $numCols;
364 $cols = intval($this->colspan
/ $numCols);
369 foreach ($this->children
as $child)
372 $colspan = (($i == $numCols && !$even) ? $cols +
1 : $cols);
373 $builder .= "\n\t\t<td" . ($colspan > 1 ? ' colspan="' . $colspan . '"' : '') . ">" . $child->paint() . "</td>";
380 * Paints the entire table row
382 * @return string Table row HTML
384 public function paint()
386 return "\t<tr" . $this->_prepareStyle() . ">" . $this->_paintChildren() . "\n\t</tr>";