<?php
require_once("class/class.bbdd.php");
$bbdd = new BBDD();

//Resposta per defecte (Negativa)
$response["success"] = "0"; 

$reciCode = 0;

$userCode = $bbdd->checkParameter('userCode');
$reciTitl = $bbdd->checkParameter('reciTitl');
$reciText = $bbdd->checkParameter('reciText');
$reciCate = $bbdd->checkParameter('reciCate');
$reciRati = $bbdd->checkParameter('reciRati');
$reciTime = $bbdd->checkParameter('reciTime');
$ingrName = $bbdd->checkParameter('ingrName');
$ingrQuan = $bbdd->checkParameter('ingrQuan');
$stepName = $bbdd->checkParameter('stepName');

//Valida que s'haguin rebut tots els paràmetres
if ($userCode !== 0 && $reciTitl !== 0 && $reciText !== 0 && $reciCate !== 0 && $reciRati !== 0 && $reciTime !== 0 && $ingrName !== 0 && $ingrQuan !== 0 && $stepName !== 0) {
	
	//Categoria
	$cateCode = $bbdd->selectUnique("cateIden","RecipesCategories","cateName = '".$reciCate."'");	
	if(empty($cateCode)) {
		$cateCode = $bbdd->insertID("RecipesCategories","cateName","'".$reciCate."'");
	}
	
	//Recepta
	$reciCode = $bbdd->insertID("Recipes", "reciUser,reciCategory,reciTitle,reciText,reciRations,reciTime,reciImage", "'".$userCode."', '".$cateCode."', '".$reciTitl."', '".$reciText."', '".$reciRati."', '".$reciTime."',NULL");
	
	//Ingredients
	ksort($_POST['ingrName']);
	ksort($_POST['ingrQuan']);
	foreach($_POST['ingrName'] as $key => $ingr) {	
		$ingrCode = $bbdd->insertID("Ingredients","ingrName","'".$ingr."'");
		$results = $bbdd->insert("IngredientsRecipes", "inreRece,inreIngr,inreQuantity", "'".$reciCode."','".$ingrCode."','".$_POST['ingrQuan'][$key]."'");	
	}
	
	//Passos	
	ksort($_POST['stepName']);
	foreach($_POST['stepName'] as $step) {	
		$stepText = addslashes(strip_tags($step));
		$results = $bbdd->insert("Steps", "stepText,stepRecipe", "'". $stepText."','".$reciCode."'");	
	}

	
	//Resposta: Positiva
	$response["success"] = "1"; 
	$response["message"] = "S'ha creat la recepta correctament.";	
		
} else {	
	//Resposta: Falten paràmetres 
	$response["message"] = "No s'han especificat tots els paràmetres.";	
}

//Imatge
if(isset($_FILES["reciImag"]["name"])) {
	if(uploadImage($_FILES["reciImag"],$reciCode) == 0) $response["message"] .= " La imatge no s'ha pogut guardar.";
}
   
//Funció per pujar la nova imatge al servidor i actualitzar la BBDD
function uploadImage($file,$reciCode) {	
	$bbdd = new BBDD();
	
	$uploadOk = 1;
	
	$target_dir = "../uploads/recipes/";
	$newFile = date("U") . "_" . $file["name"];
	$target_file = $target_dir . $newFile;
	$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
	$check = getimagesize($file["tmp_name"]);
	if ($check !== false) {
		$uploadOk = 1;
	} else {
		$uploadOk = 0;
	}
	if (file_exists($target_file)) {
		$uploadOk = 0;
	}
	if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif") {
		$uploadOk = 0;
	}
	if ($uploadOk != 0) {
		//Actualitzar la base de dades si es puja la nova imatge
		if(move_uploaded_file($file["tmp_name"], $target_file)) {
			$imagCode = $bbdd->insertID("Images","imagPath","'".$newFile."'");
			$result = $bbdd->update("Recipes","reciImage = '".$imagCode."'","reciIden = '".$reciCode."'");
		}
	} 
	
	return $uploadOk;
}

//Retorna els resultats en Json 
echo json_encode($response,JSON_UNESCAPED_UNICODE);
?>