PEAR is discontinued

This is a static archive preserving historical releases and metadata. No new packages or updates will be published. For updates to existing packages, or to publish new packages, please use Composer and Packagist.

Home » HTML » HTML_QuickForm2 » Bug #10193

[patch] add addOptionalElement() to HTML_Quickform

Details

Request #10193[patch] add addOptionalElement() to HTML_Quickform
Submitted2007-02-26 15:52 UTC
Fromgreg at mtechsolutions dot ca
StatusWont fix
PackageHTML_QuickForm2
PHP VersionIrrelevant
Roadmaps(Not assigned)

Comments

[2007-02-26 15:52 UTC] greg at mtechsolutions dot ca

Description:
------------
This may be a bit too specialized for Quickform, but I will submit the patch anyways in case someone has some interest. I also welcome feedback on the implementation.

Adds an addOptionalElement() function that adds an element which includes a checkbox next to it. If the checkbox is not selected, the element is disabled set to a 'standard value' (defined in the call to addOptionalElement()). If checked, the element becomes active and the user can set it to whatever they want. If a default value is passed via HTML_Quickform::setDefaults(), the checkbox is selected, and the default value is shown in the control. If a constant is specified with HTML_Quickform::setConstants(), the value of the control is set to the constant, and the checkbox is disabled.

The element can be any quickform element, and this requires patch 10191 (http://pear.php.net/bugs/10191).

Note that this does require prototype.js (http://www.prototypejs.org/) and prototypeUtils.js (http://jehiah.cz/archive/form-element-setvalue - for the JS function setValue()). Although it could probably be written in native JS, for my purposes prototype is fine, and I don't see much point putting in the effort and duplicating their code if this patch is in fact too specific to be added to HTML_Quickform anyways.

Original patch: http://freepbx.org/trac/changeset/3815

Test script:
---------------
Index: /freepbx/branches/quickform/amp_conf/htdocs/admin/common/HTML/QuickForm/optionalgroup.php
===================================================================
--- /freepbx/branches/quickform/amp_conf/htdocs/admin/common/HTML/QuickForm/optionalgroup.php (revision 3815)
+++ /freepbx/branches/quickform/amp_conf/htdocs/admin/common/HTML/QuickForm/optionalgroup.php (revision 3815)
@@ -0,0 +1,199 @@
+<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4: */
+// +----------------------------------------------------------------------+
+// | PHP version 4.0 |
+// +----------------------------------------------------------------------+
+// | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group |
+// +----------------------------------------------------------------------+
+// | This source file is subject to version 2.0 of the PHP license, |
+// | that is bundled with this package in the file LICENSE, and is |
+// | available at through the world-wide-web at |
+// | http://www.php.net/license/2_02.txt. |
+// | If you did not receive a copy of the PHP license and are unable to |
+// | obtain it through the world-wide-web, please send a note to |
+// | license@php.net so we can mail you a copy immediately. |
+// +----------------------------------------------------------------------+
+// | Authors: Adam Daniel <adaniel1@eesus.jnj.com> |
+// | Bertrand Mansion <bmansion@mamasam.com> |
+// +----------------------------------------------------------------------+
+//
+// $Id: group.php,v 1.38 2006/01/11 17:13:20 mansion Exp $
+
+require_once("HTML/QuickForm/group.php");
+
+/**
+ * HTML class for a form element group
+ *
+ * @author Adam Daniel <adaniel1@eesus.jnj.com>
+ * @author Bertrand Mansion <bmansion@mamasam.com>
+ * @version 1.0
+ * @since PHP4.04pl1
+ * @access public
+ */
+class HTML_QuickForm_optionalgroup extends HTML_QuickForm_group
+{
+ var $_real_name;
+ var $_standard_value;
+
+ var $_ele_standard = 0;
+ var $_ele_checkbox = 2;
+ var $_ele_control = 1;
+
+ // {{{ constructor
+
+ /**
+ * Class constructor
+ *
+ * @param mixed $standardValue (optional)The value to use if the user doesn't specify a value.
+ * If an array is passed, uses $standardValue[$elementName] if found.
+ * @param string $elementName (optional)The name of the element
+ * @param array $elementLabel (optional)The label to use
+ * @param array $elementObject (optional)The element to use as the main control
+ * @since 1.0
+ * @access public
+ * @return void
+ */
+ function HTML_QuickForm_optionalgroup($standardValue=null, $elementName=null, $elementLabel=null, $elementObject=null)
+ {
+ if (is_null($elementObject)) {
+ return;
+ }
+
+ if (is_array($standardValue) && isset($standardValue[$elementName])) {
+ $this->_standard_value = $standardValue[$elementName];
+ } else {
+ $this->_standard_value = $standardValue;
+ }
+
+ $elementObject->_generateId();
+ $id = $elementObject->getAttribute('id');
+
+ $this->_real_name = $elementObject->getName();
+
+ $elements = array();
+ // hidden element to store default value
+ $elements[$this->_ele_standard] =& HTML_QuickForm::createElement('hidden', 'qf_default_'.$elementName, null);
+ $elements[$this->_ele_standard]->_generateId();
+ $hidden_id = $elements[$this->_ele_standard]->getAttribute('id');
+
+ // checkbox element, to select that real element is editable
+ /*
+ $checkattrs = array(
+ 'onclick' => //javascript
+ 'if (this.checked) { '.
+ ' document.getElementById(\''.$id.'\').disabled = false; '.
+ ' document.getElementById(\''.$id.'\').class = \'disabled\'; '.
+ '} else { '.
+ ' document.getElementById(\''.$id.'\').disabled = true; '.
+ ' document.getElementById(\''.$id.'\').class = \'\'; '.
+ ' document.getElementById(\''.$id.'\').value = document.getElementById(\''.$hidden_id.'\').value;'.
+ ' document.getElementById(\''.$id.'\').focus();'.
+ '}',
+ );
+ */
+ $checkattrs = array(
+ 'onclick' => //javascript
+ 'if (this.checked) { '.
+ ' $(\''.$id.'\').enable();'.
+ ' $(\''.$id.'\').class = \'disabled\'; '.
+ '} else { '.
+ ' $(\''.$id.'\').disable();'.
+ ' $(\''.$id.'\').class = \'\'; '.
+ ' Form.Element.setValue(\''.$id.'\', $(\''.$hidden_id.'\').getValue() );'.
+ ' $(\''.$id.'\').activate();'.
+ '} alert($(\''.$id.'\').class);',
+ 'title' => 'Enable/disable this control',
+ 'class' => 'enablercheck',
+ );
+
+
+ $elements[$this->_ele_checkbox] =& HTML_QuickForm::createElement('checkbox', 'qf_checkbox_'.$elementName, null, null, $checkattrs);
+
+ // real control element
+ $elements[$this->_ele_control] =& $elementObject;
+
+ ksort($elements);
+
+ //'qf_optionalgroup_'.$elementName
+ parent::HTML_QuickForm_group($elementName, $elementLabel, $elements, ' ', true);
+ } //end constructor
+
+ // }}}
+
+ // {{{ onQuickFormEvent()
+
+ /**
+ * Called by HTML_QuickForm whenever form event is made on this element
+ *
+ * @param string $event Name of event
+ * @param mixed $arg event arguments
+ * @param object $caller calling object
+ * @since 1.0
+ * @access public
+ * @return void
+ */
+ function onQuickFormEvent($event, $arg, &$caller)
+ {
+ switch ($event) {
+ case 'updateValue':
+ $this->_createElementsIfNotExist();
+ // elements[0] is hidden default element
+ // elements[1] is checkbox,
+ // elements[2] is actual control
+
+ // constant values override both default and submitted ones
+ // default values are overriden by submitted
+ if (isset($caller->_constantValues[$this->getName()])) {
+ // constant in use, so this is always checked, and textbox is always disabled
+ $this->_elements[$this->_ele_checkbox]->setChecked(false);
+ $this->_elements[$this->_ele_checkbox]->setDisabled(true);
+ $this->_elements[$this->_ele_checkbox]->addClass('disabled');
+
+ $this->_elements[$this->_ele_control]->setDisabled(true);
+ $this->_elements[$this->_ele_control]->addClass('disabled');
+ $this->_elements[$this->_ele_control]->setValue($caller->_constantValues[$this->getName()]);
+ } else {
+ // checkbox control is enabled
+ $this->_elements[$this->_ele_checkbox]->setDisabled(false);
+ $this->_elements[$this->_ele_checkbox]->removeClass('disabled');
+
+ // get the 'standard' value
+ $default = isset($caller->_defaultValues[$this->getName()]) ? $caller->_defaultValues[$this->getName()] : null;
+
+ // save default value to the form
+ //TODO $this->_elements[0]->setValue($default);
+ $this->_elements[0]->setValue($this->_standard_value);
+
+ if ($caller->isSubmitted() && isset($caller->_submitValues[$this->getName()])
+ && $caller->_submitValues[$this->getName()] != $this->_standard_value) {
+ // submitted, not using "standard"
+ $this->_elements[$this->_ele_checkbox]->setChecked(true);
+ $this->_elements[$this->_ele_control]->setDisabled(false);
+ $this->_elements[$this->_ele_control]->removeClass('disabled');
+ $this->_elements[$this->_ele_control]->setValue($caller->_submitValues[$this->getName()]);
+ } else if ($default) {
+ // not submitted, and a default is set
+ $this->_elements[$this->_ele_checkbox]->setChecked(true);
+ $this->_elements[$this->_ele_control]->setDisabled(false);
+ $this->_elements[$this->_ele_control]->removeClass('disabled');
+ $this->_elements[$this->_ele_control]->setValue($default);
+ } else {
+ // not submitted, or value is = standard value
+ $this->_elements[$this->_ele_checkbox]->setChecked(false);
+ $this->_elements[$this->_ele_control]->setDisabled(true);
+ $this->_elements[$this->_ele_control]->addClass('disabled');
+ $this->_elements[$this->_ele_control]->setValue($this->_standard_value);
+ }
+ }
+
+ break;
+
+ default:
+ parent::onQuickFormEvent($event, $arg, $caller);
+ }
+ return true;
+ } // end func onQuickFormEvent
+
+ // }}}
+
+}
Index: /freepbx/branches/quickform/amp_conf/htdocs/admin/common/HTML/QuickForm.php
===================================================================
--- /freepbx/branches/quickform/amp_conf/htdocs/admin/common/HTML/QuickForm.php (revision 3661)
+++ /freepbx/branches/quickform/amp_conf/htdocs/admin/common/HTML/QuickForm.php (revision 3815)
@@ -49,4 +49,5 @@
'xbutton' =>array('HTML/QuickForm/xbutton.php','HTML_QuickForm_xbutton'),
'advmultiselect'=>array('HTML/QuickForm/advmultiselect.php','HTML_QuickForm_advmultiselect'),
+ 'optionalgroup' =>array('HTML/QuickForm/optionalgroup.php','HTML_QuickForm_optionalgroup'),
);

@@ -727,4 +728,40 @@
return $group;
} // end func addGroup
+
+ // }}}
+ // {{{ addOptionalElement()
+
+ /**
+ * Adds an optional element, using a checkbox
+ * @param array $elements array of elements composing the group
+ * @param string $name (optional)group name
+ * @param string $groupLabel (optional)group label
+ * @param string $separator (optional)string to separate elements
+ * @param string $appendName (optional)specify whether the group name should be
+ * used in the form element name ex: group[element]
+ * @return object reference to added group of elements
+ * @since 2.8
+ * @access public
+ * @throws PEAR_Error
+ */
+ function &addOptionalElement($element, $name, $standardValue, $groupLabel /*, optional multiple params.. */)
+ {
+ if (is_object($element) && is_subclass_of($element, 'html_quickform_element')) {
+ $elementObject = &$element;
+ } else {
+ // get arguments for elementObject
+ $args = array_slice(func_get_args(),3);
+ array_unshift($args, null); // stick null on the beginning (element name)
+
+ $elementObject =& $this->_loadElement('addElement', $element, $args);
+ if (PEAR::isError($elementObject)) {
+ return $elementObject;
+ }
+ }
+
+ $group =& $this->addElement('optionalgroup', $standardValue, $name, $groupLabel, $elementObject);
+ return $group;
+
+ } // end func addOptionalElement

// }}}