1 <?php /* vim: set binary expandtab tabstop=4 shiftwidth=4 textwidth=80:
2 -------------------------------------------------------------------------------
5 -------------------------------------------------------------------------------
6 This file is part of meconlib.
8 meconlib is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2 of the License, or (at your option)
13 meconlib is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License; if not,
18 write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19 Boston, MA 02111-1307 USA
20 -------------------------------------------------------------------------------
21 Creado: mar mar 30 16:06:26 ART 2004
22 Autor: Leandro Lucarella <llucar@mecon.gov.ar>
23 -------------------------------------------------------------------------------
24 $Id: Tabla.php 462 2004-01-09 21:11:01Z www-data $
25 -----------------------------------------------------------------------------*/
27 require_once 'HTML/QuickForm/Renderer.php';
28 require_once 'MECON/HTML/Tabla.php';
31 * QuickForm renderer que usa Tabla como backend. Es similar a
32 * MECON_HTML_QuickForm_Renderer_Tabla pero dibuja la tabla horizontalmente.
35 * $form = new MECON_HTML_QuickForm();
36 * $form->renderer = new MECON_HTML_QuickForm_Renderer_TablaHorizontal(array('width' => 400));
37 * // Sigo creando el formulario normalmente...
40 class MECON_HTML_QuickForm_Renderer_TablaHorizontal extends HTML_QuickForm_Renderer {
43 * Tabla usada para dibujar el formulario.
50 * Estructura de la tabla en filas a usar para renderizar.
56 * Guarda posición de la fila donde comienzan los elementos.
59 var $_elementsPos = null;
62 * Guarda elementos del formulario.
65 var $_elements = array();
68 * Guarda opciones del renderer.
74 * HTML con los scripts para poner antes del formulario (tipicamente
82 * HTML para agregar antes de la tabla (tipicamente un javascript).
89 * HTML para agregar despues de la tabla.
96 * True if we are inside a group
100 var $_inGroup = false;
103 * Group error related message.
107 var $_groupError = '';
110 * Array with HTML generated for group elements
114 var $_groupElements = array();
119 * @param $param Array o sting con el estilo de la tabla u objeto
120 * tabla alternativo para usar.
121 * @param $opts Opciones, pueden ser:
122 * - btn_row: Si es true, se dibujan los botones en una fila
123 * completa, después de los campos. Si es false, se
124 * dibuja como un elemento con un rowspan para que
125 * ocupe la celda del label y del contenido.
126 * - req_note: Si es true, se incluye una fila adicional con la
127 * 'require note'. Si es false no se la muestra.
131 function MECON_HTML_QuickForm_Renderer_TablaHorizontal($param = array(), $opts = array())
133 parent::HTML_QuickForm_Renderer();
134 $this->_opts = $opts;
135 if (is_a($param, 'Tabla')) {
136 $this->setTable($param);
139 $this->setTable(new MECON_HTML_Tabla($param));
144 * returns the HTML generated for the form
151 $pos = $this->_elementsPos;
152 $rows = count($this->_rows);
153 foreach ($this->_elements as $e) {
154 list($label, $elem) = $e;
156 for ($i = 0; $i < $pos; ++$i) {
159 if (is_null($label)) { // Para caso especial como botones.
162 } else { // Para caso común.
166 for ($i = $pos + 2; $i < $rows; ++$i) {
169 $id = $this->tabla->addCol($cols, array(array('align' => 'center')));
170 if (is_null($label)) { // Para caso especial como botones.
171 $this->tabla->updateCellAttributes($pos, $id, array('rowspan' => 2));
172 } else { // Para caso común.
173 $this->tabla->updateCellAttributes($pos, $id, array('titulo' => true));
176 foreach ($this->_rows as $i => $row) {
177 if ($row) { // Si no son las filas de los elementos...
178 list($html, $attrs) = $row;
179 $attrs['colspan'] = count($this->_elements);
180 $this->tabla->updateRowAttributes($i, $attrs);
181 $this->tabla->setCellContents($i, 0, $html);
184 return $this->_script . $this->_prepend .
185 $this->tabla->toHtml() .
190 * Called when visiting a form, before processing any form elements
192 * @param object An HTML_QuickForm object being visited
196 function startForm(&$form)
198 $attrs = $form->getAttributes(true);
199 $this->_prepend = "\n<form$attrs>\n";
201 } // end func startForm
204 * Called when visiting a form, after processing all form elements
205 * Adds required note, form attributes, validation javascript and form content.
207 * @param object An HTML_QuickForm object being visited
211 function finishForm(&$form)
213 // add a required note, if one is needed
214 if (@$this->_opts['req_note'] and !empty($form->_required) && !$form->_freezeAll) {
215 $this->_rows[] = array('<font size="-2">'.$form->getRequiredNote().'</font>', array('cabecera' => true));
217 // add a validation script
218 $this->_script = strval($form->getValidationScript());
219 $this->_append = "\n</form>\n";
220 } // end func finishForm
223 * Called when visiting a header element
225 * @param object An HTML_QuickForm_header element being visited
229 function renderHeader(&$header)
231 $this->_rows[] = array($header->toHtml(),
232 array($header->getName() => true));
233 } // end func renderHeader
236 * Renders an element Html
237 * Called when visiting an element
239 * @param object An HTML_QuickForm_element object being visited
240 * @param bool Whether an element is required
241 * @param string An error message associated with an element
245 function renderElement(&$element, $required, $error)
247 if (!$this->_inGroup) {
248 $this->_checkInElements();
249 $this->_elements[] = array(
250 $element->getLabel() . ($required ? '<font color="red">*</font>' : ''),
251 $element->toHtml() . ($error ? "<br><font color=\"red\">$error</font>" : ''));
253 $this->_groupElements[] = ($element->getLabel() ? ($element->getLabel().' ') : '') . $element->toHtml();
255 } // end func renderElement
258 * Renders an hidden element
259 * Called when visiting a hidden element
261 * @param object An HTML_QuickForm_hidden object being visited
265 function renderHidden(&$element)
267 $this->_prepend .= "\n\t". $element->toHtml();
268 } // end func renderHidden
271 * Called when visiting a raw HTML/text pseudo-element
273 * @param object An HTML_QuickForm_html element being visited
277 function renderHtml(&$data)
279 $this->_rows[] = array(&$data, array());
280 } // end func renderHtml
283 * Called when visiting a group, before processing any group elements
285 * @param object An HTML_QuickForm_group object being visited
286 * @param bool Whether a group is required
287 * @param string An error message associated with a group
291 function startGroup(&$group, $required, $error)
293 $this->_groupElements = array();
294 $this->_groupError = $error;
295 $this->_inGroup = true;
296 } // end func startGroup
299 * Called when visiting a group, after processing all group elements
301 * @param object An HTML_QuickForm_group object being visited
305 function finishGroup(&$group)
307 $name = $group->getName();
308 $sep = $group->_separator;
309 $label = $group->getLabel();
310 if (!@$this->_opts['btn_row'] and strtolower($name) == 'botones') {
313 if (@$this->_opts['btn_row']) {
314 $this->_rows[] = array(join('', $this->_groupElements), array('valign' => 'middle', 'align' => 'center'));
316 $this->_checkInElements();
317 $this->_elements[] = array($label, join($sep, $this->_groupElements));
318 $this->_inGroup = false;
320 } // end func finishGroup
322 function _checkInElements() {
323 if (is_null($this->_elementsPos)) {
324 $this->_elementsPos = count($this->_rows);
325 $this->_rows[] = false;
326 $this->_rows[] = false;
330 function setTable(&$table) {
331 $this->tabla =& $table;
335 return $this->tabla->getCSS();
338 } // end class HTML_QuickForm_Renderer_Default