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

Source for file DBTfillingForm.class.php

Documentation is available at DBTfillingForm.class.php

  1. <?php
  2. /**
  3.  * @package forms
  4.  */
  5.  
  6. if!defined"CLASSPATH" ) )
  7. {
  8.   echo "<h3>You have to define the constant CLASSPATH!</h3>\n";
  9.   echo "Example: define( 'CLASSPATH', '../path/to/classes/' );\n";
  10.   exit();
  11. }
  12.  
  13.  
  14. /**
  15.  * Including the basic class to define html forms.
  16.  */
  17. require_onceCLASSPATH."forms/HTMLForm.class.php" );
  18. /**
  19.  * Including various funktions to manipulate arrays.
  20.  */
  21. include_onceCLASSPATH."core/Arrays.class.php" );
  22.  
  23. /**
  24.  * A class to generate forms to put data in a databasetable.
  25.  *
  26.  * @package   forms
  27.  * @version   0.2.5
  28.  * @since     0.4.0.2
  29.  * @author    Daniel Plücken <daniel@debakel.net>
  30.  * @license   http://www.gnu.org/copyleft/lesser.html
  31.  *             GNU Lesser General Public License
  32.  * @copyright Copyright (C) 2003 Daniel Plücken <daniel@debakel.net>
  33.  *
  34.  *  This library is free software; you can redistribute it and/or
  35.  *  modify it under the terms of the GNU Lesser General Public
  36.  *  License as published by the Free Software Foundation; either
  37.  *  version 2.1 of the License.
  38.  *
  39.  *  This library is distributed in the hope that it will be useful,
  40.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  41.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  42.  *  GNU Lesser General Public License for more details.
  43.  *
  44.  *  You should have received a copy of the GNU Lesser General
  45.  *  Public License along with this library; if not, write to the
  46.  *  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  47.  *  Boston, MA 02111-1307 USA
  48.  */
  49. class DBTfillingForm extends HTMLForm
  50. {
  51.     /**
  52.      * The reference to the needed Databasetable-Object.
  53.      *
  54.      * @var   object  $dbt_obj 
  55.      *  access private
  56.      */
  57.     var $dbt_obj;
  58.     /**
  59.      * @var   integer $lastEntryId 
  60.      */
  61.     var $lastEntryId;
  62.  
  63.  
  64.  
  65.     /**
  66.      * Constructor
  67.      *
  68.      * @version 1.0
  69.      * @since   0.1.0
  70.      * @author  Daniel Plücken <daniel@debakel.net>
  71.      * @access  public
  72.      * @param   object  $dbt_obj        The reference to the needed
  73.      *                                   Databasetable-Object.
  74.      * @param   string  $name           The name of the form.
  75.      * @param   string  $onSubmitOrder  The javascript thatshould be execute
  76.      *                                   before submit.
  77.      * @param   string  $targetFile     The file that should receive the data.
  78.      * @param   string  $method         The Method how the data should be send
  79.      *                                   ( "post" or "get" )
  80.      */
  81.     function DBTfillingForm(
  82.                               &$dbt_obj,
  83.                               $name          "DP_Form",
  84.                               $onSubmitOrder "",
  85.                               $targetFile    "",
  86.                               $method        "POST"
  87.                            )
  88.     {
  89.       $this->dbt_obj = $dbt_obj;
  90.       $this->HTMLForm(
  91.                         $name,
  92.                         $onSubmitOrder,
  93.                         $targetFile,
  94.                         $method 
  95.                      );
  96.     }
  97.  
  98.  
  99.  
  100.     /**
  101.      * Sets up the databasetable by using the information of the form.
  102.      *
  103.      * @version 1.0
  104.      * @since   0.1.7
  105.      * @author  Daniel Plücken <daniel@debakel.net>
  106.      * @access  public
  107.      * @return  void 
  108.      */
  109.     function setupDBT()
  110.     {
  111.       $sqlQueryString "ALTER TABLE ".$this->dbt_obj->getName()." ";
  112.       
  113.       $alreadyAddedCols "";
  114.       for$i 0$i count$this->formItems )$i++ )
  115.       {
  116.          if(
  117.              strtolower(
  118.                     get_class$this->formItems[$i)
  119.                        != "htmlsubmitbutton"
  120.           && strtolower(
  121.                     get_class$this->formItems[$i)
  122.                        != "htmlbutton"
  123.            )
  124.          {
  125.            if(
  126.                strpos(
  127.                        $alreadyAddedCols,
  128.                        $this->formItems[$i]->getName()
  129.                      === false
  130.              )
  131.            {
  132.               switchstrtolowerget_class$this->formItems[$i) ) )
  133.               {
  134.                 case "htmltextinput":
  135.                      $sqlQueryString .= "ADD ".$this->formItems[$i]
  136.                                       ->getName()." VARCHAR( 255 ) NOT NULL, ";
  137.                      break;
  138.                 
  139.                 case "htmlpasswordinput":
  140.                      $sqlQueryString .= "ADD ".$this->formItems[$i]
  141.                                       ->getName()." VARCHAR( 20 ) NOT NULL, ";
  142.                      break;
  143.                 
  144.                 case "htmltextarea":
  145.                      $sqlQueryString .= "ADD ".$this->formItems[$i]
  146.                                       ->getName()." TEXT NOT NULL, ";
  147.                      break;
  148.                 
  149.                 case "htmlcheckbox":
  150.                      $sqlQueryString .= "ADD ".$this->formItems[$i]
  151.                                       ->getName()." INT( 1 ) NOT NULL, ";
  152.                      break;
  153.                 
  154.                 case "htmlselect":
  155.                      $sqlQueryString .= "ADD ".$this->formItems[$i]
  156.                                       ->getName()."` VARCHAR( 150 ) NOT NULL, ";
  157.                      break;
  158.                 
  159.                 case "fileselect":
  160.                      $sqlQueryString .= "ADD ".$this->formItems[$i]
  161.                                       ->getName()." VARCHAR( 150 ) NOT NULL, ";
  162.                      break;
  163.                 
  164.                 case "htmlradiogroup":
  165.                      $sqlQueryString .= "ADD ".$this->formItems[$i]
  166.                                       ->getName()." VARCHAR( 150 ) NOT NULL, ";
  167.                      break;
  168.                 
  169.                 case "dateselect":
  170.                      $sqlQueryString .= "ADD ".$this->formItems[$i]
  171.                                       ->getName()." INT( 15 ) NOT NULL, ";
  172.               }
  173.               
  174.               $alreadyAddedCols .= $this->formItems[$i]->getName();
  175.             }
  176.          }
  177.       }
  178.       // delete last comma
  179.       echo $sqlQueryString substr(
  180.                                       $sqlQueryString0,
  181.                                       strlen$sqlQueryString 2
  182.                                    ).";";
  183.       
  184.       $result $this->dbt_obj->parent->query$sqlQueryString );
  185.       
  186.       return $result;
  187.     }
  188.  
  189.  
  190.  
  191.     /**
  192.      * Returns a single dataset as an array if the query could be execute.
  193.      *
  194.      * @version 1.0
  195.      * @since   0.1.4
  196.      * @author  Daniel Plücken <daniel@debakel.net>
  197.      * @access  public
  198.      * @param   string  $where 
  199.      * @return  array 
  200.      */
  201.     function getSingleDataset$where )
  202.     return $this->dbt_obj->getSingleDataset$where )}
  203.  
  204.  
  205.  
  206.     /**
  207.      * Returns the datasets as an two dimensional array if the query could be
  208.      * execute. The first dimension carries the datasets. The second dimension
  209.      * carries the fields's values of the result list.
  210.      *
  211.      * Usage:
  212.      *          getDatasetsWithFields( $fieldnamesArr, $where );
  213.      * Or:
  214.      *          getDatasetsWithFields( $where );
  215.      *
  216.      * @version 1.0
  217.      * @since   0.1.2
  218.      * @author  Daniel Plücken <daniel@debakel.net>
  219.      * @access  public
  220.      * @param   array  $fieldnamesArr  The fields that should be in the result
  221.      *                                  list.
  222.      * @param   string $where          The where clause of the query.
  223.      * @return  array 
  224.      */
  225.     function getDatasetsWithFields$fieldnamesArr$where "" )
  226.     return $this->dbt_obj->getDatasetsWithFields$fieldnamesArr$where )}
  227.  
  228.  
  229.  
  230.     /**
  231.      * Returns the table's fieldnames.
  232.      *
  233.      * @version 1.0
  234.      * @since   0.1.2
  235.      * @author  Daniel Plücken <daniel@debakel.net>
  236.      * @access  public
  237.      * @return  array 
  238.      */
  239.     function getDBTFieldNames()
  240.     return $this->dbt_obj->getFieldNames()}
  241.  
  242.  
  243.  
  244.     /**
  245.      * Returns the fieldnames and values of the form by a two dimensional array.
  246.      *
  247.      * @version 1.5
  248.      * @since   0.1.4
  249.      * @author  Daniel Plücken <daniel@debakel.net>
  250.      * @access  public
  251.      * @return  array 
  252.      */
  253.     function getFieldsAndValues()
  254.     {
  255.       $tmp_arr $this->dbt_obj->getRealFieldnames();
  256.       return parent::getFieldsAndValues$tmp_arr );
  257.     }
  258.  
  259.  
  260.  
  261.     /**
  262.      * Updates the gotten Dataset.
  263.      *
  264.      * @version 1.3
  265.      * @since   0.1.4
  266.      * @author  Daniel Plücken <daniel@debakel.net>
  267.      * @access  public
  268.      * @param   $where The where-definition to the dataset.
  269.      * @return  mixed 
  270.      */
  271.     function updateDBT$where "" )
  272.     {
  273.       $tmp_vars $this->getFieldsAndValues();
  274.       
  275.       if empty$where ) )
  276.       {
  277.          $p_key =$this->dbt_obj->getPrimaryKey;
  278.          $where "";
  279.          $and "";
  280.          foreach $p_key as $tmp )
  281.          {
  282.            $where .= $and.$tmp." = '".max(
  283.                                            $_GET[$tmp],
  284.                                            $_POST[$tmp],
  285.                                            $_REQUEST[$tmp]
  286.                                          )."'";
  287.            $and " AND ";
  288.          }
  289.       }
  290.       
  291.       $result $this->dbt_obj->update(
  292.                                         $tmp_vars["fields"],
  293.                                         $tmp_vars["values"],
  294.                                         $where 
  295.                                       );
  296.       return $result;
  297.     }
  298.  
  299.  
  300.  
  301.     /**
  302.      * Inserts a Dataset into the specified database's table.
  303.      *
  304.      * @version 1.2
  305.      * @since   0.1.4
  306.      * @author  Daniel Plücken <daniel@debakel.net>
  307.      * @access  public
  308.      * @return  integer|boolean
  309.      */
  310.     function insertIntoDBT()
  311.     {
  312.       $GLOBALS["insert_id"0;
  313.       $tmp_vars $this->getFieldsAndValues();
  314.       $result $this->dbt_obj->insert(
  315.                                         $tmp_vars["fields"],
  316.                                         $tmp_vars["values"]
  317.                                       );
  318.       
  319.       $this->addHiddenFormItem"id"$GLOBALS["insert_id");
  320.       
  321.       if!empty$GLOBALS["insert_id") )
  322.         return $GLOBALS["insert_id"];
  323.       else
  324.         return $result;
  325.     }
  326.  
  327.  
  328.  
  329.     /**
  330.      * Replaces a Dataset into the specified database's table.
  331.      *
  332.      * @version 1.0
  333.      * @since   0.2.5
  334.      * @author  Daniel Plücken <daniel@debakel.net>
  335.      * @access  public
  336.      * @return  mixed 
  337.      */
  338.     function replaceIntoDBT()
  339.     {
  340.       $tmp_vars $this->getFieldsAndValues();
  341.       $result $this->dbt_obj->replace(
  342.                                          $tmp_vars["fields"],
  343.                                          $tmp_vars["values"]
  344.                                        );
  345.       
  346.       $_REQUEST["id"@mysql_insert_id();
  347.       $this->addHiddenFormItem"id"$_REQUEST["id");
  348.       
  349.       if!empty$_REQUEST["id") )
  350.         return $_REQUEST["id"];
  351.       else
  352.         return $result;
  353.     }
  354.  
  355.  
  356.  
  357.    /**
  358.     * Generates the sourcecode to build this object and returns it.
  359.     *
  360.     * @version 1.0
  361.     * @since   0.1.2
  362.     * @author  Daniel Plücken <daniel@debakel.net>
  363.     * @access  public
  364.     * @return  string 
  365.     */
  366.    function getPHPSource()
  367.    {
  368.       // setting Layouttable if exists.
  369.       if!empty$this->layoutTable ) )
  370.         $out $this->layoutTable->getPHPSource()."\r\n\r\n";
  371.       
  372.       for$i 0$i count$this->formItems )$i++ )
  373.         $out .= $this->formItems[$i]->getPHPSource()."\r\n\r\n";
  374.       
  375.       // $out .= $this->dbt_obj->getItemPHPSource();
  376.       
  377.       $out .= "\x24formname      = \"".$this->getName()."\";\r\n";
  378.       $out .= !empty$this->onSubmitOrder )
  379.               ? "\x24onSubmitOrder = \"".$this->getSubmitOrder()."\";\r\n"
  380.               : "";
  381.       $out .= !empty$this->targetFile )
  382.               ? "\x24targetFile    = \"".$this->getTargetFile()."\";\r\n"
  383.               : "";
  384.       $out .= !empty$this->method )
  385.               ? "\x24method        = \"".$this->getMethod()."\";\r\n"
  386.               : "";
  387.       
  388.       $out .= "\x24form = new DBTfillingForm(\r\n"
  389.                    ."                             \x24dbt,\r\n"
  390.                    ."                             \x24formname,\r\n"
  391.                    ."                             \x24onSubmitOrder,\r\n"
  392.                    ."                             \x24targetFile,\r\n"
  393.                    ."                             \x24method \r\n"
  394.                    ."                          );\r\n\r\n";
  395.       $out .= $this->getItemPHPSource();
  396.       
  397.       $out .= "\x24error = false;\r\n";
  398.       $out .= "if( !empty( \x24formsent ) )\r\n";
  399.       $out .= "{\r\n";
  400.       $out .= "   if( empty( \x24_REQUEST[\"id\"] ) )\r\n";
  401.       $out .= "     if( \x24id = \x24form -> insertIntoDBT() )\r\n";
  402.       $out .= "       \x24err_msg = \"<font color=\\\"green\\\">"
  403.                                     ."Erfolgreich eingetragen.</font>\";\r\n";
  404.       $out .= "     else\r\n";
  405.       $out .= "     {\r\n";
  406.       $out .= "       \x24error = true;\r\n";
  407.       $out .= "       \x24err_msg = \"<font color=\\\"#E10831\\\">"
  408.                            ."Leider ist ein Fehler aufgtreten.<br>"
  409.                            ."Bitte versuchen Sie es erneut.</font>\";\r\n";
  410.       $out .= "     }\r\n\r\n";
  411.       $out .= "   if( !empty( \x24_REQUEST[\"id\"] ) )\r\n";
  412.       $out .= "     if( "
  413.                       ."\x24form->updateDBT( "
  414.                                   ."\"id = '\".\x24_REQUEST[\"id\"].\"'\" "
  415.                                          .") "
  416.                     .")\r\n";
  417.       $out .= "       \x24err_msg = \"<font color=\\\"green\\\">"
  418.                                     ."Erfolgreich eingetragen.</font>\";\r\n";
  419.       $out .= "     else\r\n";
  420.       $out .= "     {\r\n";
  421.       $out .= "       \x24error = true;\r\n";
  422.       $out .= "       \x24err_msg = \"<font color=\\\"#E10831\\\">"
  423.                             ."Leider ist ein Fehler aufgtreten.<br>"
  424.                             ."Bitte versuchen Sie es erneut.</font>\";\r\n";
  425.       $out .= "     }\r\n\r\n";
  426.       $out .= "}\r\n";
  427.       $out .= "\x24form->addHiddenFormItem( "
  428.                         ."\"id\", \x24_REQUEST[\"id\"] "
  429.                                         .");\r\n\r\n";
  430.       
  431.       return $out;
  432.    }
  433. // End of class DBTfillingForm
  434. ?>

Documentation generated on Thu, 05 Jun 2008 19:11:09 +0200 by phpDocumentor 1.4.1