-
Notifications
You must be signed in to change notification settings - Fork 0
/
add-product.php
278 lines (181 loc) · 9.52 KB
/
add-product.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
<?php include_once __DIR__ . "/assets/php/app.php" ?>
<?php
define("PARENT_DIR", __DIR__);
if ( !chechkIfLoggedIn() )
{
header("location: login.php?returnUrl=" . htmlspecialchars($_SERVER['PHP_SELF']) );
}
$userDetails = getUserDetailsUsingUsername($dbConn, $_SESSION["SSID-USERNAME"] );
if ( $userDetails['user_role'] != 'super-admin' && $userDetails['user_role'] != 'product-manager' )
{
load404Page();
exit;
}
?>
<?php
if( $_SERVER["REQUEST_METHOD"] === "POST" )
{
if ( !isset( $_POST["addProduct"] ) ){ exit; }
$product_name = trim($_POST["product_name"]);
$product_price = trim($_POST["product_price"]);
$product_short_description = trim($_POST["product_short_description"]);
$product_long_description = trim($_POST["product_long_description"]);
$product_images = $_FILES["product_images"];
$product_category = trim($_POST['product_category']);
$product_slug = strtolower($product_name);
$product_slug = preg_replace("/\s+/", "-", $product_slug);
$product_id = generateRadmonStrings("prdct-");
$upload_dir = __DIR__ . "/assets/products/" . $product_id ;
$allowed_exts = array( 'jpg', 'png', 'jpeg' );
// Define Maxsize for files i.e 5MB
$maxSize = 5 * 1024 * 1024;
$productImagesUrls = "";
if ( !is_dir($upload_dir) )
{
mkdir( $upload_dir );
mkdir( $upload_dir . "/media" );
mkdir( $upload_dir . "/media/images" );
mkdir( $upload_dir . "/metadata" );
}
$upload_dir = $upload_dir . "/media/images/";
if ( !empty ( array_filter( $product_images['name'] ) ) )
{
foreach( $product_images['tmp_name'] as $key => $value )
{
$file_tmp_name = $product_images['tmp_name'][$key];
$file_name = $product_images['name'][$key];
$file_size = $product_images['size'][$key];
$file_ext = pathinfo($file_name, PATHINFO_EXTENSION);
$filePath = $upload_dir . $file_name;
if ( in_array( strtolower($file_ext), $allowed_exts ) )
{
if ( $file_size > $maxSize )
{
echo "File Size is large";
}
else
{
if ( file_exists($filePath) )
{
$filePath = $upload_dir.time().$file_name;
if ( move_uploaded_file( $file_tmp_name, $filePath ) )
{
$productImagesUrls.= "assets/products/$product_id/media/images/" . $file_name . ",";
}
else
{
echo "ERROR";
}
}
else
{
if ( move_uploaded_file( $file_tmp_name, $filePath ) )
{
$_SESSION["error_msg"] = "Upload Success";
$_SESSION["error_bg"] = "bg-success";
$productImagesUrls.= "assets/products/$product_id/media/images/" . $file_name . ",";
}
else
{
$_SESSION["error_msg"] = "Error While Uploading";
$_SESSION["error_bg"] = "bg-failure";
}
}
}
}
else
{
// echo "Error Uploading: ";
// echo "({$file_ext} file is not allowed)";
$_SESSION["error_msg"] = "{$file_ext} file is not allowed <br>";
$_SESSION["error_bg"] = "bg-failure";
}
}
}
else
{
$_SESSION["error_msg"] = "No FIles Selected";
$_SESSION["error_bg"] = "bg-failure";
}
$product_details = array (
"product_name" => $product_name,
"product_slug" => $product_slug,
"product_category" => $product_category,
"product_images" => $productImagesUrls,
"product_id" => $product_id,
"product_current_price" => $product_price,
"product_short_description" => $product_short_description,
"product_long_description" => $product_long_description,
);
if ( createNewProduct( $dbConn, $product_details ) )
{
$_SESSION["error_msg"] = "Registration Success";
$_SESSION["error_bg"] = "bg-success";
header("Location:" . htmlspecialchars($_SERVER['PHP_SELF']) . "?respCode=200&msg=success");
}
else
{
$_SESSION["error_msg"] = "Registration Failed";
$_SESSION["error_bg"] = "bg-failure";
}
}
?>
<?php require_once 'assets/pages/admin/head.php' ?>
<title> Create New Product - <?php echo $site_titile ?> </title>
</head>
<body>
<div class="container-fluid">
<?php require_once 'assets/pages/admin/navigation.php' ?>
<div class="contents container-fluid">
<?php require_once 'assets/pages/admin/sidebar.php' ?>
<div class="page-contents">
<section class="product-category">
<div class="heading">
<div class="details">
<h2> <i class="fa fa-add"></i> Add <span>New</span> Product </h2>
</div>
</div>
<form method="POST" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" enctype="multipart/form-data" class="add-new-product-form ">
<div class="form-group mt-3 mb-3">
<label class="mb-3" for="product_name">Product Name <span>*</span></label>
<input type="text" class="form-control" name="product_name" id="product_name" itemid="product_name" itemtype="input" placeholder="Ex. Product 1, Product 2, Product 3, ... " value="<?php isset($_POST["product_name"] ) && !empty($_POST["product_name"]) ? $_POST["product_name"] : '' ?>" autofocus required>
</div>
<div class="form-group mt-3 mb-3">
<label class="mb-3" for="product_price">Product Price <span>*</span></label>
<input type="text" min="0.00" class="form-control" name="product_price" id="product_price" itemid="product_price" itemtype="input" placeholder="Ex. 50.99" value="<?php isset($_POST["product_price"] ) && !empty($_POST["product_price"]) ? $_POST["product_price"] : '' ?>" required>
</div>
<div class="form-group mt-3 mb-3">
<label class="mb-3" for="product_image">Product Image (s) <span>*</span></label>
<input type="file" class="form-control" name="product_images[]" multiple id="product_images" itemid="product_image" itemtype="input" required>
</div>
<div class="form-group mt-3 mb-3">
<label class="mb-3" for="product_category">Product Image (s) <span>*</span></label>
<?php if ( count( $categories ) != 0 ): ?>
<select class="form-control" name="product_category" id="product_category" itemid="product_image" itemtype="input" required>
<option value="">Select A Category</option>
<?php foreach( $categories as $key => $category ): ?>
<option value="<?php echo $category["category_slug"] ?>"><?php echo $category["category_name"] ?></option>
<?php endforeach;?>
</select>
<?php else: ?>
<a href="add-category.php" class="btn btn-primary form-control"> <i class="fas fa-plus-circle"></i> Create New Category</a>
<?php endif; ?>
</div>
<div class="form-group mt-3 mb-3">
<label class="mb-3" for="product_short_description">Product Short description <span>*</span></label>
<textarea name="product_short_description" id="product_short_description" class="form-control" value="<?php isset($_POST["product_short_description"] ) && !empty($_POST["product_short_description"]) ? $_POST["product_short_description"] : '' ?>" placeholder="Write a brief detail of what your product is about..." required></textarea>
</div>
<div class="form-group mt-3 mb-3">
<label class="mb-3" for="product_long_description">Product Long description</label>
<textarea name="product_long_description" id="product_long_description" class="form-control" placeholder="Enter Additional Details..." value="<?php isset($_POST["product_long_description"] ) && !empty($_POST["product_long_description"]) ? $_POST["product_long_description"] : '' ?>"></textarea>
</div>
<div class="form-group mt-3 mb-3 text-end">
<button type="submit" name="addProduct" class="btn btn-primary" > <i class="fas fa-add"></i> Add New Product</button>
</div>
</form>
</section>
</div>
</div>
</div>
</body>
</html>