<?php include('../lib/config.php');?>
<?php 

$toDay = date("Y-m-d");
$action = isset($_POST['action'])?$_POST['action']:'';

if(!empty($action) && $action=='save')
{
	$sh_json_data = isset($_POST['jsondata'])?$_POST['jsondata']:'';
	$sh_svg_file = isset($_POST['svg'])?$_POST['svg']:'';

	$elementsDecodedData = json_decode(stripslashes($_POST['jsonElementsString']));	
	//-------------------- Update Main Table ---------------
	$updMapQry = "UPDATE ".TBL_MAPPINGS." SET sh_svg_file ='".$sh_svg_file."',sh_json_data ='".$sh_json_data."',modified = '".$toDay."',modified_by = '".$_SESSION['userID']."'	WHERE id = '1' LIMIT 1";		
	$errNo_map = $con->updateRecords($updMapQry, __FILE__,__LINE__);
	$map_id = 1;
	//------------------ Delete Previous Data ---------------	
	$query_del = "DELETE FROM ".TBL_MAPPINGS_DETAILS;
	$errNo = $con->deleteRecords($query_del);
	//------------------------------------------------------
	foreach($elementsDecodedData as $node)
	{
	  $sh_name = isset($node->name)?$node->name:'';
	  $sh_id = isset($node->shid)?$node->shid:'';
	  $node_size = isset($node->size)?$node->size:'';
	  $node_color = isset($node->color)?$node->color:'';
	  $node_position = isset($node->position)?$node->position:'';
	  $node_positionX = isset($node->position->x)?$node->position->x:'';
	  $node_positionY = isset($node->position->y)?$node->position->y:'';
	  $node_positionX2 = isset($node->position->x2)?$node->position->x2:'';
	  $node_positionY2 = isset($node->position->y2)?$node->position->y2:''; 
	  $node_width = isset($node->position->width)?$node->position->width:'';
	  $node_height = isset($node->position->height)?$node->position->height:'';
	  $node_positionCX = isset($node->position->cx)?$node->position->cx:'';
	  $node_positionCY = isset($node->position->cy)?$node->position->cy:'';
	  
	  //---------------- Delete Previous ----------------
	  /*$selQuery = "SELECT * FROM ".TBL_MAPPINGS." WHERE sh_id='".$sh_id."'";
	  $shInfo = $con->getSingleRow($selQuery);
	  if($shInfo)
	  {
		$query_del = "DELETE FROM ".TBL_MAPPINGS." WHERE sh_id='".$sh_id."' LIMIT 1";
		$errNo = $con->deleteRecords($query_del);
	  }*/  
	  //---------------- Insert New Updated Data ---------	
	  $insQry = "INSERT INTO ".TBL_MAPPINGS_DETAILS." SET 
	  map_id='".$map_id."',
	  sh_id='".$sh_id."',
	  sh_name='".$sh_name."',
	  node_color='".$node_color."',
	  node_size='".$node_size."',
	  node_positionX='".$node_positionX."',
	  node_positionY='".$node_positionY."',
	  node_positionX2='".$node_positionX2."',
	  node_positionY2='".$node_positionY2."',
	  node_width='".$node_width."',
	  node_height='".$node_height."',
	  node_positionCX='".$node_positionCX."',
	  node_positionCY='".$node_positionCY."',
	  sh_svg_file='".$sh_svg_file."',
	  sh_json_data='".$sh_json_data."',
	  date_added='".$toDay."',
	  added_by='".$_SESSION['userID']."'
	  ";        
	  $response = $con->insertRecords($insQry, __FILE__,__LINE__);
	  //-------------------------
	}
	
	if($response===TRUE )
		echo 'OK';
	else
		echo 'err';
}
elseif(!empty($action) && $action=='saveSVG')
{
	$shid = isset($_POST['shid'])?$_POST['shid']:'';
	$sh_svg_file = isset($_POST['svg'])?$_POST['svg']:'';
	
	$updMapQry = "UPDATE ".TBL_MAPPINGS_DETAILS." SET sh_svg_file ='".$sh_svg_file."' WHERE sh_id = '".$shid."' LIMIT 1";		
	$errNo_map = $con->updateRecords($updMapQry, __FILE__,__LINE__);
	
	if($errNo_map===TRUE )
		echo 'OK';
	else
		echo 'err';
}
elseif(!empty($action) && $action=='delete')
{
	$delshid = isset($_POST['delshid'])?$_POST['delshid']:'';
	
	if(!empty($delshid))
	{
		//-------------------- Update Main Table ---------------
		$updMapQry = "UPDATE ".TBL_STAKEHOLDERS." SET is_ranked ='0' WHERE id = '".$delshid."' LIMIT 1";		
		$errNo_map = $con->updateRecords($updMapQry, __FILE__,__LINE__);	
		//------------------ Delete Previous Data ---------------	
		$query_del = "DELETE FROM ".TBL_MAPPINGS_DETAILS." WHERE sh_id=".$delshid."";
		$errNo = $con->deleteRecords($query_del);
		//------------------------------------------------------
		
		if($errNo===TRUE )
			echo 'OK';
		else
			echo 'err';
	}
	else
	{
		echo 'err';
	}
}
else
{
	echo 'err';
}


/*if($response===TRUE )
	$return_arr[] = array("message" => 'OK');
else
	$return_arr[] = array("message" => 'err');
			
echo json_encode($return_arr);*/
?>