-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharticle_admin.php
158 lines (122 loc) · 4.82 KB
/
article_admin.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
<?php
require_once '../images/config_projet.php';
require_once '../session.php';
require_once '../templates/pdo.php';
require_once '../tools.php';
require_once '../images/articles.php';
require_once "category.php";
require_once "header_admin.php";
$errors = [];
$messages = [];
$article = [
'title' => '',
'description' => '',
'category_id' => ''
];
if (isset($_GET['id'])) {
$article = getArticleById($pdo, $_GET['id']);
if ($article === false) {
$errors[] = "L'article n\'existe pas";
}
$pageTitle = "Formulaire modification article";
} else {
$pageTitle = "Formulaire ajout article";
}
if (isset($_POST['saveArticle'])) {
$fileName = null;
if (isset($_FILES["file"]["tmp_name"]) && $_FILES["file"]["tmp_name"] != '') {
$checkImage = getimagesize($_FILES["file"]["tmp_name"]);
if ($checkImage !== false) {
$fileName = slugify(basename($_FILES["file"]["name"]));
$fileName = uniqid() . '-' . $fileName;
if (move_uploaded_file($_FILES["file"]["tmp_name"], dirname(__DIR__).ARTICLES_IMAGES_PROJET . $fileName)) {
if (isset($_POST['image'])) {
unlink(dirname(__DIR__).ARTICLES_IMAGES_PROJET . $_POST['image']);
}
} else {
$errors[] = 'Le fichier n\'a pas été uploadé';
}
} else {
$errors[] = 'Le fichier doit être une image';
}
} else {
if (isset($_GET['id'])) {
if (isset($_POST['delete_image'])) {
unlink(dirname(__DIR__).ARTICLES_IMAGES_PROJET . $_POST['image']);
} else {
$fileName = $_POST['image'];
}
}
}
$article = [
'title' => $_POST['title'],
'description' => $_POST['description'],
'category_id' => $_POST['category_id'],
'image' => $fileName
];
if (!$errors) {
if (isset($_GET["id"])) {
$id = (int)$_GET["id"];
} else {
$id = null;
}
$res = saveArticle($pdo, $_POST["title"], $_POST["description"], $fileName, (int)$_POST["category_id"], $id);
if ($res) {
$messages[] = "L'article a bien été sauvegardé";
if (!isset($_GET["id"])) {
$article = [
'title' => '',
'description' => '',
'category_id' => ''
];
}
} else {
$errors[] = "L'article n'a pas été sauvegardé";
}
}
}
?>
<h1><?= $pageTitle; ?></h1>
<?php foreach ($messages as $message) { ?>
<div class="alert alert-success" role="alert">
<?= $message; ?>
</div>
<?php } ?>
<?php foreach ($errors as $error) { ?>
<div class="alert alert-danger" role="alert">
<?= $error; ?>
</div>
<?php } ?>
<?php if ($article !== false) { ?>
<form method="POST" enctype="multipart/form-data">
<div class="mb-3">
<label for="title" class="form-label">Titre</label>
<input type="text" class="form-control" id="title" name="title" value="<?= $article['title']; ?>">
</div>
<div class="mb-3">
<label for="content" class="form-label">Contenu</label>
<textarea class="form-control" id="description" name="description" rows="8"><?= $article['description']; ?></textarea>
</div>
<div class="mb-3">
<label for="category" class="form-label">Catégorie</label>
<select name="category_id" id="category" class="form-select">
<?php foreach ($categories as $category) { ?>
<option value="1" <?php if (isset($article['category_id']) && $article['category_id'] == $category['id']) { ?>selected="selected" <?php }; ?>><?= $category['name'] ?></option>
<?php } ?>
</select>
</div>
<?php if (isset($_GET['id']) && isset($article['image'])) { ?>
<p>
<img src="<?= ARTICLES_IMAGES_PROJET. $article['image'] ?>" alt="<?= $article['title'] ?>" width="100">
<label for="delete_image">Supprimer l'image</label>
<input type="checkbox" name="delete_image" id="delete_image">
<input type="hidden" name="image" value="<?= $article['image']; ?>">
</p>
<?php } ?>
<p>
<input type="file" name="file" id="file">
</p>
<input type="submit" name="saveArticle" class="btn btn-primary" value="Enregistrer">
</form>
<?php } ?>
<?php require_once "footer_admin.php"; ?>