]>
src.bluestatic.org Git - isso.git/blob - PrinterTableElement.php
2 /*=====================================================================*
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright (c)2002-2007 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)2002 - 2007, 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;
71 // ###################################################################
73 * Prints a simple row of text and text
75 * @param string Label (left side)
76 * @param string Value (right side)
78 * @return BSPrinterTableElement Table row element
80 public static function RowLabel($label, $value)
82 $tr = new BSPrinterTableElement();
84 $tr->addChild(new BSPrinterLabelElement($label));
85 $tr->addChild(new BSPrinterLabelElement($value));
90 // ###################################################################
92 * Creates an input[text/password] field row
95 * @param string Field name
96 * @param mixed Default value
97 * @param bool A password field?
99 * @return BPrinterTableElement Table row element
101 public static function RowText($label, $name, $value = null, $password = false)
103 $tr = new BSPrinterTableElement();
105 $tr->addChild(new BSPrinterLabelElement($label));
106 $tr->addChild(new BSPrinterBaseElement(($password ? 'password' : 'text'), $name, $value));
111 // ###################################################################
113 * Creates a submit row
115 * @param array Child elements to add before the buttons
116 * @param string Save button text
117 * @param string Reset button text
119 * @return BSPrinterTableElement Table row element
121 public static function RowSubmit(Array $children = null, $save = ':submit:', $reset = ':reset:')
124 if (sizeof($children) > 0)
126 foreach ($children AS $child)
128 $build .= "\n\t\t\t" . $child->paint();
132 $save = ($save == ':submit:' ? _('Submit') : $save);
133 $reset = ($reset == ':reset:' ? _('Reset') : $reset);
137 $elm = new BSPrinterBaseElement('submit', '__submit__', " $save ");
138 $elm->setAccessKey('s');
139 $build .= "\n\t\t\t" . $elm->paint();
142 if (!is_null($reset))
144 $elm = new BSPrinterBaseElement('reset', '__reset__', " $reset ");
145 $elm->setAccessKey('r');
146 $build .= "\n\t\t\t" . $elm->paint() . "\n\t\t";
149 $tr = new BSPrinterTableElement();
150 $tr->addChild(new BSPrinterLabelElement($build));
151 $tr->setCssClass('tfoot');
156 // ###################################################################
158 * Constructs a <select> row from an array of BSPrinterBaseElement's
160 * @param string Label
162 * @param array Array of BSPrinterBaseElement[option]'s
164 * @return BSPrinterTableElement Table row
166 public static function RowList($label, $name, Array $options)
169 foreach ($options AS $option)
171 if ($option->getType() == 'option')
173 $build .= $option->paint();
177 throw new Exception('Only BSPrinterBaseElement\'s of type "option" are allowed in BSPrinterTableElement::RowList()');
181 $tr = new BSPrinterTableElement();
183 $tr->addChild(new BSPrinterLabelElement($label));
184 $tr->addChild(new BSPrinterBaseElement('select', $name, $build));
189 // ###################################################################
191 * Constructs a row from an array of BSPrinterBaseElement's of checkboxes
193 * @param string Label
195 * @param array Array of BSPrinterBaseElement[checkbox]'s that follow array(box label => BSPrinterBaseElement)
197 * @return BSPrinterTableElement Table row
199 public static function RowCheckbox($label, $name, Array $boxes)
202 foreach ($boxes AS $boxLabel => $box)
204 if ($box->getType() == 'checkbox')
206 if ($box->getName() == null)
208 $box->setName($name . '[]');
210 $build .= '<div>' . $box->paint() . ' ' . $boxLabel . '</div>';
214 throw new Exception('Only BSPrinterBaseElement\'s of type "checkbox" are allowed in BSPrinterTableElement::RowCheckbox()');
218 $tr = new BSPrinterTableElement();
220 $tr->addChild(new BSPrinterLabelElement($label));
221 $tr->addChild(new BSPrinterLabelElement($build));
226 // ###################################################################
228 * Factory method to create an upload form element; requires that the
229 * form this is attached to have the upload flag set
231 * @param string Label for the element
232 * @param string Name of the <input>
234 * @return BSPrinterTableElement Upload form
236 public static function RowUpload($label, $name)
238 $tr = new BSPrinterTableElement();
240 $tr->addChild(new BSPrinterLabelElement($label));
241 $tr->addChild(new BSPrinterBaseElement('upload', $name));
246 // ###################################################################
248 * Creates a row with a radio select option for yes/no
250 * @param string Row label
251 * @param string Name of the radio buttons
252 * @param bool Yes is selected? (if false, No is selected)
254 * @return BSPrinterTableElement Yes-No row
256 public static function RowYesNo($label, $name, $yes)
258 $elm = new BSPrinterBaseElement('radio', $name, 1);
259 $elm->setActive($yes);
261 $build = $elm->paint() . ' ' . _('Yes') . ' ';
263 $elm = new BSPrinterBaseElement('radio', $name, 0);
264 $elm->setActive(!$yes);
266 $build .= $elm->paint() . ' ' . _('No');
268 $tr = new BSPrinterTableElement();
269 $tr->addChild(new BSPrinterLabelElement($label));
270 $tr->addChild(new BSPrinterLabelElement($build));
274 // ###################################################################
276 * Prints a row with a textarea
278 * @param string Label
279 * @param string Textarea name
280 * @param string Value to fill with
282 * @return BSPrinterTableElement Table row
284 public static function RowTextarea($label, $name, $value = null)
286 $tr = new BSPrinterTableElement();
288 $tr->addChild(new BSPrinterLabelElement($label));
289 $tr->addChild(new BSPrinterBaseElement('textarea', $name, $value));
294 // ###################################################################
296 * Returns the number of columns in this element
298 * @return integer Column count
300 public function numberOfColumns()
302 return sizeof($this->children
);
305 // ###################################################################
307 * Adds a child node to the element
309 * @param BSPrinterElement A child element
311 public function addChild(BSPrinterElement
$child)
313 $this->children
[] = $child;
316 // ###################################################################
318 * Sets the number of columns this row should have and pads the <td>
319 * elements accordingly
321 * @param integer Column count
323 public function setColumnNumber($cols)
325 if ($cols < $this->numberOfColumns())
327 throw new Exception('You need to have at least ' . $this->numberOfColumns() . ' columns');
329 $this->colspan
= $cols;
332 // ###################################################################
334 * Returns the HTML for all printed children elements
336 * @return string Printed HTML
338 protected function _paintChildren()
342 $numCols = $this->numberOfColumns();
345 if ($this->colspan %
$numCols == 0)
347 $cols = $this->colspan
/ $numCols;
352 $cols = intval($this->colspan
/ $numCols);
357 foreach ($this->children
AS $child)
360 $colspan = (($i == $numCols AND !$even) ? $cols +
1 : $cols);
361 $builder .= "\n\t\t<td" . ($colspan > 1 ? ' colspan="' . $colspan . '"' : '') . ">" . $child->paint() . "</td>";
367 // ###################################################################
369 * Paints the entire table row
371 * @return string Table row HTML
373 public function paint()
375 return "\t<tr" . $this->_prepareStyle() . ">" . $this->_paintChildren() . "\n\t</tr>";