-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitems.php
116 lines (105 loc) · 3.52 KB
/
items.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
<?php
session_start();
if(isset($_GET['logout'])){
session_unset();
}
if(!isset($_SESSION['user'])){
header("Location: index.php");
}
?>
<?php include_once("includes/DB/Controller.php") ?>
<?php include_once("includes/Files/Upload.php") ?>
<?php include_once("includes/layout/header.php") // HTML header ?>
<?php include_once("includes/layout/nav.php") // Navigation ?>
<?php
$controller = new Controller();
// Delete item
if(isset($_GET['delete']) && is_numeric($_GET['delete'])){
$item = $controller->getItem($_GET['delete']);
unlink("uploads/{$item['categoryId']}/{$item['imgName']}");
$controller->delete($_GET['delete'],"items");
}
// Upload a new item
if(isset($_POST['uploadBtn'])){
$uploadErrors = array();
$category;
$name;
// Get input from form
if(isset($_POST['category']) && $_POST['category'] == ""){
$uploadErrors[] = "Category";
}else{
$category = $_POST['category'];
echo $category;
}
if(isset($_POST['name']) && $_POST['name'] == ""){
$uploadErrors[] = "name";
}else{
$name = $_POST['name'];
echo $name;
}
if(count($uploadErrors) == 0){
$upload = new Upload("uploadBtn","uploads/{$category}/");
// If the upload was not successful display errors for user
if(!$upload->upload()){
foreach($upload->errors() as $error){
echo "<hr>";
echo $error . " <BR/>";
}
}else{
// Store in DB
$controller->addItem($upload->getFileName(),$name,$category);
}
}
}
?>
<div class="container">
<div class="starter-template">
<h1>Upload</h1>
<form action="" method="post" enctype="multipart/form-data">
Category:
<select name="category">
<?php
$categories = $controller->allCategories();
if(count($categories) > 0){
foreach($categories as $key => $value){
echo "<option value='{$key}' ";
if(isset($_GET['cat']) && is_numeric($_GET['cat']) && $_GET['cat'] == $key){
echo " SELECTED";
}
echo ">{$value}</option>";
}
}
?>
</select>
<br>
<br>
Name of the item:
<input type="text" name="name">
<br>
<br>
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<br>
<br>
<input type="submit" value="Upload" name="uploadBtn">
</form>
</div>
<hr>
<?php
if(isset($_GET['cat']) && is_numeric($_GET['cat'])){
$items = $controller->getCategoryItems($_GET['cat']);
foreach($items as $item){
$id = $item[0];
$img = $item[1];
$name = $item[2];
$categoryId = $item[3];
echo "<img src='uploads/{$categoryId}/{$img}' class=\"img-thumbnail\" alt=\"{$name}\" width=\"300\">";
echo "<span class='alert'>{$name}</span> | ";
echo "<a href='?delete={$id}' class='alert'>Delete</a>";
echo "<BR>";
}
}
echo "<hr>";
?>
</div><!-- /.container -->
<?php include_once("includes/layout/footer.php") // HTML Footer ?>