Location: PHPKode > scripts > autoformBQ > autoformbq/autoformBQ.php
<?php
/* Do not remove or alter this section***************************


************************Class Description************************
the autoformBQ-object provides a dynamic generated form to add or edit
records to mySql-Databases. It is possible to adapt the form-layout by CSS
and several boolean members.( s.b.)

Over and above that, you are able to generate static forms to use at your Site
via http-download.
See the use of function header(...) at "create_static_form_sample.php"
(for detailed information read the file autoformBQ.doc)
*****************************************************************


************************ sorry and thx ***************************
Please excuse errors in this text. English isnt my native language,
and so  suggestions about the code and the spelling are welcome

*********************Contact and Bug report***********************
 uwe nodot stein at gmx dot de
******************************************************************


********************Licence****************************************
Copyright © 2005, Uwe Stein
All rights reserved.

Free for non-comercial use, please contact me for comercial use

Redistribution and use in source and binary forms, with or without modification, are permitted provided
that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list of conditions and the
following disclaimer.

Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
the following disclaimer in the documentation and/or other materials provided with the distribution.

Neither the name of the ActiveIntra.net nor the names of its contributors may be used to endorse or
promote products derived from this software without specific prior written permission.

THIS SOFTWARE
IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.

***************************************************************

**************End of do not remove or alter section*************************/




 require_once("class_field.php");
// autoformB(y)Q(uery)
class autoformBQ
{
    var $strName	 = "";          // the form-name
	var $strMethod   = "POST";      // the form-method get|post
	var $strAction   = "";          // the action-file
	var $strHeadline = "";          // your Text at the top, if needed
	var $strFootline = "";          // your Text at the bottom, if needed
    var $aFields     = array();     // the field-objekt array
	var $iFields     = 0;           // the number of columns in your query-result
	var $iConnID     = 0;           // mySql-connection
    var $aResult;                  // the mySql-Result 2 build the form
	var $bFormEdit  = FALSE;       // if FALSE, the Form is used to insert
	                               // (a) new record(s) into your DB
								   // if TRUE, it is used to edit and it
								   // shows the current content of your record(s)
    var $bShowDateTimeInput    = FALSE;  // if TRUE, a date-input-element is shown, use func date_input to set to true
	var $bShowPatternAsHint = FALSE; // if TRUE, the format-pattern is shown as hint
	var $bShowPrecisionAsHint = FALSE; // shows precision and decimals as hint
	var $bShowPatternAsDefault = FALSE;
	var $bMarkRequiredInput    = FALSE; // shows the $strSignInputRequired ( see lang.inc )
	var $strCssClass = "";          // to define a css-class use func css_class
	var $strEnumStyle = "radio";    // enum-columns appear as radiobuttons
	var $strSetStyle  = "select";   // set-columns appear as checkboxes
	var $strLang = "en";                 // defines the default-language
	var $iMaxSelectSize = 5;			// max number of items in a "sized" select-tag (only enum, set is always swhow as "sized" select)
										// if there are more, its shown with size 1

	var $iSmallTextareaRows   =  3;     // default values
	var $iSmallTextareaCols   = 25;     // Small-Textarea is used for varchar
    var $iTextareaRows        =  7;     //
	var $iTextareaCols        = 25;     // Textarea is used for Text/String-Blob
    var $iTextareaStartLen    = 50;     // This maxlen or more: Textarea is used, otherwise text-input
	var $iTextInputSize       = 25;     // Text-input is displayen in this size

function autoformBQ($iConnID, $result, $strAction,$bEdit= FALSE)
{
    // save params
   $this->bFormEdit = $bEdit;
   $this->iConnID   = $iConnID;
   $this->iFields   = mysql_num_fields($result);
   $this->strAction = $strAction;

   // create the field-objects
   for ($i = 0; $i < $this->iFields; $i++)
   {
       $this->aFields[] = new field($iConnID, $result, $i);
   }
}


// shows the form
function print_form()
{
// build the inc-file-name
  $strIncFile = "autoformBQ_lang_".$this->strLang.".inc.php";
  // include it
  include($strIncFile);

   // start of form-tag
  echo "<form method=\"".$this->strMethod."\" action=\"".$this->strAction."\" ";
  if ( strlen($this->strName))
     echo "name =  \"".$this->strName."\" ";
  if ( strlen($this->strCssClass))
     echo "class = \"".$this->strCssClass."\" ";
  echo " > \n";

  // create a table to arrange the form-elements
  echo "<table  cellspacing=\"2\" cellpadding=\"5\" bgcolor=\"lightgray\" border= \"1\" width=\"80%\"";
  if ( strlen($this->strCssClass))
     echo "class = \"".$this->strCssClass."\" ";
  echo ">\n";

  // print the headline if defined
  if ( strlen($this->strHeadline))
  {
     echo "<tr><td colspan=\"2\">\n";
     echo $this->strHeadline;
	 echo "\n</td></tr>\n";
  }

  // walk through the array of form-elements
  for ($i = 0; $i < $this->iFields; $i++)
  {
      // create a hidden-tag to submit the form-type edit = TRUE|FALSE
	  echo " <input type=\"hidden\"";
		   echo " name=\"FORMTYPE\"";
		   echo " value=\"";
		   ($this->bFormEdit )? print("EDIT"):print("NEW");
		   echo "\">\n";


	 // create the input-tag-name  tablename.fieldname.type
	 $fieldname = $this->aFields[$i]->strTableName.","
	             .$this->aFields[$i]->strName.","
				 .$this->aFields[$i]->strType.","
				 .$this->aFields[$i]->bitFlags;

	 if ($this->aFields[$i]->bitFlags & BINARY)
	    die("Databases with BINARY-fields are not supported");

	 // dont show a auto-increment-field
	 // but if the form is a edit-form, tell the content
	 if ($this->aFields[$i]->bitFlags & AUTO_INCREMENT)
	 {
        // if it is a "edit-form", send the content
		if ($this->bFormEdit)
		{
           echo " <input type=\"hidden\"";
		   echo " name=\"$fieldname,HIDDEN\"";
		   echo " value=\"".$this->aFields[$i]->strContent."\">\n";
		}
		continue;
	 }

     // type date,time,year,datetime ?  show it ?
	 $tmp = $this->aFields[$i]->strType;
	 if ( preg_match("/(date|datetime|time|year)/", $tmp))
	 {
        if ( $this->bShowDateTimeInput == FALSE)
        {
           if ($this->bFormEdit == TRUE)
		      $content = $this->aFields[$i]->strContent;
		   else
		      $content = "EMPTY";

		   echo "<input type=\"hidden\" ";
		   echo "name=\"$fieldname,HIDDEN\" ";
		   echo "value=\"$content\">\n";
		   continue;
        }
     }
	 // timestamp: it is assumed that timestamp in not_null and it will be automatically inserted
	 if ($this->aFields[$i]->strType == "timestamp")
	 {
		   echo "<input type=\"hidden\" ";
		   echo "name=\"$fieldname,HIDDEN\" ";
		   echo "value=\"\">\n";
		   continue;
     }
	     //continue;

      // left column the field-inscription
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	  echo "<tr><td>\n";

	  echo ucfirst($this->aFields[$i]->strName);

      // mark not_null columns

	  if ($this->bMarkRequiredInput)
	     if ($this->aFields[$i]->bitFlags & NOT_NULL)
	         echo $strSignInputRequired;

      // Pattern-hint ?
	  if ( $this->bShowPatternAsHint)
	  {
	     if ( strlen ( $this->aFields[$i]->strFormatPattern))
		 {
             if (preg_match("/(double|real|time|date|year|datetime)/"
			                 ,$this->aFields[$i]->strType))
			 {
			 	echo "\n<font size=\"-1\">";
			    echo "&nbsp;(&nbsp;".$this->aFields[$i]->strFormatPattern."&nbsp;)";
			    echo "</font>\n";
			 }
		 }
	  }

	  // precision hint ?
      if ( $this->bShowPrecisionAsHint)
	  {
         $c = count( $this->aFields[$i]->aPrecision);
		 if ($c)
		 {
		     if (eregi("(double|real)",$this->aFields[$i]->strType))
			 {
			 	echo "\n<font size=\"-1\">";
                echo "&nbsp;(&nbsp;".$this->aFields[$i]->aPrecision[0]." $strPrintPrecision";
				echo " + ".$this->aFields[$i]->aPrecision[1]." $strPrintDecimals&nbsp;)";
			    echo "</font>\n";
			 }
		 }
	  }
	  echo "</td>\n";
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

	  // right column with input-tags
	  echo "<td>\n";
      // the form-tag-name
	  $name = $this->aFields[$i]->strTableName.".".$this->aFields[$i]->strName;

      // enum-field ?
	  $set_enum_counter = 0; // needed to check for enum|set again s.b.
	  if ($this->aFields[$i]->bitFlags & ENUM)
	  {
          $set_enum_counter++;
		  $cPV = count($this->aFields[$i]->aPossibleValues);

		  // toCompare needed to decide which item is checked
		  // if it is a edit-form, it holds the current field-content
		  // else the default-value( kept in formatpattern )
		  if ($this->bFormEdit == FALSE)
             $toCompare = $this->aFields[$i]->strFormatPattern;
		  else
		     $toCompare= $this->aFields[$i]->strContent;


		  if ($this->strEnumStyle == "radio")
		  {
			 for ($j=0; $j < $cPV; $j++)
			 {
			    $val = $this->aFields[$i]->aPossibleValues[$j];
			    echo  "\n<input "
				      ."name=\"$fieldname\" "
				     ."type = \"radio\" "
					 ."value =\"".$val."\" ";
					 if ( $val == $toCompare)
					     echo " checked ";
				     echo " >";
					 echo $val."&nbsp;&nbsp;\n";
			 }
		  } // end output enumstyle radio

		  if ($this->strEnumStyle == "select")
		  {
			 echo  "<select name=\"$fieldname\" ";
			 if ($cPV <= $this->iMaxSelectSize)
			     echo " size=\"$cPV\"";
			 echo " >\n";
			 for ($j=0; $j < $cPV; $j++)
			 {
				$val = $this->aFields[$i]->aPossibleValues[$j];
				echo "<option ";
				if ( $val == $toCompare)
				   echo " selected ";
				echo "value=\"$val\">$val</option>\n";
			 }
			 echo "</select>\n";
		  } // end output enumstyle select
	  } // end output enum

//++++++++++++++++++++++++++++++++++
      // set-field
      if ($this->aFields[$i]->bitFlags & SET)
	  {
          $set_enum_counter++;
		  $cPV = count($this->aFields[$i]->aPossibleValues);

		  // toCompare needed to decide which item is checked
		  // if it is a edit-form, it holds the current field-content
		  // else the default-value( kept in formatpattern )
		  if ($this->bFormEdit == FALSE)
             $toCompare = $this->aFields[$i]->aPossibleValues[0];
		  else
		     $toCompare= $this->aFields[$i]->strContent;


		  if ($this->strSetStyle == "checkbox")
		  {
		     $fieldname .="[]";
			 for ($j=0; $j < $cPV; $j++)
			 {
				$val = $this->aFields[$i]->aPossibleValues[$j];
			    echo  "<input "
				      ."name=\"$fieldname\" "
				     ."type = \"checkbox\" "
					 ."value =\"".$val."\" ";
					 if ( eregi($val,$toCompare))
					     echo " checked ";
				     echo " >";
					 echo $val."&nbsp;&nbsp;\n";
			 }
		  } // end output setstyle radio

		  if ($this->strSetStyle == "select")
		  {
             $fieldname .="[]";
			 echo  "<select name=\"$fieldname\" multiple=\"multiple\" >\n";
			 for ($j=0; $j < $cPV; $j++)
			 {
				$val = $this->aFields[$i]->aPossibleValues[$j];
				echo "<option ";
				if ( eregi($val, $toCompare))
				   echo " selected ";
				echo "value =\"$val\" >$val</option>\n";
			 }
			 echo "</select>\n";
		  } // end output setstyle select
	  } // end output set



// ++++++++++++++++++++++++++++++++++++
    // other fields
	if ( (eregi("string|int|real|date|time|datetime|year|text|blob",$this->aFields[$i]->strType))
	      && $set_enum_counter==0 )
	{
       // Textarea  or textinput ?
	   if ( $this->aFields[$i]->iMaxLen > $this->iTextareaStartLen )
	   {
	       $tagtype = "Textarea";
	      // small or big Textarea ?
		  if ( $this->aFields[$i]->iMaxLen > 255)
		  {
              $rows = $this->iTextareaRows;
			  $cols = $this->iTextareaCols;
		  }
		  else
		  {
              $rows = $this->iSmallTextareaRows;
			  $cols = $this->iSmallTextareaCols;
		  }
	  }
      else
		$tagtype = "text";

	  // manage the tag-content

	  // is it a edit-form ?
	  if ($this->bFormEdit)
	     $tagcontent = $this->aFields[$i]->strContent;
	  else
	  {
           if ($this->bShowPatternAsDefault)
		      $tagcontent = $this->aFields[$i]->strFormatPattern;
           else
		      $tagcontent = "";

	  }

	  // show Text-Input
	  // calc maxlen
	  $maxlen = $this->aFields[$i]->iMaxLen;
	  // special handling of real-Types
	  // maxlen = precision + decimals + 1( the dot)
	  if( $this->aFields[$i]->strType == "real")
	  {
         $maxlen = 0;
		 $cp = count($this->aFields[$i]->aPrecision);
         // $cp might also be 1 ( nonsense, but possible )
		 // so we have to count the array
		 for ( $q = 0; $q < $cp; $q++)
		 {
            $maxlen += $this->aFields[$i]->aPrecision[$q];
		 }
		 if ($cp == 2)
		    $maxlen += 1; // the dot
	  }
	  if ($tagtype == "text")
	  {
          echo "<input type=\"text\" name=\"$fieldname\" "
		      ." size=\"".$this->iTextInputSize."\""
			  ." maxlength=\"$maxlen\" "
			  ." value=\"$tagcontent\" >\n";
		  continue;

	  }


	  if ($tagtype == "Textarea")
	  {
          echo "\n<Textarea name=\"$fieldname\" cols=\"$cols\" rows=\"$rows\" $actstring >$tagcontent</Textarea>\n";
	  }
	} // end output other fields



    echo "</td></tr>\n";
  } // End of walk through the elements
  // print the footline if defined
  if ( strlen($this->strFootline))
  {
     echo "<tr><td colspan=\"2\">\n";
     echo $this->strFootline;
	 echo "\n</td></tr>\n";
  }

  // the form-buttons
  echo "<tr><td colspan=\"2\" align=\"center\">\n";
  echo "<input type=\"submit\" name=\"submit\" value=\"".$strSubmitButton."\">";
  echo "&nbsp;&nbsp;&nbsp;\n";
  // in "Edit-Forms" show the restore-button
  if ( $this->bFormEdit)
      echo "<input type=\"submit\" name=\"submit\" value=\"".$strRestoreButton."\">\n";
  else
      echo "<input type=\"reset\" name=\"reset\" value=\"".$strResetButton."\">\n";
  echo "</td></tr>\n";
  echo "</table>\n";
  echo "</form>\n";
}



function smallTextareaSize($rows, $cols)
{
   $this->iSmallTextareaRows = $rows;
   $this->iSmallTextareaCols = $cols;
}

function textareaSize($rows, $cols)
{
   $this->iTextareaRows = $rows;
   $this->iTextareaCols = $cols;
}


function markRequiredInput ( $bool = TRUE)
{
  $this->bMarkRequiredInput = $bool;
}
function showPatternAsDefault ( $bool = TRUE)
{
   $this->bShowPatternAsDefault = $bool;
}

function showPatternAsHint ( $bool = TRUE)
{
   $this->bShowPatternAsHint = $bool;
}

function showPrecisionAsHint($bool = TRUE)
{
    $this->bShowPrecisionAsHint = $bool;
}

function showDateTimeInput($bool = TRUE)
{
   $this->bShowDateTimeInput = $bool;
}

function footline($strfootline)
{
   $this->strFootline = $strfootline;
}

function headline($strHeadline)
{
   $this->strHeadline = $strHeadline;
}

function name($strName)
{
   $this->strName = $strName;
}
function set_style( $type)
{
    if (!preg_match("/(select|checkbox)/",$type))
	{
	   echo "<br><b>Error: use \"select\" or \"checkbox\" when calling func set_enum_style </b>";
       exit;
	}
	$this->strSetStyle = $type;
}

function enum_style( $type)
{
    if (!preg_match("/(radio|select)/",$type))
	{
	   echo "<br><b>Error: use \"radio\" or \"select\" when calling func set_enum_style </b>";
       exit;
	}
	$this->strEnumStyle = $type;
}

function css_class($strCssClass="")
{
   $this->strCssClass = $strCssClass;
}

// change the lang-settings.
// lang-file is included at func print_form()
function lang($strLang)
{
  $this->strLang = $strLang;
}

// change the form-action-file
function action($strAction )
{
   $this->strAction = $strAction;
}

// used during development
// prints all form-data
function print_form_data()
{
  echo "<br><b>Form-Data:</b><br>";
  echo "Action: $this->strAction<br>";
  echo "Method: $this->strMethod<br>";
  echo "EnumStyle: $this->strEnumStyle<br>";
  echo "SetStyle: $this->strSetStyle<br>";
  for ($i = 0; $i < count($this->aFields); $i++)
      $this->aFields[$i]->print_field_values();
}

} // end class afgbmsq

?>

Return current item: autoformBQ