HTML_QuickForm
[ class tree: HTML_QuickForm ] [ index: HTML_QuickForm ] [ all elements ]

Source for file QuickForm.php

Documentation is available at QuickForm.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0                                                      |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Adam Daniel <adaniel1@eesus.jnj.com>                        |
  17. // |          Bertrand Mansion <bmansion@mamasam.com>                     |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: QuickForm.php,v 1.150 2004/10/18 14:01:10 avb Exp $
  21.  
  22. require_once('PEAR.php');
  23. require_once('HTML/Common.php');
  24.  
  25. $GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES'
  26.         array(
  27.             'group'         =>array('HTML/QuickForm/group.php','HTML_QuickForm_group'),
  28.             'hidden'        =>array('HTML/QuickForm/hidden.php','HTML_QuickForm_hidden'),
  29.             'reset'         =>array('HTML/QuickForm/reset.php','HTML_QuickForm_reset'),
  30.             'checkbox'      =>array('HTML/QuickForm/checkbox.php','HTML_QuickForm_checkbox'),
  31.             'file'          =>array('HTML/QuickForm/file.php','HTML_QuickForm_file'),
  32.             'image'         =>array('HTML/QuickForm/image.php','HTML_QuickForm_image'),
  33.             'password'      =>array('HTML/QuickForm/password.php','HTML_QuickForm_password'),
  34.             'radio'         =>array('HTML/QuickForm/radio.php','HTML_QuickForm_radio'),
  35.             'button'        =>array('HTML/QuickForm/button.php','HTML_QuickForm_button'),
  36.             'submit'        =>array('HTML/QuickForm/submit.php','HTML_QuickForm_submit'),
  37.             'select'        =>array('HTML/QuickForm/select.php','HTML_QuickForm_select'),
  38.             'hiddenselect'  =>array('HTML/QuickForm/hiddenselect.php','HTML_QuickForm_hiddenselect'),
  39.             'text'          =>array('HTML/QuickForm/text.php','HTML_QuickForm_text'),
  40.             'textarea'      =>array('HTML/QuickForm/textarea.php','HTML_QuickForm_textarea'),
  41.             'link'          =>array('HTML/QuickForm/link.php','HTML_QuickForm_link'),
  42.             'advcheckbox'   =>array('HTML/QuickForm/advcheckbox.php','HTML_QuickForm_advcheckbox'),
  43.             'date'          =>array('HTML/QuickForm/date.php','HTML_QuickForm_date'),
  44.             'static'        =>array('HTML/QuickForm/static.php','HTML_QuickForm_static'),
  45.             'header'        =>array('HTML/QuickForm/header.php''HTML_QuickForm_header'),
  46.             'html'          =>array('HTML/QuickForm/html.php''HTML_QuickForm_html'),
  47.             'hierselect'    =>array('HTML/QuickForm/hierselect.php''HTML_QuickForm_hierselect'),
  48.             'autocomplete'  =>array('HTML/QuickForm/autocomplete.php''HTML_QuickForm_autocomplete'),
  49.             'xbutton'       =>array('HTML/QuickForm/xbutton.php','HTML_QuickForm_xbutton')
  50.         );
  51.  
  52. $GLOBALS['_HTML_QuickForm_registered_rules'= array(
  53.     'required'      => array('html_quickform_rule_required''HTML/QuickForm/Rule/Required.php'),
  54.     'maxlength'     => array('html_quickform_rule_range',    'HTML/QuickForm/Rule/Range.php'),
  55.     'minlength'     => array('html_quickform_rule_range',    'HTML/QuickForm/Rule/Range.php'),
  56.     'rangelength'   => array('html_quickform_rule_range',    'HTML/QuickForm/Rule/Range.php'),
  57.     'email'         => array('html_quickform_rule_email',    'HTML/QuickForm/Rule/Email.php'),
  58.     'regex'         => array('html_quickform_rule_regex',    'HTML/QuickForm/Rule/Regex.php'),
  59.     'lettersonly'   => array('html_quickform_rule_regex',    'HTML/QuickForm/Rule/Regex.php'),
  60.     'alphanumeric'  => array('html_quickform_rule_regex',    'HTML/QuickForm/Rule/Regex.php'),
  61.     'numeric'       => array('html_quickform_rule_regex',    'HTML/QuickForm/Rule/Regex.php'),
  62.     'nopunctuation' => array('html_quickform_rule_regex',    'HTML/QuickForm/Rule/Regex.php'),
  63.     'nonzero'       => array('html_quickform_rule_regex',    'HTML/QuickForm/Rule/Regex.php'),
  64.     'callback'      => array('html_quickform_rule_callback''HTML/QuickForm/Rule/Callback.php'),
  65.     'compare'       => array('html_quickform_rule_compare',  'HTML/QuickForm/Rule/Compare.php')
  66. );
  67.  
  68. // {{{ error codes
  69.  
  70. /*
  71.  * Error codes for the QuickForm interface, which will be mapped to textual messages
  72.  * in the QuickForm::errorMessage() function.  If you are to add a new error code, be
  73.  * sure to add the textual messages to the QuickForm::errorMessage() function as well
  74.  */
  75.  
  76. define('QUICKFORM_OK',                      1);
  77. define('QUICKFORM_ERROR',                  -1);
  78. define('QUICKFORM_INVALID_RULE',           -2);
  79. define('QUICKFORM_NONEXIST_ELEMENT',       -3);
  80. define('QUICKFORM_INVALID_FILTER',         -4);
  81. define('QUICKFORM_UNREGISTERED_ELEMENT',   -5);
  82. define('QUICKFORM_INVALID_ELEMENT_NAME',   -6);
  83. define('QUICKFORM_INVALID_PROCESS',        -7);
  84. define('QUICKFORM_DEPRECATED',             -8);
  85. define('QUICKFORM_INVALID_DATASOURCE',     -9);
  86.  
  87. // }}}
  88.  
  89. /**
  90. * Create, validate and process HTML forms
  91. *
  92. @author      Adam Daniel <adaniel1@eesus.jnj.com>
  93. @author      Bertrand Mansion <bmansion@mamasam.com>
  94. @version     2.0
  95. @since       PHP 4.0.3pl1
  96. */
  97. class HTML_QuickForm extends HTML_Common {
  98.     // {{{ properties
  99.  
  100.     /**
  101.      * Array containing the form fields
  102.      * @since     1.0
  103.      * @var  array 
  104.      * @access   private
  105.      */
  106.     var $_elements = array();
  107.  
  108.     /**
  109.      * Array containing element name to index map
  110.      * @since     1.1
  111.      * @var  array 
  112.      * @access   private
  113.      */
  114.     var $_elementIndex = array();
  115.  
  116.     /**
  117.      * Array containing indexes of duplicate elements
  118.      * @since     2.10
  119.      * @var  array 
  120.      * @access   private
  121.      */
  122.     var $_duplicateIndex = array();
  123.  
  124.     /**
  125.      * Array containing required field IDs
  126.      * @since     1.0
  127.      * @var  array 
  128.      * @access   private
  129.      */ 
  130.     var $_required = array();
  131.  
  132.     /**
  133.      * Prefix message in javascript alert if error
  134.      * @since     1.0
  135.      * @var  string 
  136.      * @access   public
  137.      */ 
  138.     var $_jsPrefix = 'Invalid information entered.';
  139.  
  140.     /**
  141.      * Postfix message in javascript alert if error
  142.      * @since     1.0
  143.      * @var  string 
  144.      * @access   public
  145.      */ 
  146.     var $_jsPostfix = 'Please correct these fields.';
  147.  
  148.     /**
  149.      * Datasource object implementing the informal
  150.      * datasource protocol
  151.      * @since     3.3
  152.      * @var  object 
  153.      * @access   private
  154.      */
  155.     var $_datasource;
  156.  
  157.     /**
  158.      * Array of default form values
  159.      * @since     2.0
  160.      * @var  array 
  161.      * @access   private
  162.      */
  163.     var $_defaultValues = array();
  164.  
  165.     /**
  166.      * Array of constant form values
  167.      * @since     2.0
  168.      * @var  array 
  169.      * @access   private
  170.      */
  171.     var $_constantValues = array();
  172.  
  173.     /**
  174.      * Array of submitted form values
  175.      * @since     1.0
  176.      * @var  array 
  177.      * @access   private
  178.      */
  179.     var $_submitValues = array();
  180.  
  181.     /**
  182.      * Array of submitted form files
  183.      * @since     1.0
  184.      * @var  integer 
  185.      * @access   public
  186.      */
  187.     var $_submitFiles = array();
  188.  
  189.     /**
  190.      * Value for maxfilesize hidden element if form contains file input
  191.      * @since     1.0
  192.      * @var  integer 
  193.      * @access   public
  194.      */
  195.     var $_maxFileSize = 1048576; // 1 Mb = 1048576
  196.  
  197.     /**
  198.      * Flag to know if all fields are frozen
  199.      * @since     1.0
  200.      * @var  boolean 
  201.      * @access   private
  202.      */
  203.     var $_freezeAll = false;
  204.  
  205.     /**
  206.      * Array containing the form rules
  207.      * @since     1.0
  208.      * @var  array 
  209.      * @access   private
  210.      */
  211.     var $_rules = array();
  212.  
  213.     /**
  214.      * Form rules, global variety
  215.      * @var     array 
  216.      * @access  private
  217.      */
  218.     var $_formRules = array();
  219.  
  220.     /**
  221.      * Array containing the validation errors
  222.      * @since     1.0
  223.      * @var  array 
  224.      * @access   private
  225.      */
  226.     var $_errors = array();
  227.  
  228.     /**
  229.      * Note for required fields in the form
  230.      * @var       string 
  231.      * @since     1.0
  232.      * @access    public
  233.      */
  234.     var $_requiredNote = '<span style="font-size:80%; color:#ff0000;">*</span><span style="font-size:80%;"> denotes required field</span>';
  235.  
  236.     // }}}
  237.     // {{{ constructor
  238.  
  239.     /**
  240.      * Class constructor
  241.      * @param    string      $formName          Form's name.
  242.      * @param    string      $method            (optional)Form's method defaults to 'POST'
  243.      * @param    string      $action            (optional)Form's action
  244.      * @param    string      $target            (optional)Form's target defaults to '_self'
  245.      * @param    mixed       $attributes        (optional)Extra attributes for <form> tag
  246.      * @param    bool        $trackSubmit       (optional)Whether to track if the form was submitted by adding a special hidden field
  247.      * @access   public
  248.      */
  249.     function HTML_QuickForm($formName=''$method='post'$action=''$target='_self'$attributes=null$trackSubmit = false)
  250.     {
  251.         HTML_Common::HTML_Common($attributes);
  252.         $method (strtoupper($method== 'GET''get' 'post';
  253.         $action ($action == ''$_SERVER['PHP_SELF'$action;
  254.         $target (empty($target|| $target == '_self'? array(: array('target' => $target);
  255.         $attributes = array('action'=>$action'method'=>$method'name'=>$formName'id'=>$formName$target;
  256.         $this->updateAttributes($attributes);
  257.         if (!$trackSubmit || isset($_REQUEST['_qf__' $formName])) {
  258.             if (1 == get_magic_quotes_gpc()) {
  259.                 $this->_submitValues $this->_recursiveFilter('stripslashes''get' == $method$_GET$_POST);
  260.                 foreach ($_FILES as $keyFirst => $valFirst{
  261.                     foreach ($valFirst as $keySecond => $valSecond{
  262.                         if ('name' == $keySecond{
  263.                             $this->_submitFiles[$keyFirst][$keySecond$this->_recursiveFilter('stripslashes'$valSecond);
  264.                         else {
  265.                             $this->_submitFiles[$keyFirst][$keySecond$valSecond;
  266.                         }
  267.                     }
  268.                 }
  269.             else {
  270.                 $this->_submitValues 'get' == $method$_GET$_POST;
  271.                 $this->_submitFiles  = $_FILES;
  272.             }
  273.         }
  274.         if ($trackSubmit{
  275.             unset($this->_submitValues['_qf__' $formName]);
  276.             $this->addElement('hidden''_qf__' $formNamenull);
  277.         }
  278.     // end constructor
  279.  
  280.     // }}}
  281.     // {{{ apiVersion()
  282.  
  283.     /**
  284.      * Returns the current API version
  285.      *
  286.      * @since     1.0
  287.      * @access    public
  288.      * @return    float 
  289.      */
  290.     function apiVersion()
  291.     {
  292.         return 3.2;
  293.     // end func apiVersion
  294.  
  295.     // }}}
  296.     // {{{ registerElementType()
  297.  
  298.     /**
  299.      * Registers a new element type
  300.      *
  301.      * @param     string    $typeName   Name of element type
  302.      * @param     string    $include    Include path for element type
  303.      * @param     string    $className  Element class name
  304.      * @since     1.0
  305.      * @access    public
  306.      * @return    void 
  307.      */
  308.     function registerElementType($typeName$include$className)
  309.     {
  310.         $GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES'][strtolower($typeName)= array($include$className);
  311.     // end func registerElementType
  312.  
  313.     // }}}
  314.     // {{{ registerRule()
  315.  
  316.     /**
  317.      * Registers a new validation rule
  318.      *
  319.      * @param     string    $ruleName   Name of validation rule
  320.      * @param     string    $type       Either: 'regex', 'function' or 'rule' for an HTML_QuickForm_Rule object
  321.      * @param     string    $data1      Name of function, regular expression or HTML_QuickForm_Rule classname
  322.      * @param     string    $data2      Object parent of above function or HTML_QuickForm_Rule file path
  323.      * @since     1.0
  324.      * @access    public
  325.      * @return    void 
  326.      */
  327.     function registerRule($ruleName$type$data1$data2 = null)
  328.     {
  329.         include_once('HTML/QuickForm/RuleRegistry.php');
  330.         $registry =HTML_QuickForm_RuleRegistry::singleton();
  331.         $registry->registerRule($ruleName$type$data1$data2);
  332.     // end func registerRule
  333.  
  334.     // }}}
  335.     // {{{ elementExists()
  336.  
  337.     /**
  338.      * Returns true if element is in the form
  339.      *
  340.      * @param     string   $element         form name of element to check
  341.      * @since     1.0
  342.      * @access    public
  343.      * @return    boolean 
  344.      */
  345.     function elementExists($element=null)
  346.     {
  347.         return isset($this->_elementIndex[$element]);
  348.     // end func elementExists
  349.  
  350.     // }}}
  351.     // {{{ setDatasource()
  352.  
  353.     /**
  354.      * Sets a datasource object for this form object
  355.      *
  356.      * Datasource default and constant values will feed the QuickForm object if
  357.      * the datasource implements defaultValues() and constantValues() methods.
  358.      *
  359.      * @param     object   $datasource          datasource object implementing the informal datasource protocol
  360.      * @param     mixed    $defaultsFilter      string or array of filter(s) to apply to default values
  361.      * @param     mixed    $constantsFilter     string or array of filter(s) to apply to constants values
  362.      * @since     3.3
  363.      * @access    public
  364.      * @return    void 
  365.      */
  366.     function setDatasource(&$datasource$defaultsFilter = null$constantsFilter = null)
  367.     {
  368.         if (is_object($datasource)) {
  369.             $this->_datasource =$datasource;
  370.             if (is_callable(array($datasource'defaultValues'))) {
  371.                 $this->setDefaults($datasource->defaultValues($this)$defaultsFilter);
  372.             }
  373.             if (is_callable(array($datasource'constantValues'))) {
  374.                 $this->setConstants($datasource->constantValues($this)$constantsFilter);
  375.             }
  376.         else {
  377.             return PEAR::raiseError(nullQUICKFORM_INVALID_DATASOURCEnullE_USER_WARNING"Datasource is not an object in QuickForm::setDatasource()"'HTML_QuickForm_Error'true);
  378.         }
  379.     // end func setDatasource
  380.  
  381.     // }}}
  382.     // {{{ setDefaults()
  383.  
  384.     /**
  385.      * Initializes default form values
  386.      *
  387.      * @param     array    $defaultValues       values used to fill the form
  388.      * @param     mixed    $filter              (optional) filter(s) to apply to all default values
  389.      * @since     1.0
  390.      * @access    public
  391.      * @return    void 
  392.      */
  393.     function setDefaults($defaultValues = null$filter = null)
  394.     {
  395.         if (is_array($defaultValues)) {
  396.             if (isset($filter)) {
  397.                 if (is_array($filter&& (2 != count($filter|| !is_callable($filter))) {
  398.                     foreach ($filter as $val{
  399.                         if (!is_callable($val)) {
  400.                             return PEAR::raiseError(nullQUICKFORM_INVALID_FILTERnullE_USER_WARNING"Callback function does not exist in QuickForm::setDefaults()"'HTML_QuickForm_Error'true);
  401.                         else {
  402.                             $defaultValues $this->_recursiveFilter($val$defaultValues);
  403.                         }
  404.                     }
  405.                 elseif (!is_callable($filter)) {
  406.                     return PEAR::raiseError(nullQUICKFORM_INVALID_FILTERnullE_USER_WARNING"Callback function does not exist in QuickForm::setDefaults()"'HTML_QuickForm_Error'true);
  407.                 else {
  408.                     $defaultValues $this->_recursiveFilter($filter$defaultValues);
  409.                 }
  410.             }
  411.             $this->_defaultValues HTML_QuickForm::arrayMerge($this->_defaultValues$defaultValues);
  412.             foreach (array_keys($this->_elementsas $key{
  413.                 $this->_elements[$key]->onQuickFormEvent('updateValue'null$this);
  414.             }
  415.         }
  416.     // end func setDefaults
  417.  
  418.     // }}}
  419.     // {{{ setConstants()
  420.  
  421.     /**
  422.      * Initializes constant form values.
  423.      * These values won't get overridden by POST or GET vars
  424.      *
  425.      * @param     array   $constantValues        values used to fill the form
  426.      * @param     mixed    $filter              (optional) filter(s) to apply to all default values
  427.      *
  428.      * @since     2.0
  429.      * @access    public
  430.      * @return    void 
  431.      */
  432.     function setConstants($constantValues = null$filter = null)
  433.     {
  434.         if (is_array($constantValues)) {
  435.             if (isset($filter)) {
  436.                 if (is_array($filter&& (2 != count($filter|| !is_callable($filter))) {
  437.                     foreach ($filter as $val{
  438.                         if (!is_callable($val)) {
  439.                             return PEAR::raiseError(nullQUICKFORM_INVALID_FILTERnullE_USER_WARNING"Callback function does not exist in QuickForm::setConstants()"'HTML_QuickForm_Error'true);
  440.                         else {
  441.                             $constantValues $this->_recursiveFilter($val$constantValues);
  442.                         }
  443.                     }
  444.                 elseif (!is_callable($filter)) {
  445.                     return PEAR::raiseError(nullQUICKFORM_INVALID_FILTERnullE_USER_WARNING"Callback function does not exist in QuickForm::setConstants()"'HTML_QuickForm_Error'true);
  446.                 else {
  447.                     $constantValues $this->_recursiveFilter($filter$constantValues);
  448.                 }
  449.             }
  450.             $this->_constantValues HTML_QuickForm::arrayMerge($this->_constantValues$constantValues);
  451.             foreach (array_keys($this->_elementsas $key{
  452.                 $this->_elements[$key]->onQuickFormEvent('updateValue'null$this);
  453.             }
  454.         }
  455.     // end func setConstants
  456.  
  457.     // }}}
  458.     // {{{ setMaxFileSize()
  459.  
  460.     /**
  461.      * Sets the value of MAX_FILE_SIZE hidden element
  462.      *
  463.      * @param     int    $bytes    Size in bytes
  464.      * @since     3.0
  465.      * @access    public
  466.      * @return    void 
  467.      */
  468.     function setMaxFileSize($bytes = 0)
  469.     {
  470.         if ($bytes > 0{
  471.             $this->_maxFileSize = $bytes;
  472.         }
  473.         if (!$this->elementExists('MAX_FILE_SIZE')) {
  474.             $this->addElement('hidden''MAX_FILE_SIZE'$this->_maxFileSize);
  475.         else {
  476.             $el =$this->getElement('MAX_FILE_SIZE');
  477.             $el->updateAttributes(array('value' => $this->_maxFileSize));
  478.         }
  479.     // end func setMaxFileSize
  480.  
  481.     // }}}
  482.     // {{{ getMaxFileSize()
  483.  
  484.     /**
  485.      * Returns the value of MAX_FILE_SIZE hidden element
  486.      *
  487.      * @since     3.0
  488.      * @access    public
  489.      * @return    int   max file size in bytes
  490.      */
  491.     function getMaxFileSize()
  492.     {
  493.         return $this->_maxFileSize;
  494.     // end func getMaxFileSize
  495.  
  496.     // }}}
  497.     // {{{ &createElement()
  498.  
  499.     /**
  500.      * Creates a new form element of the given type.
  501.      * 
  502.      * This method accepts variable number of parameters, their
  503.      * meaning and count depending on $elementType
  504.      *
  505.      * @param     string     $elementType    type of element to add (text, textarea, file...)
  506.      * @since     1.0
  507.      * @access    public
  508.      * @return    object extended class of HTML_element
  509.      * @throws    HTML_QuickForm_Error
  510.      */
  511.     function &createElement($elementType)
  512.     {
  513.         $args func_get_args();
  514.         return HTML_QuickForm::_loadElement('createElement'$elementTypearray_slice($args1));
  515.     // end func createElement
  516.     
  517.     // }}}
  518.     // {{{ _loadElement()
  519.  
  520.     /**
  521.      * Returns a form element of the given type
  522.      *
  523.      * @param     string   $event   event to send to newly created element ('createElement' or 'addElement')
  524.      * @param     string   $type    element type
  525.      * @param     array    $args    arguments for event
  526.      * @since     2.0
  527.      * @access    private
  528.      * @return    object    new element
  529.      * @throws    HTML_QuickForm_Error
  530.      */
  531.     function &_loadElement($event$type$args)
  532.     {
  533.         $type strtolower($type);
  534.         if (!HTML_QuickForm::isTypeRegistered($type)) {
  535.             return PEAR::raiseError(nullQUICKFORM_UNREGISTERED_ELEMENTnullE_USER_WARNING"Element '$type' does not exist in HTML_QuickForm::_loadElement()"'HTML_QuickForm_Error'true);
  536.         }
  537.         $className $GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES'][$type][1];
  538.         $includeFile $GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES'][$type][0];
  539.         include_once($includeFile);
  540.         $elementObject =new $className();
  541.         for ($i = 0; $i < 5; $i++{
  542.             if (!isset($args[$i])) {
  543.                 $args[$i= null;
  544.             }
  545.         }
  546.         $err $elementObject->onQuickFormEvent($event$args$this);
  547.         if ($err !== true{
  548.             return $err;
  549.         }
  550.         return $elementObject;
  551.     // end func _loadElement
  552.  
  553.     // }}}
  554.     // {{{ addElement()
  555.  
  556.     /**
  557.      * Adds an element into the form
  558.      * 
  559.      * If $element is a string representing element type, then this
  560.      * method accepts variable number of parameters, their meaning
  561.      * and count depending on $element
  562.      *
  563.      * @param    mixed      $element        element object or type of element to add (text, textarea, file...)
  564.      * @since    1.0
  565.      * @return   object     reference to element
  566.      * @access   public
  567.      * @throws   HTML_QuickForm_Error
  568.      */
  569.     function &addElement($element)
  570.     {
  571.         if (is_object($element&& is_subclass_of($element'html_quickform_element')) {
  572.            $elementObject &$element;
  573.            $elementObject->onQuickFormEvent('updateValue'null$this);
  574.         else {
  575.             $args func_get_args();
  576.             $elementObject =$this->_loadElement('addElement'$elementarray_slice($args1));
  577.             if (PEAR::isError($elementObject)) {
  578.                 return $elementObject;
  579.             }
  580.         }
  581.         $elementName $elementObject->getName();
  582.  
  583.         // Add the element if it is not an incompatible duplicate
  584.         if (!empty($elementName&& isset($this->_elementIndex[$elementName])) {
  585.             if ($this->_elements[$this->_elementIndex[$elementName]]->getType(==
  586.                 $elementObject->getType()) {
  587.                 $this->_elements[=$elementObject;
  588.                 $this->_duplicateIndex[$elementName][end(array_keys($this->_elements));
  589.             else {
  590.                 return PEAR::raiseError(nullQUICKFORM_INVALID_ELEMENT_NAMEnullE_USER_WARNING"Element '$elementName' already exists in HTML_QuickForm::addElement()"'HTML_QuickForm_Error'true);
  591.             }
  592.         else {
  593.             $this->_elements[=$elementObject;
  594.             $this->_elementIndex[$elementNameend(array_keys($this->_elements));
  595.         }
  596.         if ($this->_freezeAll{
  597.             $elementObject->freeze();
  598.         }
  599.  
  600.         return $elementObject;
  601.     // end func addElement
  602.     
  603.     // }}}
  604.     // {{{ insertElementBefore()
  605.  
  606.    /**
  607.     * Inserts a new element right before the other element
  608.     *
  609.     * Warning: it is not possible to check whether the $element is already
  610.     * added to the form, therefore if you want to move the existing form
  611.     * element to a new position, you'll have to use removeElement():
  612.     * $form->insertElementBefore($form->removeElement('foo', false), 'bar');
  613.     *
  614.     * @access   public
  615.     * @since    3.2.4
  616.     * @param    object  HTML_QuickForm_element  Element to insert
  617.     * @param    string  Name of the element before which the new one is inserted
  618.     * @return   object  HTML_QuickForm_element  reference to inserted element
  619.     * @throws   HTML_QuickForm_Error
  620.     */
  621.     function &insertElementBefore(&$element$nameAfter)
  622.     {
  623.         if (!empty($this->_duplicateIndex[$nameAfter])) {
  624.             return PEAR::raiseError(nullQUICKFORM_INVALID_ELEMENT_NAMEnullE_USER_WARNING'Several elements named "' $nameAfter '" exist in HTML_QuickForm::insertElementBefore().''HTML_QuickForm_Error'true);
  625.         elseif (!$this->elementExists($nameAfter)) {
  626.             return PEAR::raiseError(nullQUICKFORM_NONEXIST_ELEMENTnullE_USER_WARNING"Element '$nameAfter' does not exist in HTML_QuickForm::insertElementBefore()"'HTML_QuickForm_Error'true);
  627.         }
  628.         $elementName $element->getName();
  629.         $targetIdx   $this->_elementIndex[$nameAfter];
  630.         $duplicate   = false;
  631.         // Like in addElement(), check that it's not an incompatible duplicate
  632.         if (!empty($elementName&& isset($this->_elementIndex[$elementName])) {
  633.             if ($this->_elements[$this->_elementIndex[$elementName]]->getType(!= $element->getType()) {
  634.                 return PEAR::raiseError(nullQUICKFORM_INVALID_ELEMENT_NAMEnullE_USER_WARNING"Element '$elementName' already exists in HTML_QuickForm::insertElementBefore()"'HTML_QuickForm_Error'true);
  635.             }
  636.             $duplicate = true;
  637.         }
  638.         // Move all the elements after added back one place, reindex _elementIndex and/or _duplicateIndex
  639.         for ($i end(array_keys($this->_elements))$i >= $targetIdx$i--{
  640.             if (isset($this->_elements[$i])) {
  641.                 $currentName $this->_elements[$i]->getName();
  642.                 $this->_elements[$i + 1=$this->_elements[$i];
  643.                 if ($this->_elementIndex[$currentName== $i{
  644.                     $this->_elementIndex[$currentName$i + 1;
  645.                 else {
  646.                     $dupIdx array_search($i$this->_duplicateIndex[$currentName]);
  647.                     $this->_duplicateIndex[$currentName][$dupIdx$i + 1;
  648.                 }
  649.                 unset($this->_elements[$i]);
  650.             }
  651.         }
  652.         // Put the element in place finally
  653.         $this->_elements[$targetIdx=$element;
  654.         if (!$duplicate{
  655.             $this->_elementIndex[$elementName$targetIdx;
  656.         else {
  657.             $this->_duplicateIndex[$elementName][$targetIdx;
  658.         }
  659.         $element->onQuickFormEvent('updateValue'null$this);
  660.         if ($this->_freezeAll{
  661.             $element->freeze();
  662.         }
  663.         // If not done, the elements will appear in reverse order
  664.         ksort($this->_elements);
  665.         return $element;
  666.     }
  667.  
  668.     // }}}
  669.     // {{{ addGroup()
  670.  
  671.     /**
  672.      * Adds an element group
  673.      * @param    array      $elements       array of elements composing the group
  674.      * @param    string     $name           (optional)group name
  675.      * @param    string     $groupLabel     (optional)group label
  676.      * @param    string     $separator      (optional)string to separate elements
  677.      * @param    string     $appendName     (optional)specify whether the group name should be
  678.      *                                       used in the form element name ex: group[element]
  679.      * @return   object     reference to added group of elements
  680.      * @since    2.8
  681.      * @access   public
  682.      * @throws   PEAR_Error
  683.      */
  684.     function &addGroup($elements$name=null$groupLabel=''$separator=null$appendName = true)
  685.     {
  686.         static $anonGroups = 1;
  687.  
  688.         if (0 == strlen($name)) {
  689.             $name       'qf_group_' $anonGroups++;
  690.             $appendName = false;
  691.         }
  692.         return $this->addElement('group'$name$groupLabel$elements$separator$appendName);
  693.     // end func addGroup
  694.     
  695.     // }}}
  696.     // {{{ &getElement()
  697.  
  698.     /**
  699.      * Returns a reference to the element
  700.      *
  701.      * @param     string     $element    Element name
  702.      * @since     2.0
  703.      * @access    public
  704.      * @return    object     reference to element
  705.      * @throws    HTML_QuickForm_Error
  706.      */
  707.     function &getElement($element)
  708.     {
  709.         if (isset($this->_elementIndex[$element])) {
  710.             return $this->_elements[$this->_elementIndex[$element]];
  711.         else {
  712.             return PEAR::raiseError(nullQUICKFORM_NONEXIST_ELEMENTnullE_USER_WARNING"Element '$element' does not exist in HTML_QuickForm::getElement()"'HTML_QuickForm_Error'true);
  713.         }
  714.     // end func getElement
  715.  
  716.     // }}}
  717.     // {{{ &getElementValue()
  718.  
  719.     /**
  720.      * Returns the element's raw value
  721.      * 
  722.      * This returns the value as submitted by the form (not filtered)
  723.      * or set via setDefaults() or setConstants()
  724.      *
  725.      * @param     string     $element    Element name
  726.      * @since     2.0
  727.      * @access    public
  728.      * @return    mixed     element value
  729.      * @throws    HTML_QuickForm_Error
  730.      */
  731.     function &getElementValue($element)
  732.     {
  733.         if (!isset($this->_elementIndex[$element])) {
  734.             return PEAR::raiseError(nullQUICKFORM_NONEXIST_ELEMENTnullE_USER_WARNING"Element '$element' does not exist in HTML_QuickForm::getElementValue()"'HTML_QuickForm_Error'true);
  735.         }
  736.         $value $this->_elements[$this->_elementIndex[$element]]->getValue();
  737.         if (isset($this->_duplicateIndex[$element])) {
  738.             foreach ($this->_duplicateIndex[$elementas $index{
  739.                 if (null !== ($v $this->_elements[$index]->getValue())) {
  740.                     if (is_array($value)) {
  741.                         $value[$v;
  742.                     else {
  743.                         $value (null === $value)$v: array($value$v);
  744.                     }
  745.                 }
  746.             }
  747.         }
  748.         return $value;
  749.     // end func getElementValue
  750.  
  751.     // }}}
  752.     // {{{ getSubmitValue()
  753.  
  754.     /**
  755.      * Returns the elements value after submit and filter
  756.      *
  757.      * @param     string     Element name
  758.      * @since     2.0
  759.      * @access    public
  760.      * @return    mixed     submitted element value or null if not set
  761.      */    
  762.     function getSubmitValue($elementName)
  763.     {
  764.         $value = null;
  765.         if (isset($this->_submitValues[$elementName]|| isset($this->_submitFiles[$elementName])) {
  766.             $value = isset($this->_submitValues[$elementName])$this->_submitValues[$elementName]: array();
  767.             if (is_array($value&& isset($this->_submitFiles[$elementName])) {
  768.                 foreach ($this->_submitFiles[$elementNameas $k => $v{
  769.                     $value HTML_QuickForm::arrayMerge($value$this->_reindexFiles($this->_submitFiles[$elementName][$k]$k));
  770.                 }
  771.             }
  772.  
  773.         elseif ('file' == $this->getElementType($elementName)) {
  774.             return $this->getElementValue($elementName);
  775.  
  776.         elseif ('group' == $this->getElementType($elementName)) {
  777.             $group    =$this->getElement($elementName);
  778.             $elements =$group->getElements();
  779.             foreach (array_keys($elementsas $key{
  780.                 $name $group->getElementName($key);
  781.                 $v    $this->getSubmitValue($name);
  782.                 if ($name != $elementName && null !== $v{
  783.                     // filter out radios
  784.                     $value[$name$v;
  785.                 }
  786.             }
  787.  
  788.         elseif (false !== ($pos strpos($elementName'['))) {
  789.             $base substr($elementName0$pos);
  790.             $idx  "['" str_replace(array(']''[')array(''"']['")substr($elementName$pos + 1-1)) "']";
  791.             if (isset($this->_submitValues[$base])) {
  792.                 $value = eval("return (isset(\$this->_submitValues['{$base}']{$idx})) ? \$this->_submitValues['{$base}']{$idx} : null;");
  793.             }
  794.  
  795.             if (null === $value && isset($this->_submitFiles[$base])) {
  796.                 $props = array('name''type''size''tmp_name''error');
  797.                 $code  = "if (!isset(\$this->_submitFiles['{$base}']['name']{$idx})) {\n" .
  798.                          "    return null;\n" .
  799.                          "} else {\n" .
  800.                          "    \$v = array();\n";
  801.                 foreach ($props as $prop{
  802.                     $code .= "    \$v['{$prop}'] = \$this->_submitFiles['{$base}']['{$prop}']{$idx};\n";
  803.                 }
  804.                 $value = eval($code "    return \$v;\n}\n");
  805.             }
  806.         }
  807.         return $value;
  808.     // end func getSubmitValue
  809.  
  810.     // }}}
  811.     // {{{ _reindexFiles()
  812.  
  813.    /**
  814.     * A helper function to change the indexes in $_FILES array
  815.     *
  816.     * @param  mixed   Some value from the $_FILES array
  817.     * @param  string  The key from the $_FILES array that should be appended
  818.     * @return array 
  819.     */
  820.     function _reindexFiles($value$key)
  821.     {
  822.         if (!is_array($value)) {
  823.             return array($key => $value);
  824.         else {
  825.             $ret = array();
  826.             foreach ($value as $k => $v{
  827.                 $ret[$k$this->_reindexFiles($v$key);
  828.             }
  829.             return $ret;
  830.         }
  831.     }
  832.  
  833.     // }}}
  834.     // {{{ getElementError()
  835.  
  836.     /**
  837.      * Returns error corresponding to validated element
  838.      *
  839.      * @param     string    $element        Name of form element to check
  840.      * @since     1.0
  841.      * @access    public
  842.      * @return    string    error message corresponding to checked element
  843.      */
  844.     function getElementError($element)
  845.     {
  846.         if (isset($this->_errors[$element])) {
  847.             return $this->_errors[$element];
  848.         }
  849.     // end func getElementError
  850.     
  851.     // }}}
  852.     // {{{ setElementError()
  853.  
  854.     /**
  855.      * Set error message for a form element
  856.      *
  857.      * @param     string    $element    Name of form element to set error for
  858.      * @param     string    $message    Error message
  859.      * @since     1.0
  860.      * @access    public
  861.      * @return    void 
  862.      */
  863.     function setElementError($element,$message)
  864.     {
  865.         $this->_errors[$element$message;
  866.     // end func setElementError
  867.          
  868.      // }}}
  869.      // {{{ getElementType()
  870.  
  871.      /**
  872.       * Returns the type of the given element
  873.       *
  874.       * @param      string    $element    Name of form element
  875.       * @since      1.1
  876.       * @access     public
  877.       * @return     string    Type of the element, false if the element is not found
  878.       */
  879.      function getElementType($element)
  880.      {
  881.          if (isset($this->_elementIndex[$element])) {
  882.              return $this->_elements[$this->_elementIndex[$element]]->getType();
  883.          }
  884.          return false;
  885.      // end func getElementType
  886.  
  887.      // }}}
  888.      // {{{ updateElementAttr()
  889.  
  890.     /**
  891.      * Updates Attributes for one or more elements
  892.      *
  893.      * @param      mixed    $elements   Array of element names/objects or string of elements to be updated
  894.      * @param      mixed    $attrs      Array or sting of html attributes
  895.      * @since      2.10
  896.      * @access     public
  897.      * @return     void 
  898.      */
  899.     function updateElementAttr($elements$attrs)
  900.     {
  901.         if (is_string($elements)) {
  902.             $elements split('[ ]?,[ ]?'$elements);
  903.         }
  904.         foreach (array_keys($elementsas $key{
  905.             if (is_object($elements[$key]&& is_a($elements[$key]'HTML_QuickForm_element')) {
  906.                 $elements[$key]->updateAttributes($attrs);
  907.             elseif (isset($this->_elementIndex[$elements[$key]])) {
  908.                 $this->_elements[$this->_elementIndex[$elements[$key]]]->updateAttributes($attrs);
  909.                 if (isset($this->_duplicateIndex[$elements[$key]])) {
  910.                     foreach ($this->_duplicateIndex[$elements[$key]] as $index{
  911.                         $this->_elements[$index]->updateAttributes($attrs);
  912.                     }
  913.                 }
  914.             }
  915.         }
  916.     // end func updateElementAttr
  917.  
  918.     // }}}
  919.     // {{{ removeElement()
  920.  
  921.     /**
  922.      * Removes an element
  923.      *
  924.      * The method "unlinks" an element from the form, returning the reference
  925.      * to the element object. If several elements named $elementName exist,
  926.      * it removes the first one, leaving the others intact.
  927.      * 
  928.      * @param string    $elementName The element name
  929.      * @param boolean   $removeRules True if rules for this element are to be removed too
  930.      * @access public
  931.      * @since 2.0
  932.      * @return object HTML_QuickForm_element    a reference to the removed element
  933.      * @throws HTML_QuickForm_Error
  934.      */
  935.     function &removeElement($elementName$removeRules = true)
  936.     {
  937.         if (!isset($this->_elementIndex[$elementName])) {
  938.             return PEAR::raiseError(nullQUICKFORM_NONEXIST_ELEMENTnullE_USER_WARNING"Element '$elementName' does not exist in HTML_QuickForm::removeElement()"'HTML_QuickForm_Error'true);
  939.         }
  940.         $el =$this->_elements[$this->_elementIndex[$elementName]];
  941.             unset($this->_elements[$this->_elementIndex[$elementName]]);
  942.         if (empty($this->_duplicateIndex[$elementName])) {
  943.             unset($this->_elementIndex[$elementName]);
  944.         else {
  945.             $this->_elementIndex[$elementNamearray_shift($this->_duplicateIndex[$elementName]);
  946.         }
  947.         if ($removeRules{
  948.             unset($this->_rules[$elementName]);
  949.         }
  950.         return $el;
  951.     // end func removeElement
  952.  
  953.     // }}}
  954.     // {{{ addRule()
  955.  
  956.     /**
  957.      * Adds a validation rule for the given field
  958.      *
  959.      * If the element is in fact a group, it will be considered as a whole.
  960.      * To validate grouped elements as separated entities,
  961.      * use addGroupRule instead of addRule.
  962.      *
  963.      * @param    string     $element       Form element name
  964.      * @param    string     $message       Message to display for invalid data
  965.      * @param    string     $type          Rule type, use getRegisteredRules() to get types
  966.      * @param    string     $format        (optional)Required for extra rule data
  967.      * @param    string     $validation    (optional)Where to perform validation: "server", "client"
  968.      * @param    boolean    $reset         Client-side validation: reset the form element to its original value if there is an error?
  969.      * @param    boolean    $force         Force the rule to be applied, even if the target form element does not exist
  970.      * @since    1.0
  971.      * @access   public
  972.      * @throws   HTML_QuickForm_Error
  973.      */
  974.     function addRule($element$message$type$format=null$validation='server'$reset = false$force = false)
  975.     {
  976.         if (!$force{
  977.             if (!is_array($element&& !$this->elementExists($element)) {
  978.                 return PEAR::raiseError(nullQUICKFORM_NONEXIST_ELEMENTnullE_USER_WARNING"Element '$element' does not exist in HTML_QuickForm::addRule()"'HTML_QuickForm_Error'true);
  979.             elseif (is_array($element)) {
  980.                 foreach ($element as $el{
  981.                     if (!$this->elementExists($el)) {
  982.                         return PEAR::raiseError(nullQUICKFORM_NONEXIST_ELEMENTnullE_USER_WARNING"Element '$el' does not exist in HTML_QuickForm::addRule()"'HTML_QuickForm_Error'true);
  983.                     }
  984.                 }
  985.             }
  986.         }
  987.         if (false === ($newName $this->isRuleRegistered($typetrue))) {
  988.             return PEAR::raiseError(nullQUICKFORM_INVALID_RULEnullE_USER_WARNING"Rule '$type' is not registered in HTML_QuickForm::addRule()"'HTML_QuickForm_Error'true);
  989.         elseif (is_string($newName)) {
  990.             $type $newName;
  991.         }
  992.         if (is_array($element)) {
  993.             $dependent $element;
  994.             $element   array_shift($dependent);
  995.         else {
  996.             $dependent = null;
  997.         }
  998.         if ($type == 'required' || $type == 'uploadedfile'{
  999.             $this->_required[$element;
  1000.         }
  1001.         if (!isset($this->_rules[$element])) {
  1002.             $this->_rules[$element= array();
  1003.         }
  1004.         if ($validation == 'client'{
  1005.             $this->updateAttributes(array('onsubmit'=>'return validate_'.$this->_attributes['id''(this);'));
  1006.         }
  1007.         $this->_rules[$element][= array(
  1008.             'type'        => $type,
  1009.             'format'      => $format,
  1010.             'message'     => $message,
  1011.             'validation'  => $validation,
  1012.             'reset'       => $reset,
  1013.             'dependent'   => $dependent
  1014.         );
  1015.     // end func addRule
  1016.  
  1017.     // }}}
  1018.     // {{{ addGroupRule()
  1019.  
  1020.     /**
  1021.      * Adds a validation rule for the given group of elements
  1022.      *
  1023.      * Only groups with a name can be assigned a validation rule
  1024.      * Use addGroupRule when you need to validate elements inside the group.
  1025.      * Use addRule if you need to validate the group as a whole. In this case,
  1026.      * the same rule will be applied to all elements in the group.
  1027.      * Use addRule if you need to validate the group against a function.
  1028.      *
  1029.      * @param    string     $group         Form group name
  1030.      * @param    mixed      $arg1          Array for multiple elements or error message string for one element
  1031.      * @param    string     $type          (optional)Rule type use getRegisteredRules() to get types
  1032.      * @param    string     $format        (optional)Required for extra rule data
  1033.      * @param    int        $howmany       (optional)How many valid elements should be in the group
  1034.      * @param    string     $validation    (optional)Where to perform validation: "server", "client"
  1035.      * @param    bool       $reset         Client-side: whether to reset the element's value to its original state if validation failed.
  1036.      * @since    2.5
  1037.      * @access   public
  1038.      * @throws   HTML_QuickForm_Error
  1039.      */
  1040.     function addGroupRule($group$arg1$type=''$format=null$howmany=0$validation 'server'$reset = false)
  1041.     {
  1042.         if (!$this->elementExists($group)) {
  1043.             return PEAR::raiseError(nullQUICKFORM_NONEXIST_ELEMENTnullE_USER_WARNING"Group '$group' does not exist in HTML_QuickForm::addGroupRule()"'HTML_QuickForm_Error'true);
  1044.         }
  1045.  
  1046.         $groupObj =$this->getElement($group);
  1047.         if (is_array($arg1)) {
  1048.             $required = 0;
  1049.             foreach ($arg1 as $elementIndex => $rules{
  1050.                 $elementName $groupObj->getElementName($elementIndex);
  1051.                 foreach ($rules as $rule{
  1052.                     $format (isset($rule[2])) $rule[2: null;
  1053.                     $validation (isset($rule[3]&& 'client' == $rule[3])'client''server';
  1054.                     $reset = isset($rule[4]&& $rule[4];
  1055.                     $type $rule[1];
  1056.                     if (false === ($newName $this->isRuleRegistered($typetrue))) {
  1057.                         return PEAR::raiseError(nullQUICKFORM_INVALID_RULEnullE_USER_WARNING"Rule '$type' is not registered in HTML_QuickForm::addGroupRule()"'HTML_QuickForm_Error'true);
  1058.                     elseif (is_string($newName)) {
  1059.                         $type $newName;
  1060.                     }
  1061.  
  1062.                     $this->_rules[$elementName][= array(
  1063.                                                         'type'        => $type,
  1064.                                                         'format'      => $format
  1065.                                                         'message'     => $rule[0],
  1066.                                                         'validation'  => $validation,
  1067.                                                         'reset'       => $reset,
  1068.                                                         'group'       => $group);
  1069.  
  1070.                     if ('required' == $type || 'uploadedfile' == $type{
  1071.                         $groupObj->_required[$elementName;
  1072.                         $this->_required[$elementName;
  1073.                         $required++;
  1074.                     }
  1075.                     if ('client' == $validation{
  1076.                         $this->updateAttributes(array('onsubmit'=>'return validate_'.$this->_attributes['id''(this);'));
  1077.                     }
  1078.                 }
  1079.             }
  1080.             if ($required > 0 && count($groupObj->getElements()) == $required{
  1081.                 $this->_required[$group;
  1082.             }
  1083.         elseif (is_string($arg1)) {
  1084.             if (false === ($newName $this->isRuleRegistered($typetrue))) {
  1085.                 return PEAR::raiseError(nullQUICKFORM_INVALID_RULEnullE_USER_WARNING"Rule '$type' is not registered in HTML_QuickForm::addGroupRule()"'HTML_QuickForm_Error'true);
  1086.             elseif (is_string($newName)) {
  1087.                 $type $newName;
  1088.             }
  1089.  
  1090.             // addGroupRule() should also handle <select multiple>
  1091.             if (is_a($groupObj'html_quickform_group')) {
  1092.                 // Radios need to be handled differently when required
  1093.                 if ($type == 'required' && $groupObj->getGroupType(== 'radio'{
  1094.                     $howmany ($howmany == 0? 1 : $howmany;
  1095.                 else {
  1096.                     $howmany ($howmany == 0count($groupObj->getElements()) $howmany;
  1097.                 }
  1098.             }
  1099.  
  1100.             $this->_rules[$group][= array('type'       => $type,
  1101.                                             'format'     => $format
  1102.                                             'message'    => $arg1,
  1103.                                             'validation' => $validation,
  1104.                                             'howmany'    => $howmany,
  1105.                                             'reset'      => $reset);
  1106.             if ($type == 'required'{
  1107.                 $this->_required[$group;
  1108.             }
  1109.             if ($validation == 'client'{
  1110.                 $this->updateAttributes(array('onsubmit'=>'return validate_'.$this->_attributes['id''(this);'));
  1111.             }
  1112.         }
  1113.     // end func addGroupRule
  1114.  
  1115.     // }}}
  1116.     // {{{ addFormRule()
  1117.  
  1118.    /**
  1119.     * Adds a global validation rule
  1120.     * 
  1121.     * This should be used when for a rule involving several fields or if
  1122.     * you want to use some completely custom validation for your form.
  1123.     * The rule function/method should return true in case of successful
  1124.     * validation and array('element name' => 'error') when there were errors.
  1125.     * 
  1126.     * @access   public
  1127.     * @param    mixed   Callback, either function name or array(&$object, 'method')
  1128.     * @throws   HTML_QuickForm_Error
  1129.     */
  1130.     function addFormRule($rule)
  1131.     {
  1132.         if (!is_callable($rule)) {
  1133.             return PEAR::raiseError(nullQUICKFORM_INVALID_RULEnullE_USER_WARNING'Callback function does not exist in HTML_QuickForm::addFormRule()''HTML_QuickForm_Error'true);
  1134.         }
  1135.         $this->_formRules[$rule;
  1136.     }
  1137.     
  1138.     // }}}
  1139.     // {{{ applyFilter()
  1140.  
  1141.     /**
  1142.      * Applies a data filter for the given field(s)
  1143.      *
  1144.      * @param    mixed     $element       Form element name or array of such names
  1145.      * @param    mixed     $filter        Callback, either function name or array(&$object, 'method')
  1146.      * @since    2.0
  1147.      * @access   public
  1148.      */
  1149.     function applyFilter($element$filter)
  1150.     {
  1151.         if (!is_callable($filter)) {
  1152.             return PEAR::raiseError(nullQUICKFORM_INVALID_FILTERnullE_USER_WARNING"Callback function does not exist in QuickForm::applyFilter()"'HTML_QuickForm_Error'true);
  1153.         }
  1154.         if ($element == '__ALL__'{
  1155.             $this->_submitValues $this->_recursiveFilter($filter$this->_submitValues);
  1156.         else {
  1157.             if (!is_array($element)) {
  1158.                 $element = array($element);
  1159.             }
  1160.             foreach ($element as $elName{
  1161.                 $value $this->getSubmitValue($elName);
  1162.                 if (null !== $value{
  1163.                     if (false === strpos($elName'[')) {
  1164.                         $this->_submitValues[$elName$this->_recursiveFilter($filter$value);
  1165.                     else {
  1166.                         $idx  "['" str_replace(array(']''[')array(''"']['")$elName"']";
  1167.                         eval("\$this->_submitValues{$idx} = \$this->_recursiveFilter(\$filter, \$value);");
  1168.                     }
  1169.                 }
  1170.             }
  1171.         }
  1172.     // end func applyFilter
  1173.  
  1174.     // }}}
  1175.     // {{{ _recursiveFilter()
  1176.  
  1177.     /**
  1178.      * Recursively apply a filter function
  1179.      *
  1180.      * @param     string   $filter    filter to apply
  1181.      * @param     mixed    $value     submitted values
  1182.      * @since     2.0
  1183.      * @access    private
  1184.      * @return    cleaned values
  1185.      */
  1186.     function _recursiveFilter($filter$value)
  1187.     {
  1188.         if (is_array($value)) {
  1189.             $cleanValues = array();
  1190.             foreach ($value as $k => $v{
  1191.                 $cleanValues[$k$this->_recursiveFilter($filter$value[$k]);
  1192.             }
  1193.             return $cleanValues;
  1194.         else {
  1195.             return call_user_func($filter$value);
  1196.         }
  1197.     // end func _recursiveFilter
  1198.  
  1199.     // }}}
  1200.     // {{{ arrayMerge()
  1201.  
  1202.    /**
  1203.     * Merges two arrays
  1204.     *
  1205.     * Merges two array like the PHP function array_merge but recursively.
  1206.     * The main difference is that existing keys will not be renumbered
  1207.     * if they are integers.
  1208.     *
  1209.     * @access   puplic
  1210.     * @param    array   $a  original array
  1211.     * @param    array   $b  array which will be merged into first one
  1212.     * @return   array   merged array
  1213.     */
  1214.     function arrayMerge($a$b)
  1215.     {
  1216.         foreach ($b as $k => $v{
  1217.             if (is_array($v)) {
  1218.                 if (isset($a[$k]&& !is_array($a[$k])) {
  1219.                     $a[$k$v;
  1220.                 else {
  1221.                     if (!isset($a[$k])) {
  1222.                         $a[$k= array();
  1223.                     }
  1224.                     $a[$kHTML_QuickForm::arrayMerge($a[$k]$v);
  1225.                 }
  1226.             else {
  1227.                 $a[$k$v;
  1228.             }
  1229.         }
  1230.         return $a;
  1231.     // end func arrayMerge
  1232.  
  1233.     // }}}
  1234.     // {{{ isTypeRegistered()
  1235.  
  1236.     /**
  1237.      * Returns whether or not the form element type is supported
  1238.      *
  1239.      * @param     string   $type     Form element type
  1240.      * @since     1.0
  1241.      * @access    public
  1242.      * @return    boolean 
  1243.      */
  1244.     function isTypeRegistered($type)
  1245.     {
  1246.         return isset($GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES'][$type]);
  1247.     // end func isTypeRegistered
  1248.  
  1249.     // }}}
  1250.     // {{{ getRegisteredTypes()
  1251.  
  1252.     /**
  1253.      * Returns an array of registered element types
  1254.      *
  1255.      * @since     1.0
  1256.      * @access    public
  1257.      * @return    array 
  1258.      */
  1259.     function getRegisteredTypes()
  1260.     {
  1261.         return array_keys($GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES']);
  1262.     // end func getRegisteredTypes
  1263.  
  1264.     // }}}
  1265.     // {{{ isRuleRegistered()
  1266.  
  1267.     /**
  1268.      * Returns whether or not the given rule is supported
  1269.      *
  1270.      * @param     string   $name    Validation rule name
  1271.      * @param     bool     Whether to automatically register subclasses of HTML_QuickForm_Rule
  1272.      * @since     1.0
  1273.      * @access    public
  1274.      * @return    mixed    true if previously registered, false if not, new rule name if auto-registering worked
  1275.      */
  1276.     function isRuleRegistered($name$autoRegister = false)
  1277.     {
  1278.         if (is_scalar($name&& isset($GLOBALS['_HTML_QuickForm_registered_rules'][$name])) {
  1279.             return true;
  1280.         elseif (!$autoRegister{
  1281.             return false;
  1282.         }
  1283.         // automatically register the rule if requested
  1284.         include_once 'HTML/QuickForm/RuleRegistry.php';
  1285.         $ruleName = false;
  1286.         if (is_object($name&& is_a($name'html_quickform_rule')) {
  1287.             $ruleName !empty($name->name)$name->name: strtolower(get_class($name));
  1288.         elseif (is_string($name&& class_exists($name)) {
  1289.             $parent strtolower($name);
  1290.             do {
  1291.                 if ('html_quickform_rule' == strtolower($parent)) {
  1292.                     $ruleName strtolower($name);
  1293.                     break;
  1294.                 }
  1295.             while ($parent get_parent_class($parent));
  1296.         }
  1297.         if ($ruleName{
  1298.             $registry =HTML_QuickForm_RuleRegistry::singleton();
  1299.             $registry->registerRule($ruleNamenull$name);
  1300.         }
  1301.         return $ruleName;
  1302.     // end func isRuleRegistered
  1303.  
  1304.     // }}}
  1305.     // {{{ getRegisteredRules()
  1306.  
  1307.     /**
  1308.      * Returns an array of registered validation rules
  1309.      *
  1310.      * @since     1.0
  1311.      * @access    public
  1312.      * @return    array 
  1313.      */
  1314.     function getRegisteredRules()
  1315.     {
  1316.         return array_keys($GLOBALS['_HTML_QuickForm_registered_rules']);
  1317.     // end func getRegisteredRules
  1318.  
  1319.     // }}}
  1320.     // {{{ isElementRequired()
  1321.  
  1322.     /**
  1323.      * Returns whether or not the form element is required
  1324.      *
  1325.      * @param     string   $element     Form element name
  1326.      * @since     1.0
  1327.      * @access    public
  1328.      * @return    boolean 
  1329.      */
  1330.     function isElementRequired($element)
  1331.     {
  1332.         return in_array($element$this->_requiredtrue);
  1333.     // end func isElementRequired
  1334.  
  1335.     // }}}
  1336.     // {{{ isElementFrozen()
  1337.  
  1338.     /**
  1339.      * Returns whether or not the form element is frozen
  1340.      *
  1341.      * @param     string   $element     Form element name
  1342.      * @since     1.0
  1343.      * @access    public
  1344.      * @return    boolean 
  1345.      */
  1346.     function isElementFrozen($element)
  1347.     {
  1348.          if (isset($this->_elementIndex[$element])) {
  1349.              return $this->_elements[$this->_elementIndex[$element]]->isFrozen();
  1350.          }
  1351.          return false;
  1352.     // end func isElementFrozen
  1353.  
  1354.     // }}}
  1355.     // {{{ setJsWarnings()
  1356.  
  1357.     /**
  1358.      * Sets JavaScript warning messages
  1359.      *
  1360.      * @param     string   $pref        Prefix warning
  1361.      * @param     string   $post        Postfix warning
  1362.      * @since     1.1
  1363.      * @access    public
  1364.      * @return    void 
  1365.      */
  1366.     function setJsWarnings($pref$post)
  1367.     {
  1368.         $this->_jsPrefix = $pref;
  1369.         $this->_jsPostfix = $post;
  1370.     // end func setJsWarnings
  1371.     
  1372.     // }}}
  1373.     // {{{ setRequiredNote()
  1374.  
  1375.     /**
  1376.      * Sets required-note
  1377.      *
  1378.      * @param     string   $note        Message indicating some elements are required
  1379.      * @since     1.1
  1380.      * @access    public
  1381.      * @return    void 
  1382.      */
  1383.     function setRequiredNote($note)
  1384.     {
  1385.         $this->_requiredNote = $note;
  1386.     // end func setRequiredNote
  1387.  
  1388.     // }}}
  1389.     // {{{ getRequiredNote()
  1390.  
  1391.     /**
  1392.      * Returns the required note
  1393.      *
  1394.      * @since     2.0
  1395.      * @access    public
  1396.      * @return    string 
  1397.      */
  1398.     function getRequiredNote()
  1399.     {
  1400.         return $this->_requiredNote;
  1401.     // end func getRequiredNote
  1402.  
  1403.     // }}}
  1404.     // {{{ validate()
  1405.  
  1406.     /**
  1407.      * Performs the server side validation
  1408.      * @access    public
  1409.      * @since     1.0
  1410.      * @return    boolean   true if no error found
  1411.      */
  1412.     function validate()
  1413.     {
  1414.         if (count($this->_rules== 0 && count($this->_formRules== 0 && 
  1415.             (count($this->_submitValues> 0 || count($this->_submitFiles> 0)) {
  1416.             return true;
  1417.         elseif (count($this->_submitValues== 0 && count($this->_submitFiles== 0{
  1418.             return false;
  1419.         }
  1420.  
  1421.         include_once('HTML/QuickForm/RuleRegistry.php');
  1422.         $registry =HTML_QuickForm_RuleRegistry::singleton();
  1423.  
  1424.         foreach ($this->_rules as $target => $rules{
  1425.             $submitValue $this->getSubmitValue($target);
  1426.  
  1427.             foreach ($rules as $elementName => $rule{
  1428.                 if ((isset($rule['group']&& isset($this->_errors[$rule['group']])) ||
  1429.                      isset($this->_errors[$target])) {
  1430.                     continue 2;
  1431.                 }
  1432.                 if ((!isset($submitValue|| $submitValue == ''&& 
  1433.                      !$this->isElementRequired($target)) {
  1434.                     // Element is not required
  1435.                     continue 2;
  1436.                 }
  1437.                 if (isset($rule['dependent']&& is_array($rule['dependent'])) {
  1438.                     $values = array($submitValue);
  1439.                     foreach ($rule['dependent'as $elName{
  1440.                         $values[$this->getSubmitValue($elName);
  1441.                     }
  1442.                     $result $registry->validate($rule['type']$values$rule['format']true);
  1443.                 elseif (is_array($submitValue&& !isset($rule['howmany'])) {
  1444.                     $result $registry->validate($rule['type']$submitValue$rule['format']true);
  1445.                 else {
  1446.                     $result $registry->validate($rule['type']$submitValue$rule['format']false);
  1447.                 }
  1448.  
  1449.                 if (!$result || (!empty($rule['howmany']&& $rule['howmany'> (int)$result)) {
  1450.                     if (isset($rule['group'])) {
  1451.                         $this->_errors[$rule['group']] $rule['message'];
  1452.                     else {
  1453.                         $this->_errors[$target$rule['message'];
  1454.                     }
  1455.                 }
  1456.             }
  1457.         }
  1458.  
  1459.         // process the global rules now
  1460.         foreach ($this->_formRules as $rule{
  1461.             if (true !== ($res call_user_func($rule$this->_submitValues$this->_submitFiles))) {
  1462.                 if (is_array($res)) {
  1463.                     $this->_errors += $res;
  1464.                 else {
  1465.                     return PEAR::raiseError(nullQUICKFORM_ERRORnullE_USER_WARNING'Form rule callback returned invalid value in HTML_QuickForm::validate()''HTML_QuickForm_Error'true);
  1466.                 }
  1467.             }
  1468.         }
  1469.  
  1470.         return (0 == count($this->_errors));
  1471.     // end func validate
  1472.  
  1473.     // }}}
  1474.     // {{{ freeze()
  1475.  
  1476.     /**
  1477.      * Displays elements without HTML input tags
  1478.      *
  1479.      * @param    mixed   $elementList       array or string of element(s) to be frozen
  1480.      * @since     1.0
  1481.      * @access   public
  1482.      * @throws   HTML_QuickForm_Error
  1483.      */
  1484.     function freeze($elementList=null)
  1485.     {
  1486.         if (!isset($elementList)) {
  1487.             $this->_freezeAll = true;
  1488.             $elementList = array();
  1489.         else {
  1490.             if (!is_array($elementList)) {
  1491.                 $elementList preg_split('/[ ]*,[ ]*/'$elementList);
  1492.             }
  1493.             $elementList array_flip($elementList);
  1494.         }
  1495.  
  1496.         foreach (array_keys($this->_elementsas $key{
  1497.             $name $this->_elements[$key]->getName();
  1498.             if ($this->_freezeAll || isset($elementList[$name])) {
  1499.                 $this->_elements[$key]->freeze();
  1500.                 unset($elementList[$name]);
  1501.             }
  1502.         }
  1503.  
  1504.         if (!empty($elementList)) {
  1505.             return PEAR::raiseError(nullQUICKFORM_NONEXIST_ELEMENTnullE_USER_WARNING"Nonexistant element(s): '" implode("', '"array_keys($elementList)) "' in HTML_QuickForm::freeze()"'HTML_QuickForm_Error'true);
  1506.         }
  1507.         return true;
  1508.     // end func freeze
  1509.         
  1510.     // }}}
  1511.     // {{{ isFrozen()
  1512.  
  1513.     /**
  1514.      * Returns whether or not the whole form is frozen
  1515.      *
  1516.      * @since     3.0
  1517.      * @access    public
  1518.      * @return    boolean 
  1519.      */
  1520.     function isFrozen()
  1521.     {
  1522.          return $this->_freezeAll;
  1523.     // end func isFrozen
  1524.  
  1525.     // }}}
  1526.     // {{{ process()
  1527.  
  1528.     /**
  1529.      * Performs the form data processing
  1530.      *
  1531.      * @param    mixed     $callback        Callback, either function name or array(&$object, 'method')
  1532.      * @param    bool      $mergeFiles      Whether uploaded files should be processed too
  1533.      * @since    1.0
  1534.      * @access   public
  1535.      * @throws   HTML_QuickForm_Error
  1536.      */
  1537.     function process($callback$mergeFiles = true)
  1538.     {
  1539.         if (!is_callable($callback)) {
  1540.             return PEAR::raiseError(nullQUICKFORM_INVALID_PROCESSnullE_USER_WARNING"Callback function does not exist in QuickForm::process()"'HTML_QuickForm_Error'true);
  1541.         }
  1542.         $values ($mergeFiles === trueHTML_QuickForm::arrayMerge($this->_submitValues$this->_submitFiles$this->_submitValues;
  1543.         return call_user_func($callback$values);
  1544.     // end func process
  1545.  
  1546.     // }}}
  1547.     // {{{ accept()
  1548.  
  1549.    /**
  1550.     * Accepts a renderer
  1551.     *
  1552.     * @param object     An HTML_QuickForm_Renderer object
  1553.     * @since 3.0
  1554.     * @access public
  1555.     * @return void 
  1556.     */
  1557.     function accept(&$renderer)
  1558.     {
  1559.         $renderer->startForm($this);
  1560.         foreach (array_keys($this->_elementsas $key{
  1561.             $element =$this->_elements[$key];
  1562.             $elementName $element->getName();
  1563.             $required    ($this->isElementRequired($elementName&& !$element->isFrozen());
  1564.             $error       $this->getElementError($elementName);
  1565.             $element->accept($renderer$required$error);
  1566.         }
  1567.         $renderer->finishForm($this);
  1568.     // end func accept
  1569.  
  1570.     // }}}
  1571.     // {{{ defaultRenderer()
  1572.  
  1573.    /**
  1574.     * Returns a reference to default renderer object
  1575.     *
  1576.     * @access public
  1577.     * @since 3.0
  1578.     * @return object default renderer object
  1579.     */
  1580.     function &defaultRenderer()
  1581.     {
  1582.         if (!isset($GLOBALS['_HTML_QuickForm_default_renderer'])) {
  1583.             include_once('HTML/QuickForm/Renderer/Default.php');
  1584.             $GLOBALS['_HTML_QuickForm_default_renderer'=new HTML_QuickForm_Renderer_Default();
  1585.         }
  1586.         return $GLOBALS['_HTML_QuickForm_default_renderer'];
  1587.     // end func defaultRenderer
  1588.  
  1589.     // }}}
  1590.     // {{{ toHtml ()
  1591.  
  1592.     /**
  1593.      * Returns an HTML version of the form
  1594.      *
  1595.      * @param string $in_data (optional) Any extra data to insert right
  1596.      *                before form is rendered.  Useful when using templates.
  1597.      *
  1598.      * @return   string     Html version of the form
  1599.      * @since     1.0
  1600.      * @access   public
  1601.      */
  1602.     function toHtml ($in_data = null)
  1603.     {
  1604.         if (!is_null($in_data)) {
  1605.             $this->addElement('html'$in_data);
  1606.         }
  1607.         $renderer =$this->defaultRenderer();
  1608.         $this->accept($renderer);
  1609.         return $renderer->toHtml();
  1610.     // end func toHtml
  1611.  
  1612.     // }}}
  1613.     // {{{ getValidationScript()
  1614.  
  1615.     /**
  1616.      * Returns the client side validation script
  1617.      *
  1618.      * @since     2.0
  1619.      * @access    public
  1620.      * @return    string    Javascript to perform validation, empty string if no 'client' rules were added
  1621.      */
  1622.     function getValidationScript()
  1623.     {
  1624.         if (empty($this->_rules|| empty($this->_attributes['onsubmit'])) {
  1625.             return '';
  1626.         }
  1627.  
  1628.         include_once('HTML/QuickForm/RuleRegistry.php');
  1629.         $registry =HTML_QuickForm_RuleRegistry::singleton();
  1630.         $test = array();
  1631.         $js_escape = array(
  1632.             "\r"    => '\r',
  1633.             "\n"    => '\n',
  1634.             "\t"    => '\t',
  1635.             "'"     => "\\'",
  1636.             '"'     => '\"',
  1637.             '\\'    => '\\\\'
  1638.         );
  1639.  
  1640.         foreach ($this->_rules as $elementName => $rules{
  1641.             foreach ($rules as $rule{
  1642.                 if ('client' == $rule['validation']{
  1643.                     $dependent  = isset($rule['dependent']&& is_array($rule['dependent']);
  1644.                     $rule['message'strtr($rule['message']$js_escape);
  1645.  
  1646.                     if (isset($rule['group'])) {
  1647.                         $group    =$this->getElement($rule['group']);
  1648.                         // No JavaScript validation for frozen elements
  1649.                         if ($group->isFrozen()) {
  1650.                             continue 2;
  1651.                         }
  1652.                         $elements =$group->getElements();
  1653.                         foreach (array_keys($elementsas $key{
  1654.                             if ($elementName == $group->getElementName($key)) {
  1655.                                 $element =$elements[$key];
  1656.                                 break;
  1657.                             }
  1658.                         }
  1659.                     elseif ($dependent{
  1660.                         $element   =  array();
  1661.                         $element[=$this->getElement($elementName);
  1662.                         foreach ($rule['dependent'as $idx => $elName{
  1663.                             $element[=$this->getElement($elName);
  1664.                         }
  1665.                     else {
  1666.                         $element =$this->getElement($elementName);
  1667.                     }
  1668.                     // No JavaScript validation for frozen elements
  1669.                     if (is_object($element&& $element->isFrozen()) {
  1670.                         continue 2;
  1671.                     elseif (is_array($element)) {
  1672.                         foreach (array_keys($elementas $key{
  1673.                             if ($element[$key]->isFrozen()) {
  1674.                                 continue 3;
  1675.                             }
  1676.                         }
  1677.                     }
  1678.  
  1679.                     $test[$registry->getValidationScript($element$elementName$rule);
  1680.                     unset($element);
  1681.                 }
  1682.             }
  1683.         }
  1684.         if (count($test> 0{
  1685.             return
  1686.                 "\n<script type=\"text/javascript\">\n" .
  1687.                 "//<![CDATA[\n" 
  1688.                 "function validate_" $this->_attributes['id'"(frm) {\n" .
  1689.                 "  var value = '';\n" .
  1690.                 "  var errFlag = new Array();\n" .
  1691.                 "  _qfMsg = '';\n\n" .
  1692.                 join("\n"$test.
  1693.                 "\n  if (_qfMsg != '') {\n" .
  1694.                 "    _qfMsg = '" strtr($this->_jsPrefix$js_escape"' + _qfMsg;\n" .
  1695.                 "    _qfMsg = _qfMsg + '\\n" strtr($this->_jsPostfix$js_escape"';\n" .
  1696.                 "    alert(_qfMsg);\n" .
  1697.                 "    return false;\n" .
  1698.                 "  }\n" .
  1699.                 "  return true;\n" .
  1700.                 "}\n" .
  1701.                 "//]]>\n" .
  1702.                 "</script>";
  1703.         }
  1704.         return '';
  1705.     // end func getValidationScript
  1706.  
  1707.     // }}}
  1708.     // {{{ getSubmitValues()
  1709.  
  1710.     /**
  1711.      * Returns the values submitted by the form
  1712.      *
  1713.      * @since     2.0
  1714.      * @access    public
  1715.      * @param     bool      Whether uploaded files should be returned too
  1716.      * @return    array 
  1717.      */
  1718.     function getSubmitValues($mergeFiles = false)
  1719.     {
  1720.         return $mergeFilesHTML_QuickForm::arrayMerge($this->_submitValues$this->_submitFiles)$this->_submitValues;
  1721.     // end func getSubmitValues
  1722.  
  1723.     // }}}
  1724.     // {{{ toArray()
  1725.  
  1726.     /**
  1727.      * Returns the form's contents in an array.
  1728.      *
  1729.      * The description of the array structure is in HTML_QuickForm_Renderer_Array docs
  1730.      * 
  1731.      * @since     2.0
  1732.      * @access    public
  1733.      * @param     bool      Whether to collect hidden elements (passed to the Renderer's constructor)
  1734.      * @return    array of form contents
  1735.      */
  1736.     function toArray($collectHidden = false)
  1737.     {
  1738.         include_once 'HTML/QuickForm/Renderer/Array.php';
  1739.         $renderer =new HTML_QuickForm_Renderer_Array($collectHidden);
  1740.         $this->accept($renderer);
  1741.         return $renderer->toArray();
  1742.      // end func toArray
  1743.  
  1744.     // }}}
  1745.     // {{{ exportValue()
  1746.  
  1747.     /**
  1748.      * Returns a 'safe' element's value
  1749.      * 
  1750.      * This method first tries to find a cleaned-up submitted value,
  1751.      * it will return a value set by setValue()/setDefaults()/setConstants()
  1752.      * if submitted value does not exist for the given element.
  1753.      *
  1754.      * @param  string   Name of an element
  1755.      * @access public
  1756.      * @return mixed 
  1757.      */
  1758.     function exportValue($element)
  1759.     {
  1760.         if (!isset($this->_elementIndex[$element])) {
  1761.             return PEAR::raiseError(nullQUICKFORM_NONEXIST_ELEMENTnullE_USER_WARNING"Element '$element' does not exist in HTML_QuickForm::getElementValue()"'HTML_QuickForm_Error'true);
  1762.         }
  1763.         $value $this->_elements[$this->_elementIndex[$element]]->exportValue($this->_submitValuesfalse);
  1764.         if (isset($this->_duplicateIndex[$element])) {
  1765.             foreach ($this->_duplicateIndex[$elementas $index{
  1766.                 if (null !== ($v $this->_elements[$index]->exportValue($this->_submitValuesfalse))) {
  1767.                     if (is_array($value)) {
  1768.                         $value[$v;
  1769.                     else {
  1770.                         $value (null === $value)$v: array($value$v);
  1771.                     }
  1772.                 }
  1773.             }
  1774.         }
  1775.         return $value;
  1776.     }
  1777.  
  1778.     // }}}
  1779.     // {{{ exportValues()
  1780.  
  1781.     /**
  1782.      * Returns 'safe' elements' values
  1783.      *
  1784.      * Unlike getSubmitValues(), this will return only the values
  1785.      * corresponding to the elements present in the form.
  1786.      * 
  1787.      * @param   mixed   Array/string of element names, whose values we want. If not set then return all elements.
  1788.      * @access  public
  1789.      * @return  array   An assoc array of elements' values
  1790.      * @throws  HTML_QuickForm_Error
  1791.      */
  1792.     function exportValues($elementList = null)
  1793.     {
  1794.         $values = array();
  1795.         if (null === $elementList{
  1796.             // iterate over all elements, calling their exportValue() methods
  1797.             foreach (array_keys($this->_elementsas $key{
  1798.                 $value $this->_elements[$key]->exportValue($this->_submitValuestrue);
  1799.                 if (is_array($value)) {
  1800.                     // This shit throws a bogus warning in PHP 4.3.x
  1801.                     $values HTML_QuickForm::arrayMerge($values$value);
  1802.                 }
  1803.             }
  1804.         else {
  1805.             if (!is_array($elementList)) {
  1806.                 $elementList array_map('trim'explode(','$elementList));
  1807.             }
  1808.             foreach ($elementList as $elementName{
  1809.                 $value $this->exportValue($elementName);
  1810.                 if (PEAR::isError($value)) {
  1811.                     return $value;
  1812.                 }
  1813.                 $values[$elementName$value;
  1814.             }
  1815.         }
  1816.         return $values;
  1817.     }
  1818.  
  1819.     // }}}
  1820.     // {{{ isError()
  1821.  
  1822.     /**
  1823.      * Tell whether a result from a QuickForm method is an error (an instance of HTML_QuickForm_Error)
  1824.      *
  1825.      * @access public
  1826.      * @param mixed     result code
  1827.      * @return bool     whether $value is an error
  1828.      */
  1829.     function isError($value)
  1830.     {
  1831.         return (is_object($value&& is_a($value'html_quickform_error'));
  1832.     // end func isError
  1833.  
  1834.     // }}}
  1835.     // {{{ errorMessage()
  1836.  
  1837.     /**
  1838.      * Return a textual error message for an QuickForm error code
  1839.      *
  1840.      * @access  public
  1841.      * @param   int     error code
  1842.      * @return  string  error message
  1843.      */
  1844.     function errorMessage($value)
  1845.     {
  1846.         // make the variable static so that it only has to do the defining on the first call
  1847.         static $errorMessages;
  1848.  
  1849.         // define the varies error messages
  1850.         if (!isset($errorMessages)) {
  1851.             $errorMessages = array(
  1852.                 QUICKFORM_OK                    => 'no error',
  1853.                 QUICKFORM_ERROR                 => 'unknown error',
  1854.                 QUICKFORM_INVALID_RULE          => 'the rule does not exist as a registered rule',
  1855.                 QUICKFORM_NONEXIST_ELEMENT      => 'nonexistent html element',
  1856.                 QUICKFORM_INVALID_FILTER        => 'invalid filter',
  1857.                 QUICKFORM_UNREGISTERED_ELEMENT  => 'unregistered element',
  1858.                 QUICKFORM_INVALID_ELEMENT_NAME  => 'element already exists',
  1859.                 QUICKFORM_INVALID_PROCESS       => 'process callback does not exist',
  1860.                 QUICKFORM_DEPRECATED            => 'method is deprecated',
  1861.                 QUICKFORM_INVALID_DATASOURCE    => 'datasource is not an object'
  1862.             );
  1863.         }
  1864.  
  1865.         // If this is an error object, then grab the corresponding error code
  1866.         if (HTML_QuickForm::isError($value)) {
  1867.             $value $value->getCode();
  1868.         }
  1869.  
  1870.         // return the textual error message corresponding to the code
  1871.         return isset($errorMessages[$value]$errorMessages[$value$errorMessages[QUICKFORM_ERROR];
  1872.     // end func errorMessage
  1873.  
  1874.     // }}}
  1875. // end class HTML_QuickForm
  1876.  
  1877. class HTML_QuickForm_Error extends PEAR_Error {
  1878.  
  1879.     // {{{ properties
  1880.  
  1881.     /**
  1882.     * Prefix for all error messages
  1883.     * @var string 
  1884.     */
  1885.     var $error_message_prefix = 'QuickForm Error: ';
  1886.  
  1887.     // }}}
  1888.     // {{{ constructor
  1889.  
  1890.     /**
  1891.     * Creates a quickform error object, extending the PEAR_Error class
  1892.     *
  1893.     * @param int   $code the error code
  1894.     * @param int   $mode the reaction to the error, either return, die or trigger/callback
  1895.     * @param int   $level intensity of the error (PHP error code)
  1896.     * @param mixed $debuginfo any information that can inform user as to nature of the error
  1897.     */
  1898.     function HTML_QuickForm_Error($code = QUICKFORM_ERROR$mode = PEAR_ERROR_RETURN,
  1899.                          $level = E_USER_NOTICE$debuginfo = null)
  1900.     {
  1901.         if (is_int($code)) {
  1902.             $this->PEAR_Error(HTML_QuickForm::errorMessage($code)$code$mode$level$debuginfo);
  1903.         else {
  1904.             $this->PEAR_Error("Invalid error code: $code"QUICKFORM_ERROR$mode$level$debuginfo);
  1905.         }
  1906.     }
  1907.  
  1908.     // }}}
  1909. // end class HTML_QuickForm_Error
  1910. ?>

Documentation generated on Mon, 11 Mar 2019 13:57:10 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.