implemented recipe import

This commit is contained in:
Krehan Tim 2018-11-14 16:08:36 +01:00
parent ddbdcaa728
commit 2acc164365
3 changed files with 95 additions and 2 deletions

View file

@ -56,9 +56,56 @@ $(document).ready(function(){
downloadObjectAsJson(JSON.parse(data), "recipes");
});
});
$("#export-list-button").click(function(){
$.post("/php/edit-list.php", {function:"export"}, function(data){
downloadObjectAsJson(JSON.parse(data), "list");
});
});
$("#import-button").click(function(){
$('<input type="file" accept=".json">').on('change', function () {
var file = this.files[0];
var reader = new FileReader();
reader.onload = function(){
var content = JSON.parse(reader.result);
if(content.sites!=null){
$.post("/php/edit-recipes.php",
{
function: "import",
content: reader.result
},
function(data){
if(data==0){
infoPopUp("Alle Rezepte erfolgreich Importiert!");
}
else{
infoPopUp("Nicht alle Rezepte konnten Importiert werden!");
downloadObjectAsJson(JSON.parse(data), "failed_recipe_import.json");
}
}
);
}
else if(content.list!=null){
$.post("/php/edit-list.php",
{
function: "import",
content: reader.result
},
function(data){
console.log(data);
// if(data==0){
// infoPopUp("Alle Rezepte erfolgreich Importiert!");
// }
// else{
// infoPopUp("Nicht alle Rezepte konnten Importiert werden!");
// downloadObjectAsJson(JSON.parse(data), "failed_recipe_import.json");
// }
}
);
}
};
reader.readAsText(file);
}).click();
});
});

View file

@ -20,6 +20,12 @@
}
$mysqli->close();
}
function getID($Name){
include $_SESSION["docroot"].'/php/connect.php';
$result = $mysqli->query("SELECT `ID` FROM `Einheit` WHERE `Name` = '$Name'");
$ID = $result->fetch_assoc();
return $ID["ID"];
}
}
class ingredient {
@ -89,15 +95,51 @@
$mysqli->close();
}
function importCookbook(){
include $_SESSION["docroot"].'/php/connect.php';
$units = new unitList();
$failed_sites = array();
$succeeded_sites = array();
$import = json_decode($_POST["content"]);
if($import->sites!=null){
foreach ($import->sites as $site) {
$result = $mysqli->query("SELECT * FROM `Rezept` WHERE `Name`='$site->Name'");
if($result->num_rows>0){
array_push($failed_sites, $site);
}
else{
array_push($succeeded_sites, $site);
$Zutaten = array();
foreach($site->Zutaten as $Zutat) {
$nZutat = null;
$nZutat["ID"] = $Zutat->ID;
$nZutat["Amount"] = $Zutat->Menge;
$nZutat["Unit"] = $units->getID($Zutat->Einheit);
$nZutat["Name"] = $Zutat->Name;
array_push($Zutaten, $nZutat);
}
$this->newRecipe($site->Name, $site->Dauer, $site->Beschreibung, $Zutaten);
}
}
if(sizeof($failed_sites)==0){
print_r("0");
}
else{
print_r(json_encode($failed_sites));
}
}
}
function newRecipe($Name, $Dauer, $Beschreibung, $Zutaten){
include $_SESSION["docroot"].'/php/connect.php';
$mysqli->query("INSERT INTO Rezept (Name, Dauer, Beschreibung) VALUES ('$Name', '$Dauer', '$Beschreibung')");
$mysqli->query("INSERT INTO `Rezept` (`Name`, `Dauer`, `Beschreibung`) VALUES ('$Name', '$Dauer', '$Beschreibung')");
$RezeptID = $mysqli->insert_id;
foreach ($Zutaten as $Zutat) {
$ZutatID = null;
$result = $mysqli->query("SELECT ID FROM `Zutat` WHERE `Name` LIKE '".$Zutat["Name"]."'");
if($result->num_rows>0){
while($item = $result->fetch_assoc()){$ZutatID = $item["ID"];}
$item = $result->fetch_assoc();
$ZutatID = $item["ID"];
}
else{
$mysqli->query("INSERT INTO `Zutat` (`Name`) VALUES ('".ucwords($Zutat["Name"])."')");

View file

@ -32,6 +32,10 @@
echo json_encode($book);
break;
case 'import':
$book->importCookbook();
break;
default:
// code...
break;