-
Notifications
You must be signed in to change notification settings - Fork 0
/
backadmin.php
205 lines (199 loc) · 8.8 KB
/
backadmin.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
<?php
require_once 'header.php';
$dbh = connectDB();
$connexion = $dbh->prepare('SELECT * from users');
$connexion->execute();
$resultuser = $connexion->fetchAll();
if (!empty($_SESSION['user'])) {
$id = $_SESSION['user']['admin'];
if ($id == 0) {
// nothing
} else {
header('location:index.php');
}
} else {
header('location:index.php');
}
$dbh = connectDB();
$connexion = $dbh->prepare('SELECT * from products');
$connexion->execute();
$resultats = $connexion->fetchAll();
$dbh = connectDB();
$connexion = $dbh->prepare('SELECT products.category_id, categories.name, categories.parent_id from products left join categories ON products.category_id = categories.parent_id');
$connexion->execute();
$resultstype = $connexion->fetchAll();
$errorsinsert = [];
$name = "";
$price = "";
$category_id = "";
$img = "";
$description = "";
if (!empty($_POST)) {
$name = $_POST['name'];
$price = $_POST['price'];
$category_id = $_POST['category_id'];
$img = $_POST['img'];
$description = $_POST['description'];
if (empty($name)) {
$errorsinsert[] = "Remplissez le nom du pokémon";
}
if (empty($price)) {
$errorsinsert[] = "Veuillez ajouter un prix";
}
if (empty($category_id)) {
$errorsinsert[] = "Sélectionnez un type";
}
if (empty($img)) {
$errorsinsert[] = "veuillez ajouter une url d'image";
}
if (empty($description)) {
$errorsinsert[] = "Veuillez ajouter une description";
}
//vérifie si l'email existe déjà
$dbh = connectDB();
$connexion = $dbh->prepare('SELECT * FROM products where name= :name');
$connexion->execute([
":name" => $name,
]);
$resultname = $connexion->fetchAll();
if ($resultname) {
$errorsinsert[] = "Pokémon déjà existant";
}
if (empty($errorsinsert)) {
$dbh = connectDB();
$connexion = $dbh->prepare('INSERT INTO products VALUES (NULL, :name, :price, :category_id, :img, :description )');
$connexion->execute([
":name" => $name,
":price" => $price,
":category_id" => $category_id,
":img" => $img,
":description" => $description,
]);
$results = $connexion->fetchAll();
}
}
?>
<?php foreach ($resultuser as $result) { ?>
<div class="card m-auto">
<p class='text-center'><?= $result['username'] ?></p>
<div class="card-body d-flex">
<a class="btn btn-primary" href="deleteuser.php?id=<?= $result['id'] ?>">Supprimer le profil</a>
</div>
</div>
<?php } ?>
<form class="formaddproduct" method="POST">
<div class="form-group">
<label class="nametext">Nom du Pokémon</label>
<input type="text" class="form-control" name="name">
</div>
<div class="form-group">
<label class="pricetextpokemon">Prix du Pokémon</label>
<input type="number" class="form-control" name="price" step="any">
</div>
<div class="form-group">
<label class="typechoice">Choissisez le type du Pokémon</label>
<select class="custom-select" name="category_id">
<option selected>Choissisez un type</option>
<option value="1">feu</option>
<option value="2">eau</option>
<option value="3">acier</option>
<option value="4">dragon</option>
<option value="5">electrik</option>
<option value="6">vol</option>
</select>
</div>
<div class="form-group">
<label class="imgpokemontext">Ajouter une Photo(url de la photo)</label>
<input type="url" class="form-control" name="img">
</div>
<div class="form-group">
<label class="describetext">Description du Pokémon</label>
<textarea class="form-control" aria-label="With textarea" name="description"></textarea>
</div>
<button type="submit" class="btn btn-primary mb-4">Valider</button>
<?php
if (!empty($errorsinsert)) {
echo '<div class="alert alert-danger mt-4" role="alert">';
echo implode("<br>", $errorsinsert);
echo '</div>';
}
?>
</form>
<div class="card-deck">
<?php foreach ($resultats as $resultat => $resultproduct) {
$resulttype = $resultstype[$resultat];
$nb = 100;
if (strlen($resultproduct['description']) >= $nb)
$resultproduct['description'] = substr($resultproduct['description'], 0, $nb) ?>
<div class='card'>
<div class='card-body'>
<img src="<?= $resultproduct['img'] ?>" class='card-img-top' alt='...'>
<div class='card-body'>
<p class='card-title'><?= $resultproduct['name'] ?></p>
<p class='card-text'><?= $resultproduct['description'] ?>...</p>
<p class='card-text'><?= $resulttype['name'] ?></p>
<p class='card-text'><?= $resultproduct['price'] ?> $</p>
<a href='addpanier.php?id=<?= $resultproduct['id'] ?>' class='btn btn-primary'>Ajouter au panier</a>
<button type="button" class="btn btn-primary mt-4 editBtn" data-toggle="modal" data-target="#exampleModal" data-id="<?= $resultproduct['id'] ?>">Modifier les infos</button>
<a class="btn btn-primary" href="deletepoke.php?id=<?= $resultproduct['id'] ?>">Supprimer le pokémon</a>
<script type="text/javascript">
$(".editBtn").click(function() {
console.log($(this).data("id"));
$("#itemId").val($(this).data("id"));
})
</script>
</div>
</div>
</div>
<?php } ?>
</div>
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<form class="formaddproduct" method="POST">
<script>
$('#myModal').on('show.bs.modal', function(e) {
$(this).find('.modal-body').html(' <div class="form-group"><label class="col-sm-3 control-label">ID</label><div class="col-sm-9"><input type="text" value = ' + e.relatedTarget.id + ' class="form-control" id="person_id" name="person_id" readonly="readonly"></div></div> <div class="form-group"> <label class="col-sm-3 control-label">Date</label><div class="col-sm-9"><input type="date" readonly="readonly" class="form-control" id="cedula_date" value="<?php echo date("Y-m-d"); ?>" name="cedula_date"></div></div> <div class="form-group"> <label class="col-sm-3 control-label">Cedula Number</label><div class="col-sm-9"><input type="number" class="form-control" id="cedula_number" name="cedula_number"/></div></div>');
})
</script>
<input type="hidden" id="itemId" name="itemId" value="">
<div class="form-group">
<label class="nametext">Changer le nom du Pokémon</label>
<input type="text" class="form-control" name="nameModal">
</div>
<div class="form-group">
<label class="pricetextpokemon"> Changer le Prix du Pokémon</label>
<input type="number" class="form-control" name="priceModal">
</div>
<div class="form-group">
<label class="typechoice">Changer le type du Pokémon</label>
<select class="custom-select" name="category_idModal">
<option selected>Choissisez un type</option>
<option value="1">feu</option>
<option value="2">eau</option>
<option value="3">acier</option>
<option value="4">dragon</option>
<option value="5">electrik</option>
<option value="6">vol</option>
</select>
</div>
<div class="form-group">
<label class="imgpokemontext">Changer la Photo(url de la photo)</label>
<input type="url" class="form-control" name="imgModal">
</div>
<div class="form-group">
<label class="describetext">Changer la description Pokémon</label>
<textarea class="form-control" aria-label="With textarea" name="descriptionModal"></textarea>
</div>
<button type="submit" class="btn btn-primary mb-4">Valider</button>
<?php
if (!empty($errors)) {
echo '<div class="alert alert-danger mt-4" role="alert">';
echo implode("<br>", $errors);
echo '</div>';
}
?>
</form>
</div>
</div>
</div>