forked from aqibos/CookbookNetwork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit-cookbook.php
539 lines (450 loc) · 19 KB
/
edit-cookbook.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
<?php
session_start();
$allemails = [];
if(isset($_SESSION['userid'])) //get user id
{
$user_id = $_SESSION['userid'];
}
//get cookbook id that is being edited
$cookbook_id = $_GET["cookbook_id"] ;
include 'db-credentials.php';
$link = new mysqli($servername, $username, $password, $dbname);
if ($link -> connect_error)
{
die("Connection failed: ".$link -> connect_error);
}
//Do not have access to edit, if not owner of cookbook
$isOwner = isOwner($user_id, $cookbook_id, $link);
if($isOwner == false)
{
header('Location: fail.php');
}
$title = getCookbookTitle($cookbook_id, $link); //get title of cookbook
$privacy = getPrivacy($cookbook_id, $link); //get privacy of cookbook
//Check if privacy is friendly
if($privacy == "FRIENDLY")
{
$allemails = getFriends($cookbook_id, $link);
}
$sql = "SELECT name FROM Tag WHERE type='COOKBOOK' AND type_id = '$cookbook_id'";
$result = $link -> query($sql);
$i=0;
while($row = $result->fetch_assoc()) //get tags and store in array
{
$tagsfromdb[$i] = $row['name'];
$i++;
}
//all possible tags
$alltags_array = array('1' => 'appetizer',
'2' => 'paleo',
'3' => 'american',
'4' => 'beef',
'5' => 'pork',
'6' => 'asian',
'7' => 'beverages',
'8' => 'poultry',
'9' => 'desi',
'10' => 'breakfast/brunch',
'11' => 'salad',
'12' => 'greek',
'13' => 'chicken',
'14' => 'seafood',
'15' => 'italian',
'16' => 'desserts',
'17' => 'soup',
'18' => 'jamaican',
'19' => 'gluten-free',
'20' => 'vegan',
'21' => 'latin',
'22' => 'lunch',
'23' => 'vegetarian');
if($i > 0)
{
$c = getTags($tagsfromdb, $alltags_array);
}
else
{
foreach ( $alltags_array as $k => $v )
{
$c[$k] = "";
}
}
//SUBMITTED FORM
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
var_dump($allemails);
$tbl_name="Cookbook"; // Table name
// Connect to server and select databse.
$link = new mysqli($servername, $username, $password, $dbname);
if ($link -> connect_error)
{
die("Connection failed: ".$link -> connect_error);
}
// get cbkn name, privacy, tags, friends from form
$cookbookname = $_POST['cookbookname'];
$privacy = $_POST['privacy'];
$allemails = $_POST['desiredfriends'];
//check if privacy is friendly, and get list of friends submitted
/*if(strcmp($privacy, "friendly") == 0)
{
$allemails = $_POST['desiredfriends'];
$isFriendly = true;
}*/
if(isset($_POST['tags']))
{
$tags = $_POST['tags'];
}
//DELETE TAGS from Cookbook
$sql ="DELETE FROM Tag WHERE type='COOKBOOK' AND type_id ='$cookbook_id'";
if ($link->query($sql) != true) //unsuccessful query
{
header('Location: fail.php');
}
//DELETE friends of cookbook
$sql ="DELETE FROM Friends WHERE type='COOKBOOK' AND type_id ='$cookbook_id'";
if ($link->query($sql) != true) //unsuccessful query
{
header('Location: fail.php');
}
//If its friendly, add emails
if($privacy == "friendly")
{
storeFriends($allemails, $cookbook_id, $link);
//$check = $allemails;
}
//Update cookbook name and privacy
$sql= "UPDATE Cookbook SET cb_title = '$cookbookname', visibility = '$privacy' WHERE cookbook_id = '$cookbook_id'";
if ($link->query($sql) != true) //unsuccessful query
{
$error= "ERROR: Could not able to execute $sql. " . $link->connect_error;
}
//store array of tags in database
if(isset($tags))
{
storeTags($tags, $cookbook_id, $link);
}
redirect($cookbook_id); //redirect back to view cookbook
mysqli_close($link); //close connection
}
//get title of cookbook
function getCookbookTitle($cookbook_id, $link)
{
$sql = "SELECT cb_title FROM Cookbook WHERE cookbook_id = '$cookbook_id'";
$result = $link -> query($sql);
$row = $result->fetch_assoc();
return $row['cb_title'];
}
//get privacy setting
function getPrivacy($cookbook_id, $link)
{
$sql = "SELECT visibility FROM Cookbook WHERE cookbook_id = '$cookbook_id'";
$result = $link -> query($sql);
$row = $result->fetch_assoc();
return $row['visibility'];
}
//get list of friends
function getFriends($cookbook_id, $link)
{
$emails=[];
$sql = "SELECT email FROM Friends WHERE type='COOKBOOK' AND type_id = '$cookbook_id'";
$result = $link -> query($sql);
$i=0;
while($row = $result->fetch_assoc()) //get emails and store in array
{
$emails[$i] = $row['email'];
$i++;
}
return $emails;
}
//get tags of cookbook
function getTags($tagsfromdb, $alltags_array)
{
foreach ( $alltags_array as $k => $v )
{
if (in_array($v, $tagsfromdb))
{
$c[$k] = "checked='checked'";
}
else
{
$c[$k] = "";
}
}
return $c;
}
//Store tags in database
function storeTags($tag, $cookbook_id, $link)
{
$size = count($tag);
for($i=0; $i < $size; $i++)
{
$current = $tag[$i];
$sql= "INSERT INTO Tag (name, type, type_id)
VALUES ('$current', 'COOKBOOK', '$cookbook_id')";
if ($link->query($sql) != true) //unsuccessful query
{
$error= "ERROR: Could not execute $sql. " . $link->connect_error;
}
}
}
//Store friends in databases
function storeFriends($email, $cookbook_id, $link)
{
$size = count($email);
for($i=0; $i < $size; $i++)
{
$current = $email[$i];
if(($current == null || $current == '') && $i <= $size-2) //if empty field and there is more, skip
{
$i++;
$current = $email[$i];
}
if(checkValidFriend($current, $link)) //if account exists, add as friend
{
$sql= "INSERT INTO Friends (email, type, type_id)
VALUES ('$current', 'COOKBOOK', '$cookbook_id')";
if ($link->query($sql) != true) //unsuccessful query
{
header('Location: fail.php');
}
}
else
{
//delete any previously added friends from cookbook
$sql2 = "DELETE FROM Friends WHERE type = 'COOKBOOK' AND type_id = '$cookbook_id'";
if ($link->query($sql2) != true) //unsuccessful query
{
header('Location: fail.php');
}
exit('<br/>Sorry, you have inserted invalid friend(s).');
}
}
}
//Check friend is a valid account
function checkValidFriend($email, $link)
{
$sql= "SELECT email FROM Account WHERE email = '$email'";
if ($link->query($sql) != true) //unsuccessful query
header('Location: fail.php');
$result = $link -> query($sql);
$row = $result->fetch_assoc();
if(count($row) == 1)
return true; //found friends account in database
else
return false;
}
//Change location to view cookbook
function redirect($cookbook_id)
{
header('Location: view-cookbook.php?cookbook_id='. $cookbook_id );
}
//Check user is owner of cookbook and can edit
function isOwner($user_id, $cookbook_id, $link)
{
$sql= "SELECT user_id FROM Cookbook_list WHERE user_id = '$user_id' AND cookbook_id ='$cookbook_id'";
if ($link->query($sql) != true) //unsuccessful query
header('Location: fail.php');
$result = $link -> query($sql);
$row = $result->fetch_assoc();
if(count($row) == 1)
{
return true;
}
else
{
return false;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Edit Cookbook</title>
<meta charset="UTF-8">
<meta name="description" content="A virtual cookbook that allows user's to view, create and share recipes.">
<meta name="keywords" content="recipe, cookbook, food, ingredients">
<meta name="author" content="Cookbook Network Inc.">
<link rel="stylesheet" type="text/css" href="page_style.css">
<link href='http://fonts.googleapis.com/css?family=Tangerine:700' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=IM+Fell+Double+Pica' rel='stylesheet' type='text/css'>
</head>
<body onload="loadFriends();">
<img class="background-image" src="images/loaf-delicious-cake-with-strawberries-wallpapers-1440x900.jpg" height="700"/>
<div class="navigation-bar">
<?php include 'check-menu.php'; ?>
</div>
<div class="content">
<h1 class="center">Edit Cookbook</h1>
<form name="createcbk" method="post" onsubmit="return validate()">
<table class="tableform">
<tr>
<td colspan="2" width="60%"> <h3>Name of Cookbook: </h3><br/></td>
<td colspan="2" width="30%"><input size="35" type="text" name="cookbookname" value="<?php echo $title; ?>"><br/><br/></td>
</tr>
<tr>
<td colspan="2" width="20%"> <h3>Privacy: </h3><br/></td>
<td width="15%"><input type="radio" name="privacy" value="private" id="priv" onclick="javascript:isChecked();" <?php if ($privacy === 'PRIVATE') echo 'checked="checked"'; ?> />Private<br/><br/></td>
<td width="15%"><input type="radio" name="privacy" value="registered" id="reg" onclick="javascript:isChecked();" <?php if ($privacy === 'REGISTERED') echo 'checked="checked"'; ?>/>Registered<br/><br/></td>
<td width="15%"><input type="radio" name="privacy" value="friendly" id="friendlycheck" onclick="javascript:isChecked();" <?php if ($privacy === 'FRIENDLY') echo 'checked="checked"'; ?> />Friendly<br/><br/></td>
<td width="15%"><input type="radio" name="privacy" value="public" id="pub" onclick="javascript:isChecked();" <?php if ($privacy === 'PUBLIC') echo 'checked="checked"'; ?> />Public<br/><br/></td>
</tr>
<tr>
<td colspan="2" width="60%"><div id="ifFriendly" class="hidden"><h3>Enter email of users to share: </h3></div></td>
<td colspan="2" width="30%">
<div id="ifFriendly2" class="hidden">
<div id="emailfield">
<input type="text" size="35" name="desiredfriends[]" id="first">
</div>
</div>
</td>
<td colspan="2"><a id="ifFriendly3" href="javascript:void(0);" class="addLink hidden" onclick="addEmailField();"><div class="button">+ Add More</div></a></td>
</tr>
<tr>
<td width="45%"> <h3>Tags for Cookbook:</h3></td>
</tr>
<tr>
<td><br/></td>
<td><br/></td>
<td><input type="checkbox" name="tags[]" value="appetizer" <?php echo $c[1]; ?>>Appetizer<br/></td>
<td><input type="checkbox" name="tags[]" value="paleo" <?php echo $c[2]; ?>>Paleo<br/></td>
<td><input type="checkbox" name="tags[]" value="american" <?php echo $c[3]; ?>>American<br/></td>
</tr>
<tr>
<td><br/></td>
<td><br/></td>
<td><input type="checkbox" name="tags[]" value="beef" <?php echo $c[4]; ?>>Beef<br/></td>
<td><input type="checkbox" name="tags[]" value="pork" <?php echo $c[5]; ?>>Pork<br/></td>
<td><input type="checkbox" name="tags[]" value="aasian" <?php echo $c[6]; ?>>Asian<br/></td>
</tr>
<tr>
<td><br/></td>
<td><br/></td>
<td><input type="checkbox" name="tags[]" value="beverages" <?php echo $c[7]; ?>>Beverages<br/></td>
<td><input type="checkbox" name="tags[]" value="poultry" <?php echo $c[8]; ?>>Poultry<br/></td>
<td><input type="checkbox" name="tags[]" value="desi" <?php echo $c[9]; ?>>Desi<br/></td>
</tr>
<tr>
<td><br/></td>
<td><br/></td>
<td><input type="checkbox" name="tags[]" value="breakfast/brunch" <?php echo $c[10]; ?>>Breakfast<br/></td>
<td><input type="checkbox" name="tags[]" value="salad" <?php echo $c[11]; ?>>Salad<br/></td>
<td><input type="checkbox" name="tags[]" value="greek" <?php echo $c[12]; ?>>Greek<br/></td>
</tr>
<tr>
<td><br/></td>
<td><br/></td>
<td><input type="checkbox" name="tags[]" value="chicken" <?php echo $c[13]; ?>>Chicken<br/></td>
<td><input type="checkbox" name="tags[]" value="seafood" <?php echo $c[14]; ?>>Seafood<br/></td>
<td><input type="checkbox" name="tags[]" value="italian" <?php echo $c[15]; ?>>Italian<br/></td>
</tr>
<tr>
<td><br/></td>
<td><br/></td>
<td><input type="checkbox" name="tags[]" value="desserts" <?php echo $c[16]; ?>>Desserts<br/></td>
<td><input type="checkbox" name="tags[]" value="soup" <?php echo $c[17]; ?>>Soup<br/></td>
<td><input type="checkbox" name="tags[]" value="jamaican" <?php echo $c[18]; ?>>Jamaican<br/></td>
</tr>
<tr>
<td><br/></td>
<td><br/></td>
<td><input type="checkbox" name="tags[]" value="gluten-free" <?php echo $c[19]; ?>>Gluten-free<br/></td>
<td><input type="checkbox" name="tags[]" value="vegan" <?php echo $c[20]; ?>>Vegan<br/></td>
<td><input type="checkbox" name="tags[]" value="latin" <?php echo $c[21]; ?>>Latin<br/></td>
</tr>
<tr>
<td><br/></td>
<td><br/></td>
<td><input type="checkbox" name="tags[]" value="lunch" <?php echo $c[22]; ?>>Lunch<br/></td>
<td><input type="checkbox" name="tags[]" value="vegetarian" <?php echo $c[23]; ?>>Vegetarian<br/></td>
</tr>
<br/>
<tr>
<td><br/><br/></td>
<td><br/><br/></td>
<td colspan="2"><br/><br/><br/><div class="submitbutton"><input type="submit" value="Save"/></div></td>
<td colspan="2"><br/><br/><br/><div class="submitbutton"><input type="submit" value="Cancel" onclick="window.history.back(); return false;"></div></td>
</tr>
</table>
</form>
</div>
<script type="text/javascript">
function loadFriends()
{
var visibility = "<?php echo $privacy; ?>";
if (visibility == "FRIENDLY")
{
var friends = <?php echo '["' . implode('", "', $allemails) . '"]' ?>;
document.getElementById('ifFriendly').style.display = 'block';
document.getElementById('ifFriendly2').style.display = 'block';
document.getElementById('ifFriendly3').style.display = 'block';
getFirstEmail(friends[0]);
var i;
for (i = 1; i < friends.length; i++)
{
displayFriendEmail(friends[i]);
}
}
}
function getFirstEmail(firstemail)
{
var input = document.getElementById('first');
input.value = firstemail;
}
function validate()
{
var cookbookname = document.forms["createcbk"]["cookbookname"].value;
var checkboxes = document.getElementsByName('privacy');
//Check for blank cookbook name
if(isBlank(cookbookname))
{
alert("Fill in cookbook name.");
return false;
}
//check privacy is chosen
var i = checkboxes.length - 1;
for ( ; i >= 0 ; i-- )
{
if ( checkboxes[i].checked )
return true;
}
alert("Must choose a privacy.");
return false;
}
function isChecked()
{
if(document.getElementById('friendlycheck').checked)
{
document.getElementById('ifFriendly').style.display = 'block';
document.getElementById('ifFriendly2').style.display = 'block';
document.getElementById('ifFriendly3').style.display = 'block';
}
else
{
document.getElementById('ifFriendly').style.display = 'none';
document.getElementById('ifFriendly2').style.display = 'none';
document.getElementById('ifFriendly3').style.display = 'none';
}
}
function addEmailField()
{
var input = document.createElement('input');
input.type = "text";
input.name = "desiredfriends[]";
input.size = "35";
var container = document.getElementById("emailfield");
container.appendChild(input);
}
function displayFriendEmail(friendEmail)
{
var input = document.createElement('input');
input.type = "text";
input.name = "desiredfriends[]";
input.size = "35";
input.value = friendEmail;
var container = document.getElementById("emailfield");
container.appendChild(input);
}
</script>
<div class="footer"><p>© Cookbook Network, 2015. All Rights Reserved.</p></div>
</body>
</html>