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

Source for file QuickForm.php

Documentation is available at QuickForm.php

  1. <?php
  2.  
  3. require_once 'HTML/QuickForm.php';
  4.  
  5. /**
  6. * DB_Table_QuickForm creates HTML_QuickForm objects from DB_Table properties.
  7. * DB_Table_QuickForm provides HTML form creation facilities based on
  8. * DB_Table column definitions transformed into HTML_QuickForm elements.
  9. * $Id: QuickForm.php,v 1.16 2004/04/20 15:18:30 pmjones Exp $
  10. @author Paul M. Jones <pmjones@ciaweb.net>
  11. *
  12. @package DB_Table
  13. *
  14. */
  15.  
  16.     
  17.     /**
  18.     * 
  19.     * Build a form based on DB_Table column definitions.
  20.     * 
  21.     * @static
  22.     * 
  23.     * @access public
  24.     * 
  25.     * @param array $cols A sequential array of DB_Table column definitions
  26.     *  from which to create form elements.
  27.     * 
  28.     * @param string $array_name By default, the form will use the names
  29.     *  of the columns as the names of the form elements.  If you pass
  30.     *  $array_name, the column names will become keys in an array named
  31.     *  for this parameter.
  32.     * 
  33.     * @param array $args An associative array of optional arguments to
  34.     *  pass to the QuickForm object.  The keys are...
  35.     *
  36.     *  'formName' : String, name of the form; defaults to the name of the
  37.     *  table.
  38.     * 
  39.     *  'method' : String, form method; defaults to 'post'.
  40.     * 
  41.     *  'action' : String, form action; defaults to
  42.     *  $_SERVER['REQUEST_URI'].
  43.     * 
  44.     *  'target' : String, form target target; defaults to '_self'
  45.     * 
  46.     *  'attributes' : Associative array, extra attributes for <form>
  47.     *  tag; the key is the attribute name and the value is attribute
  48.     *  value.
  49.     * 
  50.     *  'trackSubmit' : Boolean, whether to track if the form was
  51.     *  submitted by adding a special hidden field
  52.     * 
  53.     * @return object HTML_QuickForm 
  54.     * 
  55.     * @see HTML_QuickForm
  56.     * 
  57.     */
  58.     
  59.     function &getForm($cols$array_name = null$args = array())
  60.     {
  61.         $formName = isset($args['formName'])
  62.             ? $args['formName'$this->table;
  63.             
  64.         $method = isset($args['method'])
  65.             ? $args['method''post';
  66.         
  67.         $action = isset($args['action'])
  68.             ? $args['action'$_SERVER['REQUEST_URI'];
  69.         
  70.         $target = isset($args['target'])
  71.             ? $args['target''_self';
  72.         
  73.         $attributes = isset($args['attributes'])
  74.             ? $args['attributes': null;
  75.         
  76.         $trackSubmit = isset($args['trackSubmit'])
  77.             ? $args['trackSubmit': false;
  78.         
  79.         $form =new HTML_QuickForm($formName$method$action$target
  80.             $attributes$trackSubmit);
  81.             
  82.         DB_Table_QuickForm::addElements($form$cols$array_name);
  83.         DB_Table_QuickForm::addRules($form$cols$array_name);
  84.         
  85.         return $form;
  86.     }
  87.     
  88.     
  89.     /**
  90.     * 
  91.     * Adds DB_Table columns to a pre-existing HTML_QuickForm object.
  92.     * 
  93.     * @static
  94.     * 
  95.     * @access public
  96.     * 
  97.     * @param object &$form An HTML_QuickForm object.
  98.     * 
  99.     * @param array $cols A sequential array of DB_Table column definitions
  100.     *  from which to create form elements.
  101.     * 
  102.     * @param string $array_name By default, the form will use the names
  103.     *  of the columns as the names of the form elements.  If you pass
  104.     *  $array_name, the column names will become keys in an array named
  105.     *  for this parameter.
  106.     * 
  107.     * @return void 
  108.     * 
  109.     */
  110.     
  111.     function addElements(&$form$cols$array_name = null)
  112.     {
  113.         foreach ($cols as $name => $col{
  114.             
  115.             if ($array_name{
  116.                 $elemname $array_name . "[$name]";
  117.             else {
  118.                 $elemname $name;
  119.             }
  120.             
  121.             DB_Table_QuickForm::fixColDef($col$elemname);
  122.  
  123.             $tmp =DB_Table_QuickForm::getElement($col$elemname);
  124.             
  125.             if (is_array($tmp)) {
  126.                 $form->addGroup($tmp$elemname$col['qf_label']);
  127.             }
  128.             
  129.             if (is_object($tmp)) {
  130.                 $form->addElement($tmp);
  131.             }
  132.         }
  133.     }
  134.     
  135.     
  136.     /**
  137.     * 
  138.     * Build a single QuickForm element based on a DB_Table column.
  139.     * 
  140.     * @static
  141.     * 
  142.     * @access public
  143.     * 
  144.     * @param array $col A DB_Table column definition.
  145.     * 
  146.     * @param string $elemname The name to use for the generated QuickForm
  147.     *  element.
  148.     * 
  149.     * @return object HTML_QuickForm_Element 
  150.     * 
  151.     */
  152.     
  153.     function &getElement($col$elemname)
  154.     {
  155.         if (isset($col['qf_setvalue'])) {
  156.             $setval $col['qf_setvalue'];
  157.         }
  158.         
  159.         switch ($col['qf_type']{
  160.         
  161.         case 'advcheckbox':
  162.         case 'checkbox':
  163.             
  164.             $element =HTML_QuickForm::createElement(
  165.                 $col['qf_type'],
  166.                 null,
  167.                 $col['qf_label'],
  168.                 null,
  169.                 $col['qf_attrs'],
  170.                 $col['qf_vals']
  171.             );
  172.             
  173.             // WARNING: advcheckbox elements in HTML_QuickForm v3.2.2
  174.             // and earlier do not honor setChecked(); they will always
  175.             // be un-checked, unless a POST value sets them.
  176.             if (isset($setval&& $setval == true{
  177.                 $element->setChecked(true);
  178.             else {
  179.                 $element->setChecked(false);
  180.             }
  181.             
  182.             break;
  183.             
  184.         case 'date':
  185.         
  186.             $col['qf_opts']['format''Y-m-d';
  187.             
  188.             $element =HTML_QuickForm::createElement(
  189.                 'date',
  190.                 $elemname,
  191.                 $col['qf_label'],
  192.                 $col['qf_opts'],
  193.                 $col['qf_attrs']
  194.             );
  195.             
  196.             if (isset($setval)) {
  197.                 $element->setValue($setval);
  198.             }
  199.             
  200.             break;
  201.             
  202.         case 'time':
  203.         
  204.             $col['qf_opts']['format''H:i:s';
  205.             
  206.             $element =HTML_QuickForm::createElement(
  207.                 'date',
  208.                 $elemname,
  209.                 $col['qf_label'],
  210.                 $col['qf_opts'],
  211.                 $col['qf_attrs']
  212.             );
  213.             
  214.             if (isset($setval)) {
  215.                 $element->setValue($setval);
  216.             }
  217.             
  218.             break;
  219.  
  220.         case 'timestamp':
  221.         
  222.             $col['qf_opts']['format''Y-m-d H:i:s';
  223.             
  224.             $element =HTML_QuickForm::createElement(
  225.                 'date',
  226.                 $elemname,
  227.                 $col['qf_label'],
  228.                 $col['qf_opts'],
  229.                 $col['qf_attrs']
  230.             );
  231.             
  232.             if (isset($setval)) {
  233.                 $element->setValue($setval);
  234.             }
  235.             
  236.             break;
  237.         
  238.         case 'hidden':
  239.         
  240.             $element =HTML_QuickForm::createElement(
  241.                 $col['qf_type'],
  242.                 $elemname,
  243.                 $col['qf_attrs']
  244.             );
  245.             
  246.             if (isset($setval)) {
  247.                 $element->setValue($setval);
  248.             }
  249.             
  250.             break;
  251.             
  252.             
  253.         case 'radio':
  254.         
  255.             $element = array();
  256.             
  257.             foreach ($col['qf_vals'as $btnvalue => $btnlabel{
  258.                 
  259.                 if (isset($setval&& $setval == $btnvalue{
  260.                     $col['qf_attrs']['checked''checked';
  261.                 }
  262.                 
  263.                 $element[=HTML_QuickForm::createElement(
  264.                     $col['qf_type'],
  265.                     null,
  266.                     null,
  267.                     $btnlabel '<br />',
  268.                     $btnvalue,
  269.                     $col['qf_attrs']
  270.                 );
  271.             }
  272.             
  273.             break;
  274.             
  275.         case 'select':
  276.         
  277.             $element =HTML_QuickForm::createElement(
  278.                 $col['qf_type'],
  279.                 $elemname,
  280.                 $col['qf_label'],
  281.                 $col['qf_vals'],
  282.                 $col['qf_attrs']
  283.             );
  284.             
  285.             if (isset($setval)) {
  286.                 $element->setSelected($setval);
  287.             }
  288.             
  289.             break;
  290.             
  291.         case 'password':
  292.         case 'text':
  293.         case 'textarea':
  294.         
  295.             if (isset($col['qf_attrs']['maxlength']&&
  296.                 isset($col['size'])) {
  297.                 $col['qf_attrs']['maxlength'$col['size'];
  298.             }
  299.             
  300.             $element =HTML_QuickForm::createElement(
  301.                 $col['qf_type'],
  302.                 $elemname,
  303.                 $col['qf_label'],
  304.                 $col['qf_attrs']
  305.             );
  306.             
  307.             if (isset($setval)) {
  308.                 $element->setValue($setval);
  309.             }
  310.             
  311.             break;
  312.         
  313.         case 'static':
  314.             $element =HTML_QuickForm::createElement(
  315.                 $col['qf_type'],
  316.                 $col['qf_label'],
  317.                 (isset($setval$setval '')
  318.             );
  319.             break;
  320.             
  321.         default:
  322.             
  323.             /**
  324.             * @author Moritz Heidkamp <moritz.heidkamp@invision-team.de>
  325.             */
  326.             
  327.             // not a recognized type.  is it registered with QuickForm?
  328.             if (HTML_QuickForm::isTypeRegistered($col['qf_type'])) {
  329.                 
  330.                 // yes, create it with some minimalist parameters
  331.                 $element =HTML_QuickForm::createElement(
  332.                     $col['qf_type'],
  333.                     $elemname,
  334.                     $col['qf_label'],
  335.                     $col['qf_attrs']
  336.                 );
  337.                 
  338.                 // set its default value, if there is one
  339.                 if (isset($setval)) {
  340.                     $element->setValue($setval);
  341.                 }
  342.                 
  343.             else {
  344.                 // element type is not registered with QuickForm.
  345.                 $element = null;
  346.             }
  347.             
  348.             break;
  349.         }
  350.         
  351.         // done
  352.         return $element;
  353.     }
  354.     
  355.     
  356.     /**
  357.     * 
  358.     * Build an array of form elements based from DB_Table columns.
  359.     * 
  360.     * @static
  361.     * 
  362.     * @access public
  363.     * 
  364.     * @param array $cols A sequential array of DB_Table column
  365.     *  definitions from which to create form elements.
  366.     * 
  367.     * @param string $array_name By default, the form will use the names
  368.     *  of the columns as the names of the form elements.  If you pass
  369.     *  $array_name, the column names will become keys in an array named
  370.     *  for this parameter.
  371.     * 
  372.     * @return array An array of HTML_QuickForm_Element objects.
  373.     * 
  374.     */
  375.     
  376.     function &getGroup($cols$array_name = null)
  377.     {
  378.         $group = array();
  379.         
  380.         foreach ($cols as $name => $col{
  381.             
  382.             if ($array_name{
  383.                 $elemname $array_name . "[$name]";
  384.             else {
  385.                 $elemname $name;
  386.             }
  387.             
  388.             DB_Table_QuickForm::fixColDef($col$elemname);
  389.             
  390.             $group[=DB_Table_QuickForm::getElement($col$elemname);
  391.         }
  392.         
  393.         return $group;
  394.     }
  395.     
  396.     
  397.     /**
  398.     * 
  399.     * Adds element rules to a pre-existing HTML_QuickForm object.
  400.     * 
  401.     * @static
  402.     * 
  403.     * @access public
  404.     * 
  405.     * @param object &$form An HTML_QuickForm object.
  406.     * 
  407.     * @param array $cols A sequential array of DB_Table column definitions
  408.     *  from which to create form elements.
  409.     * 
  410.     * @param string $array_name By default, the form will use the names
  411.     *  of the columns as the names of the form elements.  If you pass
  412.     *  $array_name, the column names will become keys in an array named
  413.     *  for this parameter.
  414.     * 
  415.     * @return void 
  416.     * 
  417.     */
  418.     
  419.     function addRules(&$form$cols$array_name = null)
  420.     {
  421.         foreach ($cols as $name => $col{
  422.             
  423.             if ($array_name{
  424.                 $elemname $array_name . "[$name]";
  425.             else {
  426.                 $elemname $name;
  427.             }
  428.             
  429.             DB_Table_QuickForm::fixColDef($col$elemname);
  430.             
  431.             foreach ($col['qf_rules'as $type => $opts{
  432.                 
  433.                 switch ($type{
  434.                     
  435.                 case 'required':
  436.                 case 'email':
  437.                 case 'lettersonly':
  438.                 case 'alphanumeric':
  439.                 case 'numeric':
  440.                 case 'nopunctuation':
  441.                 case 'nonzero':
  442.                     // $opts is the error message
  443.                     $form->addRule($elemname$opts$type);
  444.                     break;
  445.                 
  446.                 case 'minlength':
  447.                 case 'maxlength':
  448.                 case 'regex':
  449.                     // $opts[0] is the message, $opts[1] is the size or regex
  450.                     $form->addRule($elemname$opts[0]$type$opts[1]);
  451.                     break;
  452.                 
  453.                 default:
  454.                     break;
  455.                 }
  456.             }
  457.         }
  458.     }
  459.     
  460.     
  461.     /**
  462.     * 
  463.     * "Fixes" a DB_Table column definition for QuickForm.
  464.     * 
  465.     * Makes it so that all the 'qf_*' key constants are populated
  466.     * with appropriate default values; also checks the 'require'
  467.     * value (if not set, defaults to false).
  468.     * 
  469.     * @static
  470.     * 
  471.     * @access public
  472.     * 
  473.     * @param array &$col A DB_Table column definition.
  474.     * 
  475.     * @param string $elemname The name for the target form element.
  476.     * 
  477.     * @return void 
  478.     * 
  479.     */
  480.     
  481.     function fixColDef(&$col$elemname)
  482.     {    
  483.         // always have a "require" value, false if not set
  484.         if (isset($col['require'])) {
  485.             $col['require'= false;
  486.         }
  487.         
  488.         // array of acceptable values, typically for
  489.         // 'select' or 'radio'
  490.         if (isset($col['qf_vals'])) {
  491.             $col['qf_vals'= null;
  492.         }
  493.         
  494.         // the element type; if not set,
  495.         // assigns an element type based on the column type.
  496.         // by default, the type is 'text' (unless there are
  497.         // values, in which case the type is 'select')
  498.         if (isset($col['qf_type'])) {
  499.         
  500.             switch ($col['type']{
  501.             
  502.             case 'boolean':
  503.                 $col['qf_type''select';
  504.                 if (isset($col['qf_vals'])) {
  505.                     $col['qf_vals'= array(0 => 'No'1 => 'Yes');
  506.                 }
  507.                 break;
  508.             
  509.             case 'date':
  510.                 $col['qf_type''date';
  511.                 break;
  512.                 
  513.             case 'time':
  514.                 $col['qf_type''time';
  515.                 break;
  516.                 
  517.             case 'timestamp':
  518.                 $col['qf_type''timestamp';
  519.                 break;
  520.                 
  521.             case 'clob':
  522.                 $col['qf_type''textarea';
  523.                 break;
  524.                 
  525.             default:
  526.                 if (isset($col['qf_vals'])) {
  527.                     $col['qf_type''select';
  528.                 else {
  529.                     $col['qf_type''text';
  530.                 }
  531.                 break;
  532.             }
  533.         }
  534.         
  535.         // label for the element; defaults to the element
  536.         // name
  537.         if (isset($col['qf_label'])) {
  538.             $col['qf_label'$elemname ':';
  539.         }
  540.         
  541.         // special options for the element, typically used
  542.         // for 'date' element types
  543.         if (isset($col['qf_opts'])) {
  544.             $col['qf_opts'= array();
  545.         }
  546.         
  547.         // array of additional HTML attributes for the element
  548.         if (isset($col['qf_attrs'])) {
  549.             // setting to array() generates an error in HTML_Common
  550.             $col['qf_attrs'= null;
  551.         }
  552.         
  553.         // array of QuickForm validation rules to apply
  554.         if (isset($col['qf_rules'])) {
  555.             $col['qf_rules'= array();
  556.         }
  557.         
  558.         // if the element is hidden, then we're done
  559.         // (adding rules to hidden elements is mostly useless)
  560.         if ($col['qf_type'== 'hidden'{
  561.             return;
  562.         }
  563.         
  564.         // the element is required and not hidden
  565.         if (isset($col['qf_rules']['required']&&
  566.             $col['require']{
  567.             
  568.             $col['qf_rules']['required'=
  569.                 'This element is required.';
  570.             
  571.         }
  572.         
  573.         // the element must be a number
  574.         if (isset($col['qf_rules']['numeric']&& (
  575.                 $col['type'== 'smallint' ||
  576.                 $col['type'== 'integer' ||
  577.                 $col['type'== 'bigint' ||
  578.                 $col['type'== 'decimal'||
  579.                 $col['type'== 'single' ||
  580.                 $col['type'== 'double'
  581.             ) ) {
  582.             
  583.             if (isset($col['qf_rules']['numeric'])) {
  584.                 $col['qf_rules']['numeric'=
  585.                     'This element must be numbers only.';
  586.             }
  587.         }
  588.         
  589.         // the element has a maximum length
  590.         if (isset($col['qf_rules']['maxlength']&&
  591.             isset($col['size'])) {
  592.         
  593.             $max $col['size'];
  594.             
  595.             $col['qf_rules']['maxlength'= array(
  596.                 "This element can be no longer than $max characters.",
  597.                 $max
  598.             );
  599.         }
  600.     }
  601. }
  602.  
  603. ?>

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