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

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