<?php 
$form_name = "Location Mapping";
$table_name = "custom_location_mapping";
$primary_key = "loc_mapng_id";

$mode = isset($_REQUEST['mode']) ? $_REQUEST['mode'] : "";
	
include_once("includes/config.php");
include_once("includes/common_functions.php");

$common = new General_Functions();
$css_class = "x-gray";
// $common->SetCssClass($css_class);

$mode = isset($_REQUEST['mode']) ? $_REQUEST['mode'] : "";
$act_type = isset($_REQUEST['act_type']) ? $_REQUEST['act_type'] : "";
$cancel_link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];
$action = $_SERVER['SCRIPT_NAME']."?page=".$_REQUEST['page']."&mode=submit";

switch($mode)
{
	case "submit":
		$loc_mstr = $_REQUEST['loc_mstr'];
		
		if($_REQUEST['id'] != "")
		{
			$qry = "DELETE FROM ".$table_name." WHERE loc_mapng_sur_id = '".$_REQUEST['survey_id']."'";
			$rec = $common->sql->exeQuery($qry);
		}
                
		for($i=0; $i<count($loc_mstr); $i++)
		{
			$qry = "SELECT * FROM ".$table_name." WHERE loc_mapng_sur_id = '".$_REQUEST['survey_id']."' AND loc_mapng_mstr_id = '".$loc_mstr[$i]."'";
			$common->sql->query($qry, SQL_ALL, SQL_ASSOC);
			$rec = $common->sql->record;
			
			if(count($rec) == 0)
			{
				$_query = sprintf("insert into ".$table_name." values(0, '%s', '%s', 'Y', now(), '%s', 0, 0)",
					mysql_escape_string($_REQUEST['survey_id']),
					mysql_escape_string($loc_mstr[$i]),
					mysql_escape_string(USR)
				);
				$exe_rec = $common->sql->query($_query);
				$last_id = mysql_insert_id();
			}
		}
		
		if($exe_rec)
		{
			if($_REQUEST['id'] != "")
			{
				$_SESSION['msg'] = 'Location Mapping has been updated Successfully';
				$common->UserLog($form_name, $table_name, $_REQUEST['survey_id'], 'Update');
			}
			else
			{
				$_SESSION['msg'] = 'Location Mapping has been saved Successfully';
				$common->UserLog($form_name, $table_name, $_REQUEST['survey_id'], 'Insert');
			}
		}
		
		$link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];
		header("location:".$link);
	break;
	
	case "edit":
		$query = "Select * FROM ".$table_name." WHERE loc_mapng_sur_id = '".$_REQUEST['id']."'";
		$common->sql->query($query, SQL_ALL, SQL_ASSOC);
		$rec = $common->sql->record;
                
		$id = $_REQUEST['id'];
		$survey_id = $rec[0]['loc_mapng_sur_id'];
		include("edit.php");
	break;
	
	case "delete":
		if(isset($_REQUEST[$prefix.'id']))
			$id = $_REQUEST[$prefix.'id'];
		else
			$id = implode("', '", $_REQUEST[$primary_key]);

		$query = "DELETE FROM ".$table_name." WHERE lab_mapng_sur_id IN ('".$id."')";

		$rec = $common->sql->exeQuery($query);

		if($rec)
		{
			$_SESSION['msg'] = 'Selected record has been deleted successfully';
			if(isset($_REQUEST['id']))
				$common->UserLog($form_name, $table_name, $_REQUEST['id'], 'Delete');
			else
				for($i=0; $i<count($_REQUEST[$primary_key]); $i++)
					$common->UserLog($form_name, $table_name, $_REQUEST[$primary_key][$i], 'Delete');
		}

		$query_string = str_replace("&mode=delete","",$_SERVER['QUERY_STRING']);
		$link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];
		header("location:".$link);
	break;
	
	default:
		echo '<div class="content_bg">';
			include("add.php");
		echo '</div>';
	break;
}
?>