<?
/* ************************************************************************** */
// class baseModel is for CMS
/* ************************************************************************** */

class baseModel {

	function getrow($tbl,$column,$condition){

		global $db;

		$sql="select $column from $tbl $condition"; 
		$result =& $db->query($sql);
		$data=$result->fetchRow();
		$result->free();
		return $data;
	
	}

	function getrows($tbl,$column,$condition,$sorting){

		global $db;
        	$listdata=array();
		$dcount=0;

		$sql="select $column from $tbl $condition order by $sorting"; 
		$result =& $db->query($sql);
	        while($data=$result->fetchRow()) {
        	        $listdata[$dcount]=$data;
                	$listdata[$dcount]['recordno']=$dcount+1;
			$dcount++;
		}
		$result->free();
		return $listdata;
	
	}

	function getcount($tbl,$condition){

		global $db;

		$sql="select count(*) as record_count from $tbl $condition"; 
		$result =& $db->query($sql);
		$data=$result->fetchRow();
		$result->free();
		return $data['record_count'];
	
	}

	function getextendedrow($sql){

		global $db;

		$result =& $db->query($sql);

		//echo $sql;

		//return;
		$data=$result->fetchRow();
		$result->free();
		return $data;
	
	}

	function getextendedrows($sql){

		global $db;
        	$listdata=array();
		$dcount=0;

		$result =& $db->query($sql);
	        while($data=$result->fetchRow()) {
        	        $listdata[$dcount]=$data;
                	$listdata[$dcount]['recordno']=$dcount+1;
			$dcount++;
		}
		$result->free();
		return $listdata;
	
	}


	/* DATA INSERTION */

	function add_data($module,$tbl,$column,$data,$user=""){

		global $db;
	
		$sql="insert into $tbl ($column) values ($data)";

		//echo $sql;
		//return;

		$sql_audit = "Insert into $tbl";
		$insert_audit = new auditTrail();
		$insert_audit->audittrail_add($module,$sql_audit,$sql,$user);
		if ($result =& $db->query($sql)){
			return TRUE;
		} else {
			return FALSE;
		}
	}

	function add_data_noaudit($tbl,$column,$data,$user=""){

		global $db;

		$sql="insert into $tbl ($column) values ($data)";

		if ($result =& $db->query($sql)){
			return TRUE;
		} else {
			return FALSE;
		}	
	}


	/* DATA UPDATING */

	function update_data($module,$tbl,$column,$condition,$user=""){

		global $db;
	
	 	$sql="update $tbl set $column $condition"; 

		//echo $sql;
		//return $sql;

		$sql_audit = "Update $tbl";
		$insert_audit = new auditTrail();
		$insert_audit->audittrail_add($module,$sql_audit,$sql,$user);
		if ($result =& $db->query($sql)){
			return TRUE;
		} else {
			return FALSE;
		}	
	}

	function update_data_noaudit($tbl,$column,$condition){

		global $db;
	
 		$sql="update $tbl set $column $condition"; 

		if ($result =& $db->query($sql)){
			return TRUE;
		} else {
			return FALSE;
		}	
	}


	/* DATA DELETION */

	function delete_data($module,$tbl,$condition,$user=""){

		global $db;
	
	 	$sql="delete from $tbl $condition"; 

		$sql_audit = "Delete from $tbl";
		$insert_audit = new auditTrail();
		$insert_audit->audittrail_add($module,$sql_audit,$sql,$user);
		if ($result =& $db->query($sql)){
			return TRUE;
		} else {
			return FALSE;
		}
	}

	function delete_data_noaudit($tbl,$condition){

		global $db;
	
	 	$sql="delete from $tbl $condition"; 

		if ($result =& $db->query($sql)){
			return TRUE;
		} else {
			return FALSE;
		}
	}


	function execute_nonquery($sql){
		global $db;
		if ($result =& $db->query($sql)){
			return TRUE;
		} else {
			return FALSE;
		}
	}

	



	/* INTERNAL FUNCTIONS FOR SECURITY */

	/* CURRENTLY NOT USED. SQL INJECTION TAKEN CARE OF IN lib_security.php */

	/*
	function cleanQuery($string) {

		if(get_magic_quotes_gpc()) {	// prevents duplicate backslashes
			$string = stripslashes($string);
		}

		$badWords = "(delete)|(update)|(union)|(insert)|(drop)|(http)|(--)";
		$string = eregi_replace($badWords, "", $string);

		if (phpversion() >= '4.3.0') {
			$string = mysql_real_escape_string($string);
		} else {
			$string = mysql_escape_string($string);
		}
		return $string;
	}
	*/

}

?>
