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
|
|
|
var_dump($t->idcommentc); |
|
|
|
|
40
|
|
|
$this->deleteCommentComment($t->idcommentc, true); |
41
|
|
|
$commentComments->getNext(); |
42
|
|
|
echo "deleted"; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$comment->delete("idcomment", $commentId); |
47
|
|
|
$createUrl = $this->di->get("url")->create("comment/viewAllPosts"); |
48
|
|
|
$url = isset($_SERVER["HTTP_REFERER"]) ? $_SERVER["HTTP_REFERER"] : $createUrl; |
49
|
|
|
$this->di->get("response")->redirect($url); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
View Code Duplication |
public function editComment($commentid) |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
$title = "Update comment"; |
55
|
|
|
$view = $this->di->get("view"); |
56
|
|
|
$pageRender = $this->di->get("pageRender"); |
57
|
|
|
$form = new UpdateCommentForm($this->di, $commentid); |
58
|
|
|
|
59
|
|
|
$form->check(); |
60
|
|
|
|
61
|
|
|
$data = [ |
62
|
|
|
"form" => $form->getHTML(), |
63
|
|
|
]; |
64
|
|
|
|
65
|
|
|
$view->add("comment/editComment", $data); |
66
|
|
|
|
67
|
|
|
return $pageRender->renderPage(["title" => $title]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function viewAllPosts() |
71
|
|
|
{ |
72
|
|
|
$title = "Retrieve all posts"; |
73
|
|
|
$view = $this->di->get("view"); |
74
|
|
|
$pageRender = $this->di->get("pageRender"); |
75
|
|
|
$post = new Post(); |
76
|
|
|
$post->setDb($this->di->get("db")); |
77
|
|
|
|
78
|
|
|
$posts = $post->getAllPosts(); |
79
|
|
|
|
80
|
|
|
$allPosts = []; |
81
|
|
|
|
82
|
|
View Code Duplication |
foreach ($posts as $p) { |
|
|
|
|
83
|
|
|
array_push($allPosts, [$p, $post->getTags([$p->postid])]); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$data = [ |
87
|
|
|
"items" => $allPosts, |
88
|
|
|
]; |
89
|
|
|
|
90
|
|
|
$view->add("comment/viewAllPosts", $data); |
91
|
|
|
|
92
|
|
|
return $pageRender->renderPage(["title" => $title]); |
93
|
|
|
} |
94
|
|
|
|
95
|
2 |
View Code Duplication |
public function newPost() |
|
|
|
|
96
|
|
|
{ |
97
|
1 |
|
$title = "Create new post"; |
98
|
1 |
|
$view = $this->di->get("view"); |
99
|
1 |
|
$pageRender = $this->di->get("pageRender"); |
100
|
1 |
|
$form = new CreatePostForm($this->di); |
101
|
|
|
|
102
|
1 |
|
$form->check(); |
103
|
|
|
|
104
|
|
|
$data = [ |
105
|
1 |
|
"form" => $form->getHTML(), |
106
|
2 |
|
]; |
107
|
|
|
|
108
|
1 |
|
$view->add("comment/addNewPost", $data); |
109
|
|
|
|
110
|
1 |
|
return $pageRender->renderPage(["title" => $title]); |
111
|
|
|
} |
112
|
|
|
|
113
|
1 |
View Code Duplication |
public function newComment($id) |
|
|
|
|
114
|
|
|
{ |
115
|
1 |
|
if ($this->di->get("session")->has("email")) { |
116
|
1 |
|
$title = "Create new comment"; |
117
|
1 |
|
$view = $this->di->get("view"); |
118
|
1 |
|
$pageRender = $this->di->get("pageRender"); |
119
|
1 |
|
$form = new CreateCommentForm($this->di, $id); |
120
|
|
|
|
121
|
1 |
|
$form->check(); |
122
|
|
|
|
123
|
|
|
$data = [ |
124
|
1 |
|
"form" => $form->getHTML(), |
125
|
1 |
|
]; |
126
|
|
|
|
127
|
1 |
|
$view->add("comment/addNewComment", $data); |
128
|
|
|
|
129
|
1 |
|
return $pageRender->renderPage(["title" => $title]); |
130
|
|
|
} else { |
131
|
|
|
$login = $this->di->get("url")->create("user/login"); |
132
|
|
|
$this->di->get("response")->redirect($login); |
133
|
|
|
return false; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function postAndComments($id) |
138
|
|
|
{ |
139
|
|
|
$title = "Retrieve one post with comments"; |
140
|
|
|
$view = $this->di->get("view"); |
141
|
|
|
$pageRender = $this->di->get("pageRender"); |
142
|
|
|
$post = new Post(); |
143
|
|
|
$post->setDb($this->di->get("db")); |
144
|
|
|
if ($post->checkId($id)) { |
145
|
|
|
if ($this->di->get("session")->has("email")) { |
146
|
|
|
$email = $this->di->get("session")->get("email"); |
147
|
|
|
$user = new User(); |
148
|
|
|
$user->setDb($this->di->get("db")); |
149
|
|
|
$permissions = $user->returnPermissions($email); |
150
|
|
|
} else { |
151
|
|
|
$permissions = "user"; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$comment = new Comment(); |
155
|
|
|
$comment->setDb($this->di->get("db")); |
156
|
|
|
$comments = $comment->getAllCommentsFromSpecificPost([$id]); |
157
|
|
|
|
158
|
|
|
$comment->getNext(); |
159
|
|
|
|
160
|
|
|
$commentComments = new CommentComments(); |
161
|
|
|
$commentComments->setDb($this->di->get("db")); |
162
|
|
|
|
163
|
|
|
$allComments = []; |
164
|
|
|
|
165
|
|
|
foreach ($comments as $comment) { |
166
|
|
|
array_push($allComments, [$comment, |
167
|
|
|
$commentComments->getAllCommentFromComments([$comment->idcomment])]); |
168
|
|
|
$commentComments->getNext(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$data = [ |
172
|
|
|
"post" => $post->getPostInfo([$id]), |
173
|
|
|
"comments" => $allComments, |
174
|
|
|
"permissions" => $permissions, |
175
|
|
|
"tags" => $post->getTags([$id]) |
176
|
|
|
]; |
177
|
|
|
|
178
|
|
|
$view->add("comment/onePostWithComment", $data); |
179
|
|
|
|
180
|
|
|
return $pageRender->renderPage(["title" => $title]); |
181
|
|
|
} else { |
182
|
|
|
$url = $this->di->get("url")->create("comment/viewAllPosts"); |
183
|
|
|
$this->di->get("response")->redirect($url); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
View Code Duplication |
public function newCommentComment($idcomment, $idpost) |
|
|
|
|
188
|
|
|
{ |
189
|
|
|
if ($this->di->get("session")->has("email")) { |
190
|
|
|
$title = "Create new comment"; |
191
|
|
|
$view = $this->di->get("view"); |
192
|
|
|
$pageRender = $this->di->get("pageRender"); |
193
|
|
|
$form = new CreateCommentCommentForm($this->di, $idcomment, $idpost); |
194
|
|
|
|
195
|
|
|
$form->check(); |
196
|
|
|
|
197
|
|
|
$data = [ |
198
|
|
|
"form" => $form->getHTML(), |
199
|
|
|
]; |
200
|
|
|
|
201
|
|
|
$view->add("comment/addNewComment", $data); |
202
|
|
|
|
203
|
|
|
return $pageRender->renderPage(["title" => $title]); |
204
|
|
|
} else { |
205
|
|
|
$login = $this->di->get("url")->create("user/login"); |
206
|
|
|
$this->di->get("response")->redirect($login); |
207
|
|
|
return false; |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public function deleteCommentComment($id, $nested = false) |
|
|
|
|
212
|
|
|
{ |
213
|
|
|
if (!$nested) { |
214
|
|
|
$commentcomments = new CommentComments(); |
215
|
|
|
$commentcomments->setDb($this->di->get("db")); |
216
|
|
|
$commentcomments->delete("idcommentc", $id); |
217
|
|
|
$createUrl = $this->di->get("url")->create("comment/viewAllPosts"); |
218
|
|
|
$url = isset($_SERVER["HTTP_REFERER"]) ? $_SERVER["HTTP_REFERER"] : $createUrl; |
219
|
|
|
$this->di->get("response")->redirect($url); |
220
|
|
|
} else { |
221
|
|
|
$commentcomments = new CommentComments(); |
222
|
|
|
$commentcomments->setDb($this->di->get("db")); |
223
|
|
|
$commentcomments->delete("idcommentc", $id); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
|
228
|
|
View Code Duplication |
public function returnCatId($category) |
|
|
|
|
229
|
|
|
{ |
230
|
|
|
$cat = new PostCategory(); |
231
|
|
|
$cat->setDb($this->di->get("db")); |
232
|
|
|
// var_dump($cat->getId($category)); |
|
|
|
|
233
|
|
|
$id = $cat->getId($category); |
234
|
|
|
return $id; |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
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: