Modification
Modifications des fichiers avec les fonctions du fichier model.php.
Méthode : personnes.php
1
2
require 'model.php';
3
4
$listePersonnes = all('personnes');
5
6
$page_title = 'Personnes';
7
8
9
<html lang='fr'>
10
...
11
</html>
Méthode : show.php
1
2
require 'model.php';
3
$id = $_GET['id'] ?? '';
4
5
$personne = find('personnes', $id);
6
7
if (!$personne) {
8
header('location:personnes.php');
9
exit();
10
}
11
12
$page_title = "Afficher-$personne[slug]";
13
14
15
16
<html lang='fr'>
17
...
18
</html>
Méthode : create.php
1
2
require 'model.php';
3
$page_title = 'Ajouter';
4
5
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['ajouter'])) {
6
7
// récupération des données du formulaire
8
$nom = $_POST['nom'] ?? '';
9
$prenom = $_POST['prenom'] ?? '';
10
$age = (int) $_POST['age'] ?? 0;
11
$slug = $_POST['slug'] ?? '';
12
13
$id = create('personnes', compact('nom', 'prenom', 'age', 'slug'));
14
15
// récupération photo
16
if (isset($_FILES['photo']) && is_uploaded_file($_FILES['photo']['tmp_name'])) {
17
$photo = $id . "_$slug." . strtolower(pathinfo($_FILES['photo']['name'], PATHINFO_EXTENSION));
18
$origine = $_FILES['photo']['tmp_name'];
19
$destination = "photos/$photo";
20
move_uploaded_file($origine, $destination);
21
} else {
22
$photo = $id . "_$slug.png";
23
$origine = 'photos/photo.png';
24
$destination = "photos/$photo";
25
copy($origine, $destination);
26
}
27
update('personnes', compact('photo', 'id'));
28
29
// redirection
30
header('location:personnes.php');
31
exit();
32
}
33
34
35
<html lang='fr'>
36
...
37
</html>
Méthode : update.php
1
2
require 'model.php';
3
4
$id = $_GET['id'] ?? '';
5
6
$personne = find('personnes', $id);
7
8
if (!$personne) {
9
header('location:personnes.php');
10
exit();
11
}
12
13
$page_title = "Modifier-$personne[slug]";
14
15
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['modifier'])) {
16
// récupération des données du formulaire
17
$nom = $_POST['nom'] ?? '';
18
$prenom = $_POST['prenom'] ?? '';
19
$age = (int) $_POST['age'] ?? 0;
20
$slug = $_POST['slug'] ?? '';
21
$photo = $personne['photo'];
22
23
// renommage de la photo si changement de slug
24
if ($slug != $personne['slug']) {
25
$url = "photos/$personne[photo]";
26
$photo = $id . "_$slug." . strtolower(pathinfo($url, PATHINFO_EXTENSION));
27
if (file_exists($url)) {
28
rename($url, "photos/$photo");
29
}
30
}
31
// modification de la personne
32
update('personnes', compact('nom', 'prenom', 'age', 'slug', 'photo', 'id'));
33
34
if (isset($_FILES['photo']) && is_uploaded_file($_FILES['photo']['tmp_name'])) {
35
// suppression ancienne photo
36
if (file_exists("photos/$photo")) {
37
unlink("photos/$photo");
38
}
39
// ajout de la nouvelle photo
40
$photo = $id . "_$slug." . strtolower(pathinfo($_FILES['photo']['name'], PATHINFO_EXTENSION));
41
$origine = $_FILES['photo']['tmp_name'];
42
$destination = "photos/$photo";
43
move_uploaded_file($origine, $destination);
44
}
45
46
header('location:personnes.php');
47
exit();
48
}
49
50
51
<html lang='fr'>
52
...
53
</html>
Méthode : delete.php
1
2
require 'model.php';
3
$id = $_GET['id'] ?? '';
4
5
$personne = find('personnes', $id);
6
7
if (!$personne) {
8
header('location:personnes.php');
9
exit();
10
}
11
12
$page_title = "Supprimer-$personne[slug]";
13
14
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['supprimer'])) {
15
// suppression photo
16
if (file_exists("photos/$personne[photo]")) {
17
unlink("photos/$personne[photo]");
18
}
19
20
delete('personnes', $id);
21
22
header('location:personnes.php');
23
exit();
24
}
25
26
27
<html lang='fr'>
28
...
29
</html>