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.151 2004/10/20 18:43:56 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.                 // filter out radios
  782.                 if ($name != $elementName{
  783.                     if (null !== ($v $this->getSubmitValue($name))) {
  784.                         $value[$name$v;
  785.                     }
  786.                 }
  787.             }
  788.  
  789.         elseif (false !== ($pos strpos($elementName'['))) {
  790.             $base substr($elementName0$pos);
  791.             $idx  "['" str_replace(array(']''[')array(''"']['")substr($elementName$pos + 1-1)) "']";
  792.             if (isset($this->_submitValues[$base])) {
  793.                 $value = eval("return (isset(\$this->_submitValues['{$base}']{$idx})) ? \$this->_submitValues['{$base}']{$idx} : null;");
  794.             }
  795.  
  796.             if (null === $value && isset($this->_submitFiles[$base])) {
  797.                 $props = array('name''type''size''tmp_name''error');
  798.                 $code  = "if (!isset(\$this->_submitFiles['{$base}']['name']{$idx})) {\n" .
  799.                          "    return null;\n" .
  800.                          "} else {\n" .
  801.                          "    \$v = array();\n";
  802.                 foreach ($props as $prop{
  803.                     $code .= "    \$v['{$prop}'] = \$this->_submitFiles['{$base}']['{$prop}']{$idx};\n";
  804.                 }
  805.                 $value = eval($code "    return \$v;\n}\n");
  806.             }
  807.         }
  808.         return $value;
  809.     // end func getSubmitValue
  810.  
  811.     // }}}
  812.     // {{{ _reindexFiles()
  813.  
  814.    /**
  815.     * A helper function to change the indexes in $_FILES array
  816.     *
  817.     * @param  mixed   Some value from the $_FILES array
  818.     * @param  string  The key from the $_FILES array that should be appended
  819.     * @return array 
  820.     */
  821.     function _reindexFiles($value$key)
  822.     {
  823.         if (!is_array($value)) {
  824.             return array($key => $value);
  825.         else {
  826.             $ret = array();
  827.             foreach ($value as $k => $v{
  828.                 $ret[$k$this->_reindexFiles($v$key);
  829.             }
  830.             return $ret;
  831.         }
  832.     }
  833.  
  834.     // }}}
  835.     // {{{ getElementError()
  836.  
  837.     /**
  838.      * Returns error corresponding to validated element
  839.      *
  840.      * @param     string    $element        Name of form element to check
  841.      * @since     1.0
  842.      * @access    public
  843.      * @return    string    error message corresponding to checked element
  844.      */
  845.     function getElementError($element)
  846.     {
  847.         if (isset($this->_errors[$element])) {
  848.             return $this->_errors[$element];
  849.         }
  850.     // end func getElementError
  851.     
  852.     // }}}
  853.     // {{{ setElementError()
  854.  
  855.     /**
  856.      * Set error message for a form element
  857.      *
  858.      * @param     string    $element    Name of form element to set error for
  859.      * @param     string    $message    Error message
  860.      * @since     1.0
  861.      * @access    public
  862.      * @return    void 
  863.      */
  864.     function setElementError($element,$message)
  865.     {
  866.         $this->_errors[$element$message;
  867.     // end func setElementError
  868.          
  869.      // }}}
  870.      // {{{ getElementType()
  871.  
  872.      /**
  873.       * Returns the type of the given element
  874.       *
  875.       * @param      string    $element    Name of form element
  876.       * @since      1.1
  877.       * @access     public
  878.       * @return     string    Type of the element, false if the element is not found
  879.       */
  880.      function getElementType($element)
  881.      {
  882.          if (isset($this->_elementIndex[$element])) {
  883.              return $this->_elements[$this->_elementIndex[$element]]->getType();
  884.          }
  885.          return false;
  886.      // end func getElementType
  887.  
  888.      // }}}
  889.      // {{{ updateElementAttr()
  890.  
  891.     /**
  892.      * Updates Attributes for one or more elements
  893.      *
  894.      * @param      mixed    $elements   Array of element names/objects or string of elements to be updated
  895.      * @param      mixed    $attrs      Array or sting of html attributes
  896.      * @since      2.10
  897.      * @access     public
  898.      * @return     void 
  899.      */
  900.     function updateElementAttr($elements$attrs)
  901.     {
  902.         if (is_string($elements)) {
  903.             $elements split('[ ]?,[ ]?'$elements);
  904.         }
  905.         foreach (array_keys($elementsas $key{
  906.             if (is_object($elements[$key]&& is_a($elements[$key]'HTML_QuickForm_element')) {
  907.                 $elements[$key]->updateAttributes($attrs);
  908.             elseif (isset($this->_elementIndex[$elements[$key]])) {
  909.                 $this->_elements[$this->_elementIndex[$elements[$key]]]->updateAttributes($attrs);
  910.                 if (isset($this->_duplicateIndex[$elements[$key]])) {
  911.                     foreach ($this->_duplicateIndex[$elements[$key]] as $index{
  912.                         $this->_elements[$index]->updateAttributes($attrs);
  913.                     }
  914.                 }
  915.             }
  916.         }
  917.     // end func updateElementAttr
  918.  
  919.     // }}}
  920.     // {{{ removeElement()
  921.  
  922.     /**
  923.      * Removes an element
  924.      *
  925.      * The method "unlinks" an element from the form, returning the reference
  926.      * to the element object. If several elements named $elementName exist,
  927.      * it removes the first one, leaving the others intact.
  928.      * 
  929.      * @param string    $elementName The element name
  930.      * @param boolean   $removeRules True if rules for this element are to be removed too
  931.      * @access public
  932.      * @since 2.0
  933.      * @return object HTML_QuickForm_element    a reference to the removed element
  934.      * @throws HTML_QuickForm_Error
  935.      */
  936.     function &removeElement($elementName$removeRules = true)
  937.     {
  938.         if (!isset($this->_elementIndex[$elementName])) {
  939.             return PEAR::raiseError(nullQUICKFORM_NONEXIST_ELEMENTnullE_USER_WARNING"Element '$elementName' does not exist in HTML_QuickForm::removeElement()"'HTML_QuickForm_Error'true);
  940.         }
  941.         $el =$this->_elements[$this->_elementIndex[$elementName]];
  942.             unset($this->_elements[$this->_elementIndex[$elementName]]);
  943.         if (empty($this->_duplicateIndex[$elementName])) {
  944.             unset($this->_elementIndex[$elementName]);
  945.         else {
  946.             $this->_elementIndex[$elementNamearray_shift($this->_duplicateIndex[$elementName]);
  947.         }
  948.         if ($removeRules{
  949.             unset($this->_rules[$elementName]);
  950.         }
  951.         return $el;
  952.     // end func removeElement
  953.  
  954.     // }}}
  955.     // {{{ addRule()
  956.  
  957.     /**
  958.      * Adds a validation rule for the given field
  959.      *
  960.      * If the element is in fact a group, it will be considered as a whole.
  961.      * To validate grouped elements as separated entities,
  962.      * use addGroupRule instead of addRule.
  963.      *
  964.      * @param    string     $element       Form element name
  965.      * @param    string     $message       Message to display for invalid data
  966.      * @param    string     $type          Rule type, use getRegisteredRules() to get types
  967.      * @param    string     $format        (optional)Required for extra rule data
  968.      * @param    string     $validation    (optional)Where to perform validation: "server", "client"
  969.      * @param    boolean    $reset         Client-side validation: reset the form element to its original value if there is an error?
  970.      * @param    boolean    $force         Force the rule to be applied, even if the target form element does not exist
  971.      * @since    1.0
  972.      * @access   public
  973.      * @throws   HTML_QuickForm_Error
  974.      */
  975.     function addRule($element$message$type$format=null$validation='server'$reset = false$force = false)
  976.     {
  977.         if (!$force{
  978.             if (!is_array($element&& !$this->elementExists($element)) {
  979.                 return PEAR::raiseError(nullQUICKFORM_NONEXIST_ELEMENTnullE_USER_WARNING"Element '$element' does not exist in HTML_QuickForm::addRule()"'HTML_QuickForm_Error'true);
  980.             elseif (is_array($element)) {
  981.                 foreach ($element as $el{
  982.                     if (!$this->elementExists($el)) {
  983.                         return PEAR::raiseError(nullQUICKFORM_NONEXIST_ELEMENTnullE_USER_WARNING"Element '$el' does not exist in HTML_QuickForm::addRule()"'HTML_QuickForm_Error'true);
  984.                     }
  985.                 }
  986.             }
  987.         }
  988.         if (false === ($newName $this->isRuleRegistered($typetrue))) {
  989.             return PEAR::raiseError(nullQUICKFORM_INVALID_RULEnullE_USER_WARNING"Rule '$type' is not registered in HTML_QuickForm::addRule()"'HTML_QuickForm_Error'true);
  990.         elseif (is_string($newName)) {
  991.             $type $newName;
  992.         }
  993.         if (is_array($element)) {
  994.             $dependent $element;
  995.             $element   array_shift($dependent);
  996.         else {
  997.             $dependent = null;
  998.         }
  999.         if ($type == 'required' || $type == 'uploadedfile'{
  1000.             $this->_required[$element;
  1001.         }
  1002.         if (!isset($this->_rules[$element])) {
  1003.             $this->_rules[$element= array();
  1004.         }
  1005.         if ($validation == 'client'{
  1006.             $this->updateAttributes(array('onsubmit'=>'return validate_'.$this->_attributes['id''(this);'));
  1007.         }
  1008.         $this->_rules[$element][= array(
  1009.             'type'        => $type,
  1010.             'format'      => $format,
  1011.             'message'     => $message,
  1012.             'validation'  => $validation,
  1013.             'reset'       => $reset,
  1014.             'dependent'   => $dependent
  1015.         );
  1016.     // end func addRule
  1017.  
  1018.     // }}}
  1019.     // {{{ addGroupRule()
  1020.  
  1021.     /**
  1022.      * Adds a validation rule for the given group of elements
  1023.      *
  1024.      * Only groups with a name can be assigned a validation rule
  1025.      * Use addGroupRule when you need to validate elements inside the group.
  1026.      * Use addRule if you need to validate the group as a whole. In this case,
  1027.      * the same rule will be applied to all elements in the group.
  1028.      * Use addRule if you need to validate the group against a function.
  1029.      *
  1030.      * @param    string     $group         Form group name
  1031.      * @param    mixed      $arg1          Array for multiple elements or error message string for one element
  1032.      * @param    string     $type          (optional)Rule type use getRegisteredRules() to get types
  1033.      * @param    string     $format        (optional)Required for extra rule data
  1034.      * @param    int        $howmany       (optional)How many valid elements should be in the group
  1035.      * @param    string     $validation    (optional)Where to perform validation: "server", "client"
  1036.      * @param    bool       $reset         Client-side: whether to reset the element's value to its original state if validation failed.
  1037.      * @since    2.5
  1038.      * @access   public
  1039.      * @throws   HTML_QuickForm_Error
  1040.      */
  1041.     function addGroupRule($group$arg1$type=''$format=null$howmany=0$validation 'server'$reset = false)
  1042.     {
  1043.         if (!$this->elementExists($group)) {
  1044.             return PEAR::raiseError(nullQUICKFORM_NONEXIST_ELEMENTnullE_USER_WARNING"Group '$group' does not exist in HTML_QuickForm::addGroupRule()"'HTML_QuickForm_Error'true);
  1045.         }
  1046.  
  1047.         $groupObj =$this->getElement($group);
  1048.         if (is_array($arg1)) {
  1049.             $required = 0;
  1050.             foreach ($arg1 as $elementIndex => $rules{
  1051.                 $elementName $groupObj->getElementName($elementIndex);
  1052.                 foreach ($rules as $rule{
  1053.                     $format (isset($rule[2])) $rule[2: null;
  1054.                     $validation (isset($rule[3]&& 'client' == $rule[3])'client''server';
  1055.                     $reset = isset($rule[4]&& $rule[4];
  1056.                     $type $rule[1];
  1057.                     if (false === ($newName $this->isRuleRegistered($typetrue))) {
  1058.                         return PEAR::raiseError(nullQUICKFORM_INVALID_RULEnullE_USER_WARNING"Rule '$type' is not registered in HTML_QuickForm::addGroupRule()"'HTML_QuickForm_Error'true);
  1059.                     elseif (is_string($newName)) {
  1060.                         $type $newName;
  1061.                     }
  1062.  
  1063.                     $this->_rules[$elementName][= array(
  1064.                                                         'type'        => $type,
  1065.                                                         'format'      => $format
  1066.                                                         'message'     => $rule[0],
  1067.                                                         'validation'  => $validation,
  1068.                                                         'reset'       => $reset,
  1069.                                                         'group'       => $group);
  1070.  
  1071.                     if ('required' == $type || 'uploadedfile' == $type{
  1072.                         $groupObj->_required[$elementName;
  1073.                         $this->_required[$elementName;
  1074.                         $required++;
  1075.                     }
  1076.                     if ('client' == $validation{
  1077.                         $this->updateAttributes(array('onsubmit'=>'return validate_'.$this->_attributes['id''(this);'));
  1078.                     }
  1079.                 }
  1080.             }
  1081.             if ($required > 0 && count($groupObj->getElements()) == $required{
  1082.                 $this->_required[$group;
  1083.             }
  1084.         elseif (is_string($arg1)) {
  1085.             if (false === ($newName $this->isRuleRegistered($typetrue))) {
  1086.                 return PEAR::raiseError(nullQUICKFORM_INVALID_RULEnullE_USER_WARNING"Rule '$type' is not registered in HTML_QuickForm::addGroupRule()"'HTML_QuickForm_Error'true);
  1087.             elseif (is_string($newName)) {
  1088.                 $type $newName;
  1089.             }
  1090.  
  1091.             // addGroupRule() should also handle <select multiple>
  1092.             if (is_a($groupObj'html_quickform_group')) {
  1093.                 // Radios need to be handled differently when required
  1094.                 if ($type == 'required' && $groupObj->getGroupType(== 'radio'{
  1095.                     $howmany ($howmany == 0? 1 : $howmany;
  1096.                 else {
  1097.                     $howmany ($howmany == 0count($groupObj->getElements()) $howmany;
  1098.                 }
  1099.             }
  1100.  
  1101.             $this->_rules[$group][= array('type'       => $type,
  1102.                                             'format'     => $format
  1103.                                             'message'    => $arg1,
  1104.                                             'validation' => $validation,
  1105.                                             'howmany'    => $howmany,
  1106.                                             'reset'      => $reset);
  1107.             if ($type == 'required'{
  1108.                 $this->_required[$group;
  1109.             }
  1110.             if ($validation == 'client'{
  1111.                 $this->updateAttributes(array('onsubmit'=>'return validate_'.$this->_attributes['id''(this);'));
  1112.             }
  1113.         }
  1114.     // end func addGroupRule
  1115.  
  1116.     // }}}
  1117.     // {{{ addFormRule()
  1118.  
  1119.    /**
  1120.     * Adds a global validation rule
  1121.     * 
  1122.     * This should be used when for a rule involving several fields or if
  1123.     * you want to use some completely custom validation for your form.
  1124.     * The rule function/method should return true in case of successful
  1125.     * validation and array('element name' => 'error') when there were errors.
  1126.     * 
  1127.     * @access   public
  1128.     * @param    mixed   Callback, either function name or array(&$object, 'method')
  1129.     * @throws   HTML_QuickForm_Error
  1130.     */
  1131.     function addFormRule($rule)
  1132.     {
  1133.         if (!is_callable($rule)) {
  1134.             return PEAR::raiseError(nullQUICKFORM_INVALID_RULEnullE_USER_WARNING'Callback function does not exist in HTML_QuickForm::addFormRule()''HTML_QuickForm_Error'true);
  1135.         }
  1136.         $this->_formRules[$rule;
  1137.     }
  1138.     
  1139.     // }}}
  1140.     // {{{ applyFilter()
  1141.  
  1142.     /**
  1143.      * Applies a data filter for the given field(s)
  1144.      *
  1145.      * @param    mixed     $element       Form element name or array of such names
  1146.      * @param    mixed     $filter        Callback, either function name or array(&$object, 'method')
  1147.      * @since    2.0
  1148.      * @access   public
  1149.      */
  1150.     function applyFilter($element$filter)
  1151.     {
  1152.         if (!is_callable($filter)) {
  1153.             return PEAR::raiseError(nullQUICKFORM_INVALID_FILTERnullE_USER_WARNING"Callback function does not exist in QuickForm::applyFilter()"'HTML_QuickForm_Error'true);
  1154.         }
  1155.         if ($element == '__ALL__'{
  1156.             $this->_submitValues $this->_recursiveFilter($filter$this->_submitValues);
  1157.         else {
  1158.             if (!is_array($element)) {
  1159.                 $element = array($element);
  1160.             }
  1161.             foreach ($element as $elName{
  1162.                 $value $this->getSubmitValue($elName);
  1163.                 if (null !== $value{
  1164.                     if (false === strpos($elName'[')) {
  1165.                         $this->_submitValues[$elName$this->_recursiveFilter($filter$value);
  1166.                     else {
  1167.                         $idx  "['" str_replace(array(']''[')array(''"']['")$elName"']";
  1168.                         eval("\$this->_submitValues{$idx} = \$this->_recursiveFilter(\$filter, \$value);");
  1169.                     }
  1170.                 }
  1171.             }
  1172.         }
  1173.     // end func applyFilter
  1174.  
  1175.     // }}}
  1176.     // {{{ _recursiveFilter()
  1177.  
  1178.     /**
  1179.      * Recursively apply a filter function
  1180.      *
  1181.      * @param     string   $filter    filter to apply
  1182.      * @param     mixed    $value     submitted values
  1183.      * @since     2.0
  1184.      * @access    private
  1185.      * @return    cleaned values
  1186.      */
  1187.     function _recursiveFilter($filter$value)
  1188.     {
  1189.         if (is_array($value)) {
  1190.             $cleanValues = array();
  1191.             foreach ($value as $k => $v{
  1192.                 $cleanValues[$k$this->_recursiveFilter($filter$value[$k]);
  1193.             }
  1194.             return $cleanValues;
  1195.         else {
  1196.             return call_user_func($filter$value);
  1197.         }
  1198.     // end func _recursiveFilter
  1199.  
  1200.     // }}}
  1201.     // {{{ arrayMerge()
  1202.  
  1203.    /**
  1204.     * Merges two arrays
  1205.     *
  1206.     * Merges two array like the PHP function array_merge but recursively.
  1207.     * The main difference is that existing keys will not be renumbered
  1208.     * if they are integers.
  1209.     *
  1210.     * @access   puplic
  1211.     * @param    array   $a  original array
  1212.     * @param    array   $b  array which will be merged into first one
  1213.     * @return   array   merged array
  1214.     */
  1215.     function arrayMerge($a$b)
  1216.     {
  1217.         foreach ($b as $k => $v{
  1218.             if (is_array($v)) {
  1219.                 if (isset($a[$k]&& !is_array($a[$k])) {
  1220.                     $a[$k$v;
  1221.                 else {
  1222.                     if (!isset($a[$k])) {
  1223.                         $a[$k= array();
  1224.                     }
  1225.                     $a[$kHTML_QuickForm::arrayMerge($a[$k]$v);
  1226.                 }
  1227.             else {
  1228.                 $a[$k$v;
  1229.             }
  1230.         }
  1231.         return $a;
  1232.     // end func arrayMerge
  1233.  
  1234.     // }}}
  1235.     // {{{ isTypeRegistered()
  1236.  
  1237.     /**
  1238.      * Returns whether or not the form element type is supported
  1239.      *
  1240.      * @param     string   $type     Form element type
  1241.      * @since     1.0
  1242.      * @access    public
  1243.      * @return    boolean 
  1244.      */
  1245.     function isTypeRegistered($type)
  1246.     {
  1247.         return isset($GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES'][$type]);
  1248.     // end func isTypeRegistered
  1249.  
  1250.     // }}}
  1251.     // {{{ getRegisteredTypes()
  1252.  
  1253.     /**
  1254.      * Returns an array of registered element types
  1255.      *
  1256.      * @since     1.0
  1257.      * @access    public
  1258.      * @return    array 
  1259.      */
  1260.     function getRegisteredTypes()
  1261.     {
  1262.         return array_keys($GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES']);
  1263.     // end func getRegisteredTypes
  1264.  
  1265.     // }}}
  1266.     // {{{ isRuleRegistered()
  1267.  
  1268.     /**
  1269.      * Returns whether or not the given rule is supported
  1270.      *
  1271.      * @param     string   $name    Validation rule name
  1272.      * @param     bool     Whether to automatically register subclasses of HTML_QuickForm_Rule
  1273.      * @since     1.0
  1274.      * @access    public
  1275.      * @return    mixed    true if previously registered, false if not, new rule name if auto-registering worked
  1276.      */
  1277.     function isRuleRegistered($name$autoRegister = false)
  1278.     {
  1279.         if (is_scalar($name&& isset($GLOBALS['_HTML_QuickForm_registered_rules'][$name])) {
  1280.             return true;
  1281.         elseif (!$autoRegister{
  1282.             return false;
  1283.         }
  1284.         // automatically register the rule if requested
  1285.         include_once 'HTML/QuickForm/RuleRegistry.php';
  1286.         $ruleName = false;
  1287.         if (is_object($name&& is_a($name'html_quickform_rule')) {
  1288.             $ruleName !empty($name->name)$name->name: strtolower(get_class($name));
  1289.         elseif (is_string($name&& class_exists($name)) {
  1290.             $parent strtolower($name);
  1291.             do {
  1292.                 if ('html_quickform_rule' == strtolower($parent)) {
  1293.                     $ruleName strtolower($name);
  1294.                     break;
  1295.                 }
  1296.             while ($parent get_parent_class($parent));
  1297.         }
  1298.         if ($ruleName{
  1299.             $registry =HTML_QuickForm_RuleRegistry::singleton();
  1300.             $registry->registerRule($ruleNamenull$name);
  1301.         }
  1302.         return $ruleName;
  1303.     // end func isRuleRegistered
  1304.  
  1305.     // }}}
  1306.     // {{{ getRegisteredRules()
  1307.  
  1308.     /**
  1309.      * Returns an array of registered validation rules
  1310.      *
  1311.      * @since     1.0
  1312.      * @access    public
  1313.      * @return    array 
  1314.      */
  1315.     function getRegisteredRules()
  1316.     {
  1317.         return array_keys($GLOBALS['_HTML_QuickForm_registered_rules']);
  1318.     // end func getRegisteredRules
  1319.  
  1320.     // }}}
  1321.     // {{{ isElementRequired()
  1322.  
  1323.     /**
  1324.      * Returns whether or not the form element is required
  1325.      *
  1326.      * @param     string   $element     Form element name
  1327.      * @since     1.0
  1328.      * @access    public
  1329.      * @return    boolean 
  1330.      */
  1331.     function isElementRequired($element)
  1332.     {
  1333.         return in_array($element$this->_requiredtrue);
  1334.     // end func isElementRequired
  1335.  
  1336.     // }}}
  1337.     // {{{ isElementFrozen()
  1338.  
  1339.     /**
  1340.      * Returns whether or not the form element is frozen
  1341.      *
  1342.      * @param     string   $element     Form element name
  1343.      * @since     1.0
  1344.      * @access    public
  1345.      * @return    boolean 
  1346.      */
  1347.     function isElementFrozen($element)
  1348.     {
  1349.          if (isset($this->_elementIndex[$element])) {
  1350.              return $this->_elements[$this->_elementIndex[$element]]->isFrozen();
  1351.          }
  1352.          return false;
  1353.     // end func isElementFrozen
  1354.  
  1355.     // }}}
  1356.     // {{{ setJsWarnings()
  1357.  
  1358.     /**
  1359.      * Sets JavaScript warning messages
  1360.      *
  1361.      * @param     string   $pref        Prefix warning
  1362.      * @param     string   $post        Postfix warning
  1363.      * @since     1.1
  1364.      * @access    public
  1365.      * @return    void 
  1366.      */
  1367.     function setJsWarnings($pref$post)
  1368.     {
  1369.         $this->_jsPrefix = $pref;
  1370.         $this->_jsPostfix = $post;
  1371.     // end func setJsWarnings
  1372.     
  1373.     // }}}
  1374.     // {{{ setRequiredNote()
  1375.  
  1376.     /**
  1377.      * Sets required-note
  1378.      *
  1379.      * @param     string   $note        Message indicating some elements are required
  1380.      * @since     1.1
  1381.      * @access    public
  1382.      * @return    void 
  1383.      */
  1384.     function setRequiredNote($note)
  1385.     {
  1386.         $this->_requiredNote = $note;
  1387.     // end func setRequiredNote
  1388.  
  1389.     // }}}
  1390.     // {{{ getRequiredNote()
  1391.  
  1392.     /**
  1393.      * Returns the required note
  1394.      *
  1395.      * @since     2.0
  1396.      * @access    public
  1397.      * @return    string 
  1398.      */
  1399.     function getRequiredNote()
  1400.     {
  1401.         return $this->_requiredNote;
  1402.     // end func getRequiredNote
  1403.  
  1404.     // }}}
  1405.     // {{{ validate()
  1406.  
  1407.     /**
  1408.      * Performs the server side validation
  1409.      * @access    public
  1410.      * @since     1.0
  1411.      * @return    boolean   true if no error found
  1412.      */
  1413.     function validate()
  1414.     {
  1415.         if (count($this->_rules== 0 && count($this->_formRules== 0 && 
  1416.             (count($this->_submitValues> 0 || count($this->_submitFiles> 0)) {
  1417.             return true;
  1418.         elseif (count($this->_submitValues== 0 && count($this->_submitFiles== 0{
  1419.             return false;
  1420.         }
  1421.  
  1422.         include_once('HTML/QuickForm/RuleRegistry.php');
  1423.         $registry =HTML_QuickForm_RuleRegistry::singleton();
  1424.  
  1425.         foreach ($this->_rules as $target => $rules{
  1426.             $submitValue $this->getSubmitValue($target);
  1427.  
  1428.             foreach ($rules as $elementName => $rule{
  1429.                 if ((isset($rule['group']&& isset($this->_errors[$rule['group']])) ||
  1430.                      isset($this->_errors[$target])) {
  1431.                     continue 2;
  1432.                 }
  1433.                 if ((!isset($submitValue|| $submitValue == ''&& 
  1434.                      !$this->isElementRequired($target)) {
  1435.                     // Element is not required
  1436.                     continue 2;
  1437.                 }
  1438.                 if (isset($rule['dependent']&& is_array($rule['dependent'])) {
  1439.                     $values = array($submitValue);
  1440.                     foreach ($rule['dependent'as $elName{
  1441.                         $values[$this->getSubmitValue($elName);
  1442.                     }
  1443.                     $result $registry->validate($rule['type']$values$rule['format']true);
  1444.                 elseif (is_array($submitValue&& !isset($rule['howmany'])) {
  1445.                     $result $registry->validate($rule['type']$submitValue$rule['format']true);
  1446.                 else {
  1447.                     $result $registry->validate($rule['type']$submitValue$rule['format']false);
  1448.                 }
  1449.  
  1450.                 if (!$result || (!empty($rule['howmany']&& $rule['howmany'> (int)$result)) {
  1451.                     if (isset($rule['group'])) {
  1452.                         $this->_errors[$rule['group']] $rule['message'];
  1453.                     else {
  1454.                         $this->_errors[$target$rule['message'];
  1455.                     }
  1456.                 }
  1457.             }
  1458.         }
  1459.  
  1460.         // process the global rules now
  1461.         foreach ($this->_formRules as $rule{
  1462.             if (true !== ($res call_user_func($rule$this->_submitValues$this->_submitFiles))) {
  1463.                 if (is_array($res)) {
  1464.                     $this->_errors += $res;
  1465.                 else {
  1466.                     return PEAR::raiseError(nullQUICKFORM_ERRORnullE_USER_WARNING'Form rule callback returned invalid value in HTML_QuickForm::validate()''HTML_QuickForm_Error'true);
  1467.                 }
  1468.             }
  1469.         }
  1470.  
  1471.         return (0 == count($this->_errors));
  1472.     // end func validate
  1473.  
  1474.     // }}}
  1475.     // {{{ freeze()
  1476.  
  1477.     /**
  1478.      * Displays elements without HTML input tags
  1479.      *
  1480.      * @param    mixed   $elementList       array or string of element(s) to be frozen
  1481.      * @since     1.0
  1482.      * @access   public
  1483.      * @throws   HTML_QuickForm_Error
  1484.      */
  1485.     function freeze($elementList=null)
  1486.     {
  1487.         if (!isset($elementList)) {
  1488.             $this->_freezeAll = true;
  1489.             $elementList = array();
  1490.         else {
  1491.             if (!is_array($elementList)) {
  1492.                 $elementList preg_split('/[ ]*,[ ]*/'$elementList);
  1493.             }
  1494.             $elementList array_flip($elementList);
  1495.         }
  1496.  
  1497.         foreach (array_keys($this->_elementsas $key{
  1498.             $name $this->_elements[$key]->getName();
  1499.             if ($this->_freezeAll || isset($elementList[$name])) {
  1500.                 $this->_elements[$key]->freeze();
  1501.                 unset($elementList[$name]);
  1502.             }
  1503.         }
  1504.  
  1505.         if (!empty($elementList)) {
  1506.             return PEAR::raiseError(nullQUICKFORM_NONEXIST_ELEMENTnullE_USER_WARNING"Nonexistant element(s): '" implode("', '"array_keys($elementList)) "' in HTML_QuickForm::freeze()"'HTML_QuickForm_Error'true);
  1507.         }
  1508.         return true;
  1509.     // end func freeze
  1510.         
  1511.     // }}}
  1512.     // {{{ isFrozen()
  1513.  
  1514.     /**
  1515.      * Returns whether or not the whole form is frozen
  1516.      *
  1517.      * @since     3.0
  1518.      * @access    public
  1519.      * @return    boolean 
  1520.      */
  1521.     function isFrozen()
  1522.     {
  1523.          return $this->_freezeAll;
  1524.     // end func isFrozen
  1525.  
  1526.     // }}}
  1527.     // {{{ process()
  1528.  
  1529.     /**
  1530.      * Performs the form data processing
  1531.      *
  1532.      * @param    mixed     $callback        Callback, either function name or array(&$object, 'method')
  1533.      * @param    bool      $mergeFiles      Whether uploaded files should be processed too
  1534.      * @since    1.0
  1535.      * @access   public
  1536.      * @throws   HTML_QuickForm_Error
  1537.      */
  1538.     function process($callback$mergeFiles = true)
  1539.     {
  1540.         if (!is_callable($callback)) {
  1541.             return PEAR::raiseError(nullQUICKFORM_INVALID_PROCESSnullE_USER_WARNING"Callback function does not exist in QuickForm::process()"'HTML_QuickForm_Error'true);
  1542.         }
  1543.         $values ($mergeFiles === trueHTML_QuickForm::arrayMerge($this->_submitValues$this->_submitFiles$this->_submitValues;
  1544.         return call_user_func($callback$values);
  1545.     // end func process
  1546.  
  1547.     // }}}
  1548.     // {{{ accept()
  1549.  
  1550.    /**
  1551.     * Accepts a renderer
  1552.     *
  1553.     * @param object     An HTML_QuickForm_Renderer object
  1554.     * @since 3.0
  1555.     * @access public
  1556.     * @return void 
  1557.     */
  1558.     function accept(&$renderer)
  1559.     {
  1560.         $renderer->startForm($this);
  1561.         foreach (array_keys($this->_elementsas $key{
  1562.             $element =$this->_elements[$key];
  1563.             $elementName $element->getName();
  1564.             $required    ($this->isElementRequired($elementName&& !$element->isFrozen());
  1565.             $error       $this->getElementError($elementName);
  1566.             $element->accept($renderer$required$error);
  1567.         }
  1568.         $renderer->finishForm($this);
  1569.     // end func accept
  1570.  
  1571.     // }}}
  1572.     // {{{ defaultRenderer()
  1573.  
  1574.    /**
  1575.     * Returns a reference to default renderer object
  1576.     *
  1577.     * @access public
  1578.     * @since 3.0
  1579.     * @return object default renderer object
  1580.     */
  1581.     function &defaultRenderer()
  1582.     {
  1583.         if (!isset($GLOBALS['_HTML_QuickForm_default_renderer'])) {
  1584.             include_once('HTML/QuickForm/Renderer/Default.php');
  1585.             $GLOBALS['_HTML_QuickForm_default_renderer'=new HTML_QuickForm_Renderer_Default();
  1586.         }
  1587.         return $GLOBALS['_HTML_QuickForm_default_renderer'];
  1588.     // end func defaultRenderer
  1589.  
  1590.     // }}}
  1591.     // {{{ toHtml ()
  1592.  
  1593.     /**
  1594.      * Returns an HTML version of the form
  1595.      *
  1596.      * @param string $in_data (optional) Any extra data to insert right
  1597.      *                before form is rendered.  Useful when using templates.
  1598.      *
  1599.      * @return   string     Html version of the form
  1600.      * @since     1.0
  1601.      * @access   public
  1602.      */
  1603.     function toHtml ($in_data = null)
  1604.     {
  1605.         if (!is_null($in_data)) {
  1606.             $this->addElement('html'$in_data);
  1607.         }
  1608.         $renderer =$this->defaultRenderer();
  1609.         $this->accept($renderer);
  1610.         return $renderer->toHtml();
  1611.     // end func toHtml
  1612.  
  1613.     // }}}
  1614.     // {{{ getValidationScript()
  1615.  
  1616.     /**
  1617.      * Returns the client side validation script
  1618.      *
  1619.      * @since     2.0
  1620.      * @access    public
  1621.      * @return    string    Javascript to perform validation, empty string if no 'client' rules were added
  1622.      */
  1623.     function getValidationScript()
  1624.     {
  1625.         if (empty($this->_rules|| empty($this->_attributes['onsubmit'])) {
  1626.             return '';
  1627.         }
  1628.  
  1629.         include_once('HTML/QuickForm/RuleRegistry.php');
  1630.         $registry =HTML_QuickForm_RuleRegistry::singleton();
  1631.         $test = array();
  1632.         $js_escape = array(
  1633.             "\r"    => '\r',
  1634.             "\n"    => '\n',
  1635.             "\t"    => '\t',
  1636.             "'"     => "\\'",
  1637.             '"'     => '\"',
  1638.             '\\'    => '\\\\'
  1639.         );
  1640.  
  1641.         foreach ($this->_rules as $elementName => $rules{
  1642.             foreach ($rules as $rule{
  1643.                 if ('client' == $rule['validation']{
  1644.                     $dependent  = isset($rule['dependent']&& is_array($rule['dependent']);
  1645.                     $rule['message'strtr($rule['message']$js_escape);
  1646.  
  1647.                     if (isset($rule['group'])) {
  1648.                         $group    =$this->getElement($rule['group']);
  1649.                         // No JavaScript validation for frozen elements
  1650.                         if ($group->isFrozen()) {
  1651.                             continue 2;
  1652.                         }
  1653.                         $elements =$group->getElements();
  1654.                         foreach (array_keys($elementsas $key{
  1655.                             if ($elementName == $group->getElementName($key)) {
  1656.                                 $element =$elements[$key];
  1657.                                 break;
  1658.                             }
  1659.                         }
  1660.                     elseif ($dependent{
  1661.                         $element   =  array();
  1662.                         $element[=$this->getElement($elementName);
  1663.                         foreach ($rule['dependent'as $idx => $elName{
  1664.                             $element[=$this->getElement($elName);
  1665.                         }
  1666.                     else {
  1667.                         $element =$this->getElement($elementName);
  1668.                     }
  1669.                     // No JavaScript validation for frozen elements
  1670.                     if (is_object($element&& $element->isFrozen()) {
  1671.                         continue 2;
  1672.                     elseif (is_array($element)) {
  1673.                         foreach (array_keys($elementas $key{
  1674.                             if ($element[$key]->isFrozen()) {
  1675.                                 continue 3;
  1676.                             }
  1677.                         }
  1678.                     }
  1679.  
  1680.                     $test[$registry->getValidationScript($element$elementName$rule);
  1681.                     unset($element);
  1682.                 }
  1683.             }
  1684.         }
  1685.         if (count($test> 0{
  1686.             return
  1687.                 "\n<script type=\"text/javascript\">\n" .
  1688.                 "//<![CDATA[\n" 
  1689.                 "function validate_" $this->_attributes['id'"(frm) {\n" .
  1690.                 "  var value = '';\n" .
  1691.                 "  var errFlag = new Array();\n" .
  1692.                 "  _qfMsg = '';\n\n" .
  1693.                 join("\n"$test.
  1694.                 "\n  if (_qfMsg != '') {\n" .
  1695.                 "    _qfMsg = '" strtr($this->_jsPrefix$js_escape"' + _qfMsg;\n" .
  1696.                 "    _qfMsg = _qfMsg + '\\n" strtr($this->_jsPostfix$js_escape"';\n" .
  1697.                 "    alert(_qfMsg);\n" .
  1698.                 "    return false;\n" .
  1699.                 "  }\n" .
  1700.                 "  return true;\n" .
  1701.                 "}\n" .
  1702.                 "//]]>\n" .
  1703.                 "</script>";
  1704.         }
  1705.         return '';
  1706.     // end func getValidationScript
  1707.  
  1708.     // }}}
  1709.     // {{{ getSubmitValues()
  1710.  
  1711.     /**
  1712.      * Returns the values submitted by the form
  1713.      *
  1714.      * @since     2.0
  1715.      * @access    public
  1716.      * @param     bool      Whether uploaded files should be returned too
  1717.      * @return    array 
  1718.      */
  1719.     function getSubmitValues($mergeFiles = false)
  1720.     {
  1721.         return $mergeFilesHTML_QuickForm::arrayMerge($this->_submitValues$this->_submitFiles)$this->_submitValues;
  1722.     // end func getSubmitValues
  1723.  
  1724.     // }}}
  1725.     // {{{ toArray()
  1726.  
  1727.     /**
  1728.      * Returns the form's contents in an array.
  1729.      *
  1730.      * The description of the array structure is in HTML_QuickForm_Renderer_Array docs
  1731.      * 
  1732.      * @since     2.0
  1733.      * @access    public
  1734.      * @param     bool      Whether to collect hidden elements (passed to the Renderer's constructor)
  1735.      * @return    array of form contents
  1736.      */
  1737.     function toArray($collectHidden = false)
  1738.     {
  1739.         include_once 'HTML/QuickForm/Renderer/Array.php';
  1740.         $renderer =new HTML_QuickForm_Renderer_Array($collectHidden);
  1741.         $this->accept($renderer);
  1742.         return $renderer->toArray();
  1743.      // end func toArray
  1744.  
  1745.     // }}}
  1746.     // {{{ exportValue()
  1747.  
  1748.     /**
  1749.      * Returns a 'safe' element's value
  1750.      * 
  1751.      * This method first tries to find a cleaned-up submitted value,
  1752.      * it will return a value set by setValue()/setDefaults()/setConstants()
  1753.      * if submitted value does not exist for the given element.
  1754.      *
  1755.      * @param  string   Name of an element
  1756.      * @access public
  1757.      * @return mixed 
  1758.      */
  1759.     function exportValue($element)
  1760.     {
  1761.         if (!isset($this->_elementIndex[$element])) {
  1762.             return PEAR::raiseError(nullQUICKFORM_NONEXIST_ELEMENTnullE_USER_WARNING"Element '$element' does not exist in HTML_QuickForm::getElementValue()"'HTML_QuickForm_Error'true);
  1763.         }
  1764.         $value $this->_elements[$this->_elementIndex[$element]]->exportValue($this->_submitValuesfalse);
  1765.         if (isset($this->_duplicateIndex[$element])) {
  1766.             foreach ($this->_duplicateIndex[$elementas $index{
  1767.                 if (null !== ($v $this->_elements[$index]->exportValue($this->_submitValuesfalse))) {
  1768.                     if (is_array($value)) {
  1769.                         $value[$v;
  1770.                     else {
  1771.                         $value (null === $value)$v: array($value$v);
  1772.                     }
  1773.                 }
  1774.             }
  1775.         }
  1776.         return $value;
  1777.     }
  1778.  
  1779.     // }}}
  1780.     // {{{ exportValues()
  1781.  
  1782.     /**
  1783.      * Returns 'safe' elements' values
  1784.      *
  1785.      * Unlike getSubmitValues(), this will return only the values
  1786.      * corresponding to the elements present in the form.
  1787.      * 
  1788.      * @param   mixed   Array/string of element names, whose values we want. If not set then return all elements.
  1789.      * @access  public
  1790.      * @return  array   An assoc array of elements' values
  1791.      * @throws  HTML_QuickForm_Error
  1792.      */
  1793.     function exportValues($elementList = null)
  1794.     {
  1795.         $values = array();
  1796.         if (null === $elementList{
  1797.             // iterate over all elements, calling their exportValue() methods
  1798.             foreach (array_keys($this->_elementsas $key{
  1799.                 $value $this->_elements[$key]->exportValue($this->_submitValuestrue);
  1800.                 if (is_array($value)) {
  1801.                     // This shit throws a bogus warning in PHP 4.3.x
  1802.                     $values HTML_QuickForm::arrayMerge($values$value);
  1803.                 }
  1804.             }
  1805.         else {
  1806.             if (!is_array($elementList)) {
  1807.                 $elementList array_map('trim'explode(','$elementList));
  1808.             }
  1809.             foreach ($elementList as $elementName{
  1810.                 $value $this->exportValue($elementName);
  1811.                 if (PEAR::isError($value)) {
  1812.                     return $value;
  1813.                 }
  1814.                 $values[$elementName$value;
  1815.             }
  1816.         }
  1817.         return $values;
  1818.     }
  1819.  
  1820.     // }}}
  1821.     // {{{ isError()
  1822.  
  1823.     /**
  1824.      * Tell whether a result from a QuickForm method is an error (an instance of HTML_QuickForm_Error)
  1825.      *
  1826.      * @access public
  1827.      * @param mixed     result code
  1828.      * @return bool     whether $value is an error
  1829.      */
  1830.     function isError($value)
  1831.     {
  1832.         return (is_object($value&& is_a($value'html_quickform_error'));
  1833.     // end func isError
  1834.  
  1835.     // }}}
  1836.     // {{{ errorMessage()
  1837.  
  1838.     /**
  1839.      * Return a textual error message for an QuickForm error code
  1840.      *
  1841.      * @access  public
  1842.      * @param   int     error code
  1843.      * @return  string  error message
  1844.      */
  1845.     function errorMessage($value)
  1846.     {
  1847.         // make the variable static so that it only has to do the defining on the first call
  1848.         static $errorMessages;
  1849.  
  1850.         // define the varies error messages
  1851.         if (!isset($errorMessages)) {
  1852.             $errorMessages = array(
  1853.                 QUICKFORM_OK                    => 'no error',
  1854.                 QUICKFORM_ERROR                 => 'unknown error',
  1855.                 QUICKFORM_INVALID_RULE          => 'the rule does not exist as a registered rule',
  1856.                 QUICKFORM_NONEXIST_ELEMENT      => 'nonexistent html element',
  1857.                 QUICKFORM_INVALID_FILTER        => 'invalid filter',
  1858.                 QUICKFORM_UNREGISTERED_ELEMENT  => 'unregistered element',
  1859.                 QUICKFORM_INVALID_ELEMENT_NAME  => 'element already exists',
  1860.                 QUICKFORM_INVALID_PROCESS       => 'process callback does not exist',
  1861.                 QUICKFORM_DEPRECATED            => 'method is deprecated',
  1862.                 QUICKFORM_INVALID_DATASOURCE    => 'datasource is not an object'
  1863.             );
  1864.         }
  1865.  
  1866.         // If this is an error object, then grab the corresponding error code
  1867.         if (HTML_QuickForm::isError($value)) {
  1868.             $value $value->getCode();
  1869.         }
  1870.  
  1871.         // return the textual error message corresponding to the code
  1872.         return isset($errorMessages[$value]$errorMessages[$value$errorMessages[QUICKFORM_ERROR];
  1873.     // end func errorMessage
  1874.  
  1875.     // }}}
  1876. // end class HTML_QuickForm
  1877.  
  1878. class HTML_QuickForm_Error extends PEAR_Error {
  1879.  
  1880.     // {{{ properties
  1881.  
  1882.     /**
  1883.     * Prefix for all error messages
  1884.     * @var string 
  1885.     */
  1886.     var $error_message_prefix = 'QuickForm Error: ';
  1887.  
  1888.     // }}}
  1889.     // {{{ constructor
  1890.  
  1891.     /**
  1892.     * Creates a quickform error object, extending the PEAR_Error class
  1893.     *
  1894.     * @param int   $code the error code
  1895.     * @param int   $mode the reaction to the error, either return, die or trigger/callback
  1896.     * @param int   $level intensity of the error (PHP error code)
  1897.     * @param mixed $debuginfo any information that can inform user as to nature of the error
  1898.     */
  1899.     function HTML_QuickForm_Error($code = QUICKFORM_ERROR$mode = PEAR_ERROR_RETURN,
  1900.                          $level = E_USER_NOTICE$debuginfo = null)
  1901.     {
  1902.         if (is_int($code)) {
  1903.             $this->PEAR_Error(HTML_QuickForm::errorMessage($code)$code$mode$level$debuginfo);
  1904.         else {
  1905.             $this->PEAR_Error("Invalid error code: $code"QUICKFORM_ERROR$mode$level$debuginfo);
  1906.         }
  1907.     }
  1908.  
  1909.     // }}}
  1910. // end class HTML_QuickForm_Error
  1911. ?>

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