-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin_tools.php
More file actions
463 lines (426 loc) · 18.3 KB
/
admin_tools.php
File metadata and controls
463 lines (426 loc) · 18.3 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
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
<!DOCTYPE HTML>
<?php
session_start();
error_reporting(0);
if ($_SESSION['isAdmin'] == false) {
http_response_code(404);
echo 'Oops! You need to log into an admin account to access this page.';
die();
}
error_reporting(-1);
include "scripts.php";
//Variables
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "root";
$dbname = "inventory";
//Connect and Select
$con = makeConnection($dbhost, $dbuser, $dbpass, $dbname);
$db = new PDO('mysql:host=localhost;dbname=inventory;charset=utf8', 'root', 'root');
$itemList = $db->prepare('SELECT itemName FROM items');
$itemList->execute();
$itemList = $itemList->fetchAll(PDO::FETCH_ASSOC);
$items = array();
foreach($itemList as $item) {
$items[] = $item['itemName'];
}
$CompanyName = "NUWC Juicing";
?>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<html>
<head>
<title>Admin Tools</title>
<link rel="stylesheet" type"text/css" href="admintoolsstylesheet.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="home.php"><?php echo $CompanyName; ?></a>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li><a href="home.php">Home</a></li>
<li><a href="menu.php">Menu</a></li>
<li><a href="about.php">About Us</a></li>
<li><a href="locations_contact.php">Locations & Contact Us</a></li>
</ul>
<?php
if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true) {
echo "
<ul class='nav navbar-nav navbar-right'>
<li><a href='my_account.php'><span class='glyphicon glyphicon-user'></span> My Account</a></li>
<li><a href='logout.php'><span class glyphicon-shopping-logout'></span> Logout</a><li>
</ul>
"; // End of Navbar - Logged In
} else {
echo "
<ul class='nav navbar-nav navbar-right'>
<li><a href='login.php'><span class='glyphicon glyphicon-log-in'></span> Login</a></li>
</ul>
"; // End of Navbar - Logged Out
}
?>
</div>
</div>
</nav>
<h2>Admin Tools</h2>
<div class="main">
<?php
// define variables and set to empty values
$twitter = "";
$nameErr = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
//$twitter = test_input($_POST["twitter"]);
if (!preg_match("/^[a-zA-Z ]*$/",$twitter)) {
$nameErr = "Please only enter your twitter handle without the '@'";
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<a href="my_account.php" style="background-color: rgba(255, 228, 200, 0.79);padding: 10px 10px 10px 10px;position: absolute;top: 0;left: 0;border-bottom-left-radius: 10px;border-bottom-right-radius: 10px;width: 100%;text-align: center;
">Click here to return to the Account Page.</a>
<!--***********Twitter Handle******************-->
<?php
$twitterHandleText = changeTextFile("twitterHandle.txt", "twitter");
?>
<h6>Enter Twitter Handle:</h6>
<form action="" method="post" enctype="multipart/form-data">
<textarea name="twitter"><?php echo htmlspecialchars($twitterHandleText) ?></textarea><br>
<input type="submit" />
<input type="reset" />
</form>
<br>
<!--********About Us Text*******************-->
<?php
$aboutUsText = changeTextFile("aboutUs.txt", "aboutUs");
?>
<h6>Enter About Us:</h6>
<form action="admin_tools.php?aboutUsText" method="post" enctype="multipart/form-data">
<textarea name="aboutUs" cols=40 rows=10><?php echo htmlspecialchars($aboutUsText) ?></textarea><br>
<input type="submit" />
<input type="reset" />
</form>
<!--*******************About Us Image***********-->
<h6>Upload About Us Picture:</h6>
For best results, make sure your image is 750x500.
<form action="admin_tools.php" method="post" enctype="multipart/form-data" class="pictureForm">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="aboutPicSubmit">
</form>
<?php
if(isset($_POST["aboutPicSubmit"])) {
uploadAboutPicture("fileToUpload");
}
?>
<!--********Maps Information*******************-->
<?php
$latText = changeTextFile("mapsLatitude.txt", "lat");
$longText = changeTextFile("mapsLongitude.txt", "long");
?>
<h6>Enter Latitude and Longitude:</h6>
<a href="https://support.google.com/maps/answer/18539?source=gsearch&hl=en">Click here to get the latitude and longitude of your location!</a>
<form action="" method="post" enctype="multipart/form-data">
Latitude:<br>
<textarea name="lat" ><?php echo htmlspecialchars($latText) ?></textarea>
<br>Longitude:<br>
<textarea name="long" ><?php echo htmlspecialchars($longText) ?></textarea>
<br>
<input type="submit" />
<input type="reset" />
</form>
<!--********Contact Us Info**************************-->
<?php
$contactInfoText = changeTextFile("contactInfo.txt", "contactInfo");
?>
<h6>Enter Contact Information:</h6>
<form action="" method="post" enctype="multipart/form-data">
<textarea name="contactInfo" cols=40 rows=10><?php echo htmlspecialchars($contactInfoText) ?></textarea><br>
<input type="submit" />
<input type="reset" />
</form>
<!--********Location Information*******************-->
<?php
$locInfoText = changeTextFile("locationInfo.txt", "locationInfo");
?>
<h6>Enter Location Information:</h6>
<form action="" method="post" enctype="multipart/form-data">
<textarea name="locationInfo" cols=40 rows=10><?php echo htmlspecialchars($locInfoText) ?></textarea><br>
<input type="submit" />
<input type="reset" />
</form>
<!--*********Add product to database and menu********-->
<br><br>
<h6>Add Product:</h6>
<?php
addProductForm();
if (isset($_POST["prodSubmit"])) {
if ( isset($_POST["price"]) && isset($_POST["itemName"]) && !empty($_FILES["picLink"]["name"])) { //TODO: Add issets for things that need to be submitted, price and desc?
uploadProdPic("picLink");
$itemName = mysqli_real_escape_string($con, $_REQUEST['itemName']);
$price = mysqli_real_escape_string($con, $_REQUEST['price']);
$desc = mysqli_real_escape_string($con, $_REQUEST['desc']);
$cal = mysqli_real_escape_string($con, $_REQUEST['cal']);
$prot = mysqli_real_escape_string($con, $_REQUEST['prot']);
$chol = mysqli_real_escape_string($con, $_REQUEST['chol']);
$sodi = mysqli_real_escape_string($con, $_REQUEST['sodi']);
$carb = mysqli_real_escape_string($con, $_REQUEST['carb']);
$sugar = mysqli_real_escape_string($con, $_REQUEST['sugar']);
$pic = mysqli_real_escape_string($con, basename($_FILES["picLink"]["name"]));
$sql = $db->prepare("INSERT INTO items (itemName, price, description, calories, protein, choles, sodi, picLink, carbo, sugars)
VALUES ('$itemName', '$price', '$desc', '$cal', '$prot', '$chol', '$sodi', '$pic', '$carb', '$sugar')");
if ($sql->execute()){
echo '<br>Product added successfully. Please refresh page to see changes.';
} else {
echo 'error';
}
} else {
echo '<br> Error: Please make sure to submit at least an item name, price, and picture.';
}
}
?>
<!--************Upload Slideshow Pictures*************-->
<h6>Upload Home Page Slideshow Pictures:</h6>
<form action="admin_tools.php" method="POST" enctype="multipart/form-data" class="pictureForm">
Select image to upload:
<input type="file" name="slideshowFile" id="slideshowFile">
<input type="submit" value="Upload Image" name="slideSubmit">
</form>
<?php
$fi = new FilesystemIterator("images/slideshow/", FilesystemIterator::SKIP_DOTS);
$sizeofSlideshow = iterator_count($fi);
if(isset($_POST["slideSubmit"])) {
if($sizeofSlideshow >= 4) {
echo "Sorry, there are too many images already in the slideshow. Please delete some so there are less than 4 if you would like to upload more.";
} else {
uploadSlidePicture("slideshowFile");
}
}
?>
<h6>Remove Home Page Slideshow Pictures:</h6>
<!--*********Display Images already uploaded and handle deletion of image(s)********-->
<button onclick="toggleImages()">Show Images Currently in Slideshow</button>
<div id="myDIV" style="display:none">
<?php
$dirname = "images/slideshow/";
$images = glob($dirname."*.png");
if (count($images) == 0) {
echo 'There are no images in the slideshow';
goto a;
}
$count = 0;
foreach($images as $image) {
echo '<img src="'.$image.'" /><br />';
}
echo '<form action="" method="post" enctype="multipart/form-data">';
foreach($images as $image) {
$count = $count + 1;
echo '<input type="checkbox" name="deleteImages[]" value="'.$image.'"> Delete Image '.$count.'<br>';
}
echo '<input type="submit" name="formSubmit" value="delete" />';
if(isset($_POST["formSubmit"])){
if (empty($_POST["deleteImages"])) {
echo("You didn't select any images to be deleted.");
goto a;
}
$checkedImages = $_POST['deleteImages'];
$N = count($checkedImages);
for($i=0; $i < $N; $i++)
{
if(unlink($checkedImages[$i])) {
echo '<br>Image successfully deleted(refresh page to see updated slideshow)';
} else {
echo 'Image was not deleted';
}
}
}
a:
?>
</div>
<h6>Manage Inventory:</h6>
<!--**************Display products in database and handle deletion and adding/removing from menu -->
<button type="button" onclick="toggleProducts()">Show Product Inventory</button>
<div id="myProds" style="display:none">
<?php
foreach($items as $item) {
echo '<input type="checkbox" name="checkProds[]" value="'.$item.'"> '.$item.'<br>';
}
echo '<input type="submit" name="delSubmit" value="delete" />';
echo '<input type="submit" name="remSubmit" value="remove from menu" />';
echo '<input type="submit" name="addSubmit" value="add to menu" />';
echo '<input type="submit" name="editSubmit" value="edit" />';
echo '<input type="submit" name="dotdSubmit" value="Set Deal of the Day" />';
if(isset($_POST["delSubmit"])){
if (empty($_POST["checkProds"])) {
echo("You didn't select any products to be deleted.");
goto b;
}
$checkedProds = $_POST['checkProds'];
$N = count($checkedProds);
for($i=0; $i < $N; $i++)
{
$checkedProds[$i] = mysqli_real_escape_string($con, $checkedProds[$i]);
$sql = $db->prepare("DELETE FROM items WHERE itemName='$checkedProds[$i]'");
$sql2 = $db->prepare("SELECT picLink FROM items WHERE itemName ='$checkedProds[$i]'");
$sql2->execute();
$sql2 = $sql2->fetchAll(PDO::FETCH_ASSOC);
$picToBeDel = $sql2[0]['picLink'];
if ($sql->execute() && unlink("images/$picToBeDel")) {
echo "<br>Records were deleted successfully. Please refresh page to see changes.";
}
else {
echo "error";
}
}
}
if(isset($_POST["dotdSubmit"])){
if (count($_POST["checkProds"]) != 1) {
echo("Please select only 1 product for Deal of the Day.");
goto b;
}
changeTextFile("dotd.txt", "checkProds" );
}
if(isset($_POST["remSubmit"])){
if (empty($_POST["checkProds"])) {
echo("You didn't select any products to be removed from menu.");
goto b;
}
$checkedProds = $_POST['checkProds'];
$N = count($checkedProds);
for($i=0; $i < $N; $i++)
{
$checkedProds[$i] = mysql_real_escape_string($checkedProds[$i]);
$sql = $db->prepare("UPDATE items SET inMenu =0 WHERE itemName='$checkedProds[$i]'");
if ($sql->execute()) {
echo "Records were removed from menu successfully.";
}
else {
echo "error";
}
}
}
if(isset($_POST["addSubmit"])){
if (empty($_POST["checkProds"])) {
echo("You didn't select any products to be added to menu.");
goto b;
}
$checkedProds = $_POST['checkProds'];
$N = count($checkedProds);
for($i=0; $i < $N; $i++)
{
$checkedProds[$i] = mysql_real_escape_string($checkedProds[$i]);
$sql = $db->prepare("UPDATE items SET inMenu =1 WHERE itemName='$checkedProds[$i]'");
if ($sql->execute()) {
echo "Records were added to menu successfully.";
}
else {
echo "error";
}
}
}
b:
?>
</div>
<?php
if(isset($_POST["editSubmit"])){
if (empty($_POST["checkProds"])) {
echo("You didn't select any products to be edited.");
goto c;
}
$checkedProds = $_POST['checkProds'];
$N = count($checkedProds);
if ($N > 1) {
echo("Please select only one product to be edited.");
goto c;
}
$sql = $db->prepare("SELECT * FROM items WHERE itemName ='$checkedProds[0]'");
$sql->execute();
$sql = $sql->fetchAll(PDO::FETCH_ASSOC);
$sql = $sql[0];
$_SESSION['sql'] = $sql;
//print_r($sql);
echo '<br><h6>Edit Product:</h6>';
editProductForm($sql);
}
if(isset($_POST["changeSubmit"])) {
$itemName = mysqli_real_escape_string($con, $_REQUEST['itemName']);
$price = mysqli_real_escape_string($con, $_REQUEST['price']);
$desc = mysqli_real_escape_string($con, $_REQUEST['desc']);
$cal = mysqli_real_escape_string($con, $_REQUEST['cal']);
$prot = mysqli_real_escape_string($con, $_REQUEST['prot']);
$chol = mysqli_real_escape_string($con, $_REQUEST['chol']);
$sodi = mysqli_real_escape_string($con, $_REQUEST['sodi']);
$carb = mysqli_real_escape_string($con, $_REQUEST['carb']);
$sugar = mysqli_real_escape_string($con, $_REQUEST['sugar']);
$pic = mysqli_real_escape_string($con, basename($_FILES["picLink"]["name"]));
if(!empty($pic)) {
$sql = $db->prepare("SELECT count(*) FROM items WHERE picLink='".$_SESSION['sql']['picLink']."'");
$sql->execute();
$sql = $sql->fetchAll(PDO::FETCH_ASSOC);
$count = $sql[0]['count(*)'];
if ($count == 1 && !empty($_SESSION['sql']['picLink'])) {
$delImg = $_SESSION['sql']['picLink'];
unlink("images/$delImg");
}
uploadProdPic("picLink");
$sql = $db->prepare(
"UPDATE items
SET itemName='$itemName', price='$price', description='$desc', calories='$cal', protein='$prot', choles='$chol',
sodi='$sodi', picLink='$pic', carbo='$carb', sugars='$sugar'
WHERE itemID='".$_SESSION['sql']['itemID']."'" );
if ($sql->execute()){
echo '<br>Product edited successfully. Please refresh page to see changes.';
} else {
echo '<br>Error. Product not edited successfully.';
print_r($sql->errorInfo());
}
} else {
$sql = $db->prepare(
"UPDATE items
SET itemName='$itemName', price='$price', description='$desc', calories='$cal', protein='$prot', choles='$chol',
sodi='$sodi', carbo='$carb', sugars='$sugar'
WHERE itemID='".$_SESSION['sql']['itemID']."'" );
if ($sql->execute()){
echo '<br>Product edited successfully. Please refresh page to see changes.';
} else {
echo '<br>Error. Product not edited successfully.';
print_r($sql->errorInfo());
}
}
}
c:
?>
</div>/
<!--*******Javascript that toggles displaying images********-->
<script>
function toggleImages() {
var x = document.getElementById('myDIV');
if (x.style.display === 'none') {
x.style.display = 'block';
} else {
x.style.display = 'none';
}
}
function toggleProducts() {
var x = document.getElementById('myProds');
if (x.style.display === 'none') {
x.style.display = 'block';
} else {
x.style.display = 'none';
}
}
</script>
</body>
</html>