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