-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-item.php
215 lines (199 loc) · 9.92 KB
/
create-item.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
<?php include "classes.php" ?>
<?php
/**
* Created by PhpStorm.
* User: Rob
* Date: 12/9/2017
* Time: 1:17 AM
*/
$securityuserid = SessionManager::getSecurityUserId();
if($securityuserid == 0){
header("location: admin-login.php");
}
if($_SERVER["REQUEST_METHOD"] == "POST"){
if(isset($_POST["btnDelete"])
&& is_numeric($_POST["btnDelete"])
&& $_POST["btnDelete"] > 0){
Item::remove($_POST["btnDelete"]);
header("location:shop-home.php");
}
$returnVal = true;
isset($_POST["itemname"]) && $_POST["itemname"] != "" ? $itemname = $_POST["itemname"] : $returnVal = false;
isset($_POST["description"]) && $_POST["description"] != "" ? $description = $_POST["description"] : $returnVal = false;
isset($_POST["imgurl"]) && $_POST["imgurl"] != "" ? $imgurl = $_POST["imgurl"] : $returnVal = false;
isset($_POST["price"]) && $_POST["price"] != "" ? $price = $_POST["price"] : $returnVal = false;
isset($_POST["itemstatustype"]) && $_POST["itemstatustype"] > 0 ? $itemstatustype = $_POST["itemstatustype"] : $returnVal = false;
isset($_POST["itemtype"]) && $_POST["itemtype"] > 0 ? $itemtype = $_POST["itemtype"] : $returnVal = false;
if($returnVal){
if(isset($_POST["btnEdit"]) && is_numeric($_POST["btnEdit"])){
$createDate = $_POST["hfCreateDate"];
$item = new Item($_POST["btnEdit"], $itemname, $description, $imgurl, $price, $itemtype, $itemstatustype,$createDate,null);
}
else{
$currentDate = date('Y-m-d H:i:s');
$item = new Item(0, $itemname, $description, $imgurl, $price, $itemtype, $itemstatustype,$currentDate,null);
}
$item->save();
header("location: shop-item.php?id=".$item->getId());
}
else{
$validationMsg = "Please review your entries!";
}
}
if($_SERVER["REQUEST_METHOD"] == "GET"){
if(isset($_GET["id"])
&& is_numeric($_GET["id"])
&& $_GET["id"] > 0
&& isset($_GET["cmd"])
&& $_GET["cmd"] == "edit"){
$item = new Item($_GET["id"]);
if($item != null){
}
else{
header("location: index.php");
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<?php include "head.php" ?>
<body class="bg-dark" id="page-top">
<?php include "navbar.php" ?>
<div class="container">
<?php if(isset($validationMsg)) { ?>
<div class="alert alert-danger alert-dismissible fade show mx-auto mt-5" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4> <?php echo $validationMsg; ?> </h4>
</div>
<?php } ?>
<div class="row">
<div class="col-sm-3"></div>
<div class="col-sm-6">
<div class="card mx-auto mt-5">
<div class="card-header">Create Item
<?php
if(isset($item)) {
?>
<form method="post" class="pull-right">
<button type="submit" name="btnDelete" class="btn btn-danger" value="<?php echo $item->getId(); ?>">Delete</button>
</form>
<?php
}
?>
</div>
<div class="card-body">
<form method="post">
<div class="form-group">
<label for="username">Name</label>
<input class="form-control" id="itemname" name="itemname" type="text" placeholder="Item Name" value="<?php if(isset($item)) echo $item->getName() ?>">
</div>
<div class="form-group">
<label for="description">Description</label>
<textarea class="form-control" id="description" name="description" rows="5" type="text" placeholder="Item Description"><?php if(isset($item)) echo $item->getDescription() ?></textarea>
</div>
<div class="form-group">
<div class="form-row">
<div class="col-md-6">
<label for="itemtype">Type</label>
<select class="form-control" name="itemtype">
<?php
if(isset($item)) {
$itemType = new Itemtype($item->getItemTypeId());
?>
<option value="<?php echo $itemType->getId() ?>"><?php echo $itemType->getName() ?></option>
<?php
}
else {
?>
<option value="0">--Select Type--</option>
<?php
}
$itemTypeList = Itemtype::loadall();
if(!empty($itemTypeList)){
foreach ($itemTypeList as $it) {
if(isset($item) && $it->getId() == $item->getItemTypeId()){
//skipp
}
else{
?>
<option value="<?php echo $it->getId() ?>"><?php echo $it->getName() ?></option>
<?php
}
}
}
?>
</select>
</div>
<div class="col-md-6">
<label for="statustype">Status</label>
<select class="form-control" name="itemstatustype">
<?php
if(isset($item)) {
$itemStatusType = new Itemstatustype($item->getItemStatusTypeId());
?>
<option value="<?php echo $itemStatusType->getId() ?>"><?php echo $itemStatusType->getName() ?></option>
<?php
}
else {
?>
<option value="0">--Select Status--</option>
<?php
}
$itemStatusTypeList = Itemstatustype::loadall();
if(!empty($itemStatusTypeList)){
foreach ($itemStatusTypeList as $ist) {
if(isset($item) && $ist->getId() == $item->getItemStatusTypeId()){
//skipp
}
else{
?>
<option value="<?php echo $ist->getId() ?>"><?php echo $ist->getName() ?></option>
<?php
}
}
}
?>
</select>
</div>
</div>
</div>
<div class="form-group">
<div class="form-row">
<div class="col-md-6">
<label for="imgurl">Image Url</label>
<input class="form-control" id="imgurl" name="imgurl" type="text" placeholder="Image Url" value="<?php if(isset($item)) echo $item->getImgUrl() ?>">
</div>
<div class="col-md-6">
<label for="price">Price</label>
<input class="form-control" id="price" name="price" type="text" placeholder="$ 0.00" value="<?php if(isset($item)) echo $item->getPrice() ?>">
</div>
</div>
</div>
<?php
if(isset($item)){
?>
<input type="hidden" name="hfCreateDate" value="<?php echo $item->getCreateDate(); ?>">
<button name="btnEdit" type="submit" class="btn btn-primary btn-block" value="<?php echo $item->getId(); ?>">Edit Item</button>
<?php
}
else{
?>
<button type="submit" class="btn btn-primary btn-block">Create Item</button>
<?php
}
?>
</form>
<div class="text-center">
<a class="d-block small mt-3" href="admin-home.php">Cancel</a>
</div>
</div>
</div>
</div>
</div>
</div>
<?php include "scripts.php" ?>
</body>
</html>