shopping-list/php/classes.recipe.php

201 lines
7.8 KiB
PHP
Raw Normal View History

2018-10-24 15:00:27 +02:00
<?php
class unit {
public $ID, $Name, $Standard;
}
class unitList {
public $units = array();
function addItem($ID, $Name, $Standard){
$unit = new unit;
$unit->ID = $ID;
$unit->Name = $Name;
$unit->Standard = $Standard;
array_push($this->units, $unit);
}
function unitList(){
include $_SESSION["docroot"].'/php/connect.php';
$result = $mysqli->query("SELECT * FROM `Einheit`");
while($item = $result->fetch_assoc()){
$this->addItem($item["ID"], $item["Name"], $item["Standard"]);
}
$mysqli->close();
}
2018-11-14 16:08:36 +01:00
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"];
}
2018-10-24 15:00:27 +02:00
}
class ingredient {
public $ID, $Menge, $Einheit, $Name;
function ingredient($ID, $Menge, $Einheit, $Name){
$this->ID = $ID;
$this->Menge = $Menge;
$this->Einheit = $Einheit;
$this->Name = $Name;
}
}
class recipe {
public $ID, $Name, $Dauer, $Beschreibung;
public $Zutaten = array();
function recipe($ID, $Name, $Dauer, $Beschreibung){
$this->ID = $ID;
$this->Name = $Name;
$this->Dauer = $Dauer;
$this->Beschreibung = $Beschreibung;
}
function addIngredient($ID, $Menge, $Einheit, $Name){
$current = new ingredient($ID, $Menge, $Einheit, $Name);
array_push($this->Zutaten, $current);
}
}
class cookbook {
public $sites = array();
function getRecipe($ID){
include $_SESSION["docroot"].'/php/connect.php';
$recipes = $mysqli->query("SELECT * FROM `Rezept` WHERE `ID` = $ID ORDER BY Name ASC");
while($recipe = $recipes->fetch_assoc()){
$current = new recipe($RID = $recipe["ID"], $RName = $recipe["Name"], $RDuration = $recipe["Dauer"], $RDescription = $recipe["Beschreibung"]);
$recepieIngredients = $mysqli->query("SELECT * FROM `RezeptZutat` WHERE `Rezept` = $RID");
while($recepieIngredient = $recepieIngredients->fetch_assoc()){
$IID = $recepieIngredient["Zutat"];
$IAmount = $recepieIngredient["Menge"];
$units = $mysqli->query("SELECT `Name` FROM `Einheit` WHERE `ID` = ".$recepieIngredient["Einheit"]);
while($unit = $units->fetch_assoc()){$IUnit = $unit["Name"];}
$names = $mysqli->query("SELECT `Name` FROM `Zutat` WHERE `ID` = ".$recepieIngredient["Zutat"]);
while($name = $names->fetch_assoc()){$IName = $name["Name"];}
$current->addIngredient($IID, $IAmount, $IUnit, $IName);
}
array_push($this->sites, $current);
}
$mysqli->close();
}
function fillCookbook(){
include $_SESSION["docroot"].'/php/connect.php';
$recipes = $mysqli->query("SELECT * FROM `Rezept` ORDER BY Name ASC");
while($recipe = $recipes->fetch_assoc()){
$current = new recipe($RID = $recipe["ID"], $RName = $recipe["Name"], $RDuration = $recipe["Dauer"], $RDescription = $recipe["Beschreibung"]);
$recepieIngredients = $mysqli->query("SELECT * FROM `RezeptZutat` WHERE `Rezept` = $RID");
while($recepieIngredient = $recepieIngredients->fetch_assoc()){
$IID = $recepieIngredient["Zutat"];
$IAmount = $recepieIngredient["Menge"];
$units = $mysqli->query("SELECT `Name` FROM `Einheit` WHERE `ID` = ".$recepieIngredient["Einheit"]);
while($unit = $units->fetch_assoc()){$IUnit = $unit["Name"];}
$names = $mysqli->query("SELECT `Name` FROM `Zutat` WHERE `ID` = ".$recepieIngredient["Zutat"]);
while($name = $names->fetch_assoc()){$IName = $name["Name"];}
$current->addIngredient($IID, $IAmount, $IUnit, $IName);
}
array_push($this->sites, $current);
}
$mysqli->close();
}
2018-11-14 16:08:36 +01:00
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));
}
}
}
2018-10-24 15:00:27 +02:00
function newRecipe($Name, $Dauer, $Beschreibung, $Zutaten){
include $_SESSION["docroot"].'/php/connect.php';
2018-11-14 16:08:36 +01:00
$mysqli->query("INSERT INTO `Rezept` (`Name`, `Dauer`, `Beschreibung`) VALUES ('$Name', '$Dauer', '$Beschreibung')");
2018-10-24 15:00:27 +02:00
$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){
2018-11-14 16:08:36 +01:00
$item = $result->fetch_assoc();
$ZutatID = $item["ID"];
2018-10-24 15:00:27 +02:00
}
else{
$mysqli->query("INSERT INTO `Zutat` (`Name`) VALUES ('".ucwords($Zutat["Name"])."')");
$ZutatID = $mysqli->insert_id;
}
$mysqli->query("INSERT INTO `RezeptZutat` (`Rezept`,`Menge`,`Einheit`,`Zutat`) VALUES ('{$RezeptID}','{$Zutat["Amount"]}','{$Zutat["Unit"]}','{$ZutatID}');");
}
$mysqli->close();
}
function updateRecipe($ID, $Name, $Dauer, $Beschreibung, $Zutaten){
include $_SESSION["docroot"].'/php/connect.php';
$mysqli->query("UPDATE `Rezept` SET `Name` = '$Name', `Dauer` = '$Dauer', `Beschreibung` = '$Beschreibung' WHERE `Rezept`.`ID` = $ID;");
$mysqli->query("DELETE FROM RezeptZutat WHERE Rezept = $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"];}
}
else{
$mysqli->query("INSERT INTO `Zutat` (`Name`) VALUES ('".ucwords($Zutat["Name"])."')");
2018-10-26 15:19:44 +02:00
$ZutatID = $mysqli->insert_id;
2018-10-24 15:00:27 +02:00
}
$mysqli->query("INSERT INTO `RezeptZutat` (`Rezept`,`Menge`,`Einheit`,`Zutat`) VALUES ('{$ID}','{$Zutat["Amount"]}','{$Zutat["Unit"]}','{$ZutatID}');");
}
}
function removeRecipe($ID){
include $_SESSION["docroot"].'/php/connect.php';
$mysqli->query("DELETE FROM `RezeptZutat` WHERE `Rezept`=$ID");
$mysqli->query("DELETE FROM Rezept WHERE ID=$ID");
$mysqli->close();
}
2019-05-21 09:21:59 +02:00
function getAllIngredientsContaining($q){
2018-10-24 15:00:27 +02:00
include $_SESSION["docroot"].'/php/connect.php';
$values = array();
2019-05-21 09:21:59 +02:00
$result = $mysqli->query("SELECT Name FROM Zutat WHERE Name LIKE '%$q%' ORDER BY Name ASC");
2018-10-24 15:00:27 +02:00
while($item = $result->fetch_assoc()){
2019-05-19 20:31:37 +02:00
array_push($values, $item["Name"]);
2018-10-24 15:00:27 +02:00
}
2019-05-19 20:31:37 +02:00
print_r(json_encode($values));
2018-10-24 15:00:27 +02:00
$mysqli->close();
}
function getRecipeNames(){
include $_SESSION["docroot"].'/php/connect.php';
$result = $mysqli->query("SELECT ID, Name FROM `Rezept` ORDER BY Name ASC");
$recipeList = array();
while ($item = $result->fetch_assoc()) {
$recipeList[$item["ID"]] = $item["Name"];
}
$mysqli->close();
return $recipeList;
}
}
?>