-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecipe.php
More file actions
88 lines (65 loc) · 2.55 KB
/
recipe.php
File metadata and controls
88 lines (65 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
//this is a function to check if the user is an actual user or a visitor
require_once('src/authenticate/requiresLogin.php');
//it can relocate the user if they aren't signed in yet
requiresLogin();
require_once('src/templating/ViewGenerator.php');
require_once('src/model/recipe.php');
require_once('src/database_handler/DH_recipe.php');
require_once('src/database_handler/DH_user.php');
require_once('src/model/user.php');
require_once('src/model/foodListItem.php');
$database_handler = new DH_recipe();
$DH = new DH_user();
$user = $DH->getUser($_SESSION["user_id"]);
//Here we check if the user owns the recipe if so we load it form the database
$id = $_GET["id"];
if (in_array($id, $user->recipes)) {
$recipe = $database_handler->getRecipe($id);
} else {
//this recipe is not yours.
//TODO: ReMOVe!!
$recipe = $database_handler->getRecipe($id);
}
if (isset($_POST["addAll"])) {
//get a list of all the ingredients
//add them one by one
foreach ($recipe->ingredients as $i) {
$movingItem = new FoodListItem();
$movingItem->picture = FoodListItem::$baseImage;
$spaceCount = substr_count($i, ' ');
if ($spaceCount == 0) {
$movingItem->name = "";
$movingItem->amount = $i;
} else if ($spaceCount == 1) {
$strings = explode(" ", $i);
$movingItem->name = $strings[1];
$movingItem->amount = $strings[0];
} else if ($spaceCount == 2) {
$strings = preg_split('/ /', $i);
$movingItem->name = $strings[2] . " " . $strings[3];
$movingItem->amount = $strings[0] . " " . $strings[1];
} else if ($spaceCount == 3) {
$strings = preg_split('/ /', $i);
$movingItem->name = $strings[2] . " " . $strings[3] . " " . $strings[4];
$movingItem->amount = $strings[0] . " " . $strings[1];
} elseif ($spaceCount > 3) {
$movingItem->name = "";
$movingItem->amount = $i;
}
$user->addItem(0, $movingItem);
}
$DH->updateUser($user);
header("Location: shopping_list.php");
}
if (isset($_POST["delete"])) {
$i = array_search($recipe->id, $user->recipes);
array_splice($user->recipes, $i, 1);
$DH->updateUser($user);
$database_handler->deleteRecipe($recipe);
echo "asdasd";
header("Location: recipe_list.php");
}
//generates the recipe().php missing parts of the page, this file is the controller for the recipe_view
$generator = new ViewGenerator();
$generator->generate("recipe_view", $recipe);