cvs: pear /HTML_QuickForm2/QuickForm2 Element.php Factory.php Renderer.php /HTML_QuickForm2/QuickForm2/Container Group.php
/HTML_QuickForm2/QuickForm2/Renderer Default.php

From: Date: Fri, 01 May 2009 07:46:31 +0000
Subject: cvs: pear /HTML_QuickForm2/QuickForm2 Element.php Factory.php Renderer.php /HTML_QuickForm2/QuickForm2/Container Group.php
/HTML_QuickForm2/QuickForm2/Renderer Default.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-56020@lists.php.net to get a copy of this message
mansion Fri May 1 07:46:31 2009 UTC Added files: /pear/HTML_QuickForm2/QuickForm2/Container Group.php Modified files: /pear/HTML_QuickForm2/QuickForm2 Element.php Factory.php Renderer.php /pear/HTML_QuickForm2/QuickForm2/Renderer Default.php Log: Add group support. Still needs some work.

http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm2/QuickForm2/Element.php?r1=1.12&r2=1.13&diff_format=u Index: pear/HTML_QuickForm2/QuickForm2/Element.php diff -u pear/HTML_QuickForm2/QuickForm2/Element.php:1.12 pear/HTML_QuickForm2/QuickForm2/Element.php:1.13 --- pear/HTML_QuickForm2/QuickForm2/Element.php:1.12 Sat Jun 30 20:36:00 2007 +++ pear/HTML_QuickForm2/QuickForm2/Element.php Fri May 1 07:46:30 2009 @@ -39,7 +39,7 @@ * @author Alexey Borzov <avb@php.net> * @author Bertrand Mansion <golgote@mamasam.com> * @license http://opensource.org/licenses/bsd-license.php New BSD License - * @version CVS: $Id: Element.php,v 1.12 2007/06/30 20:36:00 avb Exp $ + * @version CVS: $Id: Element.php,v 1.13 2009/05/01 07:46:30 mansion Exp $ * @link http://pear.php.net/package/HTML_QuickForm2 */ @@ -89,7 +89,7 @@ public function getName() { - return $this->attributes['name']; + return isset($this->attributes['name']) ? $this->attributes['name'] : null; } public function setName($name) http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm2/QuickForm2/Factory.php?r1=1.23&r2=1.24&diff_format=u Index: pear/HTML_QuickForm2/QuickForm2/Factory.php diff -u pear/HTML_QuickForm2/QuickForm2/Factory.php:1.23 pear/HTML_QuickForm2/QuickForm2/Factory.php:1.24 --- pear/HTML_QuickForm2/QuickForm2/Factory.php:1.23 Sun Oct 14 18:58:31 2007 +++ pear/HTML_QuickForm2/QuickForm2/Factory.php Fri May 1 07:46:30 2009 @@ -39,7 +39,7 @@ * @author Alexey Borzov <avb@php.net> * @author Bertrand Mansion <golgote@mamasam.com> * @license http://opensource.org/licenses/bsd-license.php New BSD License - * @version CVS: $Id: Factory.php,v 1.23 2007/10/14 18:58:31 avb Exp $ + * @version CVS: $Id: Factory.php,v 1.24 2009/05/01 07:46:30 mansion Exp $ * @link http://pear.php.net/package/HTML_QuickForm2 */ @@ -73,6 +73,8 @@ 'HTML/QuickForm2/Element/InputCheckbox.php'), 'fieldset' => array('HTML_QuickForm2_Container_Fieldset', 'HTML/QuickForm2/Container/Fieldset.php'), + 'group' => array('HTML_QuickForm2_Container_Group', + 'HTML/QuickForm2/Container/Group.php'), 'file' => array('HTML_QuickForm2_Element_InputFile', 'HTML/QuickForm2/Element/InputFile.php'), 'hidden' => array('HTML_QuickForm2_Element_InputHidden', http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm2/QuickForm2/Renderer.php?r1=1.2&r2=1.3&diff_format=u Index: pear/HTML_QuickForm2/QuickForm2/Renderer.php diff -u pear/HTML_QuickForm2/QuickForm2/Renderer.php:1.2 pear/HTML_QuickForm2/QuickForm2/Renderer.php:1.3 --- pear/HTML_QuickForm2/QuickForm2/Renderer.php:1.2 Sun Jul 13 21:53:26 2008 +++ pear/HTML_QuickForm2/QuickForm2/Renderer.php Fri May 1 07:46:30 2009 @@ -39,7 +39,7 @@ * @author Alexey Borzov <avb@php.net> * @author Bertrand Mansion <golgote@mamasam.com> * @license http://opensource.org/licenses/bsd-license.php New BSD License - * @version CVS: $Id: Renderer.php,v 1.2 2008/07/13 21:53:26 mansion Exp $ + * @version CVS: $Id: Renderer.php,v 1.3 2009/05/01 07:46:30 mansion Exp $ * @link http://pear.php.net/package/HTML_QuickForm2 */ @@ -67,13 +67,20 @@ protected $byName = array(); /** + * Stores renderer callbacks by element id + * @var array + */ + protected $byId = array(); + + /** * Renders a node * * The method will first check if a PHP callback was given as parameter * and then use it to render the node. Otherwise, it will look into - * the callbacks set by name, comparing them with the node name. Finally, - * it will try to find a callback using the node class. If no renderer - * callback is found it will throw an exception. + * the callbacks set by id, comparing them with the node id. If none is + * found, it will do the same with the node name. + * Finally, it will try to find a callback using the node class. + * If no renderer callback is found it will throw an exception. * * @param HTML_QuickForm2_Node Node element to render * @param mixed PHP callback used to render the node @@ -93,6 +100,10 @@ ); } } + $id = $node->getId(); + if (isset($this->byId[$id])) { + return call_user_func_array($this->byId[$id], array(&$this, &$node)); + } $name = $node->getName(); if ($name && isset($this->byName[$name])) { return call_user_func_array($this->byName[$name], array(&$this, &$node)); @@ -111,6 +122,27 @@ ); } + /** + * Set a renderer callback using node name + * + * Nodes can be rendered either using their name or their class. Upon + * rendering, if a matching callback is found using the node's name, + * it will be used to render the node. + * + * @param string Node id + * @param mixed PHP callback used to render the node + * @throws HTML_QuickForm2_InvalidArgumentException if the provided + * callback is not callable + */ + public function setById($id, $callback) + { + if (!is_callable($callback)) { + throw new HTML_QuickForm2_InvalidArgumentException( + "Renderer callback is not valid" + ); + } + $this->byId[$id] = $callback; + } /** * Set a renderer callback using node name http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm2/QuickForm2/Renderer/Default.php?r1=1.6&r2=1.7&diff_format=u Index: pear/HTML_QuickForm2/QuickForm2/Renderer/Default.php diff -u pear/HTML_QuickForm2/QuickForm2/Renderer/Default.php:1.6 pear/HTML_QuickForm2/QuickForm2/Renderer/Default.php:1.7 --- pear/HTML_QuickForm2/QuickForm2/Renderer/Default.php:1.6 Sun Mar 8 10:13:29 2009 +++ pear/HTML_QuickForm2/QuickForm2/Renderer/Default.php Fri May 1 07:46:31 2009 @@ -39,7 +39,7 @@ * @author Alexey Borzov <avb@php.net> * @author Bertrand Mansion <golgote@mamasam.com> * @license http://opensource.org/licenses/bsd-license.php New BSD License - * @version CVS: $Id: Default.php,v 1.6 2009/03/08 10:13:29 mansion Exp $ + * @version CVS: $Id: Default.php,v 1.7 2009/05/01 07:46:31 mansion Exp $ * @link http://pear.php.net/package/HTML_QuickForm2 */ @@ -124,6 +124,10 @@ array($this, 'renderContainer') ); $this->setByClassRenderer( + 'HTML_QuickForm2_Container_Group', + array($this, 'renderGroup') + ); + $this->setByClassRenderer( 'HTML_QuickForm2', array($this, 'renderForm') ); @@ -161,12 +165,11 @@ } } - $html = '<div class="qf-checkable' . $errorClass . '">' . - $errorMsg . - '<span class="' . $labelClass . '">' . $checkable->getLabel() . '</span>' . - $checkable . - '</div>'; - + $html = '<div class="qf-checkable' . $errorClass . '">' . $errorMsg; + if (!empty($label)) { + $html .= '<span class="' . $labelClass . '">' . $checkable->getLabel() . '</span>'; + } + $html .= $checkable.'</div>'; return $html; } @@ -180,7 +183,7 @@ public function renderElement(HTML_QuickForm2_Renderer $renderer, HTML_QuickForm2_Element $element) { - + $label = $element->getLabel(); $labelClass = 'qf-label'; $errorClass = ''; $errorMsg = ''; @@ -199,13 +202,57 @@ $errorMsg = '<div class="qf-message">'.$error.'</div>'; } } - $html = '<div class="qf-element'.$errorClass.'">' . - $errorMsg . - '<label for="' . $element->getId() . '" class="' . $labelClass . '">' . - $element->getLabel() . '</label>' . - $element . - '</div>'; + $html = '<div class="qf-element'.$errorClass.'">' . $errorMsg; + if (!empty($label)) { + $html .= '<label for="' . $element->getId() . '" class="' . $labelClass . '">' . + $label . '</label>'; + } + $html .= '<div class="qf-input">' . $element . '</div>' . + '</div>'; + return $html; + } + + + /** + * Renders a group container + * + * @param HTML_QuickForm2_Container_Group Group container to render + * @return string HTML output + */ + public function renderGroup(HTML_QuickForm2_Renderer $renderer, + HTML_QuickForm2_Container_Group $group) + { + $class = trim((string)($group->getAttribute('class')).' qf-group'); + $label = $group->getLabel(); + $labelClass = 'qf-label'; + $errorMsg = ''; + + if ($group->isRequired()) { + $renderer->hasRequired = true; + $labelClass .= ' qf-required'; + } + + $error = $group->getError(); + if ($error) { + $class .= ' qf-error'; + if ($renderer->options['group_errors']) { + $renderer->errors[] = $error; + } else { + $errorMsg = '<div class="qf-message">'.$error.'</div>'; + } + } + $group->setAttribute('class', $class); + $html = '<div' . $group->getAttributes(true) . '>'; + if (!empty($label)) { + $html .= '<div class="qf-label ' . $labelClass . '">' . + $label . '</div>'; + } + $html .= $errorMsg . + '<div class="qf-group-elements">' . + $group->render($renderer) . + '</div>' . + '</div>'; return $html; } http://cvs.php.net/viewvc.cgi/pear/HTML_QuickForm2/QuickForm2/Container/Group.php?view=markup&rev=1.1 Index: pear/HTML_QuickForm2/QuickForm2/Container/Group.php +++ pear/HTML_QuickForm2/QuickForm2/Container/Group.php <?php /** * Base class for HTML_QuickForm2 groups * * PHP version 5 * * LICENSE: * * Copyright (c) 2006, 2007, Alexey Borzov <avb@php.net>, * Bertrand Mansion <golgote@mamasam.com> * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * The names of the authors may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * @category HTML * @package HTML_QuickForm2 * @author Alexey Borzov <avb@php.net> * @author Bertrand Mansion <golgote@mamasam.com> * @license http://opensource.org/licenses/bsd-license.php New BSD License * @version CVS: $Id: Group.php,v 1.1 2009/05/01 07:46:31 mansion Exp $ * @link http://pear.php.net/package/HTML_QuickForm2 */ /** * Base class for all HTML_QuickForm2 containers */ require_once 'HTML/QuickForm2/Container.php'; /** * Base class for QuickForm2 groups of elements * * @category HTML * @package HTML_QuickForm2 * @author Alexey Borzov <avb@php.net> * @author Bertrand Mansion <golgote@mamasam.com> * @version Release: @package_version@ */ class HTML_QuickForm2_Container_Group extends HTML_QuickForm2_Container { /** * Group name * If set, group name will be used as prefix for contained * element names, like groupname[elementname]. * @var string */ protected $name; /** * Previous group name * Stores the previous group name when the group name is changed. * Used to restore children names if necessary. * @var string */ protected $previousName; public function getType() { return 'group'; } public function setValue($value) { throw new HTML_QuickForm2_Exception('Not implemented'); } public function getName() { return $this->name; } public function setName($name) { $this->previousName = $this->name; $this->name = $name; foreach ($this as $child) { $this->renameChild($child); } return $this; } protected function renameChild(HTML_QuickForm2_Node $element) { $tokens = explode('[', str_replace(']', '', $element->getName())); if ($this === $element->getContainer()) { // Child has already been renamed by its group before if (!is_null($this->previousName) && $this->previousName !== '') { $gtokens = explode('[', str_replace(']', '', $this->previousName)); $pos = array_search(end($gtokens), $tokens); if (!is_null($pos)) { $tokens = array_slice($tokens, $pos+1); } } } if (is_null($this->name) || $this->name === '') { if (is_null($this->previousName) || $this->previousName === '') { return $element; } else { $elname = $tokens[0]; unset($tokens[0]); foreach ($tokens as $v) { $elname .= '['.$v.']'; } } } else { $elname = $this->getName().'['.implode('][', $tokens).']'; } $element->setName($elname); return $element; } public function toggleFrozen($freeze = null) { if (null !== $freeze) { foreach ($this as $child) { $child->toggleFrozen($freeze); } } return parent::toggleFrozen($freeze); } public function persistentFreeze($persistent = null) { if (null !== $persistent) { foreach ($this as $child) { $child->persistentFreeze($persistent); } } return parent::persistentFreeze($persistent); } /** * Appends an element to the container * * If the element was previously added to the container or to another * container, it is first removed there. * * @param HTML_QuickForm2_Node Element to add * @return HTML_QuickForm2_Node Added element * @throws HTML_QuickForm2_InvalidArgumentException */ public function appendChild(HTML_QuickForm2_Node $element) { if ($this === $element->getContainer()) { $this->removeChild($element); } $this->renameChild($element); $element->setContainer($this); $this->elements[] = $element; return $element; } /** * Performs the server-side validation * * This method also calls validate() on all contained elements. * * @return boolean Whether the container and all contained elements are valid */ protected function validate() { $valid = parent::validate(); foreach ($this as $child) { $valid = $child->validate() && $valid; } return $valid; } /** * Renders the group using an HTML_QuickForm2_Renderer * * @param HTML_QuickForm2_Renderer QuickForm2 renderer * @return string HTML output * @throws HTML_QuickForm2_NotFoundException if the renderer is provided * but no render callback was defined in the renderer */ public function render(HTML_QuickForm2_Renderer $renderer) { $html = array(); foreach ($this as $element) { $html[] = $renderer->render($element); } return implode(self::getOption('linebreak'), $html); } /** * Default rendering * * @return string HTML output */ public function __toString() { $html = array(); foreach ($this as $element) { $html[] = $element; } return implode(self::getOption('linebreak'), $html); } } ?>
« previous php.pear.cvs (#56020) next »