1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Radchasay\Comment; |
4
|
|
|
|
5
|
|
|
use \Anax\DI\InjectionAwareInterface; |
6
|
|
|
use \Anax\DI\InjectionAwareTrait; |
7
|
|
|
use \Radchasay\Comment\Post; |
8
|
|
|
use \Radchasay\Comment\Comment; |
9
|
|
|
use \Radchasay\Comment\CommentComments; |
10
|
|
|
use \Radchasay\Comment\PostCategory; |
11
|
|
|
use \Radchasay\User\User; |
12
|
|
|
use \Radchasay\Comment\HTMLForm\CreatePostForm; |
13
|
|
|
use \Radchasay\Comment\HTMLForm\CreateCommentForm; |
14
|
|
|
use \Radchasay\Comment\HTMLForm\UpdateCommentForm; |
15
|
|
|
use \Radchasay\Comment\HTMLForm\CreateCommentCommentForm; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* CommentModel |
19
|
|
|
*/ |
20
|
|
|
class CommentController implements InjectionAwareInterface |
21
|
|
|
{ |
22
|
|
|
use InjectionAwareTrait; |
23
|
|
|
|
24
|
|
|
public function deleteComment($commentId) |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
$comment = new Comment(); |
27
|
|
|
$comment->setDb($this->di->get("db")); |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
$commentComments = new CommentComments(); |
31
|
|
|
$commentComments->setDb($this->di->get("db")); |
32
|
|
|
|
33
|
|
|
$test = $commentComments->getAllCommentFromComments([$commentId]); |
34
|
|
|
|
35
|
|
|
$commentComments->getNext(); |
36
|
|
|
|
37
|
|
|
if (!empty($test)) { |
38
|
|
|
foreach ($test as $t) { |
39
|
|
|
$this->deleteCommentComment($t->idcommentc, true); |
40
|
|
|
$commentComments->getNext(); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$comment->delete("idcomment", $commentId); |
45
|
|
|
$createUrl = $this->di->get("url")->create("comment/viewAllPosts"); |
46
|
|
|
$url = isset($_SERVER["HTTP_REFERER"]) ? $_SERVER["HTTP_REFERER"] : $createUrl; |
47
|
|
|
$this->di->get("response")->redirect($url); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
View Code Duplication |
public function editComment($commentid) |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
$title = "Update comment"; |
53
|
|
|
$view = $this->di->get("view"); |
54
|
|
|
$pageRender = $this->di->get("pageRender"); |
55
|
|
|
$form = new UpdateCommentForm($this->di, $commentid); |
56
|
|
|
|
57
|
|
|
$form->check(); |
58
|
|
|
|
59
|
|
|
$data = [ |
60
|
|
|
"form" => $form->getHTML(), |
61
|
|
|
]; |
62
|
|
|
|
63
|
|
|
$view->add("comment/editComment", $data); |
64
|
|
|
|
65
|
|
|
return $pageRender->renderPage(["title" => $title]); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function viewAllPosts() |
69
|
|
|
{ |
70
|
|
|
$title = "Retrieve all posts"; |
71
|
|
|
$view = $this->di->get("view"); |
72
|
|
|
$pageRender = $this->di->get("pageRender"); |
73
|
|
|
$post = new Post(); |
74
|
|
|
$post->setDb($this->di->get("db")); |
75
|
|
|
|
76
|
|
|
$posts = $post->getAllPosts(); |
77
|
|
|
|
78
|
|
|
$allPosts = []; |
79
|
|
|
|
80
|
|
View Code Duplication |
foreach ($posts as $p) { |
|
|
|
|
81
|
|
|
array_push($allPosts, [$p, $post->getTags([$p->postid])]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$data = [ |
85
|
|
|
"items" => $allPosts, |
86
|
|
|
]; |
87
|
|
|
|
88
|
|
|
$view->add("comment/viewAllPosts", $data); |
89
|
|
|
|
90
|
|
|
return $pageRender->renderPage(["title" => $title]); |
91
|
|
|
} |
92
|
|
|
|
93
|
2 |
View Code Duplication |
public function newPost() |
|
|
|
|
94
|
|
|
{ |
95
|
1 |
|
$title = "Create new post"; |
96
|
1 |
|
$view = $this->di->get("view"); |
97
|
1 |
|
$pageRender = $this->di->get("pageRender"); |
98
|
1 |
|
$form = new CreatePostForm($this->di); |
99
|
|
|
|
100
|
1 |
|
$form->check(); |
101
|
|
|
|
102
|
|
|
$data = [ |
103
|
1 |
|
"form" => $form->getHTML(), |
104
|
1 |
|
]; |
105
|
|
|
|
106
|
2 |
|
$view->add("comment/addNewPost", $data); |
107
|
|
|
|
108
|
1 |
|
return $pageRender->renderPage(["title" => $title]); |
109
|
|
|
} |
110
|
|
|
|
111
|
1 |
View Code Duplication |
public function newComment($id) |
|
|
|
|
112
|
|
|
{ |
113
|
1 |
|
if ($this->di->get("session")->has("email")) { |
114
|
1 |
|
$title = "Create new comment"; |
115
|
1 |
|
$view = $this->di->get("view"); |
116
|
1 |
|
$pageRender = $this->di->get("pageRender"); |
117
|
1 |
|
$form = new CreateCommentForm($this->di, $id); |
118
|
|
|
|
119
|
1 |
|
$form->check(); |
120
|
|
|
|
121
|
|
|
$data = [ |
122
|
1 |
|
"form" => $form->getHTML(), |
123
|
1 |
|
]; |
124
|
|
|
|
125
|
1 |
|
$view->add("comment/addNewComment", $data); |
126
|
|
|
|
127
|
1 |
|
return $pageRender->renderPage(["title" => $title]); |
128
|
|
|
} else { |
129
|
|
|
$login = $this->di->get("url")->create("user/login"); |
130
|
|
|
$this->di->get("response")->redirect($login); |
131
|
|
|
return false; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function postAndComments($id) |
136
|
|
|
{ |
137
|
|
|
$title = "Retrieve one post with comments"; |
138
|
|
|
$view = $this->di->get("view"); |
139
|
|
|
$pageRender = $this->di->get("pageRender"); |
140
|
|
|
$post = new Post(); |
141
|
|
|
$post->setDb($this->di->get("db")); |
142
|
|
|
if ($post->checkId($id)) { |
143
|
|
|
if ($this->di->get("session")->has("email")) { |
144
|
|
|
$email = $this->di->get("session")->get("email"); |
145
|
|
|
$user = new User(); |
146
|
|
|
$user->setDb($this->di->get("db")); |
147
|
|
|
$permissions = $user->returnPermissions($email); |
148
|
|
|
} else { |
149
|
|
|
$permissions = "user"; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$comment = new Comment(); |
153
|
|
|
$comment->setDb($this->di->get("db")); |
154
|
|
|
$comments = $comment->getAllCommentsFromSpecificPost([$id]); |
155
|
|
|
|
156
|
|
|
$comment->getNext(); |
157
|
|
|
|
158
|
|
|
$commentComments = new CommentComments(); |
159
|
|
|
$commentComments->setDb($this->di->get("db")); |
160
|
|
|
|
161
|
|
|
$allComments = []; |
162
|
|
|
|
163
|
|
|
foreach ($comments as $comment) { |
164
|
|
|
array_push($allComments, [$comment, |
165
|
|
|
$commentComments->getAllCommentFromComments([$comment->idcomment])]); |
166
|
|
|
$commentComments->getNext(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
$data = [ |
170
|
|
|
"post" => $post->getPostInfo([$id]), |
171
|
|
|
"comments" => $allComments, |
172
|
|
|
"permissions" => $permissions, |
173
|
|
|
"tags" => $post->getTags([$id]) |
174
|
|
|
]; |
175
|
|
|
|
176
|
|
|
$view->add("comment/onePostWithComment", $data); |
177
|
|
|
|
178
|
|
|
return $pageRender->renderPage(["title" => $title]); |
179
|
|
|
} else { |
180
|
|
|
$url = $this->di->get("url")->create("comment/viewAllPosts"); |
181
|
|
|
$this->di->get("response")->redirect($url); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
View Code Duplication |
public function newCommentComment($idcomment, $idpost) |
|
|
|
|
186
|
|
|
{ |
187
|
|
|
if ($this->di->get("session")->has("email")) { |
188
|
|
|
$title = "Create new comment"; |
189
|
|
|
$view = $this->di->get("view"); |
190
|
|
|
$pageRender = $this->di->get("pageRender"); |
191
|
|
|
$form = new CreateCommentCommentForm($this->di, $idcomment, $idpost); |
192
|
|
|
|
193
|
|
|
$form->check(); |
194
|
|
|
|
195
|
|
|
$data = [ |
196
|
|
|
"form" => $form->getHTML(), |
197
|
|
|
]; |
198
|
|
|
|
199
|
|
|
$view->add("comment/addNewComment", $data); |
200
|
|
|
|
201
|
|
|
return $pageRender->renderPage(["title" => $title]); |
202
|
|
|
} else { |
203
|
|
|
$login = $this->di->get("url")->create("user/login"); |
204
|
|
|
$this->di->get("response")->redirect($login); |
205
|
|
|
return false; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public function deleteCommentComment($id, $nested = false) |
|
|
|
|
210
|
|
|
{ |
211
|
|
|
if (!$nested) { |
212
|
|
|
$commentcomments = new CommentComments(); |
213
|
|
|
$commentcomments->setDb($this->di->get("db")); |
214
|
|
|
$commentcomments->delete("idcommentc", $id); |
215
|
|
|
$createUrl = $this->di->get("url")->create("comment/viewAllPosts"); |
216
|
|
|
$url = isset($_SERVER["HTTP_REFERER"]) ? $_SERVER["HTTP_REFERER"] : $createUrl; |
217
|
|
|
$this->di->get("response")->redirect($url); |
218
|
|
|
} else { |
219
|
|
|
$commentcomments = new CommentComments(); |
220
|
|
|
$commentcomments->setDb($this->di->get("db")); |
221
|
|
|
$commentcomments->delete("idcommentc", $id); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
|
226
|
|
View Code Duplication |
public function returnCatId($category) |
|
|
|
|
227
|
|
|
{ |
228
|
|
|
$cat = new PostCategory(); |
229
|
|
|
$cat->setDb($this->di->get("db")); |
230
|
|
|
$id = $cat->getId($category); |
231
|
|
|
return $id; |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: