1 <?php /* vim: set binary expandtab tabstop=4 shiftwidth=4 textwidth=80:
2 -------------------------------------------------------------------------------
4 -------------------------------------------------------------------------------
5 This file is part of mlib.
7 mlib is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2 of the License, or (at your option)
12 mlib is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License; if not,
17 write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 Boston, MA 02111-1307 USA
19 -------------------------------------------------------------------------------
20 Creado: mar mar 30 16:06:26 ART 2004
21 Autor: Leandro Lucarella <llucar@mecon.gov.ar>
22 -------------------------------------------------------------------------------
23 $Id: Tabla.php 462 2004-01-09 21:11:01Z www-data $
24 -----------------------------------------------------------------------------*/
26 require_once 'HTML/QuickForm/Renderer.php';
27 require_once 'MLIB/HTML/Tabla.php';
30 * QuickForm renderer que usa Tabla como backend. Es similar a
31 * MLIB_HTML_QuickForm_Renderer_Tabla pero dibuja la tabla horizontalmente.
34 * $form = new MLIB_HTML_QuickForm();
35 * $form->renderer = new MLIB_HTML_QuickForm_Renderer_TablaHorizontal(array('width' => 400));
36 * // Sigo creando el formulario normalmente...
39 class MLIB_HTML_QuickForm_Renderer_TablaHorizontal extends HTML_QuickForm_Renderer {
42 * Tabla usada para dibujar el formulario.
49 * Estructura de la tabla en filas a usar para renderizar.
55 * Guarda posición de la fila donde comienzan los elementos.
58 var $_elementsPos = null;
61 * Guarda elementos del formulario.
64 var $_elements = array();
67 * Guarda opciones del renderer.
73 * HTML con los scripts para poner antes del formulario (tipicamente
81 * HTML para agregar antes de la tabla (tipicamente un javascript).
88 * HTML para agregar despues de la tabla.
95 * True if we are inside a group
99 var $_inGroup = false;
102 * Group error related message.
106 var $_groupError = '';
109 * Array with HTML generated for group elements
113 var $_groupElements = array();
118 * @param $param Array o sting con el estilo de la tabla u objeto
119 * tabla alternativo para usar.
120 * @param $opts Opciones, pueden ser:
121 * - btn_row: Si es true, se dibujan los botones en una fila
122 * completa, después de los campos. Si es false, se
123 * dibuja como un elemento con un rowspan para que
124 * ocupe la celda del label y del contenido.
125 * - req_note: Si es true, se incluye una fila adicional con la
126 * 'require note'. Si es false no se la muestra.
130 function MLIB_HTML_QuickForm_Renderer_TablaHorizontal($param = array(), $opts = array())
132 parent::HTML_QuickForm_Renderer();
133 $this->_opts = $opts;
134 if (is_a($param, 'Tabla')) {
135 $this->setTable($param);
138 $this->setTable(new MLIB_HTML_Tabla($param));
143 * returns the HTML generated for the form
150 $pos = $this->_elementsPos;
151 $rows = count($this->_rows);
152 foreach ($this->_elements as $e) {
153 list($label, $elem) = $e;
155 for ($i = 0; $i < $pos; ++$i) {
158 if (is_null($label)) { // Para caso especial como botones.
161 } else { // Para caso común.
165 for ($i = $pos + 2; $i < $rows; ++$i) {
168 $id = $this->tabla->addCol($cols, array(array('align' => 'center')));
169 if (is_null($label)) { // Para caso especial como botones.
170 $this->tabla->updateCellAttributes($pos, $id, array('rowspan' => 2));
171 } else { // Para caso común.
172 $this->tabla->updateCellAttributes($pos, $id, array('titulo' => true));
175 foreach ($this->_rows as $i => $row) {
176 if ($row) { // Si no son las filas de los elementos...
177 list($html, $attrs) = $row;
178 $attrs['colspan'] = count($this->_elements);
179 $this->tabla->updateRowAttributes($i, $attrs);
180 $this->tabla->setCellContents($i, 0, $html);
183 return $this->_script . $this->_prepend .
184 $this->tabla->toHtml() .
189 * Called when visiting a form, before processing any form elements
191 * @param object An HTML_QuickForm object being visited
195 function startForm(&$form)
197 $attrs = $form->getAttributes(true);
198 $this->_prepend = "\n<form$attrs>\n";
200 } // end func startForm
203 * Called when visiting a form, after processing all form elements
204 * Adds required note, form attributes, validation javascript and form content.
206 * @param object An HTML_QuickForm object being visited
210 function finishForm(&$form)
212 // add a required note, if one is needed
213 if (@$this->_opts['req_note'] and !empty($form->_required) && !$form->_freezeAll) {
214 $this->_rows[] = array('<font size="-2">'.$form->getRequiredNote().'</font>', array('cabecera' => true));
216 // add a validation script
217 $this->_script = strval($form->getValidationScript());
218 $this->_append = "\n</form>\n";
219 } // end func finishForm
222 * Called when visiting a header element
224 * @param object An HTML_QuickForm_header element being visited
228 function renderHeader(&$header)
230 $this->_rows[] = array($header->toHtml(),
231 array($header->getName() => true));
232 } // end func renderHeader
235 * Renders an element Html
236 * Called when visiting an element
238 * @param object An HTML_QuickForm_element object being visited
239 * @param bool Whether an element is required
240 * @param string An error message associated with an element
244 function renderElement(&$element, $required, $error)
246 if (!$this->_inGroup) {
247 $this->_checkInElements();
248 $this->_elements[] = array(
249 $element->getLabel() . ($required ? '<font color="red">*</font>' : ''),
250 $element->toHtml() . ($error ? "<br><font color=\"red\">$error</font>" : ''));
252 $this->_groupElements[] = ($element->getLabel() ? ($element->getLabel().' ') : '') . $element->toHtml();
254 } // end func renderElement
257 * Renders an hidden element
258 * Called when visiting a hidden element
260 * @param object An HTML_QuickForm_hidden object being visited
264 function renderHidden(&$element)
266 $this->_prepend .= "\n\t". $element->toHtml();
267 } // end func renderHidden
270 * Called when visiting a raw HTML/text pseudo-element
272 * @param object An HTML_QuickForm_html element being visited
276 function renderHtml(&$data)
278 $this->_rows[] = array(&$data, array());
279 } // end func renderHtml
282 * Called when visiting a group, before processing any group elements
284 * @param object An HTML_QuickForm_group object being visited
285 * @param bool Whether a group is required
286 * @param string An error message associated with a group
290 function startGroup(&$group, $required, $error)
292 $this->_groupElements = array();
293 $this->_groupError = $error;
294 $this->_inGroup = true;
295 } // end func startGroup
298 * Called when visiting a group, after processing all group elements
300 * @param object An HTML_QuickForm_group object being visited
304 function finishGroup(&$group)
306 $name = $group->getName();
307 $sep = $group->_separator;
308 $label = $group->getLabel();
309 if (!@$this->_opts['btn_row'] and strtolower($name) == 'botones') {
312 if (@$this->_opts['btn_row']) {
313 $this->_rows[] = array(join('', $this->_groupElements), array('valign' => 'middle', 'align' => 'center'));
315 $this->_checkInElements();
316 $this->_elements[] = array($label, join($sep, $this->_groupElements));
317 $this->_inGroup = false;
319 } // end func finishGroup
321 function _checkInElements() {
322 if (is_null($this->_elementsPos)) {
323 $this->_elementsPos = count($this->_rows);
324 $this->_rows[] = false;
325 $this->_rows[] = false;
329 function setTable(&$table) {
330 $this->tabla =& $table;
334 return $this->tabla->getCSS();
337 } // end class HTML_QuickForm_Renderer_Default