-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproduct-update.php
360 lines (323 loc) · 19.1 KB
/
product-update.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
<?php
include_once './header.php';
if (!isLogin() && !isAdmin() && !isMod()) {
exit("<script>window.location.href='index'</script>");
}
?>
<?php if (isset($_GET['pid']) && !empty($_GET['pid'])) {
$pdo = pdo_connect_mysql();
$pid = $_GET['pid'];
// get all relevant data
$sql = "SELECT * FROM product p LEFT JOIN measurement m ON p.id = m.product_id WHERE p.id = $pid";
$stmt = $pdo->query($sql);
$product = $stmt->fetch(PDO::FETCH_ASSOC);
if($stmt->rowCount() == 0) {
exit("<script>window.location.href='product-list'</script>");
}
} else {
exit("<script>window.location.href='product-list'</script>");
}
?>
<div class="container">
<!-- check if error is set in URL -->
<?php if (isset($_GET['error'])):
include_once './src/inc/error.inc.php';
// check if given error exists in the error array
if (array_key_exists($_GET['error'], $errorList)): ?>
<div class="alert w-100 mt-2 text-center alert-danger">
<h3><?=$errorList[$_GET['error']]?></h3>
</div>
<!-- if given error does not exist in the error array -->
<?php else: ?>
<div class="alert w-100 mt-2 text-center alert-danger">
<h3>Zgodila se je neznana napaka. Prosim, poskusite kasneje!</h3>
</div>
<?php endif; ?>
<?php endif;
// check if status is given in URL and if the status is success
if (isset($_GET['status']) && isset($_GET['status']) == "success"):?>
<div class="alert w-100 mt-2 text-center alert-success ">
<h3>Izdelek uspešno vnesen!</h3>
</div>
<?php endif; ?>
<div class="row justify-content-center h-100">
<div class="col-12">
<div class="d-flex my-2 text-center">
<!-- update next / previous product -->
<div class="col-4 float-left">
<?php
// sql check if there is a previus recored id to current id
$sql = "SELECT id FROM product WHERE id = (SELECT max(id) FROM product WHERE id < $pid)";
$stmt = $pdo->query($sql);
if ($stmt->rowCount() != 0):
$prev = $stmt->fetch(PDO::FETCH_ASSOC);
?>
<a class="p-2 btn btn-info text-center" style="width:180px;" href="./product-update?pid=<?=$prev['id']?>"><i class="fas fa-arrow-left"></i> Prejšnji izdelek</a>
<?php endif; ?>
</div>
<div class="col-4 text-center">
<a class="p-2 btn btn-info text-center" style="width:180px;" href="./product-list"><i class="fas fa-list"></i> Seznam izdelkov</a>
</div>
<div class="col-4 text-center">
<?php
// sql check if there is a next recored id to current id
$sql = "SELECT id FROM product WHERE id = (SELECT min(id) FROM product WHERE id > $pid)";
$stmt = $pdo->query($sql);
if ($stmt->rowCount() != 0):
$next = $stmt->fetch(PDO::FETCH_ASSOC);
?>
<a class="p-2 btn btn-info text-center" style="width:180px;" href="./product-update?pid=<?=$next['id']?>">Naslednji izdelek <i class="fas fa-arrow-right"></i></a>
<?php endif; ?>
</div>
</div> <!-- d-flex my-2 text-center -->
<div class="card my-2">
<div class="card-header">
<h1>Posodobi izdelek</h1>
</div>
<div class="card-body">
<!-- action="./src/inc/product-update.inc.php" -->
<form method="post" enctype="multipart/form-data" action="" id="product-form">
<h3 class="subtitle">Osnovni podatki</h6>
<hr>
<!-- product table -->
<div class="form-group">
<label class="col-md-4 col-form-label text-left" for="title">Naslov izdelka:<span class="asterisk">*</span></label>
<input type="text" class="form-control" name="title" id="title" placeholder="Izdelek..." maxlength="150" required value="<?=$product['title']?>">
<input type="text" name="pid" id="pid" value="<?=$pid?>" hidden>
</div>
<!-- get all images for the given prtoduct ID -->
<?php
$sql = "SELECT * FROM product_image WHERE product_id = $pid";
$images = $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
?>
<div class="form-group">
<input type="file" id="img" name="img[]" class="file" accept="image/*" style="visibility: hidden;" multiple>
<div class="input-group my-1">
<input type="text" class="form-control" disabled placeholder="Upload File" id="file" value="<?php foreach($images as $image): $data = explode('/', $image['image']);
$imageName = end($data);?><?=$imageName?> <?php endforeach; ?>">
<div class="input-group-append">
<button type="button" class="browse btn btn-secondary">Išči...</button>
</div>
</div>
<div id="preview">
<?php foreach($images as $image): ?>
<img src="<?=$image['image']?>" style="height: 150px;">
<?php endforeach; ?>
</div>
</div>
<div class="form-group">
<label class="col-md-4 col-form-label text-left" for="summary">Povzetek:</label>
<textarea class="form-control" name="summary" id="summary" placeholder="Povzetek izdelka..." maxlength="255" rows="3"><?=$product['summary']?></textarea>
</div>
<div class="form-group">
<label class="col-md-4 col-form-label text-left" for="description">Opis:</label>
<textarea class="form-control" name="description" id="description" placeholder="Opis..." rows="20"><?=$product['description']?></textarea>
</div>
<div class="form-group">
<label class="col-md-4 col-form-label text-left" for="quantity">Količina:<span class="asterisk">*</span></label>
<input type="number" class="form-control hide-arrow" name="quantity" id="quantity" placeholder="Količina..." min="0" required value="<?=$product['quantity']?>">
</div>
<div class="form-group">
<label class="col-md-4 col-form-label text-left" for="price">Cena:<span class="asterisk">*</span></label>
<div class="input-group">
<input type="number" class="form-control hide-arrow" name="price" id="price" placeholder="Cena..." step="0.01" min="0" required value="<?=$product['price']?>">
<div class="input-group-append">
<div class="input-group-text">€</div>
</div>
</div>
</div>
<div class="form-group">
<label class="col" for="publishDate">Dan objave izdelka:</label>
<div class="input-group">
<input type="date" class="form-control" name="publishDate" id="publishDate" value="<?=$product['date_published']?>">
<div class="input-group-append">
<div class="input-group-text"><i class="fas fa-calendar-alt"></i></div>
</div>
</div>
</div>
<h3 class="subtitle">Šifra izdelka</h6>
<hr>
<div class="form-group">
<label class="col-md-4 col-form-label text-left" for="sku">SKU:<span class="asterisk">*</span></label>
<input type="text" class="form-control" name="sku" id="sku" placeholder="SKU..." maxlength="8" pattern="[A-z0-9]{1,}" required value="<?=$product['sku']?>">
</div>
<h3 class="subtitle">Mere izdelka</h6>
<hr>
<!-- measurement -->
<div class="form-row">
<div class="form-group col-md-3 col-form-label text-left">
<label class="col" for="height">Višina:</label>
<div class="input-group">
<input type="number" class="form-control text-center hide-arrow" name="height" id="height" min=0 step="0.01" placeholder="Višina..." value="<?=$product['height']?>">
<div class="input-group-append">
<div class="input-group-text">cm</div>
</div>
</div>
</div>
<div class="form-group col-md-3 col-form-label text-left">
<label class="col" for="length">Dolžina:</label>
<div class="input-group">
<input type="number" class="form-control text-center hide-arrow" name="length" id="length" min=0 step="0.01" placeholder="Dolžina..." value="<?=$product['length']?>">
<div class="input-group-append">
<div class="input-group-text">cm</div>
</div>
</div>
</div>
<div class="form-group col-md-3 col-form-label text-left">
<label class="col" for="width">Širina:</label>
<div class="input-group">
<input type="number" class="form-control text-center hide-arrow" name="width" id="width" min=0 step="0.01" placeholder="Širina..." value="<?=$product['width']?>">
<div class="input-group-append">
<div class="input-group-text">cm</div>
</div>
</div>
</div>
<div class="form-group col-md-3 col-form-label text-left">
<label class="col" for="weight">Teža:</label>
<div class="input-group">
<input type="number" class="form-control text-center hide-arrow" name="weight" id="weight" min=0 step="0.01" placeholder="Teža..." value="<?=$product['weight']?>">
<div class="input-group-append">
<div class="input-group-text">kg</div>
</div>
</div>
</div>
</div> <!-- form-row -->
<?php
// get all sales id that are active for the product
$sql = "SELECT * FROM product_category pc INNER JOIN category c ON pc.category_id = c.id WHERE pc.product_id = $pid";
$categories = $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
// convert the 2d array to 1d array
$categories = array_values(array_column($categories, 'id'));
?>
<!-- category -->
<div class="form-row">
<div class="form-group">
<label class="col-md-4 col-form-label text-left" for="category[]">Kategorije:</label>
<select class="form-select custom-select dropdown" id="category" name="category[]" multiple size="8" aria-label="multiple select size 3" style="min-width:300px;width:100%;">
<?php categoryTree($cid = $categories); ?>
</select>
<!-- select all / unselect all buttons -->
<div class="form-group text-center my-2">
<button type="button" id="selectAll" name="selectAll" class="btn btn-secondary" >Izberi vse</button>
<button type="button" id="unselectAll" name="deselectAll" class="btn btn-secondary">Prekliči izbiro</button>
<a type="button" class="btn btn-info" href="./category-add" target="_blank">Dodaj kategorijo</a>
</div>
</div>
</div>
<!-- brand -->
<div class="form-row col-4">
<div class="form-group">
<label class="col-md-4 col-form-label text-left" for="brand">Znamka:</label>
<select class="form-select" id="brand" name="brand" size="8" aria-label="multiple select size 3" style="min-width:300px;width:100%;">
<?php foreach(brands() as $brand):
if ($brand['id'] == $product['brand_id']): ?>
<option value="<?=$brand['id']?>" selected><?=$brand['title']?></option>
<?php else: ?>
<option value="<?=$brand['id']?>"><?=$brand['title']?></option>
<?php endif ?>
<?php endforeach; ?>
</select>
<div class="form-group text-center my-2">
<a type="button" class="btn btn-info my-2" href="./brand-add" target="_blank">Dodaj znamko</a>
</div>
</div>
</div>
<?php
// get all sales id that are active for the product
$sql = "SELECT s.id FROM product_sale ps INNER JOIN sale s ON ps.sale_id = s.id WHERE ps.product_id = $pid";
$discounts = $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
// convert the 2d array to 1d array
$discounts = array_values(array_column($discounts, 'id'));
?>
<div class="form-row">
<div class="form-group">
<label class="col-12 col-form-label text-left" for="sale">Popusti:</label>
<select class="form-select" id="sale" name="sale[]" size="8" aria-label="multiple select size 3" style="min-width:300px;width:100%;" multiple>
<?php foreach(sales() as $sale): ?>
<?php if(in_array($sale['id'], $discounts)): ?>
<option value="<?=$sale['id']?>" selected><?=format_date("d.m.Y",$sale['date_start'])?> / <?=format_date("d.m.Y",$sale['date_end'])?> | <?=$sale['discount']?>%</option>
<?php else: ?>
<option value="<?=$sale['id']?>"><?=format_date("d.m.Y",$sale['date_start'])?> / <?=format_date("d.m.Y",$sale['date_end'])?> | <?=$sale['discount']?>%</option>
<?php endif; ?>
<?php endforeach; ?>
</select>
<div class="form-group text-center my-2">
<a type="button" class="btn btn-info" href="./sale" target="_blank">Dodaj popuste</a>
</div>
</div>
</div> <!-- form-row -->
<button id="submit-btn" type="submit" name="submit" class="btn btn-primary float-right" style="width: 250px;">Posodobi</button>
<!-- <input type="button" id="submit-btn" name="update" class="btn btn-primary float-right" value="Posodobi"> -->
</form>
</div>
</div>
</div>
</div>
</div>
<?php
include_once './footer.php';
?>
<script>
$("option").mousedown(function(e) {
e.preventDefault();
$(this).prop('selected', !$(this).prop('selected'));
});
</script>
<script>
$('#selectAll').click(function() {
$('#category option').prop('selected', true);
});
$('#unselectAll').click(function() {
$('#category option').prop('selected', false);
});
</script>
<!-- image preview script -->
<script src="./src/js/image-upload-preview.js" crossorigin="anonymous"></script>
<script>
$(document).ready(function() {
$("#product-form").submit(function(e) {
e.preventDefault();
if (confirm("Želite posodobiti podatke?")) {
$.ajax({
type:'POST',
data: new FormData(this),
url: './src/inc/product-update.inc.php',
//dataType: 'json',
contentType: false,
cache: false,
processData: false,
success: function(data) {
var json = JSON.parse(data);
if (json.success == false) {
alert(json.message);
} else {
alert("Podatki uspešno spremenjeni!");
}
// move to top of page
window.scrollTo(0,0)
},
});
} else {
alert("Niste posodobili!");
}
});
});
</script>
<script type="text/javascript">
bkLib.onDomLoaded(function() {
new nicEditor({buttonList : ['bold',
'italic',
'underline',
'left',
'center',
'right',
'justify',
'ol',
'ul',
'strikethrough',
'removeformat',
'hr',
'forecolor',
'bgcolor']}).panelInstance('description')
});
</script>