Login   Register  
PHP Classes
elePHPant
Icontem

File: form.inc

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Jelte Werkhoven  >  Form and SQL generator, CSS compatible  >  form.inc  >  Download  
File: form.inc
Role: ???
Content type: text/plain
Description: This is the main body of the class.
Class: Form and SQL generator, CSS compatible
Author: By
Last change:
Date: 13 years ago
Size: 9,812 bytes
 

Contents

Class file image Download
<?
/*versie 0.9beta
19-4-01 Copyright Drs. Jelte 'YeeHaW' Werkhoven
This is free to use as long as you keep the comments (and of course my name =) ) in place.

This class is meant to be used to quickly make content managementsystems with PHP/MySQL. It'll do a couple of things:

1: It'll create a RawHTML variable containing all the form elementtypes you called. Each form element has it's own CSS identifier
2: When the form is submitted, it'll construct a SQL statement that you can directly feed to your database. I've tested this with MySQL, but my 
   guess is it should work with PostgreSQL too. It isn't ANSI-SQL though. Use the SQLInsert or the SQLUpdate method to generate SQL.
3: It'll automatically generate a hidden field, which will keep track of which of the textfields/textareas had the focus last. I used this to allow 
   users to automatically add pictures and tags to the textfield/-area they were working on.
4: It'll also create snippets of JavaScript for some form elements. E.g. I sometimes like my selectboxes to automatically submit if changed.
5: There's a method AddHTML, which'll allow you to add pieces of HTML in the formbody.

When using this, don't forget to print the RawHTML object, and the JavaScript object in the calling script, otherwise it won't show anything ;-)

That's basically it. By flicking ShowVal, one of the parameters of the StartForm method on and off, you can control if the selected values are shown or not. I did this because most users don't like their values still in the form fields once they've submitted, but the form has to be called after submitting, otherwise the SQL won't be generated.

There's also an extension, formextra.inc, for this class, which'll allow you to use my custom multiple selectboxes.
*/




class Form
{

var $RawHTML;
var $SQL;
var $ShowValues;
var $JavaScript;

	 
	



function AddHTML($HTML, $Label = "", $Class = "")
	{
	 	 
	 

	 if ($Label != "")
		{
		 $this -> RawHTML .= "<div class='$Class' id='$Label'>\n";
		 $this -> RawHTML .= $HTML."\n";
		 $this -> RawHTML .= "</div>\n";
		}
	 else
		{
		 $this -> RawHTML .= $HTML."\n";
		}
	}

## Use ShowVal to select whether or not to show the values in the form-elements. 
function StartForm($Name, $Action, $ShowVal="0", $Type="multipart/form-data", $Target="")
	{
	 $this -> ShowValues = $ShowVal;
	 $this -> RawHTML .= "<form name='$Name' enctype='$Type' action='$Action' target='$Target' method='post'>\n";
	 $this -> RawHTML .= "<input type='hidden' name='LastFocus' value=''>\n";  
	 
	}

## FocusTrack controls whether or not it's recorded if this textfield gets focus in the LastFocus hidden field.
function TextField($Label, $Value="", $Size="60", $Name="", $FocusTrack="1")

	{
	 if ($Name=="")
		{
		 $Name = $Label;
		}
	 if ($this -> ShowValues == 1)
		{
		 $ValStr = "value='".htmlspecialchars($Value)."'";
		}

	 if ($FocusTrack == 1)
		{
		 $FocusJS = "onFocus='top.buttons.document.Controller.LastForm.value=this.form.name;top.buttons.document.Controller.LastElement.value=this.name;'";
		}
	 
	 $this -> RawHTML .= "<div id='$Label'>\n
			<input type='text' name='$Name' size='$Size' $ValStr $FocusJS>\n
		     </div>\n";
	 
	 $this -> SQL .= ", ".$Name." = '".$Value."'";
	}

function TextArea($Label, $Value="", $Rows="10", $Cols="90", $Name="", $FocusTrack="1")
	{
	 if ($Name=="")
		{
		 $Name = $Label;
		}

	 if ($this -> ShowValues == 1)
		{
		 $ValStr = ereg_replace("<br>","", stripslashes($Value));
		}
	 
	 if ($FocusTrack == 1)
		{
		 $FocusJS = "onFocus='top.buttons.document.Controller.LastForm.value=this.form.name;top.buttons.document.Controller.LastElement.value=this.name;'";
		}
	 
	 $this -> RawHTML .= "<div id='$Label'>\n
			<textarea name='$Name' wrap='soft' rows='$Rows' cols='$Cols' $FocusJS>\n".$ValStr."</textarea>\n
		     </div>\n";

	 $this -> SQL .= ", ".$Name." = '".nl2br($Value)."'"; 
	}

## Values are the value/text pairs you want to be the options. Use an array with the optionvalue as key and optiontext as value
## If JSSubmit is set to 1, the Selectbox will cause the form to be submitted when changed
function SelectBox($Label, $Values="", $SelValue="", $JSSubmit="", $Name="")	
	{
	 if ($Name=="")
		{
		 $Name = $Label;
		}

	 if ($JSSubmit == 1)
		{
		 $JSSubmit = "onChange='this.form.submit()'";
		}

	 if ($this -> ShowValues == 1)
		{
		 $ValStr = "selected";
		}

	 $this -> RawHTML .= "<div id='$Label'>\n
		     <select name='$Name' $JSSubmit>\n";
			
			foreach($Values as $Value)
				{
				 if (key($Values) == $SelValue)
					{
					 $this -> RawHTML .= "<option value='".key($Values)."' $ValStr>$Value";
					}
				 else
					{
					 $this -> RawHTML .= "<option value='".key($Values)."'>$Value";
					}
				 next($Values);
				}
	
	 $this -> RawHTML .= "</select></div>\n";

	 $this -> SQL .= ", ".$Name." = '".$SelValue."'";
	}





function CheckBox($Label, $SelValue="", $Name="")
	{
	 if ($Name == "")
		{
		 $Name = $Label;
		}
	
	 $this -> RawHTML .= "<div id='$Label'>\n";
	 
	 if ($this -> ShowValues == 1)
		{
		 $ValStr = "checked";
		}
	 
	 if ($SelValue)
		{ 
		 $this -> RawHTML .= "<input type='checkbox' name='$Name' value='1' $ValStr>\n";
		}
	 else
		{
		 $this -> RawHTML .= "<input type='checkbox' name='$Name' value='1'>\n";
		}

	 $this -> RawHTML .= "</div>";
				 
	 $this -> SQL .= ", ".$Name." = '".$SelValue."'";
	}
	
	
## Here again, the JSScript causes the form to be submitted when the selected radiobutton is changed
function RadioButtons($Label, $Values="", $SelValue="", $JSScript="", $Name="")
	{
	 if ($Name == "")
		{
		 $Name = $Label;
		}

	 if ($JSScript == 1)
		{
		 $JSScript = "onClick='this.form.submit()'";
		}

	 if ($this -> ShowValues == 1)
		{
		 $ValStr = "checked";
		}


	 foreach ($Values as $Value)
		{
		 $x++;
		 if ($Value == $SelValue)
			{
			 $this -> RawHTML .= 
				"<input type='radio' name='$Name' value='$Value' $JSScript $ValStr>";
			}
		 else
			{
			 $this -> RawHTML .= 
				"<input type='radio' name='$Name' value='$Value' $JSScript>";
			}
	 	}
	 

	 $this -> SQL .= ", ".$Name." = '".$SelValue."'";
	}

/* You need to do some tricky stuff to get this datefield to work:
   When then form is submitted, you'll have to concentate the Year, Month and Day values, and call 'em $Value to get it to work
*/
function DateField($Label, $Value="", $Name="")
	{
	 
	 
	 if ($Value != "" && $this -> ShowValues == 1)
		{
		 $ValueDay = substr($Value, 6, 2);
		 $ValueMonth = substr($Value, 4, 2);
	 	 $ValueYear = substr($Value, 0, 4);
		}
	 

	 if ($Name=="")
		{
		 $Name = $Label;
		}

	 $this -> RawHTML .= "<select id='".$Label."day' name='Day'>\n";
	 
	 for ($Day = 1; $Day < 32; $Day++)
		{
		 $Day = str_pad($Day, 2, "0", STR_PAD_LEFT);
		 if ($Day == $ValueDay)
			{
			 $this -> RawHTML .= "<option value='$Day' selected>".$Day."\n";
			}
		 else
			{
			 $this -> RawHTML .= "<option value='$Day'>".$Day."\n";
			}
		}
         $this -> RawHTML .= "</select>\n";

	 



	
	 $this -> RawHTML .= "<select id='".$Label."month' name='Month'>\n";
	
	 for ($Month = 1; $Month < 13; $Month++)
		{
	 	 $Month = str_pad($Month, 2, "0", STR_PAD_LEFT);
		 if ($Month == $ValueMonth)
			{
			 $this -> RawHTML .= "<option value='$Month' selected>".$Month."\n";
			}
		 else
			{
			 $this -> RawHTML .= "<option value='$Month'>".$Month."\n";
			}
		}
	 $this -> RawHTML .= "</select>\n";



	
	 $this -> RawHTML .= "<select id='".$Label."year' name='Year'>\n";

	 for ($Year = 2000; $Year < 2004; $Year++)
		{
		 if ($Year == $ValueYear)
			{
			 $this -> RawHTML .= "<option value='$Year' selected>".$Year."\n";
			}
		 else
			{
			 $this -> RawHTML .= "<option value='$Year'>".$Year."\n";
			}
		}
	 
	 
	 $this -> RawHTML .= "</select>\n";
	
	 
	 $this -> SQL .= ", ".$Name."= '".$Value."'";
	 
	}

## This'll generate a upload form field
function Uploadable($Label, $Bestand, $Maxfilesize="50000", $Name="")
	{
	 if ($Name=="")
		{
		 $Name = $Label;
		}
	
	 if ($this -> ShowValues == 1)
		{
		 $BestandStr = $Bestand;
		}
	
	 $this -> RawHTML .= "<input type='Hidden' name='MAX_FILE_SIZE' value='$Maxfilesize'>\n
				<div id='$Label'>\n
				<input type='file' name='$Name'>\nNu: $BestandStr\n
				</div>\n";

	 $this -> SQL .= ", ".$Name."= '".$Bestand."'";
	}


function Hidden($Name, $Value)
	{
	 $this -> RawHTML .= "<input type='Hidden' name='$Name' value='$Value'>";

	 $this -> SQL .= ", ".$Name."= '".$Value."'";
	}



function Reset($Label, $Value)
	{
	 $this -> RawHTML .= "<div id='$Label'>\n
				<input type='Reset' value='$Value'>\n
				</div>\n";
	}
			



function Submit($Label, $Value, $Name="")
	{
	 if ($Name == "")
		{
		 $Name = "Submit";
		}

	 $this -> RawHTML .= "<div id='$Label'>\n
				<input type='Submit' Name='$Name' value='$Value'>\n
				</div>\n";
	}

function EndForm()
	{
	 $this -> RawHTML .= "</form>\n";
	
	 
	}

## These two last functions take the tablename as parameter, and Update needs an table idnumber, and they'll output the line of SQL needed to
## put the entered values into the database.
function SQLInsert($Table, $id="")
	{
	 if ($id != "")
		{
		 $this -> SQL .= ", id = ".$id;
		}
	 $this -> SQL = substr_replace($this -> SQL, "SET ", 0 , 1);
	 $this -> SQL = "INSERT INTO $Table ".$this -> SQL;
	}


function SQLUpdate($Table, $id)
	{
	 $this -> SQL = substr_replace($this -> SQL, "SET ", 0 , 1);
	 $this -> SQL = "UPDATE $Table ".$this -> SQL." WHERE id=$id";
	}

}

###That's it. Enjoy!
?>