diff --git a/bin/settings.js b/bin/settings.js index 4693a12..6f8071b 100644 --- a/bin/settings.js +++ b/bin/settings.js @@ -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(){ + $('').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(); + }); }); diff --git a/php/classes.recipe.php b/php/classes.recipe.php index a4b1ec3..666f88d 100644 --- a/php/classes.recipe.php +++ b/php/classes.recipe.php @@ -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"])."')"); diff --git a/php/edit-recipes.php b/php/edit-recipes.php index 857bda4..aa068c6 100644 --- a/php/edit-recipes.php +++ b/php/edit-recipes.php @@ -32,6 +32,10 @@ echo json_encode($book); break; + case 'import': + $book->importCookbook(); + break; + default: // code... break;