home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / DB / Table / QuickForm.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  35.4 KB  |  1,174 lines

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4.  
  5. /**
  6.  * DB_Table_QuickForm creates HTML_QuickForm objects from DB_Table properties.
  7.  * 
  8.  * PHP versions 4 and 5
  9.  *
  10.  * LICENSE:
  11.  * 
  12.  * Copyright (c) 1997-2007, Paul M. Jones <pmjones@php.net>
  13.  *                          David C. Morse <morse@php.net>
  14.  *                          Mark Wiesemann <wiesemann@php.net>
  15.  * All rights reserved.
  16.  *
  17.  * Redistribution and use in source and binary forms, with or without
  18.  * modification, are permitted provided that the following conditions
  19.  * are met:
  20.  *
  21.  *    * Redistributions of source code must retain the above copyright
  22.  *      notice, this list of conditions and the following disclaimer.
  23.  *    * Redistributions in binary form must reproduce the above copyright
  24.  *      notice, this list of conditions and the following disclaimer in the 
  25.  *      documentation and/or other materials provided with the distribution.
  26.  *    * The names of the authors may not be used to endorse or promote products 
  27.  *      derived from this software without specific prior written permission.
  28.  *
  29.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  30.  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  31.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  32.  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  33.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  34.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  35.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  36.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  37.  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  38.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  39.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40.  *
  41.  * @category Database
  42.  * @package  DB_Table
  43.  * @author   Paul M. Jones <pmjones@php.net>
  44.  * @author   David C. Morse <morse@php.net>
  45.  * @author   Mark Wiesemann <wiesemann@php.net>
  46.  * @license  http://opensource.org/licenses/bsd-license.php New BSD License
  47.  * @version  CVS: $Id: QuickForm.php,v 1.45 2008/03/28 20:00:38 wiesemann Exp $
  48.  * @link     http://pear.php.net/package/DB_Table
  49.  */
  50.  
  51. /**
  52. * Needed to build forms.
  53. */
  54. require_once 'HTML/QuickForm.php';
  55.  
  56. /**
  57. * US-English messages for some QuickForm rules.  Moritz Heidkamp
  58. * suggested this approach for easier i18n.
  59. */
  60. if (! isset($GLOBALS['_DB_TABLE']['qf_rules'])) {
  61.     $GLOBALS['_DB_TABLE']['qf_rules'] = array(
  62.       'required'  => 'The item %s is required.',
  63.       'numeric'   => 'The item %s must be numbers only.',
  64.       'maxlength' => 'The item %s can have no more than %d characters.'
  65.     );
  66. }
  67.  
  68. /**
  69. * If you want to use an extended HTML_QuickForm object, you can specify the
  70. * class name in $_DB_TABLE['qf_class_name'].
  71. * ATTENTION: You have to include the class file yourself, DB_Table does
  72. * not take care of this!
  73. */
  74. if (!isset($GLOBALS['_DB_TABLE']['qf_class_name'])) {
  75.     $GLOBALS['_DB_TABLE']['qf_class_name'] = 'HTML_QuickForm';
  76. }
  77.  
  78. /**
  79.  * DB_Table_QuickForm creates HTML_QuickForm objects from DB_Table properties.
  80.  * 
  81.  * DB_Table_QuickForm provides HTML form creation facilities based on
  82.  * DB_Table column definitions transformed into HTML_QuickForm elements.
  83.  * 
  84.  * @category Database
  85.  * @package  DB_Table
  86.  * @author   Paul M. Jones <pmjones@php.net>
  87.  * @author   David C. Morse <morse@php.net>
  88.  * @author   Mark Wiesemann <wiesemann@php.net>
  89.  * @version  Release: 1.5.5
  90.  * @link     http://pear.php.net/package/DB_Table
  91.  */
  92.  
  93. class DB_Table_QuickForm {
  94.     
  95.     
  96.     /**
  97.     * 
  98.     * Build a form based on DB_Table column definitions.
  99.     * 
  100.     * @static
  101.     * 
  102.     * @access public
  103.     * 
  104.     * @param array $cols A sequential array of DB_Table column definitions
  105.     * from which to create form elements.
  106.     * 
  107.     * @param string $arrayName By default, the form will use the names
  108.     * of the columns as the names of the form elements.  If you pass
  109.     * $arrayName, the column names will become keys in an array named
  110.     * for this parameter.
  111.     * 
  112.     * @param array $args An associative array of optional arguments to
  113.     * pass to the QuickForm object.  The keys are...
  114.     *
  115.     * 'formName' : String, name of the form; defaults to the name of the
  116.     * table.
  117.     * 
  118.     * 'method' : String, form method; defaults to 'post'.
  119.     * 
  120.     * 'action' : String, form action; defaults to
  121.     * $_SERVER['REQUEST_URI'].
  122.     * 
  123.     * 'target' : String, form target target; defaults to '_self'
  124.     * 
  125.     * 'attributes' : Associative array, extra attributes for <form>
  126.     * tag; the key is the attribute name and the value is attribute
  127.     * value.
  128.     * 
  129.     * 'trackSubmit' : Boolean, whether to track if the form was
  130.     * submitted by adding a special hidden field
  131.     * 
  132.     * @param string $clientValidate By default, validation will match
  133.     * the 'qf_client' value from the column definition.  However,
  134.     * if you set $clientValidate to true or false, this will
  135.     * override the value from the column definition.
  136.     *
  137.     * @param array $formFilters An array with filter function names or
  138.     * callbacks that will be applied to all form elements.
  139.     * 
  140.     * @return object HTML_QuickForm
  141.     * 
  142.     * @see HTML_QuickForm
  143.     *
  144.     * @see DB_Table_QuickForm::createForm()
  145.     * 
  146.     */
  147.     
  148.     function &getForm($cols, $arrayName = null, $args = array(),
  149.         $clientValidate = null, $formFilters = null)
  150.     {
  151.         $form =& DB_Table_QuickForm::createForm($args);
  152.         DB_Table_QuickForm::addElements($form, $cols, $arrayName);
  153.         DB_Table_QuickForm::addRules($form, $cols, $arrayName, $clientValidate);
  154.         DB_Table_QuickForm::addFilters($form, $cols, $arrayName, $formFilters);
  155.         
  156.         return $form;
  157.     }
  158.     
  159.     
  160.     /**
  161.     * 
  162.     * Creates an empty form object.
  163.     *
  164.     * In case you want more control over your form, you can call this function
  165.     * to create it, then add whatever elements you want.
  166.     *
  167.     * @static
  168.     * 
  169.     * @access public
  170.     * 
  171.     * @author Ian Eure <ieure@php.net>
  172.     * 
  173.     * @param array $args An associative array of optional arguments to
  174.     * pass to the QuickForm object.  The keys are...
  175.     *
  176.     * 'formName' : String, name of the form; defaults to the name of the
  177.     * table.
  178.     * 
  179.     * 'method' : String, form method; defaults to 'post'.
  180.     * 
  181.     * 'action' : String, form action; defaults to
  182.     * $_SERVER['REQUEST_URI'].
  183.     * 
  184.     * 'target' : String, form target target; defaults to '_self'
  185.     * 
  186.     * 'attributes' : Associative array, extra attributes for <form>
  187.     * tag; the key is the attribute name and the value is attribute
  188.     * value.
  189.     * 
  190.     * 'trackSubmit' : Boolean, whether to track if the form was
  191.     * submitted by adding a special hidden field
  192.     * 
  193.     * @return object HTML_QuickForm
  194.     * 
  195.     */
  196.     
  197.     function &createForm($args = array())
  198.     {
  199.         if (isset($args['formName'])) {
  200.             $formName = $args['formName'];
  201.         } elseif (isset($this)) {
  202.             $formName = $this->table;
  203.         } else {
  204.             $formName = '_db_table_form_';
  205.         }
  206.             
  207.         $method = isset($args['method'])
  208.             ? $args['method'] : 'post';
  209.         
  210.         $action = isset($args['action'])
  211.             ? $args['action'] : $_SERVER['REQUEST_URI'];
  212.         
  213.         $target = isset($args['target'])
  214.             ? $args['target'] : '_self';
  215.         
  216.         $attributes = isset($args['attributes'])
  217.             ? $args['attributes'] : null;
  218.         
  219.         $trackSubmit = isset($args['trackSubmit'])
  220.             ? $args['trackSubmit'] : false;
  221.         
  222.         $form =& new $GLOBALS['_DB_TABLE']['qf_class_name']($formName, $method,
  223.             $action, $target, $attributes, $trackSubmit);
  224.  
  225.         return $form;
  226.     }
  227.     
  228.     
  229.     /**
  230.     * 
  231.     * Adds DB_Table columns to a pre-existing HTML_QuickForm object.
  232.     * 
  233.     * @author Ian Eure <ieure@php.net>
  234.     * 
  235.     * @static
  236.     * 
  237.     * @access public
  238.     * 
  239.     * @param object &$form An HTML_QuickForm object.
  240.     * 
  241.     * @param array $cols A sequential array of DB_Table column definitions
  242.     * from which to create form elements.
  243.     * 
  244.     * @param string $arrayName By default, the form will use the names
  245.     * of the columns as the names of the form elements.  If you pass
  246.     * $arrayName, the column names will become keys in an array named
  247.     * for this parameter.
  248.     * 
  249.     * @return void
  250.     * 
  251.     */
  252.     
  253.     function addElements(&$form, $cols, $arrayName = null)
  254.     {
  255.         $elements =& DB_Table_QuickForm::getElements($cols, $arrayName);
  256.         $cols_keys = array_keys($cols);
  257.         foreach (array_keys($elements) as $k) {
  258.         
  259.             $element =& $elements[$k];
  260.             
  261.             // are we adding a group?
  262.             if (is_array($element)) {
  263.                 
  264.                 // get the label for the group.  have to do it this way
  265.                 // because the group of elements does not itself have a
  266.                 // label, there are only the labels for the individual
  267.                 // elements.
  268.                 $tmp = $cols[$cols_keys[$k]];
  269.                 if (! isset($tmp['qf_label'])) {
  270.                     $label = $cols_keys[$k];
  271.                     if ($arrayName) {
  272.                         $label = $arrayName . "[$label]";
  273.                     }
  274.                 } else {
  275.                     $label = $tmp['qf_label'];
  276.                 }
  277.                    
  278.                 // set the element name
  279.                 if ($arrayName) {
  280.                     $name = $arrayName . '[' . $cols_keys[$k] . ']';
  281.                 } else {
  282.                     $name = $cols_keys[$k];
  283.                 }
  284.  
  285.                 // fix the column definition temporarily to get the separator
  286.                 // for the group
  287.                 $col = $cols[$cols_keys[$k]];
  288.                 DB_Table_QuickForm::fixColDef($col, $name);
  289.  
  290.                 // done
  291.                 $group =& $form->addGroup($element, $name, $label,
  292.                                           $col['qf_groupsep']);
  293.  
  294.                 // set default value (if given) for radio elements
  295.                 // (reason: QF "resets" the checked state, when adding a group)
  296.                 if ($tmp['qf_type'] == 'radio' && isset($tmp['qf_setvalue'])) {
  297.                     $form->setDefaults(array($name => $tmp['qf_setvalue']));
  298.                 }
  299.  
  300.             } elseif (is_object($element)) {
  301.                 $form->addElement($element);
  302.             }
  303.         }
  304.     }
  305.  
  306.     /**
  307.     * 
  308.     * Gets controls for a list of columns
  309.     * 
  310.     * @author Ian Eure <ieure@php.net>
  311.     * 
  312.     * @static
  313.     * 
  314.     * @access public
  315.     * 
  316.     * @param object &$form An HTML_QuickForm object.
  317.     * 
  318.     * @param array $cols A sequential array of DB_Table column definitions
  319.     * from which to create form elements.
  320.     * 
  321.     * @param string $arrayName By default, the form will use the names
  322.     * of the columns as the names of the form elements.  If you pass
  323.     * $arrayName, the column names will become keys in an array named
  324.     * for this parameter.
  325.     * 
  326.     * @return array Form elements
  327.     * 
  328.     */
  329.     
  330.     function &getElements($cols, $arrayName = null)
  331.     {
  332.         $elements = array();
  333.         
  334.         foreach ($cols as $name => $col) {
  335.             
  336.             if ($arrayName) {
  337.                 $elemname = $arrayName . "[$name]";
  338.             } else {
  339.                 $elemname = $name;
  340.             }
  341.             
  342.             DB_Table_QuickForm::fixColDef($col, $elemname);
  343.  
  344.             $elements[] =& DB_Table_QuickForm::getElement($col, $elemname);
  345.         }
  346.         
  347.         return $elements;
  348.     }
  349.     
  350.     
  351.     /**
  352.     * 
  353.     * Build a single QuickForm element based on a DB_Table column.
  354.     * 
  355.     * @static
  356.     * 
  357.     * @access public
  358.     * 
  359.     * @param array $col A DB_Table column definition.
  360.     * 
  361.     * @param string $elemname The name to use for the generated QuickForm
  362.     * element.
  363.     * 
  364.     * @return object HTML_QuickForm_Element
  365.     * 
  366.     */
  367.     
  368.     function &getElement($col, $elemname)
  369.     {
  370.         if (isset($col['qf_setvalue'])) {
  371.             $setval = $col['qf_setvalue'];
  372.         }
  373.         
  374.         switch ($col['qf_type']) {
  375.         
  376.         case 'advcheckbox':
  377.         case 'checkbox':
  378.             
  379.             $element =& HTML_QuickForm::createElement(
  380.                 'advcheckbox',
  381.                 $elemname,
  382.                 $col['qf_label'],
  383.                 isset($col['qf_label_append']) ?
  384.                     $col['qf_label_append'] : null,
  385.                 $col['qf_attrs'],
  386.                 $col['qf_vals']
  387.             );
  388.             
  389.             // WARNING: advcheckbox elements in HTML_QuickForm v3.2.2
  390.             // and earlier do not honor setChecked(); they will always
  391.             // be un-checked, unless a POST value sets them.  Upgrade
  392.             // to QF 3.2.3 or later.
  393.             if (isset($setval) && $setval == true) {
  394.                 $element->setChecked(true);
  395.             } else {
  396.                 $element->setChecked(false);
  397.             }
  398.             
  399.             break;
  400.             
  401.         case 'autocomplete':
  402.         
  403.             $element =& HTML_QuickForm::createElement(
  404.                 $col['qf_type'],
  405.                 $elemname,
  406.                 $col['qf_label'],
  407.                 $col['qf_vals'],
  408.                 $col['qf_attrs']
  409.             );
  410.             
  411.             if (isset($setval)) {
  412.                 $element->setValue($setval);
  413.             }
  414.             
  415.             break;
  416.             
  417.         case 'date':
  418.         
  419.             if (! isset($col['qf_opts']['format'])) {
  420.                 $col['qf_opts']['format'] = 'Y-m-d';
  421.             }
  422.             
  423.             $element =& HTML_QuickForm::createElement(
  424.                 'date',
  425.                 $elemname,
  426.                 $col['qf_label'],
  427.                 $col['qf_opts'],
  428.                 $col['qf_attrs']
  429.             );
  430.             
  431.             if (isset($setval)) {
  432.                 $element->setValue($setval);
  433.             }
  434.             
  435.             break;
  436.             
  437.         case 'time':
  438.         
  439.             if (! isset($col['qf_opts']['format'])) {
  440.                 $col['qf_opts']['format'] = 'H:i:s';
  441.             }
  442.             
  443.             $element =& HTML_QuickForm::createElement(
  444.                 'date',
  445.                 $elemname,
  446.                 $col['qf_label'],
  447.                 $col['qf_opts'],
  448.                 $col['qf_attrs']
  449.             );
  450.             
  451.             if (isset($setval)) {
  452.                 $element->setValue($setval);
  453.             }
  454.             
  455.             break;
  456.  
  457.         case 'timestamp':
  458.         
  459.             if (! isset($col['qf_opts']['format'])) {
  460.                 $col['qf_opts']['format'] = 'Y-m-d H:i:s';
  461.             }
  462.             
  463.             $element =& HTML_QuickForm::createElement(
  464.                 'date',
  465.                 $elemname,
  466.                 $col['qf_label'],
  467.                 $col['qf_opts'],
  468.                 $col['qf_attrs']
  469.             );
  470.             
  471.             if (isset($setval)) {
  472.                 $element->setValue($setval);
  473.             }
  474.             
  475.             break;
  476.         
  477.         case 'hidden':
  478.         
  479.             $element =& HTML_QuickForm::createElement(
  480.                 $col['qf_type'],
  481.                 $elemname,
  482.                 null,
  483.                 $col['qf_attrs']
  484.             );
  485.             
  486.             if (isset($setval)) {
  487.                 $element->setValue($setval);
  488.             }
  489.             
  490.             break;
  491.             
  492.             
  493.         case 'radio':
  494.         
  495.             $element = array();
  496.             
  497.             foreach ((array) $col['qf_vals'] as $btnvalue => $btnlabel) {
  498.                 
  499.                 $element[] =& HTML_QuickForm::createElement(
  500.                     $col['qf_type'],
  501.                     null, // elemname not added because this is a group
  502.                     null,
  503.                     $btnlabel,
  504.                     $btnvalue,
  505.                     $col['qf_attrs']
  506.                 );
  507.                 
  508.             }
  509.             
  510.             break;
  511.             
  512.         case 'select':
  513.             
  514.             $element =& HTML_QuickForm::createElement(
  515.                 $col['qf_type'],
  516.                 $elemname,
  517.                 $col['qf_label'],
  518.                 $col['qf_vals'],
  519.                 $col['qf_attrs']
  520.             );
  521.             
  522.             if (isset($setval)) {
  523.                 $element->setSelected($setval);
  524.             }
  525.             
  526.             break;
  527.             
  528.         case 'password':
  529.         case 'text':
  530.         case 'textarea':
  531.         
  532.             if (! isset($col['qf_attrs']['maxlength']) &&
  533.                 isset($col['size'])) {
  534.                 $col['qf_attrs']['maxlength'] = $col['size'];
  535.             }
  536.             
  537.             $element =& HTML_QuickForm::createElement(
  538.                 $col['qf_type'],
  539.                 $elemname,
  540.                 $col['qf_label'],
  541.                 $col['qf_attrs']
  542.             );
  543.             
  544.             if (isset($setval)) {
  545.                 $element->setValue($setval);
  546.             }
  547.             
  548.             break;
  549.         
  550.         case 'static':
  551.             $element =& HTML_QuickForm::createElement(
  552.                 $col['qf_type'],
  553.                 $elemname,
  554.                 $col['qf_label'],
  555.                 (isset($setval) ? $setval : '')
  556.             );
  557.             break;
  558.  
  559.         case 'hierselect':
  560.  
  561.             $element =& HTML_QuickForm::createElement(
  562.                 $col['qf_type'],
  563.                 $elemname,
  564.                 $col['qf_label'],
  565.                 $col['qf_attrs'],
  566.                 $col['qf_groupsep']
  567.             );
  568.  
  569.             if (isset($setval)) {
  570.                 $element->setValue($setval);
  571.             }
  572.  
  573.             break;
  574.  
  575.         case 'jscalendar':
  576.  
  577.             $element =& HTML_QuickForm::createElement(
  578.                 $col['qf_type'],
  579.                 $elemname,
  580.                 $col['qf_label'],
  581.                 $col['qf_opts'],
  582.                 $col['qf_attrs']
  583.             );
  584.  
  585.             if (isset($setval)) {
  586.                 $element->setValue($setval);
  587.             }
  588.  
  589.             break;
  590.  
  591.         case 'header':
  592.  
  593.             $element =& HTML_QuickForm::createElement(
  594.                 $col['qf_type'],
  595.                 $elemname
  596.             );
  597.  
  598.             if (isset($setval)) {
  599.                 $element->setValue($setval);
  600.             }
  601.  
  602.             break;
  603.  
  604.         case 'static':
  605.  
  606.             $element =& HTML_QuickForm::createElement(
  607.                 $col['qf_type'],
  608.                 $elemname,
  609.                 $col['qf_label']
  610.             );
  611.  
  612.             if (isset($setval)) {
  613.                 $element->setValue($setval);
  614.             }
  615.  
  616.             break;
  617.  
  618.         case 'link':
  619.  
  620.             $element =& HTML_QuickForm::createElement(
  621.                 $col['qf_type'],
  622.                 $elemname,
  623.                 $col['qf_label'],
  624.                 $col['qf_href'], // link href
  625.                 $setval,  // link text
  626.                 $col['qf_attrs']
  627.             );
  628.  
  629.             break;
  630.  
  631.         case 'reset':
  632.         case 'submit':
  633.  
  634.             $element =& HTML_QuickForm::createElement(
  635.                 $col['qf_type'],
  636.                 $elemname,
  637.                 null,
  638.                 $col['qf_attrs']
  639.             );
  640.  
  641.             if (isset($setval)) {
  642.                 $element->setValue($setval);
  643.             }
  644.  
  645.             break;
  646.  
  647.         case 'callback':  // custom QF elements that need more than
  648.                           // the standard parameters
  649.                           // code from Arne Bippes <arne.bippes@brandao.de>
  650.  
  651.             if (is_callable(array($col['qf_callback'], 'createElement'))) {
  652.                 // Does an object with name from $col['qf_callback'] and
  653.                 // a method with name 'createElement' exist?
  654.                 $ret_value = call_user_func_array(
  655.                     array($col['qf_callback'], 'createElement'),
  656.                     array(&$element, &$col, &$elemname, &$setval));
  657.             }
  658.             elseif (is_callable($col['qf_callback'])) {
  659.                 // Does a method with name from $col['qf_callback'] exist?
  660.                 $ret_value = call_user_func_array(
  661.                     $col['qf_callback'],
  662.                     array(&$element, &$col, &$elemname, &$setval));
  663.             }
  664.             if ($ret_value) {
  665.                 break;
  666.             }
  667.             // fall into default block of switch statement:
  668.             // - if $col['qf_callback'] is ...
  669.             //   - not a valid object
  670.             //   - a valid object, but a method 'createElement' doesn't exist
  671.             //   - not a valid method name
  672.             // - if an error occured in 'createElement' or in the method
  673.             
  674.         default:
  675.             
  676.             /**
  677.             * @author Moritz Heidkamp <moritz.heidkamp@invision-team.de>
  678.             */
  679.             
  680.             // not a recognized type.  is it registered with QuickForm?
  681.             if (HTML_QuickForm::isTypeRegistered($col['qf_type'])) {
  682.                 
  683.                 // yes, create it with some minimalist parameters
  684.                 $element =& HTML_QuickForm::createElement(
  685.                     $col['qf_type'],
  686.                     $elemname,
  687.                     $col['qf_label'],
  688.                     $col['qf_attrs']
  689.                 );
  690.                 
  691.                 // set its default value, if there is one
  692.                 if (isset($setval)) {
  693.                     $element->setValue($setval);
  694.                 }
  695.                 
  696.             } else {
  697.                 // element type is not registered with QuickForm.
  698.                 $element = null;
  699.             }
  700.             
  701.             break;
  702.  
  703.         }
  704.         
  705.         // done
  706.         return $element;
  707.     }
  708.     
  709.     
  710.     /**
  711.     * 
  712.     * Build an array of form elements based from DB_Table columns.
  713.     * 
  714.     * @static
  715.     * 
  716.     * @access public
  717.     * 
  718.     * @param array $cols A sequential array of DB_Table column
  719.     * definitions from which to create form elements.
  720.     * 
  721.     * @param string $arrayName By default, the form will use the names
  722.     * of the columns as the names of the form elements.  If you pass
  723.     * $arrayName, the column names will become keys in an array named
  724.     * for this parameter.
  725.     * 
  726.     * @return array An array of HTML_QuickForm_Element objects.
  727.     * 
  728.     */
  729.     
  730.     function &getGroup($cols, $arrayName = null)
  731.     {
  732.         $group = array();
  733.         
  734.         foreach ($cols as $name => $col) {
  735.             
  736.             if ($arrayName) {
  737.                 $elemname = $arrayName . "[$name]";
  738.             } else {
  739.                 $elemname = $name;
  740.             }
  741.             
  742.             DB_Table_QuickForm::fixColDef($col, $elemname);
  743.             
  744.             $group[] =& DB_Table_QuickForm::getElement($col, $elemname);
  745.         }
  746.         
  747.         return $group;
  748.     }
  749.     
  750.     
  751.     /**
  752.     * 
  753.     * Adds static form elements like 'header', 'static', 'submit' or 'reset' to
  754.     * a pre-existing HTML_QuickForm object.
  755.     * 
  756.     * @static
  757.     * 
  758.     * @access public
  759.     * 
  760.     * @param object &$form An HTML_QuickForm object.
  761.     * 
  762.     * @param array $elements A sequential array of form element definitions.
  763.     * 
  764.     * @return void
  765.     * 
  766.     */
  767.     
  768.     function addStaticElements(&$form, $elements)
  769.     {
  770.         foreach ($elements as $name => $elemDef) {
  771.  
  772.             DB_Table_QuickForm::fixColDef($elemDef, $name);
  773.  
  774.             $element =& DB_Table_QuickForm::getElement($elemDef, $name);
  775.  
  776.             if (!is_object($element)) {
  777.                 continue;
  778.             }
  779.  
  780.             if (isset($elemDef['before']) && !empty($elemDef['before'])) {
  781.                 $form->insertElementBefore($element, $elemDef['before']);
  782.             } else {
  783.                 $form->addElement($element);
  784.             }
  785.         }
  786.     }
  787.     
  788.     
  789.     /**
  790.     *
  791.     * Adds DB_Table filters to a pre-existing HTML_QuickForm object.
  792.     *
  793.     * @static
  794.     *
  795.     * @access public
  796.     *
  797.     * @param object &$form An HTML_QuickForm object.
  798.     *
  799.     * @param array $cols A sequential array of DB_Table column definitions
  800.     * from which to create form elements.
  801.     *
  802.     * @param string $arrayName By default, the form will use the names
  803.     * of the columns as the names of the form elements.  If you pass
  804.     * $arrayName, the column names will become keys in an array named
  805.     * for this parameter.
  806.     *
  807.     * @param array $formFilters An array with filter function names or
  808.     * callbacks that will be applied to all form elements.
  809.     *
  810.     * @return void
  811.     *
  812.     */
  813.     function addFilters(&$form, $cols, $arrayName = null,
  814.         $formFilters = null)
  815.     {
  816.         foreach ($cols as $name => $col) {
  817.             if ($arrayName) {
  818.                 $elemname = $arrayName . "[$name]";
  819.             } else {
  820.                 $elemname = $name;
  821.             }
  822.  
  823.             DB_Table_QuickForm::fixColDef($col, $elemname);
  824.  
  825.             foreach (array_keys($col['qf_filters']) as $fk) {
  826.                 $form->applyFilter($elemname, $col['qf_filters'][$fk]);
  827.             }
  828.         }
  829.  
  830.         if (is_array($formFilters)) {
  831.             foreach (array_keys($formFilters) as $fk) {
  832.                 $form->applyFilter('__ALL__', $formFilters[$fk]);
  833.             }
  834.         }
  835.     }
  836.  
  837.  
  838.     /**
  839.     * 
  840.     * Adds element rules to a pre-existing HTML_QuickForm object.
  841.     * 
  842.     * @static
  843.     * 
  844.     * @access public
  845.     * 
  846.     * @param object &$form An HTML_QuickForm object.
  847.     * 
  848.     * @param array $cols A sequential array of DB_Table column definitions
  849.     * from which to create form elements.
  850.     * 
  851.     * @param string $arrayName By default, the form will use the names
  852.     * of the columns as the names of the form elements.  If you pass
  853.     * $arrayName, the column names will become keys in an array named
  854.     * for this parameter.
  855.     * 
  856.     * @param string $clientValidate By default, validation will match
  857.     * the 'qf_client' value from the column definition.  However,
  858.     * if you set $clientValidate to true or false, this will
  859.     * override the value from the column definition.
  860.     * 
  861.     * @return void
  862.     * 
  863.     */
  864.     
  865.     function addRules(&$form, $cols, $arrayName = null,
  866.         $clientValidate = null)
  867.     {
  868.         foreach ($cols as $name => $col) {
  869.             
  870.             if ($arrayName) {
  871.                 $elemname = $arrayName . "[$name]";
  872.             } else {
  873.                 $elemname = $name;
  874.             }
  875.             
  876.             // make sure all necessary elements are in place
  877.             DB_Table_QuickForm::fixColDef($col, $elemname);
  878.             
  879.             // if clientValidate is specified, override the column
  880.             // definition.  otherwise use the col def as it is.
  881.             if (! is_null($clientValidate)) {
  882.                 // override
  883.                 if ($clientValidate) {
  884.                     $validate = 'client';
  885.                 } else {
  886.                     $validate = 'server';
  887.                 }
  888.             } else {
  889.                 // use as-is
  890.                 if ($col['qf_client']) {
  891.                     $validate = 'client';
  892.                 } else {
  893.                     $validate = 'server';
  894.                 }
  895.             }
  896.             
  897.             // **always** override these rules to make them 
  898.             // server-side only.  suggested by Mark Wiesemann,
  899.             // debugged by Hero Wanders.
  900.             $onlyServer = array('filename', 'maxfilesize', 'mimetype',
  901.                 'uploadedfile');
  902.             
  903.             // loop through the rules and add them
  904.             foreach ($col['qf_rules'] as $type => $opts) {
  905.                 
  906.                 // some rules (e.g. rules for file elements) can only be
  907.                 // checked on the server; therefore, don't use client-side
  908.                 // validation for these rules
  909.                 $ruleValidate = $validate;
  910.                 if (in_array($type, $onlyServer)) {
  911.                     $ruleValidate = 'server';
  912.                 }
  913.                 
  914.                 switch ($type) {
  915.                     
  916.                 case 'alphanumeric':
  917.                 case 'email':
  918.                 case 'lettersonly':
  919.                 case 'nonzero':
  920.                 case 'nopunctuation':
  921.                 case 'numeric':
  922.                 case 'required':
  923.                 case 'uploadedfile':
  924.                     // $opts is the error message
  925.                     $message = $opts;
  926.                     $format = null;
  927.                     break;
  928.                 
  929.                 case 'filename':
  930.                 case 'maxfilesize':
  931.                 case 'maxlength':
  932.                 case 'mimetype':
  933.                 case 'minlength':
  934.                 case 'regex':
  935.                     // $opts[0] is the message
  936.                     // $opts[1] is the size, mimetype, or regex
  937.                     $message = $opts[0];
  938.                     $format = $opts[1];
  939.                     break;
  940.                 
  941.                 default:
  942.                     // by Alex Hoebart: this should allow any registered rule.
  943.                     if (!in_array($type, $form->getRegisteredRules())) {
  944.                         // rule is not registered ==> do not add a rule
  945.                         continue;
  946.                     }
  947.                     if (is_array($opts)) {
  948.                         // $opts[0] is the message
  949.                         // $opts[1] is the size or regex
  950.                         $message = $opts[0];
  951.                         $format = $opts[1];
  952.                     } else {
  953.                         // $opts is the error message
  954.                         $message = $opts;
  955.                         $format = null;
  956.                     }
  957.                     break;
  958.                 }
  959.                 
  960.                 switch ($col['qf_type']) {
  961.  
  962.                 case 'date':
  963.                 case 'time':
  964.                 case 'timestamp':
  965.                     // date "elements" are groups ==> use addGroupRule()
  966.                     $form->addGroupRule($elemname, $message, $type, $format,
  967.                         null, $ruleValidate);
  968.                     break;
  969.  
  970.                 default:  // use addRule() for all other elements
  971.                     $form->addRule($elemname, $message, $type, $format,
  972.                         $ruleValidate);
  973.                     break;
  974.  
  975.                 }
  976.  
  977.             }
  978.         }
  979.     }
  980.     
  981.     
  982.     /**
  983.     * 
  984.     * "Fixes" a DB_Table column definition for QuickForm.
  985.     * 
  986.     * Makes it so that all the 'qf_*' key constants are populated
  987.     * with appropriate default values; also checks the 'require'
  988.     * value (if not set, defaults to false).
  989.     * 
  990.     * @static
  991.     * 
  992.     * @access public
  993.     * 
  994.     * @param array &$col A DB_Table column definition.
  995.     * 
  996.     * @param string $elemname The name for the target form element.
  997.     * 
  998.     * @return void
  999.     * 
  1000.     */
  1001.     
  1002.     function fixColDef(&$col, $elemname)
  1003.     {    
  1004.         // always have a "require" value, false if not set
  1005.         if (! isset($col['require'])) {
  1006.             $col['require'] = false;
  1007.         }
  1008.         
  1009.         // array of acceptable values, typically for
  1010.         // 'select' or 'radio'
  1011.         if (! isset($col['qf_vals'])) {
  1012.             $col['qf_vals'] = null;
  1013.         }
  1014.         
  1015.         // are we doing client validation in addition to 
  1016.         // server validation?  by default, no.
  1017.         if (! isset($col['qf_client'])) {
  1018.             $col['qf_client'] = false;
  1019.         }
  1020.  
  1021.         if (! isset($col['qf_filters'])) {
  1022.             $col['qf_filters'] = array();
  1023.         }
  1024.         
  1025.         // the element type; if not set,
  1026.         // assigns an element type based on the column type.
  1027.         // by default, the type is 'text' (unless there are
  1028.         // values, in which case the type is 'select')
  1029.         if (! isset($col['qf_type'])) {
  1030.         
  1031.             // if $col['type'] is not set, set it to null
  1032.             // ==> in the switch statement below, the
  1033.             //     default case will be used
  1034.             if (!isset($col['type'])) {
  1035.                 $col['type'] = null;
  1036.             }
  1037.  
  1038.             switch ($col['type']) {
  1039.             
  1040.             case 'boolean':
  1041.                 $col['qf_type'] = 'checkbox';
  1042.                 $col['qf_vals'] = array(0,1);
  1043.                 break;
  1044.             
  1045.             case 'date':
  1046.                 $col['qf_type'] = 'date';
  1047.                 break;
  1048.                 
  1049.             case 'time':
  1050.                 $col['qf_type'] = 'time';
  1051.                 break;
  1052.                 
  1053.             case 'timestamp':
  1054.                 $col['qf_type'] = 'timestamp';
  1055.                 break;
  1056.                 
  1057.             case 'clob':
  1058.                 $col['qf_type'] = 'textarea';
  1059.                 break;
  1060.                 
  1061.             default:
  1062.                 if (isset($col['qf_vals'])) {
  1063.                     $col['qf_type'] = 'select';
  1064.                 } else {
  1065.                     $col['qf_type'] = 'text';
  1066.                 }
  1067.                 break;
  1068.  
  1069.             }
  1070.         }
  1071.         
  1072.         // label for the element; defaults to the element
  1073.         // name.  adds both quickform label and table-header
  1074.         // label if qf_label is not set.
  1075.         if (! isset($col['qf_label'])) {
  1076.             if (isset($col['label'])) {
  1077.                 $col['qf_label'] = $col['label'];
  1078.             }
  1079.             else {
  1080.                 $col['qf_label'] = $elemname . ':';
  1081.             }
  1082.         }
  1083.         
  1084.         // special options for the element, typically used
  1085.         // for 'date' element types
  1086.         if (! isset($col['qf_opts'])) {
  1087.             $col['qf_opts'] = array();
  1088.         }
  1089.         
  1090.         // array of additional HTML attributes for the element
  1091.         if (! isset($col['qf_attrs'])) {
  1092.             // setting to array() generates an error in HTML_Common
  1093.             $col['qf_attrs'] = null;
  1094.         }
  1095.         
  1096.         // array of QuickForm validation rules to apply
  1097.         if (! isset($col['qf_rules'])) {
  1098.             $col['qf_rules'] = array();
  1099.         }
  1100.         
  1101.         // if the element is hidden, then we're done
  1102.         // (adding rules to hidden elements is mostly useless)
  1103.         if ($col['qf_type'] == 'hidden') {
  1104.             return;
  1105.         }
  1106.         
  1107.         // code to keep BC for the separator for grouped QF elements
  1108.         if (isset($col['qf_radiosep'])) {
  1109.             $col['qf_groupsep'] = $col['qf_radiosep'];
  1110.         }
  1111.  
  1112.         // add a separator for grouped elements
  1113.         if (!isset($col['qf_groupsep'])) {
  1114.             $col['qf_groupsep'] = '<br />';
  1115.         }
  1116.         
  1117.         // $col['qf_set_default_rules'] === false allows to turn off
  1118.         // the automatic creation of QF rules for this "column"
  1119.         // (suggested by Arne Bippes)
  1120.         if (isset($col['qf_set_default_rules']) &&
  1121.                   $col['qf_set_default_rules'] === false) {
  1122.             return;
  1123.         }        
  1124.  
  1125.         // the element is required
  1126.         // ==> set 'uploadedfile' (for file elements) or 'required' (for all
  1127.         // other elements) rule if it is was not already set
  1128.         $req_rule_name = ($col['qf_type'] == 'file') ? 'uploadedfile' : 'required';
  1129.         if (!isset($col['qf_rules'][$req_rule_name]) && $col['require']) {
  1130.  
  1131.             $col['qf_rules'][$req_rule_name] = sprintf(
  1132.                 $GLOBALS['_DB_TABLE']['qf_rules']['required'],
  1133.                 $col['qf_label']
  1134.             );
  1135.  
  1136.         }
  1137.  
  1138.         // for file elements the 'numeric' and 'maxlength' rules must not be set
  1139.         if ($col['qf_type'] == 'file') {
  1140.             return;
  1141.         }
  1142.  
  1143.         $numeric = array('smallint', 'integer', 'bigint', 'decimal', 
  1144.             'single', 'double');
  1145.  
  1146.         // the element is numeric
  1147.         if (!isset($col['qf_rules']['numeric']) && isset($col['type']) &&
  1148.             in_array($col['type'], $numeric)) {
  1149.  
  1150.             $col['qf_rules']['numeric'] = sprintf(
  1151.                 $GLOBALS['_DB_TABLE']['qf_rules']['numeric'],
  1152.                 $col['qf_label']
  1153.             );
  1154.  
  1155.         }
  1156.  
  1157.         // the element has a maximum length
  1158.         if (!isset($col['qf_rules']['maxlength']) && isset($col['size'])) {
  1159.  
  1160.             $max = $col['size'];
  1161.  
  1162.             $msg = sprintf(
  1163.                 $GLOBALS['_DB_TABLE']['qf_rules']['maxlength'],
  1164.                 $col['qf_label'],
  1165.                 $max
  1166.             );
  1167.  
  1168.             $col['qf_rules']['maxlength'] = array($msg, $max);
  1169.         }
  1170.     }
  1171. }
  1172.  
  1173. ?>
  1174.