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