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

$data = json_decode(stripslashes($_POST['jsonElementsString']));

// here i would like use foreach:
foreach($data as $d)
{
 print($d);
}
  
/*$jsonarr = json_decode($_POST['jsondata']);
foreach ($jsonarr as $key => $jsons) { // This will search in the 2 jsons
     foreach($jsons as $key => $value) {
         echo $value; // This will show jsut the value f each key like "var1" will print 9
                       // And then goes print 16,16,8 ...
    }
}*/
//print_r($_POST);

/*echo $_POST['svg'];
exit;*/

$sh_map_file = isset($_POST['svg'])?$_POST['svg']:''; 
$sh_id = isset($_POST['shid'])?$_POST['shid']:'';
$map_value = isset($_POST['map_value'])?$_POST['map_value']:'';
$map_color = isset($_POST['map_color'])?$_POST['map_color']:'';
$action = isset($_POST['action'])?$_POST['action']:'';

if(!empty($action) && $action=='saveRec')
{
	$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);
	}
	
	$insQry = "INSERT INTO ".TBL_MAPPINGS." SET sh_id='".$sh_id."',sh_map_file='".$sh_map_file."',map_value='".$map_value."',map_color='".$map_color."' ";        
	$response = $con->insertRecords($insQry, __FILE__,__LINE__);
	
	if($response===TRUE )
		echo 'OK';
	else
		echo 'err';
}
elseif(!empty($action) && $action=='saveSVG')
{
	$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);
	}
	
	$insQry = "INSERT INTO ".TBL_MAPPINGS." SET sh_id='".$sh_id."',sh_map_file='".$sh_map_file."' ";        
	$response = $con->insertRecords($insQry, __FILE__,__LINE__);
	
	if($response===TRUE )
		echo 'OK';
	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);*/
?>