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.15 2004/12/20 18:59:16 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.     * @author Ian Eure <ieure@php.net>
  160.     * 
  161.     * @static
  162.     * 
  163.     * @access public
  164.     * 
  165.     * @param object &$form An HTML_QuickForm object.
  166.     * 
  167.     * @param array $cols A sequential array of DB_Table column definitions
  168.     *  from which to create form elements.
  169.     * 
  170.     * @param string $arrayName By default, the form will use the names
  171.     *  of the columns as the names of the form elements.  If you pass
  172.     *  $arrayName, the column names will become keys in an array named
  173.     *  for this parameter.
  174.     * 
  175.     * @return void 
  176.     * 
  177.     */
  178.     
  179.     function addElements(&$form$cols$arrayName = null)
  180.     {
  181.         $elements =DB_Table_QuickForm::getElements($cols$arrayName);
  182.         foreach (array_keys($elementsas $k{
  183.             $element =$elements[$k];
  184.             if (is_array($element)) {
  185.                 $form->addGroup($element$element->getName()$col['qf_label']);
  186.             else if (is_object($element)) {
  187.                 $form->addElement($element);
  188.             }
  189.         }
  190.     }
  191.  
  192.     /**
  193.     * 
  194.     * Gets controls for a list of columns
  195.     * 
  196.     * @author Ian Eure <ieure@php.net>
  197.     * 
  198.     * @static
  199.     * 
  200.     * @access public
  201.     * 
  202.     * @param object &$form An HTML_QuickForm object.
  203.     * 
  204.     * @param array $cols A sequential array of DB_Table column definitions
  205.     *  from which to create form elements.
  206.     * 
  207.     * @param string $arrayName By default, the form will use the names
  208.     *  of the columns as the names of the form elements.  If you pass
  209.     *  $arrayName, the column names will become keys in an array named
  210.     *  for this parameter.
  211.     * 
  212.     * @return void 
  213.     * 
  214.     */
  215.     
  216.     function &getElements($cols$arrayName = null)
  217.     {
  218.         $elements = array();
  219.         
  220.         foreach ($cols as $name => $col{
  221.             
  222.             if ($arrayName{
  223.                 $elemname $arrayName . "[$name]";
  224.             else {
  225.                 $elemname $name;
  226.             }
  227.             
  228.             DB_Table_QuickForm::fixColDef($col$elemname);
  229.  
  230.             $elements[=DB_Table_QuickForm::getElement($col$elemname);
  231.         }
  232.         return $elements;
  233.     }
  234.     
  235.     
  236.     /**
  237.     * 
  238.     * Build a single QuickForm element based on a DB_Table column.
  239.     * 
  240.     * @static
  241.     * 
  242.     * @access public
  243.     * 
  244.     * @param array $col A DB_Table column definition.
  245.     * 
  246.     * @param string $elemname The name to use for the generated QuickForm
  247.     *  element.
  248.     * 
  249.     * @return object HTML_QuickForm_Element 
  250.     * 
  251.     */
  252.     
  253.     function &getElement($col$elemname)
  254.     {
  255.         if (isset($col['qf_setvalue'])) {
  256.             $setval $col['qf_setvalue'];
  257.         }
  258.         
  259.         switch ($col['qf_type']{
  260.         
  261.         case 'advcheckbox':
  262.         case 'checkbox':
  263.             
  264.             $element =HTML_QuickForm::createElement(
  265.                 'advcheckbox',
  266.                 $elemname,
  267.                 $col['qf_label'],
  268.                 null,
  269.                 $col['qf_attrs'],
  270.                 $col['qf_vals']
  271.             );
  272.             
  273.             // WARNING: advcheckbox elements in HTML_QuickForm v3.2.2
  274.             // and earlier do not honor setChecked(); they will always
  275.             // be un-checked, unless a POST value sets them.  Upgrade
  276.             // to QF 3.2.3 or later.
  277.             if (isset($setval&& $setval == true{
  278.                 $element->setChecked(true);
  279.             else {
  280.                 $element->setChecked(false);
  281.             }
  282.             
  283.             break;
  284.             
  285.         case 'autocomplete':
  286.         
  287.             $element =HTML_QuickForm::createElement(
  288.                 $col['qf_type'],
  289.                 $elemname,
  290.                 $col['qf_label'],
  291.                 $col['qf_vals'],
  292.                 $col['qf_attrs']
  293.             );
  294.             
  295.             if (isset($setval)) {
  296.                 $element->setValue($setval);
  297.             }
  298.             
  299.             break;
  300.             
  301.         case 'date':
  302.         
  303.             $col['qf_opts']['format''Y-m-d';
  304.             
  305.             $element =HTML_QuickForm::createElement(
  306.                 'date',
  307.                 $elemname,
  308.                 $col['qf_label'],
  309.                 $col['qf_opts'],
  310.                 $col['qf_attrs']
  311.             );
  312.             
  313.             if (isset($setval)) {
  314.                 $element->setValue($setval);
  315.             }
  316.             
  317.             break;
  318.             
  319.         case 'time':
  320.         
  321.             $col['qf_opts']['format''H:i:s';
  322.             
  323.             $element =HTML_QuickForm::createElement(
  324.                 'date',
  325.                 $elemname,
  326.                 $col['qf_label'],
  327.                 $col['qf_opts'],
  328.                 $col['qf_attrs']
  329.             );
  330.             
  331.             if (isset($setval)) {
  332.                 $element->setValue($setval);
  333.             }
  334.             
  335.             break;
  336.  
  337.         case 'timestamp':
  338.         
  339.             $col['qf_opts']['format''Y-m-d H:i:s';
  340.             
  341.             $element =HTML_QuickForm::createElement(
  342.                 'date',
  343.                 $elemname,
  344.                 $col['qf_label'],
  345.                 $col['qf_opts'],
  346.                 $col['qf_attrs']
  347.             );
  348.             
  349.             if (isset($setval)) {
  350.                 $element->setValue($setval);
  351.             }
  352.             
  353.             break;
  354.         
  355.         case 'hidden':
  356.         
  357.             $element =HTML_QuickForm::createElement(
  358.                 $col['qf_type'],
  359.                 $elemname,
  360.                 $col['qf_attrs']
  361.             );
  362.             
  363.             if (isset($setval)) {
  364.                 $element->setValue($setval);
  365.             }
  366.             
  367.             break;
  368.             
  369.             
  370.         case 'radio':
  371.         
  372.             $element = array();
  373.             
  374.             foreach ($col['qf_vals'as $btnvalue => $btnlabel{
  375.                 
  376.                 if (isset($setval&& $setval == $btnvalue{
  377.                     $col['qf_attrs']['checked''checked';
  378.                 }
  379.                 
  380.                 $element[=HTML_QuickForm::createElement(
  381.                     $col['qf_type'],
  382.                     null// elemname not added because this is a group
  383.                     null,
  384.                     $btnlabel '<br />',
  385.                     $btnvalue,
  386.                     $col['qf_attrs']
  387.                 );
  388.             }
  389.             
  390.             break;
  391.             
  392.         case 'select':
  393.             
  394.             $element =HTML_QuickForm::createElement(
  395.                 $col['qf_type'],
  396.                 $elemname,
  397.                 $col['qf_label'],
  398.                 $col['qf_vals'],
  399.                 $col['qf_attrs']
  400.             );
  401.             
  402.             if (isset($setval)) {
  403.                 $element->setSelected($setval);
  404.             }
  405.             
  406.             break;
  407.             
  408.         case 'password':
  409.         case 'text':
  410.         case 'textarea':
  411.         
  412.             if (isset($col['qf_attrs']['maxlength']&&
  413.                 isset($col['size'])) {
  414.                 $col['qf_attrs']['maxlength'$col['size'];
  415.             }
  416.             
  417.             $element =HTML_QuickForm::createElement(
  418.                 $col['qf_type'],
  419.                 $elemname,
  420.                 $col['qf_label'],
  421.                 $col['qf_attrs']
  422.             );
  423.             
  424.             if (isset($setval)) {
  425.                 $element->setValue($setval);
  426.             }
  427.             
  428.             break;
  429.         
  430.         case 'static':
  431.             $element =HTML_QuickForm::createElement(
  432.                 $col['qf_type'],
  433.                 null,
  434.                 $col['qf_label'],
  435.                 (isset($setval$setval '')
  436.             );
  437.             break;
  438.             
  439.         default:
  440.             
  441.             /**
  442.             * @author Moritz Heidkamp <moritz.heidkamp@invision-team.de>
  443.             */
  444.             
  445.             // not a recognized type.  is it registered with QuickForm?
  446.             if (HTML_QuickForm::isTypeRegistered($col['qf_type'])) {
  447.                 
  448.                 // yes, create it with some minimalist parameters
  449.                 $element =HTML_QuickForm::createElement(
  450.                     $col['qf_type'],
  451.                     $elemname,
  452.                     $col['qf_label'],
  453.                     $col['qf_attrs']
  454.                 );
  455.                 
  456.                 // set its default value, if there is one
  457.                 if (isset($setval)) {
  458.                     $element->setValue($setval);
  459.                 }
  460.                 
  461.             else {
  462.                 // element type is not registered with QuickForm.
  463.                 $element = null;
  464.             }
  465.             
  466.             break;
  467.         }
  468.         
  469.         // done
  470.         return $element;
  471.     }
  472.     
  473.     
  474.     /**
  475.     * 
  476.     * Build an array of form elements based from DB_Table columns.
  477.     * 
  478.     * @static
  479.     * 
  480.     * @access public
  481.     * 
  482.     * @param array $cols A sequential array of DB_Table column
  483.     *  definitions from which to create form elements.
  484.     * 
  485.     * @param string $arrayName By default, the form will use the names
  486.     *  of the columns as the names of the form elements.  If you pass
  487.     *  $arrayName, the column names will become keys in an array named
  488.     *  for this parameter.
  489.     * 
  490.     * @return array An array of HTML_QuickForm_Element objects.
  491.     * 
  492.     */
  493.     
  494.     function &getGroup($cols$arrayName = null)
  495.     {
  496.         $group = array();
  497.         
  498.         foreach ($cols as $name => $col{
  499.             
  500.             if ($arrayName{
  501.                 $elemname $arrayName . "[$name]";
  502.             else {
  503.                 $elemname $name;
  504.             }
  505.             
  506.             DB_Table_QuickForm::fixColDef($col$elemname);
  507.             
  508.             $group[=DB_Table_QuickForm::getElement($col$elemname);
  509.         }
  510.         
  511.         return $group;
  512.     }
  513.     
  514.     
  515.     /**
  516.     * 
  517.     * Adds element rules to a pre-existing HTML_QuickForm object.
  518.     * 
  519.     * @static
  520.     * 
  521.     * @access public
  522.     * 
  523.     * @param object &$form An HTML_QuickForm object.
  524.     * 
  525.     * @param array $cols A sequential array of DB_Table column definitions
  526.     *  from which to create form elements.
  527.     * 
  528.     * @param string $arrayName By default, the form will use the names
  529.     *  of the columns as the names of the form elements.  If you pass
  530.     *  $arrayName, the column names will become keys in an array named
  531.     *  for this parameter.
  532.     * 
  533.     * @param string $clientValidate By default, validation will match
  534.     *  the 'qf_client' value from the column definition.  However,
  535.     *  if you set $clientValidate to true or false, this will
  536.     *  override the value from the column definition.
  537.     * 
  538.     * @return void 
  539.     * 
  540.     */
  541.     
  542.     function addRules(&$form$cols$arrayName = null,
  543.         $clientValidate = null)
  544.     {
  545.         foreach ($cols as $name => $col{
  546.             
  547.             if ($arrayName{
  548.                 $elemname $arrayName . "[$name]";
  549.             else {
  550.                 $elemname $name;
  551.             }
  552.             
  553.             // make sure all necessary elements are in place
  554.             DB_Table_QuickForm::fixColDef($col$elemname);
  555.             
  556.             // if clientValidate is specified, override the column
  557.             // definition.  otherwise use the col def as it is.
  558.             if (is_null($clientValidate)) {
  559.                 // override
  560.                 if ($clientValidate{
  561.                     $validate 'client';
  562.                 else {
  563.                     $validate 'server';
  564.                 }
  565.             else {
  566.                 // use as-is
  567.                 if ($col['qf_client']{
  568.                     $validate 'client';
  569.                 else {
  570.                     $validate 'server';
  571.                 }
  572.             }
  573.             
  574.             // **always** override these rules to make them 
  575.             // server-side only.  suggested by Mark Wiesemann,
  576.             // debugged by Hero Wanders.
  577.             $onlyServer = array('filename''maxfilesize''mimetype',
  578.                 'uploadedfile');
  579.             
  580.             // loop through the rules and add them
  581.             foreach ($col['qf_rules'as $type => $opts{
  582.                 
  583.                 // override the onlyServer types so that we don't attempt
  584.                 // client-side validation at all.
  585.                 if (in_array($type$onlyServer)) {
  586.                     $validate 'server';
  587.                 }
  588.                 
  589.                 switch ($type{
  590.                     
  591.                 case 'alphanumeric':
  592.                 case 'email':
  593.                 case 'lettersonly':
  594.                 case 'nonzero':
  595.                 case 'nopunctuation':
  596.                 case 'numeric':
  597.                 case 'required':
  598.                 case 'uploadedfile':
  599.                     // $opts is the error message
  600.                     $form->addRule($elemname$opts$typenull$validate);
  601.                     break;
  602.                 
  603.                 case 'filename':
  604.                 case 'maxfilesize':
  605.                 case 'maxlength':
  606.                 case 'mimetype':
  607.                 case 'minlength':
  608.                 case 'regex':
  609.                     // $opts[0] is the message
  610.                     // $opts[1] is the size, mimetype, or regex
  611.                     $form->addRule($elemname$opts[0]$type$opts[1],
  612.                         $validate);
  613.                     break;
  614.                 
  615.                 default:
  616.                     // by Alex Hoebart: this should allow any registered rule.
  617.                     if (in_array($type,$form->getRegisteredRules())) {
  618.                         if (is_array($opts)) {
  619.                             // $opts[0] is the message, $opts[1] is the size or regex
  620.                             $form->addRule($elemname$opts[0]$type$opts[1]$validate);
  621.                         else {
  622.                             // $opts is the error message
  623.                             $form->addRule($elemname$opts$type$validate);
  624.                         }
  625.                     }
  626.                     break;
  627.                 }
  628.             }
  629.         }
  630.     }
  631.     
  632.     
  633.     /**
  634.     * 
  635.     * "Fixes" a DB_Table column definition for QuickForm.
  636.     * 
  637.     * Makes it so that all the 'qf_*' key constants are populated
  638.     * with appropriate default values; also checks the 'require'
  639.     * value (if not set, defaults to false).
  640.     * 
  641.     * @static
  642.     * 
  643.     * @access public
  644.     * 
  645.     * @param array &$col A DB_Table column definition.
  646.     * 
  647.     * @param string $elemname The name for the target form element.
  648.     * 
  649.     * @return void 
  650.     * 
  651.     */
  652.     
  653.     function fixColDef(&$col$elemname)
  654.     {    
  655.         // always have a "require" value, false if not set
  656.         if (isset($col['require'])) {
  657.             $col['require'= false;
  658.         }
  659.         
  660.         // array of acceptable values, typically for
  661.         // 'select' or 'radio'
  662.         if (isset($col['qf_vals'])) {
  663.             $col['qf_vals'= null;
  664.         }
  665.         
  666.         // are we doing client validation in addition to 
  667.         // server validation?  by default, no.
  668.         if (isset($col['qf_client'])) {
  669.             $col['qf_client'= false;
  670.         }
  671.         
  672.         // the element type; if not set,
  673.         // assigns an element type based on the column type.
  674.         // by default, the type is 'text' (unless there are
  675.         // values, in which case the type is 'select')
  676.         if (isset($col['qf_type'])) {
  677.         
  678.             switch ($col['type']{
  679.             
  680.             case 'boolean':
  681.                 $col['qf_type''checkbox';
  682.                 $col['qf_vals'= array(0,1);
  683.                 break;
  684.             
  685.             case 'date':
  686.                 $col['qf_type''date';
  687.                 break;
  688.                 
  689.             case 'time':
  690.                 $col['qf_type''time';
  691.                 break;
  692.                 
  693.             case 'timestamp':
  694.                 $col['qf_type''timestamp';
  695.                 break;
  696.                 
  697.             case 'clob':
  698.                 $col['qf_type''textarea';
  699.                 break;
  700.                 
  701.             default:
  702.                 if (isset($col['qf_vals'])) {
  703.                     $col['qf_type''select';
  704.                 else {
  705.                     $col['qf_type''text';
  706.                 }
  707.                 break;
  708.             }
  709.         }
  710.         
  711.         // label for the element; defaults to the element
  712.         // name
  713.         if (isset($col['qf_label'])) {
  714.             $col['qf_label'$elemname ':';
  715.         }
  716.         
  717.         // special options for the element, typically used
  718.         // for 'date' element types
  719.         if (isset($col['qf_opts'])) {
  720.             $col['qf_opts'= array();
  721.         }
  722.         
  723.         // array of additional HTML attributes for the element
  724.         if (isset($col['qf_attrs'])) {
  725.             // setting to array() generates an error in HTML_Common
  726.             $col['qf_attrs'= null;
  727.         }
  728.         
  729.         // array of QuickForm validation rules to apply
  730.         if (isset($col['qf_rules'])) {
  731.             $col['qf_rules'= array();
  732.         }
  733.         
  734.         // if the element is hidden, then we're done
  735.         // (adding rules to hidden elements is mostly useless)
  736.         if ($col['qf_type'== 'hidden'{
  737.             return;
  738.         }
  739.         
  740.         // the element is required
  741.         if (isset($col['qf_rules']['required']&& $col['require']{
  742.             
  743.             $col['qf_rules']['required'sprintf(
  744.                 $GLOBALS['_DB_TABLE']['qf_rules']['required'],
  745.                 $elemname
  746.             );
  747.             
  748.         }
  749.         
  750.         $numeric = array('smallint''integer''bigint''decimal'
  751.             'single''double');
  752.         
  753.         // the element is numeric
  754.         if (isset($col['qf_rules']['numeric']&&
  755.             in_array($col['type']$numeric)) {
  756.             
  757.             $col['qf_rules']['numeric'sprintf(
  758.                 $GLOBALS['_DB_TABLE']['qf_rules']['numeric'],
  759.                 $elemname
  760.             );
  761.             
  762.         }
  763.         
  764.         // the element has a maximum length
  765.         if (isset($col['qf_rules']['maxlength']&&
  766.             isset($col['size'])) {
  767.         
  768.             $max $col['size'];
  769.             
  770.             $msg sprintf(
  771.                 $GLOBALS['_DB_TABLE']['qf_rules']['maxlength'],
  772.                 $elemname,
  773.                 $max
  774.             );
  775.             
  776.             $col['qf_rules']['maxlength'= array($msg$max);
  777.         }
  778.     }
  779. }
  780.  
  781. ?>

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