<?php

class target_Model {
	
	public function new_target($Trg_Obj) {
		
		$new_trg_id =0 ;
		
		$new_trg_sql = "select max(trg_id) as trg_id from mst_targets where srv_mrkt = " . $Trg_Obj->get_srv_mrkt();
		$new_trg_rslt= mysql_query($new_trg_sql);
		if(mysql_num_rows($new_trg_rslt) >0) {
			$newtrgary = mysql_fetch_assoc($new_trg_rslt);
			$new_trg_id = $newtrgary['trg_id'] +1;
			mysql_free_result($new_trg_rslt);	
		}
		
		if($new_trg_id >0) {
			
			
			
			$sql_survey = 'insert into mst_targets (srv_mrkt,trg_id,trg_name,trg_grp,trg_grpdesc,factorid,opt_id,user_id) values (
							"' . $Trg_Obj->get_srv_mrkt() . '"
						   ,"' . $new_trg_id . '"
						   ,"' . $Trg_Obj->get_trg_name() . '"
						   ,"' . $Trg_Obj->get_trg_grp() . '"
						   ,"' . $Trg_Obj->get_trg_grpdesc() . '"
						   ,"' . $Trg_Obj->get_factorid() . '"
						   ,"' . $Trg_Obj->get_opt_id() . '"
						   ,"' . $Trg_Obj->get_user_id()  . '")';
	
			mysql_query($sql_survey);
			
			if(strlen(mysql_error()) >0) {
				return true;
			}
			else {
				return false;	
			}
		}
		else {
			return false;
		}
	}
	
	public function update_target($user_id,$mrkt_id,$trg_id,$newtrg) {
		
		$sql_survey = 'update mst_targets 
						set trg_name 	= "' . $newtrg . '"
						where user_id = ' . $user_id . ' and srv_mrkt =  "' . $mrkt_id . '" and  trg_id 	= '  . $trg_id;

		mysql_query($sql_survey);
		
		if(strlen(mysql_error()) <=0) {
			return true;
		}
		else {
			return false;	
		}
		
	}	
	
	public function list_target($user_id,$mrkt_id) {
		$target_lsit = array();
		
		$sql_target = "select distinct srv_mrkt,trg_id,trg_name from mst_targets
						where user_id = " . $user_id . " and srv_mrkt = " . $mrkt_id ." order by srv_mrkt,trg_id";
						
		$rslt_target= mysql_query($sql_target);
		while($trg_rw = mysql_fetch_assoc($rslt_target)) {
			if(trim($trg_rw['trg_name']) != "") {
				$target_lsit[] = $trg_rw;
			}
		}
		mysql_free_result($rslt_target);
		
		return $target_lsit;
	}
	
	
	public function del_target($user_id,$mrkt_id,$trg_id) {
		$sql_del = "delete from mst_targets where user_id =" . $user_id . " and trg_id = " . $trg_id . " and srv_mrkt = " . $mrkt_id;
		
		$rslt_del = mysql_query($sql_del);
		
		if(strlen(mysql_error()) <=0) {
			return true;
		}
		else {
			return false;
		}
	}


	public function del_target_factor($user_id,$mrkt_id,$trg_id,$factid,$optid) {
		$sql_del = "delete from mst_targets where 
						user_id =" . $user_id . " 
						and trg_id = " . $trg_id . " 
						and srv_mrkt = " . $mrkt_id . "
						and factorid = " . $factid . " 
						and opt_id = " . $optid;
		
		$rslt_del = mysql_query($sql_del);
		
		if(strlen(mysql_error()) <=0) {
			return true;
		}
		else {
			return false;
		}
	}

		
	public function list_target_factors($user_id,$mrkt_id,$trg_id) {
		$target_fact_lsit = array();
		
		$sql_target_fact = "select distinct mst_targets.factorid,mst_factor.factordes,mst_targets.opt_id,mst_socio.opt_desc
							from mst_targets
							left join mst_factor on mst_targets.srv_mrkt = mst_factor.srv_mrkt and mst_targets.factorid = mst_factor.factorid
							left join mst_socio on mst_targets.srv_mrkt = mst_socio.srv_mrkt and mst_targets.factorid = mst_socio.factorid and mst_targets.opt_id = mst_socio.opt_id
							where mst_targets.user_id =" . $user_id . " and mst_targets.factorid >0 and mst_targets.srv_mrkt = " . $mrkt_id . " and trg_id = " . $trg_id ." 
							order by mst_targets.srv_mrkt,mst_targets.trg_id,mst_targets.factorid,mst_targets.opt_id";
		
						
		$rslt_target_fact= mysql_query($sql_target_fact);
		while($trg_rw = mysql_fetch_assoc($rslt_target_fact)) {
			$target_fact_lsit[] = $trg_rw;
		}
		mysql_free_result($rslt_target_fact);
		
		return $target_fact_lsit;
	}

				
}

?>