From 2d6ca96b34bdd5d21deca5afa95603ae7ba0a7c0 Mon Sep 17 00:00:00 2001 From: Leandro Lucarella Date: Tue, 30 Mar 2004 20:42:11 +0000 Subject: [PATCH 1/1] Se agrega un nuevo renderer. --- .../QuickForm/Renderer/TablaHorizontal.php | 315 ++++++++++++++++++ 1 file changed, 315 insertions(+) create mode 100644 lib/MECON/HTML/QuickForm/Renderer/TablaHorizontal.php diff --git a/lib/MECON/HTML/QuickForm/Renderer/TablaHorizontal.php b/lib/MECON/HTML/QuickForm/Renderer/TablaHorizontal.php new file mode 100644 index 0000000..5b436a9 --- /dev/null +++ b/lib/MECON/HTML/QuickForm/Renderer/TablaHorizontal.php @@ -0,0 +1,315 @@ + +------------------------------------------------------------------------------- +$Id: Tabla.php 462 2004-01-09 21:11:01Z www-data $ +-----------------------------------------------------------------------------*/ + +require_once 'HTML/QuickForm/Renderer.php'; +require_once 'MECON/HTML/Tabla.php'; + +/** + * QuickForm renderer que usa Tabla como backend. Es similar a + * MECON_HTML_QuickForm_Renderer_Tabla pero dibuja la tabla horizontalmente. + * Ej: + * + * $form = new MECON_HTML_QuickForm(); + * $form->renderer = new MECON_HTML_QuickForm_Renderer_TablaHorizontal(array('width' => 400)); + * // Sigo creando el formulario normalmente... + * + */ +class MECON_HTML_QuickForm_Renderer_TablaHorizontal extends HTML_QuickForm_Renderer { + + /** + * Tabla usada para dibujar el formulario. + * @var object Tabla + * @access private + */ + var $tabla; + + /** + * Estructura de la tabla en filas a usar para renderizar. + * @access private + */ + var $_rows = array(); + + /** + * Guarda posición de la fila donde comienzan los elementos. + * @private + */ + var $_elementsPos = null; + + /** + * Guarda elementos del formulario. + * @private + */ + var $_elements = array(); + + /** + * HTML con los scripts para poner antes del formulario (tipicamente + * un javascript). + * @var string + * @access private + */ + var $_script = ''; + + /** + * HTML para agregar antes de la tabla (tipicamente un javascript). + * @var string + * @access private + */ + var $_prepend = ''; + + /** + * HTML para agregar despues de la tabla. + * @var string + * @access private + */ + var $_append = ''; + + /** + * True if we are inside a group + * @var bool + * @access private + */ + var $_inGroup = false; + + /** + * Group error related message. + * @var string + * @access private + */ + var $_groupError = ''; + + /** + * Array with HTML generated for group elements + * @var array + * @access private + */ + var $_groupElements = array(); + + /** + * Constructor. + * + * @param mixed $param Array o sting con el estilo de la tabla u objeto + * tabla alternativo para usar. + * + * @access public + */ + function MECON_HTML_QuickForm_Renderer_TablaHorizontal($param = array()) + { + parent::HTML_QuickForm_Renderer(); + if (is_a($param, 'Tabla')) { + $this->setTable($param); + } + else { + $this->setTable(new MECON_HTML_Tabla($param)); + } + } // end constructor + + /** + * returns the HTML generated for the form + * + * @access public + * @return string + */ + function toHtml() + { + $pos = $this->_elementsPos; + $rows = count($this->_rows); + foreach ($this->_elements as $e) { + list($label, $elem, $req, $err) = $e; + $cols = array(); + for ($i = 0; $i < $pos; ++$i) { + $cols[] = ''; + } + $cols[] = $label . ($req ? '*' : ''); + $cols[] = $elem . ($err ? "
$err" : ''); + for ($i = $pos + 2; $i < $rows; ++$i) { + $cols[] = ''; + } + $id = $this->tabla->addCol($cols, array(array('align' => 'center'))); + $this->tabla->updateCellAttributes($pos, $id, array('titulo' => true)); + } + foreach ($this->_rows as $i => $row) { + if ($row) { // Si no son las filas de los elementos... + list($html, $attrs) = $row; + $attrs['colspan'] = count($this->_elements); + $this->tabla->updateRowAttributes($i, $attrs); + $this->tabla->setCellContents($i, 0, $html); + } + } + return $this->_script . $this->_prepend . + $this->tabla->toHtml() . + $this->_append; + } // end func toHtml + + /** + * Called when visiting a form, before processing any form elements + * + * @param object An HTML_QuickForm object being visited + * @access public + * @return void + */ + function startForm(&$form) + { + $attrs = $form->getAttributes(true); + $this->_prepend = "\n\n"; + + } // end func startForm + + /** + * Called when visiting a form, after processing all form elements + * Adds required note, form attributes, validation javascript and form content. + * + * @param object An HTML_QuickForm object being visited + * @access public + * @return void + */ + function finishForm(&$form) + { + // add a required note, if one is needed + if (!empty($form->_required) && !$form->_freezeAll) { + $this->_rows[] = array(''.$form->getRequiredNote().'', array('cabecera' => true)); + } + // add a validation script + $this->_script = strval($form->getValidationScript()); + $this->_append = "\n\n"; + } // end func finishForm + + /** + * Called when visiting a header element + * + * @param object An HTML_QuickForm_header element being visited + * @access public + * @return void + */ + function renderHeader(&$header) + { + $this->_rows[] = array($header->toHtml(), + array($header->getName() => true)); + } // end func renderHeader + + /** + * Renders an element Html + * Called when visiting an element + * + * @param object An HTML_QuickForm_element object being visited + * @param bool Whether an element is required + * @param string An error message associated with an element + * @access public + * @return void + */ + function renderElement(&$element, $required, $error) + { + if (!$this->_inGroup) { + $this->_checkInElements(); + $this->_elements[] = array( + $element->getLabel() . ($required ? '*' : ''), + $element->toHtml() . ($error ? "
$error" : ''), + $required, $error); + } else { + $this->_groupElements[] = ($element->getLabel() ? ($element->getLabel().' ') : '') . $element->toHtml(); + } + } // end func renderElement + + /** + * Renders an hidden element + * Called when visiting a hidden element + * + * @param object An HTML_QuickForm_hidden object being visited + * @access public + * @return void + */ + function renderHidden(&$element) + { + $this->_prepend .= "\n\t". $element->toHtml(); + } // end func renderHidden + + /** + * Called when visiting a raw HTML/text pseudo-element + * + * @param object An HTML_QuickForm_html element being visited + * @access public + * @return void + */ + function renderHtml(&$data) + { + $this->_rows[] = array(&$data, array()); + } // end func renderHtml + + /** + * Called when visiting a group, before processing any group elements + * + * @param object An HTML_QuickForm_group object being visited + * @param bool Whether a group is required + * @param string An error message associated with a group + * @access public + * @return void + */ + function startGroup(&$group, $required, $error) + { + $this->_groupElements = array(); + $this->_groupError = $error; + $this->_inGroup = true; + } // end func startGroup + + /** + * Called when visiting a group, after processing all group elements + * + * @param object An HTML_QuickForm_group object being visited + * @access public + * @return void + */ + function finishGroup(&$group) + { + $name = $group->getName(); + $sep = $group->_separator; + if (strtolower($name) == 'botones') { + $this->_rows[] = array(join('', $this->_groupElements), array('valign' => 'middle', 'align' => 'center')); + } else { + $this->_checkInElements(); + $this->_elements[] = array(array($group->getLabel(), + join($sep, $this->_groupElements)), false, false); + } + $this->_inGroup = false; + } // end func finishGroup + + function _checkInElements() { + if (is_null($this->_elementsPos)) { + $this->_elementsPos = count($this->_rows); + $this->_rows[] = false; + $this->_rows[] = false; + } + } + + function setTable(&$table) { + $this->tabla =& $table; + } + + function getCSS() { + return $this->tabla->getCSS(); + } + +} // end class HTML_QuickForm_Renderer_Default + +?> -- 2.43.0