Merging changes from the 2.1.x branch back to trunk (r860-862)
[isso.git] / PrinterTableElement.php
1 <?php
2 /*=====================================================================*
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright ©2002-[#]year[#] 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 [#]gpl[#] 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)2002 - [#]year[#], Blue Static
38 * @version $Revision$
39 * @package ISSO
40 *
41 */
42 class BSPrinterTableElement extends BSPrinterElement
43 {
44 /**
45 * Array of child nodes
46 * @var array
47 */
48 private $children = array();
49
50 /**
51 * Number of columns to span
52 * @var integer
53 */
54 private $colspan = 0;
55
56 // ###################################################################
57 /**
58 * Creates a table element; takes a variable number of arguments which
59 * are added as children in the order passed
60 *
61 * @param BSPrinterTableElement... Variable number (or no) children
62 */
63 public function __construct()
64 {
65 $childs = func_get_args();
66 if (is_array($childs))
67 {
68 $this->children = $childs;
69 }
70 }
71
72 // ###################################################################
73 /**
74 * Prints a simple row of text and text
75 *
76 * @param string Label (left side)
77 * @param string Value (right side)
78 *
79 * @return BSPrinterTableElement Table row element
80 */
81 public static function RowLabel($label, $value)
82 {
83 $tr = new BSPrinterTableElement();
84
85 $tr->addChild(new BSPrinterLabelElement($label));
86 $tr->addChild(new BSPrinterLabelElement($value));
87
88 return $tr;
89 }
90
91 // ###################################################################
92 /**
93 * Creates an input[text/password] field row
94 *
95 * @param string Label
96 * @param string Field name
97 * @param mixed Default value
98 * @param bool A password field?
99 *
100 * @return BPrinterTableElement Table row element
101 */
102 public static function RowText($label, $name, $value = null, $password = false)
103 {
104 $tr = new BSPrinterTableElement();
105
106 $tr->addChild(new BSPrinterLabelElement($label));
107 $tr->addChild(new BSPrinterBaseElement(($password ? 'password' : 'text'), $name, $value));
108
109 return $tr;
110 }
111
112 // ###################################################################
113 /**
114 * Creates a submit row
115 *
116 * @param array Child elements to add before the buttons
117 * @param string Save button text
118 * @param string Reset button text
119 *
120 * @return BSPrinterTableElement Table row element
121 */
122 public static function RowSubmit(Array $children = null, $save = ':submit:', $reset = ':reset:')
123 {
124 $build = '';
125 if (sizeof($children) > 0)
126 {
127 foreach ($children AS $child)
128 {
129 $build .= "\n\t\t\t" . $child->paint();
130 }
131 }
132
133 $save = ($save == ':submit:' ? _('Submit') : $save);
134 $reset = ($reset == ':reset:' ? _('Reset') : $reset);
135
136 if (!is_null($save))
137 {
138 $elm = new BSPrinterBaseElement('submit', '__submit__', " $save ");
139 $elm->setAccessKey('s');
140 $build .= "\n\t\t\t" . $elm->paint();
141 }
142
143 if (!is_null($reset))
144 {
145 $elm = new BSPrinterBaseElement('reset', '__reset__', " $reset ");
146 $elm->setAccessKey('r');
147 $build .= "\n\t\t\t" . $elm->paint() . "\n\t\t";
148 }
149
150 $tr = new BSPrinterTableElement();
151 $tr->addChild(new BSPrinterLabelElement($build));
152 $tr->setCssClass('tfoot');
153
154 return $tr;
155 }
156
157 // ###################################################################
158 /**
159 * Constructs a <select> row from an array of BSPrinterBaseElement's
160 *
161 * @param string Label
162 * @param string Name
163 * @param array Array of BSPrinterBaseElement[option]'s
164 *
165 * @return BSPrinterTableElement Table row
166 */
167 public static function RowList($label, $name, Array $options)
168 {
169 $build = '';
170 foreach ($options AS $option)
171 {
172 if ($option->getType() == 'option')
173 {
174 $build .= $option->paint();
175 }
176 else
177 {
178 trigger_error('Only BSPrinterBaseElement\'s of type "option" are allowed in BSPrinterTableElement::RowList()');
179 }
180 }
181
182 $tr = new BSPrinterTableElement();
183
184 $tr->addChild(new BSPrinterLabelElement($label));
185 $tr->addChild(new BSPrinterBaseElement('select', $name, $build));
186
187 return $tr;
188 }
189
190 // ###################################################################
191 /**
192 * Constructs a row from an array of BSPrinterBaseElement's of checkboxes
193 *
194 * @param string Label
195 * @param string Name
196 * @param array Array of BSPrinterBaseElement[checkbox]'s that follow array(box label => BSPrinterBaseElement)
197 *
198 * @return BSPrinterTableElement Table row
199 */
200 public static function RowCheckbox($label, $name, Array $boxes)
201 {
202 $build = '';
203 foreach ($boxes AS $boxLabel => $box)
204 {
205 if ($box->getType() == 'checkbox')
206 {
207 if ($box->getName() == null)
208 {
209 $box->setName($name . '[]');
210 }
211 $build .= '<div>' . $box->paint() . ' ' . $boxLabel . '</div>';
212 }
213 else
214 {
215 trigger_error('Only BSPrinterBaseElement\'s of type "checkbox" are allowed in BSPrinterTableElement::RowCheckbox()');
216 }
217 }
218
219 $tr = new BSPrinterTableElement();
220
221 $tr->addChild(new BSPrinterLabelElement($label));
222 $tr->addChild(new BSPrinterLabelElement($build));
223
224 return $tr;
225 }
226
227 // ###################################################################
228 /**
229 * Factory method to create an upload form element; requires that the
230 * form this is attached to have the upload flag set
231 *
232 * @param string Label for the element
233 * @param string Name of the <input>
234 *
235 * @return BSPrinterTableElement Upload form
236 */
237 public static function RowUpload($label, $name)
238 {
239 $tr = new BSPrinterTableElement();
240
241 $tr->addChild(new BSPrinterLabelElement($label));
242 $tr->addChild(new BSPrinterBaseElement('upload', $name));
243
244 return $tr;
245 }
246
247 // ###################################################################
248 /**
249 * Creates a row with a radio select option for yes/no
250 *
251 * @param string Row label
252 * @param string Name of the radio buttons
253 * @param bool Yes is selected? (if false, No is selected)
254 *
255 * @return BSPrinterTableElement Yes-No row
256 */
257 public static function RowYesNo($label, $name, $yes)
258 {
259 $elm = new BSPrinterBaseElement('radio', $name, 1);
260 $elm->setActive($yes);
261
262 $build = $elm->paint() . ' ' . _('Yes') . ' ';
263
264 $elm = new BSPrinterBaseElement('radio', $name, 0);
265 $elm->setActive(!$yes);
266
267 $build .= $elm->paint() . ' ' . _('No');
268
269 $tr = new BSPrinterTableElement();
270 $tr->addChild(new BSPrinterLabelElement($label));
271 $tr->addChild(new BSPrinterLabelElement($build));
272 return $tr;
273 }
274
275 // ###################################################################
276 /**
277 * Prints a row with a textarea
278 *
279 * @param string Label
280 * @param string Textarea name
281 * @param string Value to fill with
282 *
283 * @return BSPrinterTableElement Table row
284 */
285 public static function RowTextarea($label, $name, $value = null)
286 {
287 $tr = new BSPrinterTableElement();
288
289 $tr->addChild(new BSPrinterLabelElement($label));
290 $tr->addChild(new BSPrinterBaseElement('textarea', $name, $value));
291
292 return $tr;
293 }
294
295 // ###################################################################
296 /**
297 * Returns the number of columns in this element
298 *
299 * @return integer Column count
300 */
301 public function numberOfColumns()
302 {
303 return sizeof($this->children);
304 }
305
306 // ###################################################################
307 /**
308 * Adds a child node to the element
309 *
310 * @param BSPrinterElement A child element
311 */
312 public function addChild(BSPrinterElement $child)
313 {
314 $this->children[] = $child;
315 }
316
317 // ###################################################################
318 /**
319 * Sets the number of columns this row should have and pads the <td>
320 * elements accordingly
321 *
322 * @param integer Column count
323 */
324 public function setColumnNumber($cols)
325 {
326 if ($cols < $this->numberOfColumns())
327 {
328 trigger_error('You need to have at least ' . $this->numberOfColumns() . ' columns');
329 }
330 $this->colspan = $cols;
331 }
332
333 // ###################################################################
334 /**
335 * Returns the HTML for all printed children elements
336 *
337 * @return string Printed HTML
338 */
339 protected function _paintChildren()
340 {
341 $builder = '';
342
343 $numCols = $this->numberOfColumns();
344 $even = true;
345
346 if ($this->colspan % $numCols == 0)
347 {
348 $cols = $this->colspan / $numCols;
349 $even = true;
350 }
351 else
352 {
353 $cols = intval($this->colspan / $numCols);
354 $even = false;
355 }
356
357 $i = 0;
358 foreach ($this->children AS $child)
359 {
360 $i++;
361 $colspan = (($i == $numCols AND !$even) ? $cols + 1 : $cols);
362 $builder .= "\n\t\t<td" . ($colspan > 1 ? ' colspan="' . $colspan . '"' : '') . ">" . $child->paint() . "</td>";
363 }
364
365 return $builder;
366 }
367
368 // ###################################################################
369 /**
370 * Paints the entire table row
371 *
372 * @return string Table row HTML
373 */
374 public function paint()
375 {
376 return "\t<tr" . $this->_prepareStyle() . ">" . $this->_paintChildren() . "\n\t</tr>";
377 }
378 }
379
380 /*=====================================================================*
381 || ###################################################################
382 || # $HeadURL$
383 || # $Id$
384 || ###################################################################
385 \*=====================================================================*/
386 ?>