Location: PHPKode > scripts > PHP or Java Class Code Generator > php-or-java-class-code-generator/programtemplates/template_php_code_generator.html
<?php
/*
 CODE GENERATED BY: GRAFXSOFTWARE CODE GENERATOR
 http://www.grafxsoftware.com
====================================== 
 CLASS MADE BY: {AUTHOR}
 DATE: {DATE}
 PROJECT: {PROJECT_NAME}
 PHP: version 4.x
======================================
*/

/*

${CLASSVAR}=new {NAME}();
   
<!-- BEGIN DYNAMIC BLOCK: getfunctionsvar -->
${CLASSVAR}->get{FUNC_NAME}();
<!-- END DYNAMIC BLOCK: getfunctionsvar -->

<!-- BEGIN DYNAMIC BLOCK: setfunctionsvar -->
${CLASSVAR}->set{FUNC_NAME}();
<!-- END DYNAMIC BLOCK: setfunctionsvar -->

${CLASSVAR}->save();


*/

/**
*
* @author - {AUTHOR}
* @desc -
* @vers - 1.0
*/
class {NAME}
{
 var $where_clause;
 var $enable_undo={ENABLE_UNDO};
 var $id_key='id';
 var $history_table='{HISTORY_TABLE}';


 var $valid=false;     //do not modify
 var $where_mode=false;//do not modify

 <!-- BEGIN DYNAMIC BLOCK: row -->
 var  ${VARIABLES};
 <!-- END DYNAMIC BLOCK: row -->

/**  
* @author   - {AUTHOR}
* @type     - public
* @desc     - Constructor
* @param    - string ${IDCLASS} - the ID
* @return   - mixed rewrite array
* @vers     - 1.0
* @Mod  	- 
**/
function {NAME}(${IDCLASS})
	{
		if ((is_int($id)&&($id==0))||(is_string($id)&&($id=="0"))) {
			$this->valid=true;
			$this->where_mode=false;
			return;
		};
	
		$this->{IDCLASS} = ${IDCLASS};

		if ((is_string($id))&&(stristr($id,"where"))) {
			$this->where_clause=$id;
			$this->where_mode=true;
		}else {
			$this->where_clause=" WHERE `id`='".$this->id."'";
			$this->where_mode=false;
		};
            
		$SQL = " SELECT {LIST} FROM {DBNAME} ".$this->where_clause;
		$retid = mysql_query($SQL) or die(mysql_error());

		if ($row = mysql_fetch_array($retid))
		{
			$this->valid=true;
			
		 <!-- BEGIN DYNAMIC BLOCK: value -->
 	 	  $this->{FIELD_NAME} = $row["{FIELD_NAME}"];
         <!-- END DYNAMIC BLOCK: value -->
	
		}
		else
		{
			$this->valid=false;
		
		 <!-- BEGIN DYNAMIC BLOCK: nullvalue -->
 	 	  $this->{FIELD_NAME} = "";
         <!-- END DYNAMIC BLOCK: nullvalue -->
		}
	}//end constructor {NAME}

<!-- BEGIN DYNAMIC BLOCK: getfunctions -->

/**  
* @author   - {AUTHOR}
* @type     - public
* @desc     - get $this->{VAR_NAME}
* @return   - string
* @vers     - 1.0
* @Mod  	- 
**/
function get{FUNC_NAME}()
{
  return $this->{VAR_NAME};

} // end get{FUNC_NAME}()	
<!-- END DYNAMIC BLOCK: getfunctions -->

<!-- BEGIN DYNAMIC BLOCK: setfunctions -->

/**  
* @author   - {AUTHOR}
* @type     - public
* @desc     - set $this->{VAR_NAME} to ${VAR_NAME}
* @param    - string ${VAR_NAME}
* @return   - void
* @vers     - 1.0
* @Mod  	- 
**/
function set{FUNC_NAME}(${VAR_NAME})
{
  $this->{VAR_NAME}=${VAR_NAME};
} // end set{FUNC_NAME}()
	
<!-- END DYNAMIC BLOCK: setfunctions -->

/**  
* @author   - {AUTHOR}
* @type     - public
* @desc     - check if the data is valid
* @return   - void
* @vers     - 1.0
* @Mod  	- 
**/
function is_valid() {
	return $this->valid;
}
/**  
* @author   - {AUTHOR}
* @type     - public
* @desc     - saves data into database
* @return   - void
* @vers     - 1.0
* @Mod  	- 
**/
function save() {
		if (!$this->valid) return;
 		//$this->slashesAdd ();

		if ((($this->{IDCLASS})==0)&&(!$this->where_mode))
		{
			$SQL = "INSERT INTO {DBNAME} ({LIST})";
			$SQL .= " VALUES({LIST_INSERT})";

			$retid = mysql_query($SQL);
			if (!$retid) { echo( mysql_error()); }
			$this->{IDCLASS} = mysql_insert_id();
			if ($this->enable_undo) {
				$this->write_insert_undo($this->{IDCLASS});
			};
		}
		else
		{
			if ($this->enable_undo){
				$this->write_update_undo();
			}
            $SQL = "UPDATE {DBNAME} SET {LIST_UPDATE}";
			$SQL .= $this->where_clause;
			$retid = mysql_query($SQL);
			if (!$retid) { echo( mysql_error()); }
		}
		
}//end save()


/**  
* @author   - {AUTHOR}
* @type     - public
* @desc     - delete ${IDCLASS} from database
* @return   - void
* @vers     - 1.0
* @Mod  	- 
**/

function delete(${IDCLASS})
{
if (is_int(${IDCLASS})) $where_clause="WHERE `{IDCLASS}`='".${IDCLASS}."'";
else $where_clause=${IDCLASS};
if ($this->enable_undo){
$this->write_delete_undo($where_clause);
};
$SQL = "DELETE FROM {DBNAME} ".$where_clause;
$retid = mysql_query($SQL);
if (!$retid) { echo( mysql_error()); }
} // end delete()

/**  
* @author   - {AUTHOR}
* @type     - public
* @desc     - write undo into history table
* @return   - void
* @vers     - 1.0
* @Mod  	- 
**/

/*
//--- create a history table from this SQL
CREATE TABLE `{HISTORY_TABLE}` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`item_id` int(11) NOT NULL, 
`tablename` varchar(100) COLLATE utf8_bin DEFAULT NULL,
`command` text COLLATE utf8_bin,
`date` datetime DEFAULT NULL,
`userid` int(11) DEFAULT NULL,
`valid` tinyint(4) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM;
*/


function write_undo($item_id,$sql_text){
	if ((!$this->enable_undo)||($sql_text=="")) return;
	$sql_text=base64_encode($sql_text);
	$userid=0;
	$tablename="{DBNAME}";
	$SQL="INSERT INTO `$this->history_table` (`item_id`,`tablename`,`command`,`date`,`userid`,`valid`)";
	$SQL.=" VALUES('".$item_id."','".$tablename."','".$sql_text."',NOW(),'".$userid."','1');";
	$retid = mysql_query($SQL);
	if (!$retid) { echo( mysql_error()); }

}


/**  
* @author   - {AUTHOR}
* @type     - public
* @desc     - write undo for UPDATE
* @return   - void
* @vers     - 1.0
* @Mod  	- 
**/
function write_update_undo(){
	$this->make_replace_row_sql($this->where_clause);
}

/**  
* @author   - {AUTHOR}
* @type     - public
* @desc     - write INSERT
* @return   - void
* @vers     - 1.0
* @Mod  	- 
**/
function write_insert_undo(${IDCLASS}){
	$this->make_sql_for_insert(${IDCLASS});
}

/**  
* @author   - {AUTHOR}
* @type     - public
* @desc     - get SQL query for DELETE
* @return   - string
* @vers     - 1.0
* @Mod  	- 
**/
function write_delete_undo($id_or_where_clause){
	$this->make_replace_row_sql($id_or_where_clause);
}


/**  
* @author   - {AUTHOR}
* @type     - public
* @desc     - get SQL query for INSERT
* @return   - string
* @vers     - 1.0
* @Mod  	- 
**/
function make_sql_for_insert(${IDCLASS}){
	$SQL="SELECT * FROM `{DBNAME}` ".$where_clause;
	$result="";
	$retid=mysql_query($SQL);
	if (!$retid) return "";
	$id_value="";
	while ($row = mysql_fetch_assoc($retid)){
		$keys="";
		$values="";
		foreach($row as $key => $value){
			if ($key==$this->id_key) {
				$id_value=$value;
			};
			if ($keys!="") $keys.=",";
			$keys.="`".$key."` ";
			if ($values!="") $values.=",";
			$values.="'".$value."' ";
		};
	};
	$this->write_undo($id_value, "DELETE FROM `{DBNAME}` WHERE `{IDCLASS}`='".${IDCLASS}."' AND $this->id_key='$id_value';");
}

/**  
* @author   - {AUTHOR}
* @type     - public
* @desc     - get the SQL query which make the existing rows
* @return   - void
* @vers     - 1.0
* @Mod  	- 
**/
function make_replace_row_sql($id_or_where_clause){
if (is_int($id_or_where_clause)) $where_clause="WHERE `{IDCLASS}`='".$id_or_where_clause."'";
		else $where_clause=$id_or_where_clause;
	$SQL="SELECT * FROM `{DBNAME}` ".$where_clause;
	$result="";
	$retid=mysql_query($SQL);
	if (!$retid) return "";
	while ($row = mysql_fetch_assoc($retid)){
		$keys="";
		$values="";
		$id_value="";
		foreach($row as $key => $value){
			if ($key==$this->id_key) $id_value=$value;
			if ($keys!="") $keys.=",";
			$keys.="`".$key."` ";
			if ($values!="") $values.=",";
			$values.="'".$value."' ";
		};
		$this->write_undo($id_value,"REPLACE INTO `{DBNAME}` (".$keys.") VALUES (".$values.");");
	};
}

/**  
* @author   - {AUTHOR}
* @type     - public
* @desc     - debugging function which shows what's in history table
* @return   - void
* @vers     - 1.0
* @Mod  	- 
**/
function debug_print_history(){
	$SQL="SELECT * from $this->history_table;";
	$retid=mysql_query($SQL);
	if (!$retid) return "";
	while ($row = mysql_fetch_assoc($retid)){
		foreach($row as $key => $value){
			if ($key=='command') $value=base64_decode($value);
			print("`$key` = '$value'<br>");
		};
		print("<br />");

	};
}

/**  
* @author   - {AUTHOR}
* @type     - public
* @desc     - do multiple query, each query is separted by ";;\n"
* @return   - string
* @vers     - 1.0
* @Mod  	- 
**/
function mysql_multiple_query($q) {
	$tok = strtok($q, ";;\n");
	while ($tok) {
		$results=mysql_query("$tok");
		$tok = strtok(";;\n");
	}
	return $results;
}

/**  
* @author   - {AUTHOR}
* @type     - public
* @desc     - undo function
* @return   - boolean
* @vers     - 1.0
* @Mod  	- 
**/
function undo($item_id){
	$SQL="SELECT * from `$this->history_table` WHERE tablename='{DBNAME}' AND valid='1' AND item_id='$item_id'  ORDER BY date DESC LIMIT 1;";
	$retid=mysql_query($SQL);
	if (!$retid) return "";
	if ($row = mysql_fetch_assoc($retid)){
		$command=$row['command'];
		if ($command=="") return false;
		$command=base64_decode($command);		

		$SQL="DELETE from `$this->history_table` ORDER BY date DESC LIMIT 1;";
		if (!mysql_query($SQL)) return false;
		return $this->mysql_multiple_query($command);
	};
	return false;
}

/**  
* @author   - {AUTHOR}
* @type     - public
* @desc     - adding slashes if necesary
* @return   - void
* @vers     - 1.0
* @Mod  	- 
**/
function slashes($str){
	if ((get_magic_quotes_gpc()) && (!empty($str))) return addslashes($str);
		else return $str;
}
/*
function slashesAdd() {
  $class_vars = get_class_vars ( get_class ( $this ) );
  foreach ( $class_vars as $name => $value ) {
   if (get_magic_quotes_gpc () && ! empty ( $name ))
    $this->$name = addslashes ( $this->$name );
  }
}
*/
/**************************************************************/
/*          ADD YOUR CUSTOM FUNCTIONS BELOW                   */
/**************************************************************/


} // end class {NAME}
?>
Return current item: PHP or Java Class Code Generator