<?php
global $con;
if(stripos($_SERVER['SCRIPT_NAME'],"/admin/")===false?false:true){
	include_once("../connect.php");
	
}else{
	include_once("connect.php");
	
}

$function = $_REQUEST['f'];
if($function == "getSupplierCatalogInfo"){	
	getSupplierCatalogInfo($_REQUEST['cid']);
}elseif($function == "deleteSupplierCatalog"){
	deleteSupplierCatalog($_REQUEST['cid']);
}elseif($function == "saveSupplierCatalog"){
	saveSupplierCatalogInfo($_REQUEST);
}
function getSupplierCatalogInfo($supCatalogID){
	global $con;
	$query = "SELECT id, Name, ButtonImage, Link, Position, AdditionalText, Target FROM SUPPLIER_CATALOG_LINK WHERE id = " . mysqli_real_escape_string($con, $supCatalogID) . ";";
	$result = mysqli_query($con, $query);
	
	$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
	if($row['ButtonImage'] === null || $row['ButtonImage'] === "" || $row['ButtonImage'] === "."){
		$row['ButtonImage'] = "./img/no_image_available.png";
	}
	if($isAdmin){
		$row['ButtonImage'] = "." . $row['ButtonImage'];	
	}
	
	echo json_encode($row);
	return true;
}
function deleteSupplierCatalog($supCatalogID){
	global $con;
	$query = "DELETE FROM SUPPLIER_CATALOG_LINK WHERE id = " . mysqli_real_escape_string($con, $supCatalogID) . ";";
	$result = mysqli_query($con, $query);
	if($result){
		echo "true";
	}else{
		echo "false";
	}
}
function saveSupplierCatalogInfo($data){
	global $con;
	$data = array_map(array($con, 'real_escape_string'), $data);
	//$data['ButtonImage']
	$query = "INSERT INTO SUPPLIER_CATALOG_LINK (id, Name, ButtonImage, Link, Position, AdditionalText, `Target`) VALUES(" . $data['id'] . ", '" . $data['Name'] . "', '" . substr($data['ButtonImage'],1) . "', '" . $data['Link'] . "', '" . $data['Position'] . "', '" . $data['AdditionalText'] . "', '" . $data['Target'] . "') ON DUPLICATE KEY UPDATE `Name` = '" . $data['Name'] . "', `ButtonImage` = '" . substr($data['ButtonImage'],1) . "', `Link` = '" . $data['Link'] . "', `Position` = '" . $data['Position'] . "', AdditionalText = '" . $data['AdditionalText'] . "', `Target` = '" . $data['Target'] . "'";
	$result = mysqli_query($con, $query);
	if($result){
		echo "true";
	}else{
		echo "false";
	}
}

?>