/
controller.php
380 lines (354 loc) · 9.98 KB
/
controller.php
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<?php
//ajax handling
if(isset($_POST['action'])) {
session_start();
$action = $_POST['action'];
if(isset($_POST['recipeId'])){
$recipeId = $_POST['recipeId'];
}
if(isset($_POST['type'])){
$type = $_POST['type'];
}
$username = $_SESSION["username"];
include ('connection.php');
switch($action){
case 'save':
setFavorite(1, $username, $recipeId);
break;
case 'delete':
setFavorite(0, $username, $recipeId);
break;
case 'byName':
if($type == "myFridge"){
recipes($username);
}
else if($type == "quick"){
quickRecipes(15);
}
else{
searchRecipes($type);
}
break;
case 'byRating':
if($type == "myFridge"){
recipes($username);
}
else if($type == "quick"){
quickRecipes(15);
}
else{
searchRecipes($type);
}
break;
case 'getRecipesList':
recipes($username);
break;
case 'recipeDetails':
getRecipe($recipeId);
break;
case 'saveRecipe':
$id = $_POST['id'];
$name = $_POST['name'];
$ingredients = stripslashes($_POST['ingredients']);
$pic_url = $_POST['pic'];
saveRecipe($id, $name, $username, $pic_url, $ingredients);
break;
case 'getOuickRecipes':
quickRecipes(15);
break;
case 'searchRecipes':
$searchParam = $_POST['search'];
searchRecipes($searchParam);
break;
}
}
$data;
$prods = array();
$amount = array();
$unit = array();
$expiry = array();
$place = array();
$user = array();
$recipe_pic;
$recipes_ingredients= array();
$recipes_prep= array();
$recipe_rating;
$recipe_preptime;
$recipeId;
$recipeData;
$title;
$prep_time;
$total_time;
$recipe_pic;
$rating;
$missing;
/*Authenticate user*/
function authenticate($username, $password){
include ('connection.php');
if(isset($connection)){
$query = "SELECT password FROM tbl_users_213 WHERE user_name = '$username'";
$result = mysqli_query($connection, $query);
}
$row = mysqli_fetch_assoc($result);
if(isset($connection)){
mysqli_close($connection);
unset($connection);
}
if ($row){
if($row["password"] === $password){
return "OK";
}
else return "Wrong password";
}
else return "User not found";
}
/*Create new user*/
function createUser($username, $password){
include ('connection.php');
$name = "";
$gluten = 0;
$dairy = 0;
$vegeterian = 0;
if(isset($_POST['name'])) $name = $_POST['name'];
foreach ($_POST['Preferences'] as $value) {
if($value == "Gluten Free") $gluten = 1;
if($value == "Dairy Free") $dairy = 1;
if($value == "Vegetarian") $vegeterian = 1;
}
if(isset($connection)){
$query = "SELECT user_name FROM tbl_users_213 WHERE user_name = '$username'";
$result = mysqli_query($connection, $query);
}
$row = mysqli_fetch_assoc($result);
if (! $row){
$query = "INSERT INTO tbl_users_213(user_name, password, name, gluten, dairy, vegeterian)
VALUES('$username', '$password', '$name', '$gluten', '$dairy', '$vegeterian')";
if(isset($connection)){
mysqli_query($connection, $query);
mysqli_close($connection);
unset($connection);
}
return "OK";
}
else return "User already exists";
}
/*Get user's profile*/
function profile($username){
include ('connection.php');
if(isset($connection)){
$query = "SELECT * FROM tbl_users_213 WHERE user_name= '$username'";
$result = mysqli_query($connection, $query);
}
$index = 0;
while ($row = mysqli_fetch_assoc($result)) {
$GLOBALS['user'][$index++] = $row["name"];
$GLOBALS['user'][$index++] = $row["user_name"];
$GLOBALS['user'][$index++] = $row["gluten"];
$GLOBALS['user'][$index++] = $row["dairy"];
$GLOBALS['user'][$index] = $row["vegeterian"];
}
if (isset($connection)){
mysqli_close($connection);
unset($connection);
}
}
/*Profile change*/
function changeProfile($username){
include ('connection.php');
$name = "";
$gluten = 0;
$dairy = 0;
$vegeterian = 0;
if(isset($_POST['name'])) $name = $_POST['name'];
if(isset($_POST['Preferences'])){
foreach ($_POST['Preferences'] as $value) {
if($value == "Gluten Free") $gluten = 1;
if($value == "Dairy Free") $dairy = 1;
if($value == "Vegetarian") $vegeterian = 1;
}
}
if(isset($connection)){
if($name != ""){
$query = "UPDATE tbl_users_213
SET name = '$name', gluten = '$gluten', dairy = '$dairy', vegeterian = '$vegeterian'
WHERE user_name = '$username'";
}
else{
$query = "UPDATE tbl_users_213
SET gluten = '$gluten', dairy = '$dairy', vegeterian = '$vegeterian'
WHERE user_name = '$username'";
}
$result = mysqli_query($connection, $query);
if(isset($connection)){
mysqli_query($connection, $query);
mysqli_close($connection);
unset($connection);
}
}
}
function setFavorite($status, $username, $recipeId){
include ('connection.php');
if(isset($connection)){
$query = "UPDATE tbl_recipes_213
SET favorite = '$status'
WHERE user_name = '$username'
AND rId = '$recipeId'";
mysqli_query($connection, $query);
mysqli_close($connection);
unset($connection);
}
}
/*Log out*/
function logOut(){
session_unset();
session_destroy();
header('Location: index.html');
}
/*Connects to DB and shows the products in my fridge*/
function myFridge($username){
include ('connection.php');
if(isset($connection)){
$query = "SELECT * FROM tbl_ingredients_213 WHERE user_name= '$username'";
$result = mysqli_query($connection, $query);
}
$index = 0;
while ($row = mysqli_fetch_assoc($result)) {
$GLOBALS['prods'][$index] = $row["product"];
$GLOBALS['amount'][$index] = $row["amount"];
$GLOBALS['unit'][$index] = $row["unit"];
$GLOBALS['place'][$index] = $row["place"];
$GLOBALS['expiry'][$index++] = $row["expiry_date"];
}
if (isset($connection)){
mysqli_close($connection);
unset($connection);
}
}
/*Connects to json from 'campbellskitchen'*/
function recipes($username){
include ('connection.php');
include ('jsonurl.php');
if(isset($connection)){
$query = "SELECT product FROM tbl_ingredients_213 WHERE user_name= '$username'";
$result = mysqli_query($connection, $query);
$ingredients = "";
while ($row = mysqli_fetch_assoc($result)) {
$ingredients = $ingredients . $row["product"] . "|";
}
rtrim($ingredients, "|");
$url = $baseUrlSearch . $ingrediantSearch . $ingredients . $format . $appId . $appKey;
$json = file_get_contents($url);
mysqli_close($connection);
unset($connection);
echo $json;
}
}
function quickRecipes($time){
include ('jsonurl.php');
$url = $baseUrlSearch . $preptime . $time . $format . $appId . $appKey;
$json = file_get_contents($url);
echo $json;
}
function getRecipe($recipeId){
include ('jsonurl.php');
$url = $baseUrlGet . $recipeId . "?" . $get_format . $appId . $appKey;
$json = file_get_contents($url);
echo $json;
}
function searchRecipes($searchParam){
include ('jsonurl.php');
$url = $baseUrlSearch . $keywords . $searchParam . $format . $appId . $appKey;
$json = file_get_contents($url);
echo $json;
}
/*Saves specsific recipe*/
function saveRecipe($id, $name, $username, $pic_url, $ingredients){
include('connection.php');
$name = str_replace("'", "\'", $name);
$ingredients = json_decode($ingredients);
if (isset($id)){
if(isset($connection)){
$query = "SELECT rId FROM tbl_recipes_213 WHERE rId = '$id' AND user_name = '$username'";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_assoc($result);
if(! $row){
$query = "INSERT INTO tbl_recipes_213
(title, rId, user_name, recipe_pic)
VALUES
('$name', '$id', '$username', '$pic_url')";
mysqli_query($connection, $query);
}
$query = "SELECT rId FROM tbl_recipe_ingredients_213 WHERE rId = '$id'";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_assoc($result);
if(! $row){
foreach($ingredients as $key => $ingredient){
$ingredient_name = $ingredient->ingredient_name;
$ingredient = $ingredient->ingredient;
$query = "INSERT INTO tbl_recipe_ingredients_213
(rId, ingredient, ingredient_name)
VALUES
('$id', '$ingredient', '$ingredient_name')";
mysqli_query($connection, $query);
}
}
mysqli_close($connection);
unset($connection);
}
}
}
function missingIngredients($username, $recipeId){
include ('connection.php');
if(isset($connection)){
$query = "SELECT
ingredient_name
FROM
tbl_recipe_ingredients_213
WHERE
rId = '$recipeId'
AND ingredient_name NOT IN
(SELECT
product
FROM
tbl_ingredients_213
WHERE
user_name = '$username')";
$result = mysqli_query($connection, $query);
$index = 0;
while ($row = mysqli_fetch_assoc($result)) {
if($row["ingredient_name"])
$GLOBALS['missing'][$index++] = $row["ingredient_name"];
}
mysqli_close($connection);
unset($connection);
}
}
function myRecipes($username){
include ('connection.php');
if(isset($connection)){
$query = "SELECT title, rId, recipe_pic FROM tbl_recipes_213 WHERE user_name = '$username' AND favorite = 1";
$result = mysqli_query($connection, $query);
$index = 0;
while ($row = mysqli_fetch_assoc($result)) {
if($row["title"])
$GLOBALS['title'][$index] = $row["title"];
$GLOBALS['recipe_pic'][$index] = $row["recipe_pic"];
$GLOBALS['recipeId'][$index++] = $row["rId"];
}
mysqli_close($connection);
unset($connection);
}
}
function favorite($recipeId, $username){
include ('connection.php');
if(isset($connection)){
$query = "SELECT favorite FROM tbl_recipes_213 WHERE user_name = '$username' AND rId = $recipeId";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_assoc($result);
mysqli_close($connection);
unset($connection);
if ($row["favorite"] == 1) return true;
else return false;
}
}
?>