1 | <?php |
||
12 | class CommentController implements InjectionAwareInterface |
||
13 | { |
||
14 | use InjectionAwareTrait; |
||
15 | |||
16 | |||
17 | |||
18 | /** |
||
19 | * @var \LRC\Repository\SoftDbRepository Comment repository. |
||
20 | */ |
||
21 | private $comments; |
||
22 | |||
23 | |||
24 | |||
25 | /** |
||
26 | * Configuration. |
||
27 | */ |
||
28 | 2 | public function init() |
|
38 | |||
39 | |||
40 | |||
41 | public function showComments($postid) |
||
72 | |||
73 | |||
74 | |||
75 | public function replyComment($postid) |
||
115 | |||
116 | |||
117 | |||
118 | public function editComment($postid) |
||
119 | { |
||
120 | $actionID = (int)$this->di->request->getGet("id"); |
||
121 | $currentComment = $this->comments->findSoft('id', $actionID); |
||
122 | if (!$currentComment) { |
||
123 | $this->di->response->redirect("comment/$postid"); |
||
124 | } |
||
125 | |||
126 | $loggedInUser = $this->di->userController->getLoggedInUserId(); |
||
127 | if (!$this->canComment($loggedInUser, $currentComment)) { |
||
128 | $this->di->response->redirect("comment/$postid"); |
||
129 | } |
||
130 | |||
131 | $editForm = new ModelForm('edit-comment-form', $currentComment); |
||
132 | |||
133 | if ($this->di->request->getMethod() == 'POST') { |
||
134 | $comment = $editForm->populateModel(null, ['id', 'post_id', 'parent_id']); |
||
135 | //Prevent edited column from being set to NULL |
||
136 | unset($comment->edited); |
||
137 | $editForm->validate(); |
||
138 | if ($editForm->isValid()) { |
||
139 | $this->comments->save($comment); |
||
140 | $this->di->response->redirect("comment/$postid#{$comment->id}"); |
||
141 | } |
||
142 | } |
||
143 | |||
144 | $comments = $this->getComments($postid, $loggedInUser); |
||
145 | $sortBy = $this->sortBy(); |
||
146 | |||
147 | $viewData = [ |
||
148 | "comments" => $this->buildCommentTree($comments, $sortBy), |
||
149 | "textfilter" => $this->di->textfilter, |
||
150 | "postid" => $postid, |
||
151 | "action" => "edit", |
||
152 | "actionID" => $actionID, |
||
153 | "newForm" => new ModelForm('new-comment-form', Comment::class), |
||
154 | "editForm" => $editForm, |
||
155 | "isLoggedIn" => $loggedInUser |
||
156 | ]; |
||
157 | |||
158 | $this->di->view->add("comment/comment-section", $viewData, "main", 2); |
||
159 | } |
||
160 | |||
161 | |||
162 | |||
163 | public function deleteComment($postid) |
||
164 | { |
||
165 | $actionID = (int)$this->di->request->getGet("id"); |
||
166 | $currentComment = $this->comments->findSoft('id', $actionID); |
||
167 | if (!$currentComment) { |
||
168 | $this->di->response->redirect("comment/$postid"); |
||
169 | } |
||
170 | |||
171 | $loggedInUser = $this->di->userController->getLoggedInUserId(); |
||
172 | if (!$this->canComment($loggedInUser, $currentComment)) { |
||
173 | $this->di->response->redirect("comment/$postid"); |
||
174 | } |
||
175 | |||
176 | if ($this->di->request->getMethod() == 'POST') { |
||
177 | if ($this->di->request->getPost('delete') == 'delete') { |
||
178 | $this->comments->deleteSoft($currentComment); |
||
179 | } |
||
180 | $this->di->response->redirect("comment/$postid#{$currentComment->id}"); |
||
181 | } |
||
182 | |||
183 | $comments = $this->getComments($postid, $loggedInUser); |
||
184 | $sortBy = $this->sortBy(); |
||
185 | |||
186 | $viewData = [ |
||
187 | "comments" => $this->buildCommentTree($comments, $sortBy), |
||
188 | "textfilter" => $this->di->textfilter, |
||
189 | "postid" => $postid, |
||
190 | "action" => "delete", |
||
191 | "actionID" => $actionID, |
||
192 | "newForm" => new ModelForm('new-comment-form', Comment::class), |
||
193 | "isLoggedIn" => $loggedInUser |
||
194 | ]; |
||
195 | |||
196 | $this->di->view->add("comment/comment-section", $viewData, "main", 2); |
||
197 | } |
||
198 | |||
199 | |||
200 | |||
201 | public function voteComment($postid) |
||
221 | |||
222 | |||
223 | |||
224 | 1 | public function getComments($postid, $loggedInUser) |
|
235 | |||
236 | |||
237 | |||
238 | 1 | public function sortBy() |
|
244 | |||
245 | |||
246 | |||
247 | 1 | public function buildCommentTree(array &$elements, $sortBy, $parentId = 0) |
|
259 | |||
260 | |||
261 | |||
262 | 2 | public function sortBranchComments(array &$branch, $sortBy = "best") |
|
284 | |||
285 | |||
286 | |||
287 | 1 | public function canComment($user, $comment) |
|
291 | } |
||
292 |