1
|
|
|
<?php |
2
|
|
|
namespace modules\blog\admin\controller; |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
use core\App; |
6
|
|
|
use core\functions\ChaineCaractere; |
7
|
|
|
use core\HTML\flashmessage\FlashMessage; |
8
|
|
|
use Intervention\Image\ImageManager; |
9
|
|
|
use modules\blog\app\controller\Article; |
10
|
|
|
use modules\blog\app\controller\Blog; |
11
|
|
|
|
12
|
|
|
class AdminArticle extends Article { |
13
|
|
|
private $error_title; |
14
|
|
|
private $error_article; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
|
18
|
|
|
//-------------------------- GETTER ----------------------------------------------------------------------------// |
19
|
|
|
/** |
20
|
|
|
* @param $title |
21
|
|
|
* @return bool |
22
|
|
|
* function that verify if title of the article is ok |
23
|
|
|
*/ |
24
|
|
|
private function getTestTitle($title, $id_article=null) { |
25
|
|
|
$dbc = App::getDb(); |
26
|
|
|
|
27
|
|
|
if (strlen($title) < 5){ |
28
|
|
|
$this->error_title = "votre titre doit être supérieur à 4 caractères"; |
29
|
|
|
return false; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if (strlen($title) > 20) { |
33
|
|
|
$this->error_title = "votre titre ne doit pas eccéder 20 caractères"; |
34
|
|
|
return false; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if ($id_article == null) { |
38
|
|
|
$query = $dbc->select()->from("_blog_article")->where("title", "=", $title)->get(); |
39
|
|
|
} |
40
|
|
|
else { |
41
|
|
|
$query = $dbc->select()->from("_blog_article") |
42
|
|
|
->where("title", "=", $title, "AND")->where("ID_article", "!=", $id_article)->get(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if (count($query) > 0) { |
46
|
|
|
$this->error_title = "votre titre existe déjà merci d'en choisir un autre"; |
47
|
|
|
return false; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return true; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param $article |
55
|
|
|
* @return bool |
56
|
|
|
* function that verify if article is ok |
57
|
|
|
*/ |
58
|
|
|
private function getTestArticle($article) { |
59
|
|
|
if (ChaineCaractere::testMinLenght($article, 10) === false) { |
60
|
|
|
$this->error_article = "votre article doit être supérieur à 10 caractères"; |
61
|
|
|
return false; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return true; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param $id_article |
69
|
|
|
* @return bool |
70
|
|
|
* test if an article exist in bdd |
71
|
|
|
*/ |
72
|
|
|
private function getTestArticleExist($id_article) { |
73
|
|
|
$dbc = App::getDb(); |
74
|
|
|
|
75
|
|
|
$query = $dbc->select()->from("_blog_article")->where("ID_article", "=", $id_article)->get(); |
76
|
|
|
|
77
|
|
|
if (count($query) == 0) { |
78
|
|
|
return false; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return true; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param $id_article |
86
|
|
|
* @return mixed |
87
|
|
|
* function that get url of an article |
88
|
|
|
*/ |
89
|
|
|
private function getUrl($id_article) { |
90
|
|
|
$dbc = App::getDb(); |
91
|
|
|
|
92
|
|
|
$query = $dbc->select("url")->from("_blog_article")->where("ID_article", "=", $id_article)->get(); |
93
|
|
|
|
94
|
|
|
foreach ($query as $obj) { |
95
|
|
|
return $obj->url; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* this function get last articles |
101
|
|
|
*/ |
102
|
|
|
public function getAllArticle($id_state = null) { |
103
|
|
|
$dbc = App::getDb(); |
104
|
|
|
|
105
|
|
|
if ($id_state === null) { |
106
|
|
|
$query = $dbc->select() |
107
|
|
|
->from("_blog_article") |
108
|
|
|
->from("_blog_state") |
109
|
|
|
->from("identite") |
110
|
|
|
->where("_blog_article.ID_identite", "=", "identite.ID_identite", "AND", true) |
111
|
|
|
->where("_blog_article.ID_state", "=", "_blog_state.ID_state", "", true) |
112
|
|
|
->orderBy("publication_date", "DESC") |
113
|
|
|
->get(); |
114
|
|
|
} |
115
|
|
|
else { |
116
|
|
|
$query = $dbc->select() |
117
|
|
|
->from("_blog_article") |
118
|
|
|
->from("_blog_state") |
119
|
|
|
->from("identite") |
120
|
|
|
->where("_blog_article.ID_state", "=", $id_state, "AND") |
121
|
|
|
->where("_blog_article.ID_identite", "=", "identite.ID_identite", "AND", true) |
122
|
|
|
->where("_blog_article.ID_state", "=", "_blog_state.ID_state", "", true) |
123
|
|
|
->orderBy("publication_date", "DESC") |
124
|
|
|
->get(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
128
|
|
|
$articles = []; |
129
|
|
|
|
130
|
|
|
foreach ($query as $obj) { |
131
|
|
|
$articles[] = [ |
132
|
|
|
"id_article" => $obj->ID_article, |
133
|
|
|
"title" => $obj->title, |
134
|
|
|
"url" => $obj->url, |
135
|
|
|
"image" => $this->getImageArticle($obj->url), |
136
|
|
|
"article" => $obj->article, |
137
|
|
|
"id_state" => $obj->ID_state, |
138
|
|
|
"state" => $obj->state, |
139
|
|
|
"pseudo" => $obj->pseudo, |
140
|
|
|
"publication_date" => $this->getDateFr($obj->publication_date), |
141
|
|
|
"categories" => Blog::getCategory()->getCategoryArticle($obj->url) |
142
|
|
|
]; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
Blog::setValues(["articles" => $articles]); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
//-------------------------- END GETTER ----------------------------------------------------------------------------// |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
//-------------------------- SETTER ----------------------------------------------------------------------------// |
152
|
|
|
/** |
153
|
|
|
* @param $title |
154
|
|
|
* function that add image to an article |
155
|
|
|
*/ |
156
|
|
|
private function setImageArticle($title) { |
157
|
|
|
if (!empty($_FILES['image']['tmp_name'])) { |
158
|
|
|
$title_image = ChaineCaractere::setUrl($title); |
159
|
|
|
|
160
|
|
|
$name_image = ROOT."modules/blog/images/".$title_image.".png"; |
161
|
|
|
|
162
|
|
|
$manager = new ImageManager(); |
163
|
|
|
$image = $manager->make($_FILES['image']['tmp_name']); |
164
|
|
|
$image->crop(400, 400); |
165
|
|
|
$image->save($name_image); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param $title |
171
|
|
|
* @param $categories |
172
|
|
|
* @param $article |
173
|
|
|
* @param $state |
174
|
|
|
* @return bool |
175
|
|
|
* function to add an article and his categories |
176
|
|
|
*/ |
177
|
|
|
public function setAddArticle($title, $categories, $article, $state) { |
178
|
|
|
$dbc = App::getDb(); |
179
|
|
|
|
180
|
|
View Code Duplication |
if ($this->getTestTitle($title) === false || $this->getTestArticle($article) === false) { |
181
|
|
|
FlashMessage::setFlash($this->error_title.$this->error_article); |
182
|
|
|
return false; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
$this->setImageArticle($title); |
186
|
|
|
|
187
|
|
|
$dbc->insert("title", $title) |
188
|
|
|
->insert("url", ChaineCaractere::setUrl($title)) |
189
|
|
|
->insert("publication_date", date("Y-m-d H:i:s")) |
190
|
|
|
->insert("article", $article) |
191
|
|
|
->insert("ID_identite", $_SESSION['idlogin'.CLEF_SITE]) |
192
|
|
|
->insert("ID_state", $state) |
193
|
|
|
->into("_blog_article")->set(); |
194
|
|
|
|
195
|
|
|
$id_article = $dbc->lastInsertId(); |
196
|
|
|
|
197
|
|
|
AdminBlog::getAdminCategory()->setCategoriesArticle($categories, $id_article); |
198
|
|
|
return true; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param $title |
203
|
|
|
* @param $categories |
204
|
|
|
* @param $article |
205
|
|
|
* @param $state |
206
|
|
|
* @return bool |
207
|
|
|
* function to edit an article and his categories |
208
|
|
|
*/ |
209
|
|
|
public function setEditArticle($title, $categories, $article, $state, $id_article) { |
210
|
|
|
$dbc = App::getDb(); |
211
|
|
|
|
212
|
|
View Code Duplication |
if ($this->getTestTitle($title, $id_article) === false || $this->getTestArticle($article) === false) { |
213
|
|
|
FlashMessage::setFlash($this->error_title.$this->error_article); |
214
|
|
|
return false; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
if ($this->getTestArticleExist($id_article) === false) { |
218
|
|
|
FlashMessage::setFlash("votre article n'existe pas"); |
219
|
|
|
return false; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
$dbc->update("title", $title) |
223
|
|
|
->update("url", ChaineCaractere::setUrl($title)) |
224
|
|
|
->update("article", $article) |
225
|
|
|
->update("ID_identite", $_SESSION['idlogin'.CLEF_SITE]) |
226
|
|
|
->update("ID_state", $state) |
227
|
|
|
->from("_blog_article") |
228
|
|
|
->where("ID_article", "=", $id_article) |
229
|
|
|
->set(); |
230
|
|
|
|
231
|
|
|
AdminBlog::getAdminCategory()->setUpdateCategoriesArticle($categories, $id_article); |
232
|
|
|
return true; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @param $id_article |
237
|
|
|
* function that is used to delete an article |
238
|
|
|
*/ |
239
|
|
|
public function setTrashArticle($id_article) { |
240
|
|
|
$dbc = App::getDb(); |
241
|
|
|
|
242
|
|
|
if ($this->getTestArticleExist($id_article) === true) { |
243
|
|
|
$dbc->update("ID_state", 4)->from("_blog_article")->where("ID_article", "=", $id_article)->set(); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
FlashMessage::setFlash("Votre message a bien été archivé", "success"); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @param $id_article |
251
|
|
|
* function that is used to delete an article |
252
|
|
|
*/ |
253
|
|
|
public function setDeleteArticle($id_article) { |
254
|
|
|
$dbc = App::getDb(); |
255
|
|
|
|
256
|
|
|
if ($this->getTestArticleExist($id_article) === true) { |
257
|
|
|
unlink(ROOT."modules/blog/images/".$this->getUrl($id_article).".png"); |
258
|
|
|
|
259
|
|
|
$dbc->delete()->from("_blog_article")->where("ID_article", "=", $id_article)->del(); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
FlashMessage::setFlash("Votre message a bien été supprimé", "success"); |
263
|
|
|
} |
264
|
|
|
//-------------------------- END SETTER ----------------------------------------------------------------------------// |
265
|
|
|
} |