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

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