134 lines
4.5 KiB
PHP
134 lines
4.5 KiB
PHP
<?php
|
|
|
|
class Recipes {
|
|
static function getAll(): array{
|
|
global $user_id, $db;
|
|
$return = array();
|
|
|
|
$sql = "SELECT * FROM recipe WHERE public = 1 OR author = $user_id;";
|
|
$res = $db->query($sql);
|
|
while($row = $res->fetch_assoc()){
|
|
$return[$row['recipe_id']] = $row;
|
|
unset($return[$row['recipe_id']]['author']);
|
|
if($row['author'] == $user_id){
|
|
$return[$row['recipe_id']]['owner'] = true;
|
|
}
|
|
|
|
$itemsRes = $db->query("SELECT * FROM recipe_item WHERE `recipe_id` = '$row[recipe_id]'");
|
|
while ($item = $itemsRes->fetch_assoc()){
|
|
$return[$row['recipe_id']]['items'][] = $item;
|
|
}
|
|
}
|
|
|
|
if(empty($return)){
|
|
$return[] = "No recipes found";
|
|
}
|
|
|
|
return $return;
|
|
}
|
|
|
|
static function createRecipe($name, $portions = 1, $public = 0): bool {
|
|
global $user_id, $db;
|
|
|
|
if($portions == null){
|
|
$portions = 1;
|
|
}
|
|
|
|
if(strlenBetween($name,2,200) && is_numeric($public)){
|
|
$createRecipeSQL = "INSERT INTO `recipe` (name, author, portions, public) VALUES ('$name', $user_id, $portions, $public);";
|
|
if($db->query($createRecipeSQL)){
|
|
return $db->insert_id;
|
|
}
|
|
else {
|
|
// file_put_contents('test.txt', $createRecipeSQL);
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
|
|
private $recipeId;
|
|
|
|
function __construct($recipeID = 0){
|
|
global $db, $user_id;
|
|
|
|
if(!is_numeric($recipeID)){ return false; }
|
|
|
|
$checkRecipeOwnerRes = $db->query("SELECT `recipe_id` FROM recipe WHERE `recipe_id` = '$recipeID' AND `author` = '$user_id' LIMIT 1");
|
|
if($checkRecipeOwnerRes->fetch_row()[0] == $recipeID){
|
|
$this->recipeId = $recipeID;
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function addItemToRecipe($name, $amount, $price = 0): bool{
|
|
global $db;
|
|
|
|
if((is_numeric($name) || (strlen($name) > 0 && strlen($name) < 255)) && $amount > 0 && $amount < 99){
|
|
$nextItemIdQue = $db->query('SELECT count(0)+1 FROM recipe_item WHERE `recipe_id` = '.$this->recipeId);
|
|
$nextItemId = $nextItemIdQue->fetch_row()[0];
|
|
if(is_numeric($name)){
|
|
$addItemSql = "INSERT INTO `recipe_item` (recipe_id, item_num, name, price, amount, item_id) VALUES (".verifyRecipeOwnerSQL($this->recipeId).", $nextItemId, '$name', $price, $amount, $name)";
|
|
}
|
|
else {
|
|
$addItemSql = "INSERT INTO `recipe_item` (recipe_id, item_num, name, price, amount) VALUES (".verifyRecipeOwnerSQL($this->recipeId).", $nextItemId, '$name', $price, $amount)";
|
|
}
|
|
|
|
if($db->query($addItemSql)){
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function editRecipeItem($recipe_item_id, $newName, $newPrice, $newAmount, $newItemId = null): bool{
|
|
global $db;
|
|
|
|
if($newName && strlenBetween($newName,0,255)){ $set[] = "`name` = '$newName'"; }
|
|
if(is_numeric($newPrice)){ $set[] = "`price` = '$newPrice'"; }
|
|
if($newAmount && $newAmount > 0 && $newAmount < 99){ $set[] = "`amount` = '$newAmount'"; }
|
|
if(is_numeric($newItemId)){ $set[] = "`item_id` = '$newItemId'"; }
|
|
|
|
if(!empty($set)){
|
|
$editItemSql = "UPDATE `recipe_item` SET ".implode(', ', $set)." WHERE `recipe_id` = $this->recipeId AND `recipe_item_id` = '$recipe_item_id';";
|
|
|
|
// file_put_contents('test.txt', $editItemSql);
|
|
if($db->query($editItemSql)){
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function deleteRecipeItem($recipe_item_id, $name): bool{
|
|
global $db;
|
|
|
|
$deleteItemSql = "DELETE FROM `recipe_item` WHERE recipe_id = $this->recipeId AND `recipe_item_id` = '$recipe_item_id' AND name LIKE '$name' LIMIT 1";
|
|
|
|
if($db->query($deleteItemSql) && $db->affected_rows > 0){
|
|
return true;
|
|
}
|
|
else {
|
|
returnsErr("No rows deleted");
|
|
}
|
|
|
|
// file_put_contents('test.txt', $deleteItemSql);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function verifyRecipeOwnerSQL($recipeId): string{
|
|
global $user_id;
|
|
return "(SELECT `recipe_id` FROM recipe WHERE `recipe_id` = '$recipeId' AND `author` = '$user_id' LIMIT 1)";
|
|
}
|
|
|
|
function strlenBetween($str, $min, $max): bool{
|
|
return strlen($str) > $min && strlen($str) <= $max;
|
|
}
|
|
|
|
// file_put_contents('test.txt', $addItemSql);
|