-
Notifications
You must be signed in to change notification settings - Fork 0
/
produits_edit.php
356 lines (341 loc) · 24.8 KB
/
produits_edit.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
<?php
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
include 'php/header.php';
include 'php/bddData.php';
$sql = "SELECT * FROM categorie";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$categories = array();
while ($row = $result->fetch_assoc()) {
$categories[] = $row;
}
} else {
$categories = array();
}
$est_vide=false;
$sql = "SELECT produits.*, categorie.libelle AS categorie
FROM produits
INNER JOIN categorie ON produits.categorie_id = categorie.id ORDER BY categorie.id, description;";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$products = $result->fetch_all(MYSQLI_ASSOC);
} else {
$est_vide = true;
}
$sql = "SELECT MAX(id) AS max_id FROM produits";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$max_id = $result->fetch_assoc();
$max_id_produit_value = $max_id['max_id']+1;
}else{
$max_id_produit_value=1;
}
$sql = "SELECT MAX(id) AS max_id FROM categorie";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$max_id = $result->fetch_assoc();
$max_id_categorie_value = $max_id['max_id']+1;
}else{
$max_id_categorie_value=1;
}
$conn->close();
?>
<div class="container">
<div class="row">
<h1 class="text-center font-weight-bold">Produits & Catégories</h1>
<div class="card p-4 mt-2">
<div class="row mb-3 justify-content-center">
<div class="col-md-1"></div>
<div class="col-md-10">
<div class="input-group">
<!-- <div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-search"></i></span>
</div>
<input type="text" class="form-control" id="searchInput" placeholder="Rechercher..."> -->
<div class="col-12">
<div class="row">
<div class="col-auto">
<button class="btn btn-primary" type="button" data-bs-toggle="modal" data-bs-target="#modalCategories"><i class="fa-solid fa-layer-group"></i> Catégories</button>
</div>
<div class="col-auto">
<button class="btn btn-primary" type="button" id="btn-ajouter-produit"><i class="fa-regular fa-plus"></i> Ajouter produit</button>
</div>
<div class="col-auto">
<form id="input-products-csv-form" method="post" action="php/importer_produits.php" enctype="multipart/form-data">
<div>
<input type="file" id="input-products-csv" class="form-control" name="csv_file" accept=".csv" style="display: none;">
<button id="button-products-csv" class="btn btn-success" type="button"><i class="fa-regular fa-file-excel"></i> Importer Produits</button>
</div>
</form>
</div>
<div class="col-auto">
<form id="" method="post" action="php/download_produits.php" enctype="multipart/form-data">
<div>
<button id="" class="btn btn-dark" type="submit"><i class="fa-solid fa-download"></i> Télécharger csv</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
<div id="error-message">
<?php if (isset($_GET["error"])){
echo '<div class="alert alert-danger alert-dismissible" role="alert">'.htmlspecialchars($_GET["error"]).
'<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button></div>';
}elseif(isset($_GET["success"])){
echo '<div class="alert alert-success alert-dismissible" role="alert">'.htmlspecialchars($_GET["success"]).
'<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button></div>';
}
?>
</div>
<table class="table table-hover table-sm" id="productsTable">
<thead>
<tr>
<th class="text-center" scope="col"></th>
<th class="text-center" scope="col">Référence</th>
<th class="text-center" scope="col">Description</th>
<th class="text-center" scope="col">Prix</th>
<th class="text-center table-stock" scope="col">Stock</th>
<th class="text-center" scope="col">Catégorie</th>
<th class="text-center" scope="col"></th>
</tr>
</thead>
<tbody>
<tr id="tr-ajouter-produit" hidden>
<th class="align-middle text-center">
<div class="h-10 image-container">
<form class="edit-image-form" action="php/upload_image.php" method="POST" enctype="multipart/form-data">
<img src="img/product.jpg" height="100px" alt="">
<div class="edit-image" style="display: inline;">
<i class="fa fa-add edit-icon product-edit-icon"></i>
</div>
<input type="file" name="file" class="edit-image-input" accept=".jpg" hidden>
<input type="hidden" name="destination_folder" value="../img/produits/">
<input type="hidden" name="original_page" value="../produits_edit.php">
<input type="hidden" name="product_id" value="<?php echo $max_id_produit_value; ?>" accept=".jpg" hidden>
</form>
</div>
</th>
<form method="post" id="form-ajouter-produit" action="php/ajouter_produit.php">
<td class="align-middle text-center"><input name="reference" class="form-control" style="width: 100px;" type="text" value=""></td>
<td class="align-middle text-center"><input name="description" class="form-control" type="text" value=""></td>
<td class="align-middle text-center"><input name="prix" class="form-control" style="width: 100px;" type="number" step=".01" value=""></td>
<td class="align-middle text-center table-stock"><input name="stock" class="form-control input-only-numbers" style="width: 60px;" type="text" value=""></td>
<td class="align-middle text-center table-stock"><select name="categorie" class="form-select" style="width: 180px;">
<?php foreach ($categories as $category) : ?>
<option value="<?php echo $category['id']; ?>"><?php echo $category['libelle']; ?></option>
<?php endforeach; ?>
</select></td>
<td class="align-middle text-center">
<form method="post" action="php/ajouter_produit.php">
<input type="hidden" name="product_id" value="<?php echo $max_id_produit_value; ?>">
<button class="btn btn-primary btn-add-to-cart" type="submit"><i class="fa-solid fa-plus"></i></button>
</form>
<button id="btn-hide-produit" type="submit" class="btn btn-trash btn-primary"><i class="fa fa-close"></i></button>
</td>
</form>
</tr>
<?php
if($est_vide){
echo "<tr>";
echo "<td colspan='6' class='text-center'>Désolé, mais pour l'instant, aucun produit n'est disponible.</td>";
echo "</tr>";}
foreach ($products as $product) : ?>
<tr>
<th class="align-middle text-center">
<div class="h-10 image-container">
<form class="edit-image-form" action="php/upload_image.php" method="POST" enctype="multipart/form-data">
<img src="img/produits/<?php echo $product['id']; ?>.jpg" height="100px" onerror="this.onerror=null; this.src='img/product.jpg';" alt="">
<div class="edit-image" style="display: inline;">
<i class="fa fa-edit edit-icon product-edit-icon"></i>
</div>
<input type="file" name="file" class="edit-image-input" accept=".jpg" hidden>
<input type="hidden" name="destination_folder" value="../img/produits/">
<input type="hidden" name="original_page" value="../produits_edit.php">
<input type="hidden" name="product_id" value="<?php echo $product['id']; ?>" accept=".jpg" hidden>
</form>
</div>
</th>
<form method="post" action="php/update_product.php">
<td class="align-middle text-center"><input name="reference" class="form-control" style="width: 100px;" type="text" value="<?php echo $product['reference']; ?>"></td>
<td class="align-middle text-center"><input name="description" class="form-control" type="text" value="<?php echo $product['description']; ?>"></td>
<td class="align-middle text-center"><input name="prix" class="form-control" style="width: 100px;" type="number" step=".01" value="<?php echo $product['prix']; ?>"></td>
<td class="align-middle text-center table-stock"><input name="stock" class="form-control input-only-numbers" style="width: 60px;" type="text" value="<?php echo $product['stock']; ?>"></td>
<td class="align-middle text-center table-stock"><select name="categorie" class="form-select" style="width: 180px;">
<?php foreach ($categories as $category) : ?>
<option <?php if ($category['id'] == $product['categorie_id']) { echo "selected"; } ?> value="<?php echo $category['id']; ?>"><?php echo $category['libelle']; ?></option>
<?php endforeach; ?>
</select></td>
<td class="align-middle text-center">
<form method="post" action="php/update_product.php">
<input type="hidden" name="product_id" value="<?php echo $product['id']; ?>">
<button class="btn btn-primary btn-add-to-cart" type="submit"><i class="fa-solid fa-floppy-disk"></i></button>
</form>
<form method="post" action="php/remove_product.php" style="display: inline;">
<input type="hidden" name="product_id" value="<?php echo $product['id']; ?>">
<button type="submit" class="btn btn-trash btn-primary"><i class="fa fa-trash"></i></button>
</form>
</td>
</form>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<nav aria-label="Page navigation" id="pagination_productsTable">
<ul class="pagination justify-content-center">
<li class="page-item disabled">
<a class="page-link" href="#" tabindex="-1" aria-disabled="true">Précédent</a>
</li>
<li class="page-item active"><a class="page-link" href="#">1</a></li>
<li class="page-item"><a class="page-link" href="#">2</a></li>
<li class="page-item"><a class="page-link" href="#">3</a></li>
<li class="page-item">
<a class="page-link" href="#">Suivant</a>
</li>
</ul>
</nav>
</div>
</div>
</div>
</div>
</div>
<!-- Modal Categories -->
<div id="modalCategories" class="modal fade" tabindex="-1">
<div class="modal-dialog modal-dialog-centered modal-lg">
<div class="modal-content">
<div class="modal-header">
<button class="btn btn-primary ml-1" type="button" id="btn-ajouter-categorie">Ajouter catégorie</button>
</div>
<div class=container>
<table class="table table-hover" id="productTable">
<thead>
<tr>
<th class="text-center" scope="col">Catégorie</th>
<th class="text-center" scope="col">Icon</th>
<th class="text-center" scope="col"></th>
</tr>
</thead>
<tbody>
<tr id="tr-ajouter-categorie" hidden>
<?php
$categories_icons = [
"-- Icône par défaut --" => "fa-solid fa-tags",
"Audio" => "fa-solid fa-headphones",
"Caméras" => "fa-solid fa-camera",
"Composantes" => "fa-solid fa-microchip",
"Imprimantes et scanners" => "fa-solid fa-print",
"Jeux vidéo" => "fa-solid fa-gamepad",
"Montres" => "fa-solid fa-clock",
"Ordinateurs" => "fa-solid fa-laptop",
"Réseaux" => "fa-solid fa-wifi",
"Robots" => "fa-solid fa-robot",
"Sécurité" => "fa-solid fa-shield-halved",
"Téléphones" => "fa-solid fa-mobile-screen-button",
"Télévisions" => "fa-solid fa-tv",
];
?>
<form method="post" action="php/ajouter_categorie.php">
<td class="align-middle text-center"><input name="categorie_libelle" class="form-control" type="text" value=""></td>
<td class="align-middle text-center">
<select name="select_categorie_icon" class="form-select">
<?php
foreach ($categories_icons as $label => $value) {
echo '<option value="' . $value . '">' . $label . '</option>';
}
?>
</select>
</td>
<td class="align-middle text-center">
<form method="post" action="php/update_categorie.php">
<input type="hidden" name="categorie_id" value="<?php echo $max_id_categorie_value; ?>">
<button class="btn btn-primary btn-add-to-cart" type="submit"><i class="fa fa-add"></i></button>
</form>
<button id="btn-hide-categorie" type="submit" class="btn btn-trash btn-primary"><i class="fa fa-close"></i></button>
</td>
</form>
</tr>
<?php
if($est_vide){
echo "<tr>";
echo "<td colspan='6' class='text-center'>Désolé, mais pour l'instant, aucune catégorie n'est disponible.</td>";
echo "</tr>";}
foreach ($categories as $category) : ?>
<tr>
<form method="post" action="php/update_categorie.php">
<td class="align-middle text-center"><input name="categorie_libelle" class="form-control" type="text" value="<?php echo $category["libelle"];?>"></td>
<td class="align-middle text-center">
<select name="select_categorie_icon" class="form-select">
<?php
foreach ($categories_icons as $label => $value) {
$selected = ($category["icon"] == $value) ? "selected" : "";
echo '<option ' . $selected . ' value="' . $value . '">' . $label . '</option>';
}
?>
</select>
</td>
<td class="align-middle text-center">
<form method="post" action="php/update_categorie.php">
<input type="hidden" name="categorie_id" value="<?php echo $category['id']; ?>">
<button class="btn btn-primary btn-add-to-cart" type="submit"><i class="fa-solid fa-floppy-disk"></i></button>
</form>
<form method="post" action="php/remove_categorie.php" style="display: inline;">
<input type="hidden" name="categorie_id" value="<?php echo $category['id']; ?>">
<button type="submit" class="btn btn-trash btn-primary"><i class="fa fa-trash"></i></button>
</form>
</td>
</form>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<?php include 'php/footer.php'; ?>
<script>
$(document).ready(function() {
$('#product-image').click(function() {
$('#image-file').click();
});
$('#image-file').change(function() {
var file = this.files[0];
if (file) {
var reader = new FileReader();
reader.onload = function(e) {
$('#product-image').attr('src', e.target.result);
};
reader.readAsDataURL(file);
}
});
$('.edit-image-form').submit(function(e) {
e.preventDefault();
var formData = new FormData(this);
var img = $(this).find('img');
var src = img.attr('src');
$.ajax({
type: 'POST',
url: 'php/upload_image.php',
data: formData,
processData: false,
contentType: false,
dataType: 'json',
success: function(response) {
if(response.success){
$('#error-message').html('<div class="alert alert-success alert-dismissible" role="alert">'+response.message+'<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button></div>');
img.attr('src', src + '?' + new Date().getTime());
}else{
$('#error-message').html('<div class="alert alert-danger alert-dismissible" role="alert">'+response.message+'<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button></div>');
}
},
error: function(xhr, status, error) {
$('#error-message').html('<div class="alert alert-danger alert-dismissible" role="alert">'+response.message+' '+error+'<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button></div>');
}
});
});
});
</script>