-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgestion_matieres.php
More file actions
190 lines (177 loc) · 9.79 KB
/
gestion_matieres.php
File metadata and controls
190 lines (177 loc) · 9.79 KB
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
<?php
session_start();
require_once "auth_check.php";
require_once "config/database.php";
require_once "models/Matiere.php";
// Vérifier si l'utilisateur est connecté et est le super admin
if (!isset($_SESSION['user_id']) || !$_SESSION['is_super_admin']) {
$_SESSION['redirect_url'] = $_SERVER['PHP_SELF'];
header("Location: login.php");
exit();
}
$database = new Database();
$db = $database->getConnection();
$matiere = new Matiere($db);
$message = '';
$error = '';
//Traitement du formulaire d'ajout/modification
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['action'])) {
// Vérifier le jeton CSRF
if (!isset($_POST['csrf_token']) || $_POST['csrf_token'] != $_SESSION['csrf_token']) {
$error = "Erreur de sécurité. Veuillez réessayer.";
} else {
if ($_POST['action'] === 'add') {
$matiere->nom = trim($_POST['nom']);
$matiere->credit = floatval($_POST['credit']);
if ($matiere->exists($matiere->nom)) {
$error = "Cette matière existe déjà.";
} else {
if ($matiere->create()) {
$message = "Matière ajoutée avec succès.";
} else {
$error = "Une erreur est survenue lors de l'ajout de la matière.";
}
}
} elseif ($_POST['action'] === 'edit' && isset($_POST['id'])) {
$matiere->id = $_POST['id'];
$matiere->nom = trim($_POST['nom']);
$matiere->credit = floatval($_POST['credit']);
if ($matiere->update()) {
$message = "Matière mise à jour avec succès.";
} else {
$error = "Une erreur est survenue lors de la mise à jour de la matière.";
}
} elseif ($_POST['action'] === 'delete' && isset($_POST['id'])) {
$matiere->id = $_POST['id'];
if ($matiere->delete()) {
$message = "Matière supprimée avec succès.";
} else {
$error = "Une erreur est survenue lors de la suppression de la matière.";
}
}
}
}
}
// Générer un nouveau jeton CSRF
if (!isset($_SESSION['csrf_token'])) {
$_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}
$matieres = $matiere->read();
?>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Gestion des Matières</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.8.1/font/bootstrap-icons.css" rel="stylesheet">
</head>
<body>
<?php include 'header.php'; ?>
<div class="container mt-4">
<div class="card">
<div class="card-header bg-primary text-white d-flex justify-content-between align-items-center">
<h1 class="h3 mb-0">Gestion des Matières et Crédits</h1>
</div>
<div class="card-body">
<?php if ($message): ?>
<div class="alert alert-success"><?php echo htmlspecialchars($message); ?></div>
<?php endif; ?>
<?php if ($error): ?>
<div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div>
<?php endif; ?>
<form method="POST" class="mb-4">
<input type="hidden" name="csrf_token" value="<?php echo $_SESSION['csrf_token']; ?>">
<input type="hidden" name="action" value="add">
<div class="row g-3">
<div class="col-md-5">
<input type="text" name="nom" class="form-control" placeholder="Nom de la matière" required>
</div>
<div class="col-md-4">
<input type="number" name="credit" class="form-control" placeholder="Nombre de crédits" min="1" step="1" required>
</div>
<div class="col-md-3">
<button type="submit" class="btn btn-primary w-100">
<i class="bi bi-plus-circle"></i> Ajouter une matière
</button>
</div>
</div>
</form>
<div class="table-responsive">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>Matière</th>
<th>Crédits</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php while ($row = $matieres->fetch(PDO::FETCH_ASSOC)): ?>
<tr>
<td><?php echo htmlspecialchars($row['nom']); ?></td>
<td><?php echo htmlspecialchars($row['credit']); ?></td>
<td>
<form method="POST" class="d-inline">
<input type="hidden" name="csrf_token" value="<?php echo $_SESSION['csrf_token']; ?>">
<input type="hidden" name="action" value="edit">
<input type="hidden" name="id" value="<?php echo $row['id']; ?>">
<div class="btn-group">
<button type="button" class="btn btn-sm btn-outline-primary" data-bs-toggle="modal" data-bs-target="#editModal<?php echo $row['id']; ?>">
<i class="bi bi-pencil"></i> Modifier
</button>
</div>
</form>
<form method="POST" class="d-inline">
<input type="hidden" name="csrf_token" value="<?php echo $_SESSION['csrf_token']; ?>">
<input type="hidden" name="action" value="delete">
<input type="hidden" name="id" value="<?php echo $row['id']; ?>">
<div class="btn-group">
<button type="submit" class="btn btn-sm btn-outline-danger">
<i class="bi bi-trash"></i> Supprimer
</button>
</div>
</form>
</td>
</tr>
<!-- Modal pour modification -->
<div class="modal fade" id="editModal<?php echo $row['id']; ?>" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modifier la matière</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<form method="POST">
<div class="modal-body">
<input type="hidden" name="csrf_token" value="<?php echo $_SESSION['csrf_token']; ?>">
<input type="hidden" name="action" value="edit">
<input type="hidden" name="id" value="<?php echo $row['id']; ?>">
<div class="mb-3">
<label class="form-label">Nom de la matière</label>
<input type="text" name="nom" class="form-control" value="<?php echo htmlspecialchars($row['nom']); ?>" required>
</div>
<div class="mb-3">
<label class="form-label">Crédits</label>
<input type="number" name="credit" class="form-control" value="<?php echo htmlspecialchars($row['credit']); ?>" min="1" step="1" required>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Annuler</button>
<button type="submit" class="btn btn-primary">Enregistrer</button>
</div>
</form>
</div>
</div>
</div>
<?php endwhile; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>