------------------------------------------------------------------------------- $Id: Tabla.php 462 2004-01-09 21:11:01Z www-data $ -----------------------------------------------------------------------------*/ require_once 'HTML/QuickForm/Renderer.php'; require_once 'MLIB/HTML/Tabla.php'; /** * QuickForm renderer que usa Tabla como backend. Es similar a * MLIB_HTML_QuickForm_Renderer_Tabla pero dibuja la tabla horizontalmente. * Ej: * * $form = new MLIB_HTML_QuickForm(); * $form->renderer = new MLIB_HTML_QuickForm_Renderer_TablaHorizontal(array('width' => 400)); * // Sigo creando el formulario normalmente... * */ class MLIB_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(); /** * Guarda opciones del renderer. * @private */ var $_opts = 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 $param Array o sting con el estilo de la tabla u objeto * tabla alternativo para usar. * @param $opts Opciones, pueden ser: * - btn_row: Si es true, se dibujan los botones en una fila * completa, después de los campos. Si es false, se * dibuja como un elemento con un rowspan para que * ocupe la celda del label y del contenido. * - req_note: Si es true, se incluye una fila adicional con la * 'require note'. Si es false no se la muestra. * * @access public */ function MLIB_HTML_QuickForm_Renderer_TablaHorizontal($param = array(), $opts = array()) { parent::HTML_QuickForm_Renderer(); $this->_opts = $opts; if (is_a($param, 'Tabla')) { $this->setTable($param); } else { $this->setTable(new MLIB_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) = $e; $cols = array(); for ($i = 0; $i < $pos; ++$i) { $cols[] = ''; } if (is_null($label)) { // Para caso especial como botones. $cols[] = $elem; $cols[] = ''; } else { // Para caso común. $cols[] = $label; $cols[] = $elem; } for ($i = $pos + 2; $i < $rows; ++$i) { $cols[] = ''; } $id = $this->tabla->addCol($cols, array(array('align' => 'center'))); if (is_null($label)) { // Para caso especial como botones. $this->tabla->updateCellAttributes($pos, $id, array('rowspan' => 2)); } else { // Para caso común. $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 (@$this->_opts['req_note'] and !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" : '')); } 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; $label = $group->getLabel(); if (!@$this->_opts['btn_row'] and strtolower($name) == 'botones') { $label = null; } if (@$this->_opts['btn_row']) { $this->_rows[] = array(join('', $this->_groupElements), array('valign' => 'middle', 'align' => 'center')); } else { $this->_checkInElements(); $this->_elements[] = array($label, join($sep, $this->_groupElements)); $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 ?>