shopping-list/php/classes.recipe.php

259 lines
11 KiB
PHP
Raw Normal View History

2018-10-24 15:00:27 +02:00
<?php
class unit {
public $ID, $Name, $Standard;
function __construct(){}
2018-10-24 15:00:27 +02:00
}
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 __construct(){
2018-10-24 15:00:27 +02:00
include $_SESSION["docroot"].'/php/connect.php';
2019-05-22 10:16:13 +02:00
$result = $mysqli->query("SELECT * FROM `Einheit`;");
2018-10-24 15:00:27 +02:00
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';
2019-05-22 10:16:13 +02:00
$selectQuery = $mysqli->prepare("SELECT `ID` FROM `Einheit` WHERE `Name` = ?;");
2019-06-03 16:02:41 +02:00
$selectQuery->bind_param("s", $Name);
2019-05-22 10:16:13 +02:00
$selectQuery->execute();
$result = $selectQuery->get_result();
2018-11-14 16:08:36 +01:00
$ID = $result->fetch_assoc();
return $ID["ID"];
}
2018-10-24 15:00:27 +02:00
}
class ingredient {
public $ID, $Menge, $Einheit, $Name;
function __construct($ID, $Menge, $Einheit, $Name){
2018-10-24 15:00:27 +02:00
$this->ID = $ID;
$this->Menge = $Menge;
$this->Einheit = $Einheit;
$this->Name = $Name;
}
}
class recipe {
public $ID, $Name, $Dauer, $Beschreibung;
public $Zutaten = array();
function __construct($ID, $Name, $Dauer, $Beschreibung){
2018-10-24 15:00:27 +02:00
$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';
2019-05-22 10:16:13 +02:00
$selectQuery = $mysqli->prepare("SELECT * FROM `Rezept` WHERE `ID` = ? ORDER BY Name ASC;");
$selectQuery->bind_param("s", $ID);
$selectQuery->execute();
$recipes = $selectQuery->get_result();
2018-10-24 15:00:27 +02:00
while($recipe = $recipes->fetch_assoc()){
$current = new recipe($RID = $recipe["ID"], $RName = $recipe["Name"], $RDuration = $recipe["Dauer"], $RDescription = $recipe["Beschreibung"]);
2019-05-22 10:16:13 +02:00
$selectIngredientsQuery = $mysqli->prepare("SELECT * FROM `RezeptZutat` WHERE `Rezept` = ?;");
$selectIngredientsQuery->bind_param("s", $RID);
$selectIngredientsQuery->execute();
$recepieIngredients = $selectIngredientsQuery->get_result();
2018-10-24 15:00:27 +02:00
while($recepieIngredient = $recepieIngredients->fetch_assoc()){
$IID = $recepieIngredient["Zutat"];
$IAmount = $recepieIngredient["Menge"];
2019-05-22 10:16:13 +02:00
$selectUnitQuery = $mysqli->prepare("SELECT `Name` FROM `Einheit` WHERE `ID` = ?;");
$selectUnitQuery->bind_param("s", $recepieIngredient["Einheit"]);
$selectUnitQuery->execute();
$units = $selectUnitQuery->get_result();
2018-10-24 15:00:27 +02:00
while($unit = $units->fetch_assoc()){$IUnit = $unit["Name"];}
2019-05-22 10:16:13 +02:00
$selectNamesQuery = $mysqli->prepare("SELECT `Name` FROM `Zutat` WHERE `ID` = ?;");
$selectNamesQuery->bind_param("s", $recepieIngredient["Zutat"]);
$selectNamesQuery->execute();
$names = $selectNamesQuery->get_result();
2018-10-24 15:00:27 +02:00
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"]);
2019-05-22 10:16:13 +02:00
$selectIngredientsQuery = $mysqli->prepare("SELECT * FROM `RezeptZutat` WHERE `Rezept` = ?;");
$selectIngredientsQuery->bind_param("s", $RID);
$selectIngredientsQuery->execute();
$recepieIngredients = $selectIngredientsQuery->get_result();
2018-10-24 15:00:27 +02:00
while($recepieIngredient = $recepieIngredients->fetch_assoc()){
$IID = $recepieIngredient["Zutat"];
$IAmount = $recepieIngredient["Menge"];
2019-05-22 10:16:13 +02:00
$selectUnitQuery = $mysqli->prepare("SELECT `Name` FROM `Einheit` WHERE `ID` = ?;");
$selectUnitQuery->bind_param("s", $recepieIngredient["Einheit"]);
$selectUnitQuery->execute();
$units = $selectUnitQuery->get_result();
2018-10-24 15:00:27 +02:00
while($unit = $units->fetch_assoc()){$IUnit = $unit["Name"];}
2019-05-22 10:16:13 +02:00
$selectNamesQuery = $mysqli->prepare("SELECT `Name` FROM `Zutat` WHERE `ID` = ?;");
$selectNamesQuery->bind_param("s", $recepieIngredient["Zutat"]);
$selectNamesQuery->execute();
$names = $selectNamesQuery->get_result();
2018-10-24 15:00:27 +02:00
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) {
2019-05-22 10:16:13 +02:00
$selectQuery = $mysqli->prepare("SELECT * FROM `Rezept` WHERE `Name`=?;");
$selectQuery->bind_param("s", $site->Name);
$selectQuery->execute();
$result = $selectQuery->get_result();
2018-11-14 16:08:36 +01:00
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';
2019-05-22 10:16:13 +02:00
$insertQuery = $mysqli->prepare("INSERT INTO `Rezept` (`Name`, `Dauer`, `Beschreibung`) VALUES (?, ?, ?);");
$insertQuery->bind_param("sss", $Name, $Dauer, $Beschreibung);
$insertQuery->execute();
2018-10-24 15:00:27 +02:00
$RezeptID = $mysqli->insert_id;
foreach ($Zutaten as $Zutat) {
$ZutatID = null;
2019-05-22 10:16:13 +02:00
$selectIngredientsQuery = $mysqli->prepare("SELECT ID FROM `Zutat` WHERE `Name` LIKE ?;");
$selectIngredientsQuery->bind_param("s", $Zutat["Name"]);
$selectIngredientsQuery->execute();
$result = $selectIngredientsQuery->get_result();
2018-10-24 15:00:27 +02:00
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{
2019-05-22 10:16:13 +02:00
$UppercaseName = ucwords($Zutat["Name"]);
$insertIngredientsQuery = $mysqli->prepare("INSERT INTO `Zutat` (`Name`) VALUES (?);");
$insertIngredientsQuery->bind_param("s", $UppercaseName);
$insertIngredientsQuery->execute();
2018-10-24 15:00:27 +02:00
$ZutatID = $mysqli->insert_id;
}
2019-05-22 10:16:13 +02:00
$inserRecipeQuery = $mysqli->prepare("INSERT INTO `RezeptZutat` (`Rezept`,`Menge`,`Einheit`,`Zutat`) VALUES (?,?,?,?);");
$inserRecipeQuery->bind_param("ssss", $RezeptID, $Zutat["Amount"], $Zutat["Unit"], $ZutatID);
$inserRecipeQuery->execute();
2018-10-24 15:00:27 +02:00
}
$mysqli->close();
}
function updateRecipe($ID, $Name, $Dauer, $Beschreibung, $Zutaten){
include $_SESSION["docroot"].'/php/connect.php';
2019-05-22 10:16:13 +02:00
$updateQuery = $mysqli->prepare("UPDATE `Rezept` SET `Name` = ?, `Dauer` = ?, `Beschreibung` = ? WHERE `Rezept`.`ID` = ?;");
$updateQuery->bind_param("ssss", $Name, $Dauer, $Beschreibung, $ID);
$updateQuery->execute();
$deleteQuery = $mysqli->prepare("DELETE FROM RezeptZutat WHERE Rezept = ?;");
$deleteQuery->bind_param("s", $ID);
$deleteQuery->execute();
2018-10-24 15:00:27 +02:00
foreach ($Zutaten as $Zutat) {
$ZutatID = null;
2019-05-22 10:16:13 +02:00
$selectIngredientsQuery = $mysqli->prepare("SELECT ID FROM `Zutat` WHERE `Name` LIKE ?;");
$selectIngredientsQuery->bind_param("s", $Zutat["Name"]);
$selectIngredientsQuery->execute();
$result = $selectIngredientsQuery->get_result();
2018-10-24 15:00:27 +02:00
if($result->num_rows>0){
while($item = $result->fetch_assoc()){ $ZutatID = $item["ID"];}
}
else{
2019-05-22 10:16:13 +02:00
$uppercaseName = ucwords($Zutat["Name"]);
$insertIngredientsQuery = $mysqli->prepare("INSERT INTO `Zutat` (`Name`) VALUES (?);");
$insertIngredientsQuery->bind_param("s", $uppercaseName);
$insertIngredientsQuery->execute();
2018-10-26 15:19:44 +02:00
$ZutatID = $mysqli->insert_id;
2018-10-24 15:00:27 +02:00
}
2019-05-22 10:16:13 +02:00
$insertRecipeIngredientsQuery = $mysqli->prepare("INSERT INTO `RezeptZutat` (`Rezept`,`Menge`,`Einheit`,`Zutat`) VALUES (?,?,?,?);");
$insertRecipeIngredientsQuery->bind_param("ssss", $ID, $Zutat["Amount"], $Zutat["Unit"], $ZutatID);
$insertRecipeIngredientsQuery->execute();
2018-10-24 15:00:27 +02:00
}
}
function removeRecipe($ID){
include $_SESSION["docroot"].'/php/connect.php';
2019-05-22 10:16:13 +02:00
$deleteQuery = $mysqli->prepare("DELETE FROM `RezeptZutat` WHERE `Rezept`=?;");
$deleteQuery->bind_param("s", $ID);
$deleteQuery->execute();
$deleteQuery = $mysqli->prepare("DELETE FROM Rezept WHERE ID=?;");
$deleteQuery->bind_param("s", $ID);
$deleteQuery->execute();
2018-10-24 15:00:27 +02:00
$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-22 10:16:13 +02:00
$filterValue = "%$q%";
$selectQuery = $mysqli->prepare("SELECT Name FROM Zutat WHERE Name LIKE ? ORDER BY Name ASC");
$selectQuery->bind_param("s", $filterValue);
$selectQuery->execute();
$result = $selectQuery->get_result();
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;
}
}
?>