Put spacing in between the submit row buttons
[isso.git] / PrinterTableElement.php
1 <?php
2 /*=====================================================================*
3 || ###################################################################
4 || # Blue Static ISSO Framework [#]issoversion[#]
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 * Prints a simple row of text and text
59 *
60 * @param string Label (left side)
61 * @param string Value (right side)
62 *
63 * @return BSPrinterTableElement Table row element
64 */
65 public static function RowLabel($label, $value)
66 {
67 $tr = new BSPrinterTableElement();
68
69 $tr->addChild(new BSPrinterLabelElement($label));
70 $tr->addChild(new BSPrinterLabelElement($value));
71
72 return $tr;
73 }
74
75 // ###################################################################
76 /**
77 * Creates an input[text/password] field row
78 *
79 * @param string Label
80 * @param string Field name
81 * @param mixed Default value
82 * @param bool A password field?
83 *
84 * @return BPrinterTableElement Table row element
85 */
86 public static function RowText($label, $name, $value = null, $password = false)
87 {
88 $tr = new BSPrinterTableElement();
89
90 $tr->addChild(new BSPrinterLabelElement($label));
91 $tr->addChild(new BSPrinterBaseElement(($password ? 'password' : 'text'), $name, $value));
92
93 return $tr;
94 }
95
96 // ###################################################################
97 /**
98 * Creates a submit row
99 *
100 * @param array Child elements to add before the buttons
101 * @param string Save button text
102 * @param string Reset button text
103 *
104 * @return BSPrinterTableElement Table row element
105 */
106 public static function RowSubmit(Array $children = null, $save = ':submit:', $reset = ':reset:')
107 {
108 $build = '';
109 if (sizeof($children) > 0)
110 {
111 foreach ($children AS $child)
112 {
113 $build .= "\n\t\t\t" . $child->paint();
114 }
115 }
116
117 $save = ($save == ':submit:' ? _('Submit') : $save);
118 $reset = ($reset == ':reset:' ? _('Reset') : $reset);
119
120 $elm = new BSPrinterBaseElement('submit', '__submit__', " $save ");
121 $build .= "\n\t\t\t" . $elm->paint();
122
123 $elm = new BSPrinterBaseElement('reset', '__reset__', " $reset ");
124 $build .= "\n\t\t\t" . $elm->paint() . "\n\t\t";
125
126 $tr = new BSPrinterTableElement();
127 $tr->addChild(new BSPrinterLabelElement($build));
128 $tr->setCssClass('tfoot');
129
130 return $tr;
131 }
132
133 // ###################################################################
134 /**
135 * Factory method that creates a checkbox row
136 *
137 * @param string Label
138 * @param string Element name
139 * @param mixed Value
140 * @param boolean Checked?
141 *
142 * @return BSPrinterTableElement A proper table element
143 */
144 public static function RowCheckbox($label, $name, $value, $checked = false)
145 {
146 $tr = new BSPrinterTableElement();
147
148 $tr->addChild(new BSPrinterLabelElement($label));
149
150 $elm = new BSPrinterBaseElement('checkbox', $name, $value);
151 $elm->setActive($checked);
152
153 $tr->addChild($elm);
154
155 return $tr;
156 }
157
158 // ###################################################################
159 /**
160 * Returns the number of columns in this element
161 *
162 * @return integer Column count
163 */
164 public function numberOfColumns()
165 {
166 return sizeof($this->children);
167 }
168
169 // ###################################################################
170 /**
171 * Adds a child node to the element
172 *
173 * @param BSPrinterElement A child element
174 */
175 public function addChild(BSPrinterElement $child)
176 {
177 $this->children[] = $child;
178 }
179
180 // ###################################################################
181 /**
182 * Sets the number of columns this row should have and pads the <td>
183 * elements accordingly
184 *
185 * @param integer Column count
186 */
187 public function setColumnNumber($cols)
188 {
189 if ($cols < $this->numberOfColumns())
190 {
191 trigger_error('You need to have at least ' . $this->numberOfColumns() . ' columns');
192 }
193 $this->colspan = $cols;
194 }
195
196 // ###################################################################
197 /**
198 * Returns the HTML for all printed children elements
199 *
200 * @return string Printed HTML
201 */
202 protected function _paintChildren()
203 {
204 $builder = '';
205
206 $numCols = $this->numberOfColumns();
207 $even = true;
208
209 if ($this->colspan % $numCols == 0)
210 {
211 $cols = $this->colspan / $numCols;
212 $even = true;
213 }
214 else
215 {
216 $cols = intval($this->colspan / $numCols);
217 $even = false;
218 }
219
220 $i = 0;
221 foreach ($this->children AS $child)
222 {
223 $i++;
224 $colspan = (($i == $numCols AND !$even) ? $cols + 1 : $cols);
225 $builder .= "\n\t\t<td" . ($colspan > 1 ? ' colspan="' . $colspan . '"' : '') . ">" . $child->paint() . "</td>";
226 }
227
228 return $builder;
229 }
230
231 // ###################################################################
232 /**
233 * Paints the entire table row
234 *
235 * @return string Table row HTML
236 */
237 public function paint()
238 {
239 return "\t<tr" . $this->_prepareStyle() . ">" . $this->_paintChildren() . "\n\t</tr>";
240 }
241 }
242
243 /*=====================================================================*
244 || ###################################################################
245 || # $HeadURL$
246 || # $Id$
247 || ###################################################################
248 \*=====================================================================*/
249 ?>