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