mirror of
https://github.com/tim-krehan/shopping-list.git
synced 2024-11-27 15:40:00 +01:00
implemented recipe import
This commit is contained in:
parent
ddbdcaa728
commit
2acc164365
3 changed files with 95 additions and 2 deletions
|
@ -56,9 +56,56 @@ $(document).ready(function(){
|
||||||
downloadObjectAsJson(JSON.parse(data), "recipes");
|
downloadObjectAsJson(JSON.parse(data), "recipes");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
$("#export-list-button").click(function(){
|
$("#export-list-button").click(function(){
|
||||||
$.post("/php/edit-list.php", {function:"export"}, function(data){
|
$.post("/php/edit-list.php", {function:"export"}, function(data){
|
||||||
downloadObjectAsJson(JSON.parse(data), "list");
|
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();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -20,6 +20,12 @@
|
||||||
}
|
}
|
||||||
$mysqli->close();
|
$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 {
|
class ingredient {
|
||||||
|
@ -89,15 +95,51 @@
|
||||||
$mysqli->close();
|
$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){
|
function newRecipe($Name, $Dauer, $Beschreibung, $Zutaten){
|
||||||
include $_SESSION["docroot"].'/php/connect.php';
|
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;
|
$RezeptID = $mysqli->insert_id;
|
||||||
foreach ($Zutaten as $Zutat) {
|
foreach ($Zutaten as $Zutat) {
|
||||||
$ZutatID = null;
|
$ZutatID = null;
|
||||||
$result = $mysqli->query("SELECT ID FROM `Zutat` WHERE `Name` LIKE '".$Zutat["Name"]."'");
|
$result = $mysqli->query("SELECT ID FROM `Zutat` WHERE `Name` LIKE '".$Zutat["Name"]."'");
|
||||||
if($result->num_rows>0){
|
if($result->num_rows>0){
|
||||||
while($item = $result->fetch_assoc()){$ZutatID = $item["ID"];}
|
$item = $result->fetch_assoc();
|
||||||
|
$ZutatID = $item["ID"];
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
$mysqli->query("INSERT INTO `Zutat` (`Name`) VALUES ('".ucwords($Zutat["Name"])."')");
|
$mysqli->query("INSERT INTO `Zutat` (`Name`) VALUES ('".ucwords($Zutat["Name"])."')");
|
||||||
|
|
|
@ -32,6 +32,10 @@
|
||||||
echo json_encode($book);
|
echo json_encode($book);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 'import':
|
||||||
|
$book->importCookbook();
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
// code...
|
// code...
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Reference in a new issue