1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controllers\Admin; |
4
|
|
|
|
5
|
|
|
use App\Models\CategoryModel; |
6
|
|
|
use App\Models\PostModel; |
7
|
|
|
use App\Models\SlugModel; |
8
|
|
|
use App\Models\TagModel; |
9
|
|
|
use Core\AdminController; |
10
|
|
|
use Core\Container; |
11
|
|
|
|
12
|
|
|
class Post extends AdminController |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
protected $siteConfig; |
16
|
|
|
protected $pagination; |
17
|
|
|
|
18
|
|
|
private $categoryModel; |
19
|
|
|
private $tagModel; |
20
|
|
|
private $postModel; |
21
|
|
|
private $slugModel; |
22
|
|
|
|
23
|
|
|
public function __construct(Container $container) |
24
|
|
|
{ |
25
|
|
|
$this->loadModules[] = 'SiteConfig'; |
26
|
|
|
$this->loadModules[] = 'pagination'; |
27
|
|
|
parent::__construct($container); |
28
|
|
|
|
29
|
|
|
$this->categoryModel = new CategoryModel($this->container); |
30
|
|
|
$this->tagModel = new TagModel($this->container); |
31
|
|
|
$this->postModel = new PostModel($this->container); |
32
|
|
|
$this->slugModel = new SlugModel($this->container); |
33
|
|
|
|
34
|
|
|
//adding the necessary default data |
35
|
|
|
$this->data['configs'] = $this->siteConfig->getSiteConfig(); |
36
|
|
|
$this->data['categories'] = $this->categoryModel->getCategories(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* add tags to a post |
41
|
|
|
* @param array $tags list of tags to apply |
42
|
|
|
* @param int $postId the post to add tags to |
43
|
|
|
* @throws \Exception |
44
|
|
|
*/ |
45
|
|
|
private function addTags(array $tags, int $postId):void |
46
|
|
|
{ |
47
|
|
|
foreach ($tags as $tag) { |
48
|
|
|
if (isset($tag["id"])) { |
49
|
|
|
$this->tagModel->addTagToPost($postId, $tag["id"]); |
50
|
|
|
continue; |
51
|
|
|
} |
52
|
|
|
$this->tagModel->addNewTagToPost($postId, $tag["name"]); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* page for new post |
58
|
|
|
*/ |
59
|
|
|
public function new() |
60
|
|
|
{ |
61
|
|
|
$this->onlyAdmin(); |
62
|
|
|
$this->data['tags'] = $this->tagModel->getTags(); |
63
|
|
|
$this->renderView('Admin/Post'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Lists all the posts |
68
|
|
|
* @param string $page |
69
|
|
|
* @param int $postsPerPage |
70
|
|
|
* @throws \ReflectionException |
71
|
|
|
* @throws \Twig_Error_Loader |
72
|
|
|
* @throws \Twig_Error_Runtime |
73
|
|
|
* @throws \Twig_Error_Syntax |
74
|
|
|
*/ |
75
|
|
|
public function list(string $page = "page-1", int $postsPerPage = 20) |
76
|
|
|
{ |
77
|
|
|
$this->onlyAdmin(); |
78
|
|
|
|
79
|
|
|
$defaultPostsPerpage = 20; |
80
|
|
|
|
81
|
|
|
$totalPosts = $this->postModel->totalNumberFullPosts(); |
82
|
|
|
$pagination = $this->pagination->getPagination($page, $totalPosts, $postsPerPage); |
83
|
|
|
|
84
|
|
|
if($postsPerPage !== $defaultPostsPerpage){ |
85
|
|
|
$this->data['paginationPostsPerPage'] = $postsPerPage; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$this->data["posts"] = $this->postModel->getFullPosts($pagination["offset"], $postsPerPage); |
89
|
|
|
$this->data['pagination'] = $pagination; |
90
|
|
|
$this->renderView("Admin/ListPost"); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Shows the post to modify and update |
95
|
|
|
* @throws \ReflectionException |
96
|
|
|
* @throws \Twig_Error_Loader |
97
|
|
|
* @throws \Twig_Error_Runtime |
98
|
|
|
* @throws \Twig_Error_Syntax |
99
|
|
|
* @throws \ErrorException |
100
|
|
|
*/ |
101
|
|
|
public function modify(string $slug): void |
102
|
|
|
{ |
103
|
|
|
$this->onlyAdmin(); |
104
|
|
|
|
105
|
|
|
$postId = $this->slugModel->getIdFromSlug($slug, "posts", "posts_slug", "idposts"); |
106
|
|
|
|
107
|
|
|
$this->data['post'] = $this->postModel->getSinglePost($postId); |
108
|
|
|
$this->data['postTags'] = $this->tagModel->getTagsOnPost($postId); |
109
|
|
|
$this->data['tags'] = $this->tagModel->getTags(); |
110
|
|
|
$this->renderView('Admin/Post'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Create a new post |
115
|
|
|
* @throws \ErrorException |
116
|
|
|
*/ |
117
|
|
|
public function createNewPost() |
118
|
|
|
{ |
119
|
|
|
//Security checks |
120
|
|
|
$this->onlyAdmin(); |
121
|
|
|
if (!$this->request->isPost()) { |
122
|
|
|
$this->alertBox->setAlert('Only post messages allowed', 'error'); |
123
|
|
|
$this->response->redirect('admin'); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$posts = $this->container->getRequest()->getDataFull(); |
127
|
|
|
$userSessionId = $this->container->getSession()->get("user_id"); |
128
|
|
|
|
129
|
|
|
|
130
|
|
|
$title = trim($posts["postTitle"]); |
131
|
|
|
$postImage = $posts["postImage"]; |
132
|
|
|
$postSlug = trim($posts["postSlug"]); |
133
|
|
|
$article = $posts["postTextArea"]; |
134
|
|
|
$idCategory = $posts["categorySelector"]; |
135
|
|
|
$published = $posts["isPublished"]; |
136
|
|
|
$onFrontpage = $posts["isOnFrontPage"]; |
137
|
|
|
$idUser = $userSessionId; |
138
|
|
|
|
139
|
|
|
if(!is_int($idUser) || $idUser === null) |
140
|
|
|
{ |
141
|
|
|
throw new \Error("Invalid userID"); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
//security and error checks |
145
|
|
|
$error = false; |
146
|
|
|
if ($title == "") { |
147
|
|
|
$error = true; |
148
|
|
|
$this->alertBox->setAlert("empty title not allowed", "error"); |
149
|
|
|
} |
150
|
|
|
if ($postSlug == "") { |
151
|
|
|
$error = true; |
152
|
|
|
$this->alertBox->setAlert("empty slug not allowed", "error"); |
153
|
|
|
} |
154
|
|
|
if (!$this->slugModel->isUnique($postSlug, "posts", "posts_slug")) { |
155
|
|
|
$error = true; |
156
|
|
|
$this->alertBox->setAlert("Slug not unique", "error"); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
if ($error) { |
160
|
|
|
$this->container->getResponse()->redirect("admin/post/new"); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$postId = $this->postModel->newPost($title, $postImage, $idCategory, $article, $idUser, $published, $onFrontpage, |
164
|
|
|
$postSlug); |
165
|
|
|
|
166
|
|
|
//Taking care of tags. |
167
|
|
|
if (isset($posts["tags"])) { |
168
|
|
|
$this->addTags($posts["tags"], $postId); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
//checking result and redirecting |
172
|
|
|
if ($postId != null) { |
173
|
|
|
$this->alertBox->setAlert("Post " . $title . " Created"); |
174
|
|
|
$this->container->getResponse()->redirect("admin/post/modify/" . $postSlug); |
175
|
|
|
} |
176
|
|
|
$this->alertBox->setAlert("Error creating " . $title, "error"); |
177
|
|
|
$this->container->getResponse()->redirect("admin/post/new"); |
178
|
|
|
|
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* update a post |
183
|
|
|
* @throws \Exception |
184
|
|
|
*/ |
185
|
|
|
public function modifyPost() |
186
|
|
|
{ |
187
|
|
|
//Security checks |
188
|
|
|
$this->onlyAdmin(); |
189
|
|
|
if (!$this->request->isPost()) { |
190
|
|
|
$this->alertBox->setAlert('Only post messages allowed', 'error'); |
191
|
|
|
$this->response->redirect('admin'); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
$posts = $this->container->getRequest()->getDataFull(); |
195
|
|
|
|
196
|
|
|
$postId = $posts["postId"]; |
197
|
|
|
$title = trim($posts["postTitle"]); |
198
|
|
|
$postImage = $posts["postImage"]; |
199
|
|
|
$postSlug = trim($posts["postSlug"]); |
200
|
|
|
$article = $posts["postTextArea"]; |
201
|
|
|
$idCategory = $posts["categorySelector"]; |
202
|
|
|
$published = $posts["isPublished"]; |
203
|
|
|
$onFrontpage = $posts["isOnFrontPage"]; |
204
|
|
|
|
205
|
|
|
//security and error checks |
206
|
|
|
$originalPostSlug = $this->slugModel->getSlugFromId($postId, "posts", "idposts", |
207
|
|
|
"posts_slug"); |
208
|
|
|
$error = false; |
209
|
|
|
if ($title == "") { |
210
|
|
|
$error = true; |
211
|
|
|
$this->alertBox->setAlert("empty title not allowed", "error"); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
if ($postSlug == "") { |
215
|
|
|
$error = true; |
216
|
|
|
$this->alertBox->setAlert("empty slug not allowed", "error"); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
if ($postSlug != $originalPostSlug) //if the slug has been updated |
220
|
|
|
{ |
221
|
|
|
if (!$this->slugModel->isUnique($postSlug, "posts", "posts_slug")) { |
222
|
|
|
$error = true; |
223
|
|
|
$originalPostSlug = $this->slugModel->getSlugFromId($postId, "posts", "idposts", "posts_slug"); |
224
|
|
|
$this->alertBox->setAlert("Slug not unique", "error"); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
if ($error) { |
228
|
|
|
$this->container->getResponse()->redirect("admin/post/modify/$originalPostSlug"); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
//Update the post |
232
|
|
|
$postUpdate = $this->postModel->modifyPost($postId, $title, $postImage, $idCategory, $article, $published, |
233
|
|
|
$onFrontpage, $postSlug); |
234
|
|
|
|
235
|
|
|
// Tags |
236
|
|
|
//remove all tags |
237
|
|
|
$this->tagModel->removeTagsOnPost($postId); |
238
|
|
|
//set new tags |
239
|
|
|
if (isset($posts["tags"])) { |
240
|
|
|
$this->addTags($posts["tags"], $postId); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
//checking result and redirecting |
244
|
|
|
if ($postUpdate) { |
245
|
|
|
$this->alertBox->setAlert("Post " . $title . " Updated"); |
246
|
|
|
$this->container->getResponse()->redirect("admin/post/modify/" . $postSlug); |
247
|
|
|
} |
248
|
|
|
$this->alertBox->setAlert("Error updating " . $title, "error"); |
249
|
|
|
$this->container->getResponse()->redirect("admin/post/modify/" . $originalPostSlug); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* deletes a specific post |
255
|
|
|
* @param int $postId |
256
|
|
|
* @throws \Exception |
257
|
|
|
*/ |
258
|
|
|
public function deletePost(int $postId) |
259
|
|
|
{ |
260
|
|
|
$postTitle = $this->postModel->getTitleFromId($postId); |
261
|
|
|
//first remove tags or foreign key error |
262
|
|
|
$this->tagModel->removeTagsOnPost($postId); |
263
|
|
|
$removedPost = $this->postModel->deletePost($postId); |
264
|
|
|
|
265
|
|
|
if($removedPost) |
266
|
|
|
{ |
267
|
|
|
$this->alertBox->setAlert("Post ".$postTitle." deleted"); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
$this->response->redirect("admin/post/list/"); |
271
|
|
|
} |
272
|
|
|
} |