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

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

if(!empty($action) && $action=='saveSHMap')
{
	$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)
	{
	  $orientation_type = isset($node->otype)?$node->otype:'';
	  $sh_id = isset($node->shid)?$node->shid:'';
	  $sh_name = isset($node->name)?$node->name:'';	  
	  $node_size = isset($node->size)?$node->size:'';
	  $node_color = isset($node->color)?$node->color:'';
	  $node_update = isset($node->update)?$node->update:'';
	  $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:'';
	  //---------------- Update Data --------------------------------------	  
	  if(isset($orientation_type) && $orientation_type=='I')
		{
			//------------------------------------------------
			$updQry_sh = "UPDATE ".TBL_STAKEHOLDERS." SET 
			orientation_sh_influence='".addslashes($node_size)."',
			sh_initial_orientation='".addslashes($node_color)."',
			date_modified='".$toDay."' 		
			WHERE id = '".$sh_id."' LIMIT 1";			
			$errNo_sh = $con->updateRecords($updQry_sh, __FILE__,__LINE__);
			//------------------------------------------------
			$updQry_map_det = "UPDATE ".TBL_MAPPINGS_DETAILS." SET 
			  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."',
			  modified='".$toDay."',
			  modified_by='".$_SESSION['userID']."'
			  WHERE orientation_type = 'I' 
			  AND sh_id = '".$sh_id."' ";    
		    $response = $con->updateRecords($updQry_map_det, __FILE__,__LINE__);
			//-------------------------------------------------
		}
	  elseif(isset($orientation_type) && $orientation_type=='C')
		{
			//-------------------------------------------------
			$updQry_sh = "UPDATE ".TBL_STAKEHOLDERS." SET 
			orientation_sh_influence='".addslashes($node_size)."',
			sh_current_orientation='".addslashes($node_color)."',
			date_modified='".$toDay."' 		
			WHERE id = '".$sh_id."' LIMIT 1";			
			$errNo_sh = $con->updateRecords($updQry_sh, __FILE__,__LINE__);
			//-------------------------------------------------
			$selQuery_his = "SELECT * FROM  ".TBL_RANKING_HISTORY." WHERE sh_id='".$sh_id."' ";
			$hisInfo = $con->getMultiRow($selQuery_his);
			$tot_mappingshistoryInfo = count($hisInfo);
			//-------- Get previous position of the node and save --------
			$selQuery_map_det_prev = "SELECT * FROM  ".TBL_MAPPINGS_DETAILS." WHERE sh_id='".$sh_id."' and orientation_type='C';";
			$mapdetPrevInfo = $con->getSingleRow($selQuery_map_det_prev);
			
			//if($mapdetPrevInfo['node_positionX']!=$node_positionX && $mapdetPrevInfo['node_positionY']!=$node_positionY && $mapdetPrevInfo['node_positionX2']!=$node_positionX2 && $mapdetPrevInfo['node_positionY2']!=$node_positionY2 && $mapdetPrevInfo['node_positionCX']!=$node_positionCX && $mapdetPrevInfo['node_positionCY']!=$node_positionCY)
			
			if( $mapdetPrevInfo['node_positionY']!=$node_positionY && $mapdetPrevInfo['node_positionY2']!=$node_positionY2 && $mapdetPrevInfo['node_positionCY']!=$node_positionCY)
			{
				if($tot_mappingshistoryInfo>0)
				{					
					$insQry_cu = "INSERT INTO ".TBL_RANKING_HISTORY." SET 
					  sh_id='".$mapdetPrevInfo['sh_id']."',
					  node_color='".$mapdetPrevInfo['node_color']."',
					  node_size='".$mapdetPrevInfo['node_size']."',
					  node_positionX='".$mapdetPrevInfo['node_positionX']."',
					  node_positionY='".$mapdetPrevInfo['node_positionY']."',
					  node_positionX2='".$mapdetPrevInfo['node_positionX2']."',
					  node_positionY2='".$mapdetPrevInfo['node_positionY2']."',
					  node_width='".$mapdetPrevInfo['node_width']."',
					  node_height='".$mapdetPrevInfo['node_height']."',
					  node_positionCX='".$mapdetPrevInfo['node_positionCX']."',
					  node_positionCY='".$mapdetPrevInfo['node_positionCY']."',
					  update_detail='".$node_update."',
					  status='1',			  
					  date_added='".$toDay."',
					  added_by='".$_SESSION['userID']."' ";        
					//$response_cu = $con->insertRecords($insQry_cu, __FILE__,__LINE__);
				}
				else
				{
					$insQry_cu = "INSERT INTO ".TBL_RANKING_HISTORY." SET 
					  sh_id='".$mapdetPrevInfo['sh_id']."',
					  node_color='".$mapdetPrevInfo['node_color']."',
					  node_size='".$mapdetPrevInfo['node_size']."',
					  node_positionX='".$mapdetPrevInfo['node_positionX']."',
					  node_positionY='".$mapdetPrevInfo['node_positionY']."',
					  node_positionX2='".$mapdetPrevInfo['node_positionX2']."',
					  node_positionY2='".$mapdetPrevInfo['node_positionY2']."',
					  node_width='".$mapdetPrevInfo['node_width']."',
					  node_height='".$mapdetPrevInfo['node_height']."',
					  node_positionCX='".$mapdetPrevInfo['node_positionCX']."',
					  node_positionCY='".$mapdetPrevInfo['node_positionCY']."',
					  status='0',					  			  
					  date_added='".$toDay."',
					  added_by='".$_SESSION['userID']."' ";        
					//$response_cu = $con->insertRecords($insQry_cu, __FILE__,__LINE__);
				}
				
				//echo $insQry_cu;				
			}
			
			//-------------------------------------------------
			//echo "<br>";
			$updQry_map_det = "UPDATE ".TBL_MAPPINGS_DETAILS." SET 
			  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."',
			  update_detail='".$node_update."',
			  sh_svg_file='".$sh_svg_file."',
			  sh_json_data='".$sh_json_data."',
			  modified='".$toDay."',
			  modified_by='".$_SESSION['userID']."'
			  WHERE orientation_type = 'C' 
			  AND sh_id = '".$sh_id."' ";    
		    $response = $con->updateRecords($updQry_map_det, __FILE__,__LINE__);
			//-------------------------------------------------			
		}
		elseif(isset($orientation_type) && $orientation_type=='D')
		{
			//-------------------------------------------------
			$updQry_sh = "UPDATE ".TBL_STAKEHOLDERS." SET 
			orientation_sh_influence='".addslashes($node_size)."',
			sh_desired_orientation='".addslashes($node_color)."',
			date_modified='".$toDay."' 		
			WHERE id = '".$sh_id."' LIMIT 1";			
			$errNo_sh = $con->updateRecords($updQry_sh, __FILE__,__LINE__);
			//-------------------------------------------------
			$updQry_map_det = "UPDATE ".TBL_MAPPINGS_DETAILS." SET 
			  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."',
			  modified='".$toDay."',
			  modified_by='".$_SESSION['userID']."'
			  WHERE orientation_type = 'D' 
			  AND sh_id = '".$sh_id."' ";     
		    $response = $con->updateRecords($updQry_map_det, __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=='saveSVGImg')
{
	$shid = isset($_POST['shid'])?$_POST['shid']:'';
	$sh_svg_file = isset($_POST['svg'])?$_POST['svg']:'';
	$sh_svg_img_file = isset($_POST['svgImg'])?$_POST['svgImg']:'';
	
	$svg_filename = $shid.".svg";
	$img_filename = $shid.".png";
	
	$svg_filename_path = $_SERVER['DOCUMENT_ROOT']."/shmap-online/uploads/svgs/".$svg_filename;
	$img_filename_path = $_SERVER['DOCUMENT_ROOT']."/shmap-online/uploads/svgs/".$img_filename;
	
	$updRankQry = "UPDATE ".TBL_STAKEHOLDERS." SET sh_ranking_svg_file ='".$sh_svg_file."',sh_ranking_svg_img ='".$img_filename."' WHERE id = '".$shid."' LIMIT 1";		
	$errNo_rank = $con->updateRecords($updRankQry, __FILE__,__LINE__);
	
	if($errNo_rank===TRUE )
	{
		//--------------------------------------------------------------
		ob_start();
		echo '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
			<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" baseProfile="full" width="100" height="100">
			<circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
		</svg>';
		
		$content = ob_get_clean();
		
		if(file_put_contents($svg_filename_path, $sh_svg_file)) { // Filename for storing purpose
			//----------------------------------
			$img = $_POST['svgImg']; // Your data 'data:image/png;base64,AAAFBfj42Pj4';
			$img = str_replace('data:image/png;base64,', '', $img);
			$img = str_replace(' ', '+', $img);
			$data = base64_decode($img);
			file_put_contents($img_filename_path, $data);
			//file_put_contents($img_filename_path, $data);
			//----------------------------------
			echo "OK";
		}
		else 
		{
			echo "err";
		}
		//--------------------------------------------------------------
		//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';
	}
}
elseif(!empty($action) && $action=='deleteRHistory')
{
	$shid = isset($_POST['shid'])?$_POST['shid']:'';
	$delshrhid = isset($_POST['delshrhid'])?$_POST['delshrhid']:'';
	
	if(!empty($delshid))
	{
		//------------------ Delete Previous Data ---------------	
		$query_del = "DELETE FROM ".TBL_RANKING_HISTORY." WHERE sh_id='".$shid."' AND id='".$delshrhid."'";
		$errNo = $con->deleteRecords($query_del);
		//------------------------------------------------------
		
		if($errNo===TRUE )
			echo 'OK';
		else
			echo 'err';
	}
	else
	{
		echo 'err';
	}
}
elseif(!empty($action) && $action=='deleteNode')
{
	$shid = isset($_POST['shid'])?$_POST['shid']:'';
	$nodeid = isset($_POST['curnodeid'])?$_POST['curnodeid']:'';
	$nodeorttype = isset($_POST['curnodeorttype'])?$_POST['curnodeorttype']:'';
	
	if(!empty($shid) && !empty($nodeorttype))
	{
		if(!empty($nodeorttype) && ($nodeorttype=='I' or $nodeorttype=='C' or $nodeorttype=='D'))
		{
			//------------------ Delete Previous Data ---------------	
			$query_del = "DELETE FROM ".TBL_MAPPINGS_DETAILS." WHERE id='".$nodeid."' AND sh_id='".$shid."' LIMIT 1";
			$errNo = $con->deleteRecords($query_del);
			//-------------------------------------------------------
		}
		else if(!empty($nodeorttype) && $nodeorttype=='CU')
		{
			//------------------ Delete Previous Data ---------------	
			$query_del = "DELETE FROM ".TBL_RANKING_HISTORY." WHERE id='".$nodeid."' AND sh_id='".$shid."' LIMIT 1";
			$errNo = $con->deleteRecords($query_del);
			//-------------------------------------------------------
		}		
		
		if($errNo===TRUE )
			echo 'OK';
		else
			echo 'err';
	}
	else
	{
		echo 'err';
	}
}
else
{
	echo 'err';
}
?>