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

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