Location: PHPKode > projects > Webgenerator-X content management system > wgx_rc1.5/WG-X/html.php
<?
/*
* This file is part of Webgenerator-X,
* an object oriented website management engine working an top of
* Apache/PHP4/MySQL.
* http://www.webgenerator-x.com
* @2001 REGNI Giorgio
* hide@address.com
*
* Webgenerator-X is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Webgenerator-X is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/*************************************************************************/
/* Somme basic HTML helper functions
   REGNI Giorgio 2001
   Web Generator-X

	08/05/2001 added the dbfrom class
	08/05/2001 security improved in dbform->process: can't add or delete then corresponding php vars are not set
	15/05/2001 function debug which write to the file $debug_file
*/

function build_select_box ($result, $name, $checked_val="xzxz",$other="",$dummyvalue="") {
    /*
        Takes a result set, with the first column being the "id" or value
        and the second column being the text you want displayed

        The second parameter is the name you want assigned to this form element

        The third parameter is optional. Pass the value of the item that should be checked ( compare the id)
		$other is any param who want to add ( ex onclick,...)
		if $dummyvalue: the select will show a dummyvalue at the first option with value dummy and text dummyvalue
    */

	$selectbox = "<select name=\"$name\" $other>";
	if ($dummyvalue!="")
		$selectbox.= "<option value=\"dummy\">$dummyvalue</option>";
	while ( list($id,$name)= mysql_fetch_row( $result) )
	{
		if ($id == $checked_val) {
            $select ='SELECTED';
        }
		else
			$select="";
			
		$selectbox.= "<option value=\"$id\" $select>$name";
		
		$selectbox.="</option>";
	}
	
	$selectbox.="</select>";
	return $selectbox;
}

function build_input_text ($name,$text="",$size="40",$max="120") {
    /*
        will return a form input with value=text and name=name
    */

	return "<input type=\"text\" name=\"$name\" value=\"$text\" size=\"$size\" maxlength=\"$max\">";
}

function build_input_password ($name,$text="",$size="40") {
    /*
        will return a form input with value=text and name=name
    */

	return "<input type=\"password\" name=\"$name\" value=\"$text\" size=\"$size\">";
}

function build_input_hidden ($name,$text) {
    /*
        will return a form input with value=text and name=name
    */

	return "<input type=\"hidden\" name=\"$name\" value=\"$text\">";
}

function build_input_submit ($name,$text) {
    /*
        will return a form input with value=text and name=name
    */

	return "<input type=\"submit\" name=\"$name\" value=\"$text\">";
}

function build_textarea ($name,$text="",$rows="20",$cols="60") {
    /*
        will return a form input with value=text and name=name
    */

	return "<textarea cols=\"$cols\" rows=\"$rows\" name=\"$name\">$text</textarea>";
}

function build_goback () {
    /*
        will return a form input with value=text and name=name
    */

	return "<input type=\"button\" value=\"Go Back\" onclick=\"javascript: history.back();\">";
}

/*
	This class is an utility which create forms form database to ease the administration
	It's inspired form phpDBForm
	It will add $this->pre to all field names to be sure not to conflict with other get or post vars
	
	12/05/01 added the possibility to register a validation function with a field only works with add now
	function register_validation: when content is modified or added, the class calls the validation function
	and if it returns 1 else it returns the error text
	set_Add_Text	// if you want to change the text of the add button
*/
class dbform {
	var $table;					// name of db table in use
	var $fields;				// Form fields
	var $colspan;				// max number of colums in the form table
	var $showadd;				// true if we show the add button
	var $showselectbox;			// true you'll be able to select an element by its selectval and indexed by $selectid
	var $showmodify;			// display a modify button
	var $showdelete;			// display the delete button
	
	var $selectid;
	var $selectval;
	
	var $wherecause;			// you can had a where cause to add to the select list and all database queries like delete and update
								// useful to allow for exemple only users who made an article to delete it
	var $pre="db";				// this will be added to all fields name
	
	var $notvalid;				// used by data validation system
	var $errors;				// error text used by data validation system
	
	var $add_text;
	
	// if $showselectbox = true, you'll be able to select an element by its selectval and indexed by $selectid
	// modify doesn't work if there's no selectbox !!!!!
	// the same for delete !
	function dbform( $table,$colspan="2",$showadd="1",$showmodify="0",$showdelete="0",$showselectbox="0",$selectid="",$selectval="")
	{
		$this->table = $table;
		$this->colspan = $colspan;
		$this->showadd = $showadd;
		$this->showselectbox = $showselectbox;
		$this->showmodify = $showmodify;
		$this->showdelete = $showdelete;

		$this->selectid = $selectid;
		$this->selectval = $selectval;
		
		$this->wherecause = "";
		$this->notvalid = false;
		$this->errors = "";
		$this->add_text = "Add";
		$fields = array();
	}
	
	// you can had a where cause to add to the select list and all database queries like delete and update
	// useful to allow for exemple only users who made an article to delete it
	function set_where_cause( $cause )
	{
		$this->wherecause = $cause;
	}
	
	function set_add_text( $new)
	{
		$this->add_text = $new;
	}
	
	// Set the prefix added to all http variable by this class
	// default:db
	function set_prefix( $pre )
	{
		$this->pre=$pre;
	}
	
	// field_link = field in another table to link to this one(display purpose only)
	// field_link_val = value to display from other table
	// field_name = field name in database
	function add_field( $field_name, $field_title, $field_type, $field_colspan,$field_size, $field_max, $data="",$field_link="" , $field_link_val="",$field_link_order="",$field_link_table="")
	{
		$this->fields[$field_name] = array( title=> $field_title, type=>$field_type, colspan=>$field_colspan,size=>$field_size,max=>$field_max,data=>$data,link=>$field_link,link_val=>$field_link_val,link_order=> $field_link_order,link_table=>$field_link_table,valid_function=>"");
	}
	
	function add_textbox( $field_name, $field_title, $field_colspan,$field_size, $field_max,$data="") {
		$this->add_field( $field_name, $field_title, "textbox", $field_colspan,$field_size, $field_max,$data);
	}
	
	// add an hidden field but with correspondence in the database
	function add_hidden( $field_name, $field_colspan,$data="") {
		$this->add_field( $field_name, "", "hidden",$field_colspan, "", "",$data);
	}
	
	// will add a listbox with its values taken from an other table
	// for example,an article as a sectionID but section names are listed in table articleSection...
	function add_listbox( $field_name, $field_title, $field_colspan,$field_link, $field_link_val,$field_link_order,$field_link_table,$data="" ) {
		$this->add_field( $field_name, $field_title, "listbox", $field_colspan,"", "",$data,$field_link, $field_link_val,$field_link_order,$field_link_table);
	}
	
	// will add a textarea
	// with col and row as size,max
	function add_textarea( $field_name, $field_title, $field_colspan,$field_col, $field_row ,$data="") {
		$this->add_field( $field_name, $field_title, "textarea", $field_colspan,$field_col, $field_row,$data);
	}
	
	// the class can call a function when adding or modifing data
	// you musr register a function after creating a field
	// see the top of this class for more help
	function register_validation( $field_name, $function )
	{
		$this->fields[$field_name][valid_function] = $function;
	}
	
	// return the form html code
	function draw()
	{
		global $HTTP_ENV_VARS;
		
			
		// show input errors if there are any
		if ($this->notvalid)
			$content.= "<br>".$this->errors;
		
		// show the select box
		if ($this->showselectbox=="1")
		{
			$content .="<form method='post' action='".$HTTP_ENV_VARS["REQUEST_URI"]."' name='db_selectform'>
			<input type=hidden name='$this->pre"."select' value='true'>
			<table border='0' align='center'><tr><td>";
			
			if ($this->wherecause=="")
				$query =  "select $this->selectid,$this->selectval from $this->table";
			else
				$query =  "select $this->selectid,$this->selectval from $this->table where $this->wherecause";
			
			$res = mysql_query($query);
			$onchange="onchange=\"document.db_selectform.submit()\"";
			$content.=build_select_box( $res,$this->pre."selectbox",$GLOBALS[$this->pre.'selectbox'],$onchange,"You can select a record here");
			mysql_free_result($res);
			$content.="</td></tr></table></form>";
		}
		
		$content .="<form method='post' action='".$HTTP_ENV_VARS["REQUEST_URI"]."' name='db_form'>
		<input type='hidden' name='$this->pre"."id' value='".$this->table."'>
		<table border='0' align='center'>";
		
		if ($this->showselectbox=="1")		// add the number of actuel selected record for modification
			$content.="<input type='hidden' name='$this->pre"."selectbox' value='".$GLOBALS[$this->pre.'selectbox']."'>";
			
		$cols=0;
		reset($this->fields);
		$content .= "<tr>";
		while ( list($fieldname,$field) = each($this->fields) )
		{
			$content .= "<td colspan=\"$field[colspan]\"><b>$field[title]</b><br>";
			$cols+=$field[colspan];
			switch ( $field[type] )
			{
				case "textbox":
					$content.=build_input_text( $this->pre.$fieldname,$field[data],$field[size],$field[max]);
					break;
				case "textarea":
					$content.=build_textarea( $this->pre.$fieldname,$field[data],$field[max],$field[size]);
					$content.="<br><a href=\"#\" onClick=\"window.open('../editor/editor.htm?content=$this->pre$fieldname', 'ed$this->pre$fieldname', 'width=650, height=450, resizable=yes');\">Edit Text In Editor</a>";
					break;
				case "hidden":
					$content.=build_input_hidden( $this->pre.$fieldname,$field[data]);
					break;
				case "listbox":
					$query =  "select b.$field[link],b.$field[link_val] from $field[link_table] as b order by b.$field[link_order]";
					$res = mysql_query($query);
					$content.=build_select_box( $res,$this->pre.$fieldname,$field[data]);
					mysql_free_result($res);
					break;
			}
			
			// handle colspan for tables
			if ($cols>=$this->colspan)
			{
				$content.="</td></tr><tr>";
				$cols=0;
			}
			else
				$content.="</td>";
		}
		
		// The bottom buttons
		$content.= "<tr><td colspan=\"$this->colspan\">";
		
		// show the add button
		if ($this->showadd=="1")
		{
			$content.= build_input_submit($this->pre."add",$this->add_text);
		}
		
		// show the modify button
		if ($this->showmodify=="1")
		{
			$content.= build_input_submit($this->pre."modify","Modify");
		}
		
		// show the delete button
		if ($this->showdelete=="1")
		{
			$content.= build_input_submit($this->pre."delete","Delete");
		}
		
		$content.="</td></tr></table></form>";
		return $content;
	}
	
	// when you call this and the form was submitted,it will insert/delete/select the corresponding values in the base
	// if you call it without form submitted (ie the first time) it won't do anything
	function process()
	{
		global $VALIDATOR;
		
		if ( $GLOBALS[$this->pre."add"]==$this->add_text and $this->showadd=="1")
		{
			$sql = "insert into $this->table ";
			// we must insert the values entered in the database
			reset($this->fields);
			
			// the first key ( to solve coma problem in query)
			list($fieldname,$field) = each($this->fields);
			// checks wether a validation function exists or not
			if ($field[valid_function]!="")
			{
				// a function exists !
				$err=$field[valid_function]( $GLOBALS[$this->pre.$fieldname]);	// call it
				if ( $err!="1" )
				{
					// data not validated !!!!!
					$this->notvalid=true;
					$this->errors.=$err."<br>";
				}
			}
			
			// you must select the added content to see it after adding
			
			$sql.="set $fieldname='".$VALIDATOR->make_dbrecord($GLOBALS[$this->pre.$fieldname])."'";
			$this->fields[$fieldname][data] = "";//stripslashes($GLOBALS[$this->pre.$fieldname]);
			
			while ( list($fieldname,$field) = each($this->fields) )
			{
				// checks wether a validation function exists or not
				if ($field[valid_function]!="")
				{
					// a function exists !
					$err=$field[valid_function]( $GLOBALS[$this->pre.$fieldname]);	// call it
					if ( $err!="1" )
					{
						// data not validated !!!!!
						$this->notvalid=true;
						$this->errors.=$err."<br>";
					}
				}
				
				$sql.=", $fieldname='".$VALIDATOR->make_dbrecord($GLOBALS[$this->pre.$fieldname])."'";
				$this->fields[$fieldname][data] = "";//stripslashes($GLOBALS[$this->pre.$fieldname]);
			}
			
			
			if (!$this->notvalid)
				if (!mysql_query( $sql ))
					echo "unable to insert in the database";
				
		}
		else
		if ( $GLOBALS[$this->pre."select"]=="true" and $this->showselectbox=="1")	//set the fields
		{
			reset($this->fields);
			$key = $GLOBALS[$this->pre.'selectbox'];
			while ( list($fieldname,$field) = each($this->fields) )
			{
				if ($field[type]!="hidden")			// never change an hidden data
				{
					if ($this->wherecause=="")
						$sql ="select $fieldname from $this->table where $this->selectid='$key'";
					else
						$sql ="select $fieldname from $this->table where $this->selectid='$key' and $this->wherecause";
				
					if (! ($res=mysql_query( $sql )) )
						echo "unable to select $fieldname in the database";
					else
					{
						list($val)= mysql_fetch_row( $res);
						$this->fields[$fieldname][data] = stripslashes($val);
						mysql_free_result($res);
					}
				}
			}
		}
		else
		if ( $GLOBALS[$this->pre."modify"]=="Modify" and $this->showmodify=="1")	// Modify the record and set the fields
		{
			$key = $GLOBALS[$this->pre.'selectbox'];	// $keys will exist only if $this->showselectbox=="1"
			$sql = "update $this->table ";
			// we must insert the values entered in the database
			reset($this->fields);
			
			// the first key ( to solve coma problem in query)
			list($fieldname,$field) = each($this->fields);
			$sql.="set $fieldname='".$VALIDATOR->make_dbrecord($GLOBALS[$this->pre.$fieldname])."'";
			$this->fields[$fieldname][data] = stripslashes($GLOBALS[$this->pre.$fieldname])  ;
			
			while ( list($fieldname,$field) = each($this->fields) )
			{
				$this->fields[$fieldname][data] = stripslashes($GLOBALS[$this->pre.$fieldname]);
				$sql.=", $fieldname='".$VALIDATOR->make_dbrecord($GLOBALS[$this->pre.$fieldname])."'";
			}
			
			if ($this->wherecause=="")
				$sql.=" where $this->selectid='$key'";
			else
				if ($this->showselectbox=="1")
					$sql.=" where $this->selectid='$key' and $this->wherecause";
				else
					$sql.=" where $this->wherecause";
			
			if (!mysql_query( $sql ))
				echo "unable to update database";
		}
		else
		if ( $GLOBALS[$this->pre."delete"]=="Delete" and $this->showdelete=="1")	// Delete the record
		{
			$key = $GLOBALS[$this->pre.'selectbox'];
			if ($key!='dummy')
			{
				$sql = "delete from $this->table ";
				
				if ($this->wherecause=="")
				$sql.=" where $this->selectid='$key'";
				else
					if ($this->showselectbox=="1")
						$sql.=" where $this->selectid='$key' and $this->wherecause";
					else
						$sql.=" where $this->wherecause";
				
				if (!mysql_query( $sql ))
					echo "unable to delete from database";
			}
		}
		else
		if ( $this->wherecause!="" and $this->showselectbox=="0") // it's a one record view + modify + delete form
		{
			reset($this->fields);
			while ( list($fieldname,$field) = each($this->fields) )
			{
				if ($field[type]!="hidden")			// never change an hidden data
				{
					$sql ="select $fieldname from $this->table where $this->wherecause";
				
					if (! ($res=mysql_query( $sql )) )
						echo "unable to select $fieldname in the database";
					else
					{
						list($val)= mysql_fetch_row( $res);
						$this->fields[$fieldname][data] = stripslashes($val);
						mysql_free_result($res);
					}
				}
			}
		}
	}
}

/* This class takes dbqueries and draws trees with <ul><li></ul> with each results
   The class computes what it must show at runtime with get values.
   The values are prefixed with $this->pre and are composed of the menu name which is going
   to be expanded.
*/
class dbTreeMenu
{
	var $menus;
	var $pre = "dbtm";		// string added before each get variables
	var $defaulturl;
	var $clicurl;
	var $link_attribute;
	
	// the constructor needs to know what the default url will be
	// $clicurl is the url to go when an item is clicked
	// $link_attribute are the attribute of an item link
	// the url of the clics will have a get variable clicid = to the id of the cliqued item
	function dbTreeMenu($defaulturl,$clicurl,$link_attribute="")
	{
		$this->menus = array();
		$this->defaulturl = $defaulturl;
		$this->clicurl = $clicurl;
		$this->link_attribute = $link_attribute;
	}
	
	// Set the prefix added to all http variable by this class
	// default:dbtm
	function set_prefix( $pre )
	{
		$this->pre=$pre;
	}
	/*
		add a menu to the list
		menu name
		menu query: an sql query who returns an id and a description for the items in the menu
		state=0: shrink 1=expended
	*/
	function addmenu($menu_name,$menu_caption,$menu_query,$menu_state=0)
	{
		$this->menus[$menu_name]= array( query=>$menu_query,caption=>$menu_caption,state=>$menu_state);
	}
	
	//change states according to get variables
	// is called by draw
	// if no variables are set, do nothing
	function process()
	{
		reset ($this->menus);
		while( list($name,$menu) = each($this->menus) )
		{
			if ( $GLOBALS[$this->pre.$name] == "1")
			{
				$this->menus[$name][state]=1;
			}
			else
			if ( $GLOBALS[$this->pre.$name] == "0")
			{
				$this->menus[$name][state]=0;
			}
		}
	}
	
	// take current url and make it a new url with info about the menu states
	// url is made to reflect the changement of menu $menu state
	function make_url($menu_name)
	{
		$menus = $this->menus;
		reset ($menus);
		
		// swap the menu state
		if ($menus[$menu_name][state]=="1")
			$menus[$menu_name][state]=0;
		else
			$menus[$menu_name][state]=1;

		list($name,$menu) = each($menus);

		// little trick to choose between ? or & for the first term
		if (ereg("\?",$this->defaulturl)) {
			$string = "&$this->pre".$name."=$menu[state]";
		} else {
			$string = "?$this->pre".$name."=$menu[state]";
		}

		while( list($name,$menu) = each($menus) )
		{
			$string.= "&$this->pre".$name."=$menu[state]";
		}

		$string = $this->defaulturl . $string;

		return($string);
	}
	
	function make_clic_url( $id )
	{
		// little trick to choose between ? or &
		if (ereg("\?",$this->clicurl)) {
			$string = "&clicid=".$id;
		} else {
			$string = "?clicid=".$id;
		}
		
		$string = $this->clicurl . $string;
		
		return $string;
	}
	
	/*
		return the menu html code
		and draws content according to get variables
		these variables are set by the class when it's clicked
	*/
	function draw()
	{
		$this->process();		// get state values from get variables
		
		$content="<ul>\n";
		reset ($this->menus);
		while( list($name,$menu) = each($this->menus) )
		{
			if ( $menu[state] == "1")
			{
				$content.= "<li>-<a href=\"".$this->make_url($name)."\">$menu[caption]</a>\n";
				// we must expand this node
				$res=mysql_query( $menu[query] );
				if ($res)
				{
					$content.="<ul>\n";
					while( list($id,$value) = mysql_fetch_row( $res) )
					{
						$content.= "<li><a href=\"".$this->make_clic_url($id)."\" $this->link_attribute >$value</a>\n";
					}
					$content.="</ul>\n";
					mysql_free_result($res);
				}
			}
			else
				$content.= "<li>+<a href=\"".$this->make_url($name)."\">$menu[caption]</a>\n";
		}
		
		$content.="</ul>\n";
		return $content;
	}
}


 // send a mail if $from="" send mail using config $siteemail
function newmail( $dest ,  $subject , $text ,$from="")
{
	global $sitename,$siteemail;
    // 
	if ($from=="")
		$from = $siteemail;
	
    $entetedate  = date("D, j M Y H:i:s -0600"); // avec offset horaire
    $entetemail  = "From: $from \n"; // Adresse expéditeur
    $entetemail .= "Cc: \n"; 
    $entetemail .= "Bcc: \n"; // Copies cachées
    $entetemail .= "Reply-To: $from \n"; // Adresse de retour
    $entetemail .= "X-Mailer: $sitename\n" ;
    $entetemail .= "Date: $entetedate";  

    mail(
          $dest,
          $subject,
          $text,
          $entetemail
    );
  }

// add data $data to file $debug_file
function debug( $data )
{
	global $debug_file;
	
	if ( $fp=fopen( "$debug_file", "a" ) )
	{
		fwrite($fp,"$data\n");
		fclose( $fp );
	}
}
?>
Return current item: Webgenerator-X content management system