These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | namespace App\Controller; |
||
3 | |||
4 | use App\Event\Badges; |
||
5 | use App\Event\Statistics; |
||
6 | use Cake\Core\Configure; |
||
7 | use Cake\Event\Event; |
||
8 | use Cake\Network\Exception\NotFoundException; |
||
9 | use Cake\Routing\Router; |
||
10 | |||
11 | class BlogController extends AppController |
||
12 | { |
||
13 | |||
14 | /** |
||
15 | * Initialization hook method. |
||
16 | * |
||
17 | * @return void |
||
18 | */ |
||
19 | public function initialize() |
||
20 | { |
||
21 | parent::initialize(); |
||
22 | |||
23 | $this->loadComponent('RequestHandler'); |
||
24 | } |
||
25 | |||
26 | /** |
||
27 | * BeforeFilter handle. |
||
28 | * |
||
29 | * @param Event $event The beforeFilter event that was fired. |
||
30 | * |
||
31 | * @return void |
||
32 | */ |
||
33 | public function beforeFilter(Event $event) |
||
34 | { |
||
35 | parent::beforeFilter($event); |
||
36 | |||
37 | $this->Auth->allow(['index', 'category', 'article', 'go', 'archive', 'search']); |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * Display all Articles. |
||
42 | * |
||
43 | * @return void |
||
44 | */ |
||
45 | View Code Duplication | public function index() |
|
46 | { |
||
47 | $this->loadModel('BlogArticles'); |
||
48 | $this->paginate = [ |
||
49 | 'maxLimit' => Configure::read('Blog.article_per_page') |
||
50 | ]; |
||
51 | |||
52 | $articles = $this->BlogArticles |
||
53 | ->find() |
||
54 | ->contain([ |
||
55 | 'BlogCategories', |
||
56 | 'Users' => function ($q) { |
||
57 | return $q->find('short'); |
||
58 | } |
||
59 | ]) |
||
60 | ->order([ |
||
61 | 'BlogArticles.created' => 'desc' |
||
62 | ]) |
||
63 | ->where([ |
||
64 | 'BlogArticles.is_display' => 1 |
||
65 | ]); |
||
66 | |||
67 | $articles = $this->paginate($articles); |
||
68 | |||
69 | $this->set(compact('articles')); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Display a specific category with all its articles. |
||
74 | * |
||
75 | * @return \Cake\Network\Response|void |
||
76 | */ |
||
77 | public function category() |
||
78 | { |
||
79 | $this->loadModel('BlogCategories'); |
||
80 | |||
81 | $category = $this->BlogCategories |
||
82 | ->find() |
||
83 | ->where([ |
||
84 | 'BlogCategories.id' => $this->request->id |
||
85 | ]) |
||
86 | ->contain([ |
||
87 | 'BlogArticles' |
||
88 | ]) |
||
89 | ->first(); |
||
90 | |||
91 | //Check if the category is found. |
||
92 | if (empty($category)) { |
||
93 | $this->Flash->error(__('This category doesn\'t exist or has been deleted.')); |
||
94 | |||
95 | return $this->redirect(['action' => 'index']); |
||
96 | } |
||
97 | |||
98 | //Paginate all Articles. |
||
99 | $this->loadModel('BlogArticles'); |
||
100 | $this->paginate = [ |
||
101 | 'maxLimit' => Configure::read('Blog.article_per_page') |
||
102 | ]; |
||
103 | |||
104 | $articles = $this->BlogArticles |
||
105 | ->find() |
||
106 | ->contain([ |
||
107 | 'Users' => function ($q) { |
||
108 | return $q->find('short'); |
||
109 | } |
||
110 | ]) |
||
111 | ->where([ |
||
112 | 'BlogArticles.category_id' => $category->id, |
||
113 | 'BlogArticles.is_display' => 1 |
||
114 | ]) |
||
115 | ->order([ |
||
116 | 'BlogArticles.created' => 'desc' |
||
117 | ]); |
||
118 | |||
119 | $articles = $this->paginate($articles); |
||
120 | |||
121 | $this->set(compact('category', 'articles')); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Display a specific article. |
||
126 | * |
||
127 | * @return \Cake\Network\Response|void |
||
128 | */ |
||
129 | public function article() |
||
130 | { |
||
131 | $this->loadModel('BlogArticles'); |
||
132 | |||
133 | $article = $this->BlogArticles |
||
134 | ->find() |
||
135 | ->where([ |
||
136 | 'BlogArticles.id' => $this->request->id, |
||
137 | 'BlogArticles.is_display' => 1 |
||
138 | ]) |
||
139 | ->contain([ |
||
140 | 'BlogCategories', |
||
141 | 'BlogAttachments', |
||
142 | 'Users' => function ($q) { |
||
143 | return $q->find('full'); |
||
144 | }, |
||
145 | 'Polls', |
||
146 | 'Polls.PollsAnswers', |
||
147 | 'Polls.PollsAnswers.Polls' => function ($q) { |
||
148 | return $q->select(['id', 'user_count']); |
||
149 | }, |
||
150 | 'Polls.PollsUsers' |
||
151 | ]) |
||
152 | ->first(); |
||
153 | |||
154 | //Check if the article is found. |
||
155 | if (is_null($article)) { |
||
156 | $this->Flash->error(__('This article doesn\'t exist or has been deleted.')); |
||
157 | |||
158 | return $this->redirect(['action' => 'index']); |
||
159 | } |
||
160 | |||
161 | $this->loadModel('BlogArticlesComments'); |
||
162 | |||
163 | //A comment has been posted. |
||
164 | if ($this->request->is('post')) { |
||
165 | //Check if the user is connected. |
||
166 | if (!$this->Auth->user()) { |
||
167 | $this->Flash->error(__('You must be connected to post a comment.')); |
||
168 | |||
169 | return $this->redirect([ |
||
170 | '_name' => 'blog-article', |
||
171 | 'slug' => h($article->title), |
||
172 | 'id' => $article->id |
||
173 | ]); |
||
174 | } |
||
175 | |||
176 | $this->request = $this->request |
||
177 | ->withData('article_id', $article->id) |
||
178 | ->withData('user_id', $this->Auth->user('id')); |
||
179 | |||
180 | $newComment = $this->BlogArticlesComments->newEntity($this->request->getParsedBody(), ['validate' => 'create']); |
||
0 ignored issues
–
show
|
|||
181 | |||
182 | //Attach Event. |
||
183 | $this->BlogArticlesComments->eventManager()->attach(new Badges($this)); |
||
184 | |||
185 | if ($insertComment = $this->BlogArticlesComments->save($newComment)) { |
||
186 | $this->eventManager()->attach(new Statistics()); |
||
187 | $event = new Event('Model.BlogArticlesComments.new'); |
||
188 | $this->eventManager()->dispatch($event); |
||
189 | |||
190 | $this->Flash->success(__('Your comment has been posted successfully !')); |
||
191 | //Redirect the user to the last page of the article. |
||
192 | $this->redirect([ |
||
193 | 'action' => 'go', |
||
194 | $insertComment->id |
||
195 | ]); |
||
196 | } |
||
197 | } |
||
198 | |||
199 | $this->loadModel('PollsUsers'); |
||
200 | $hasVoted = $this->PollsUsers |
||
201 | ->find() |
||
202 | ->contain([ |
||
203 | 'Polls' => function ($q) { |
||
204 | return $q->select(['id']); |
||
205 | }, |
||
206 | 'PollsAnswers' |
||
207 | ]) |
||
208 | ->where([ |
||
209 | 'PollsUsers.user_id' => $this->Auth->user('id'), |
||
210 | 'Polls.id' => $article->poll ? $article->poll->id : null |
||
211 | ]) |
||
212 | ->first(); |
||
213 | |||
214 | //Paginate all comments related to the article. |
||
215 | $this->paginate = [ |
||
216 | 'maxLimit' => Configure::read('Blog.comment_per_page') |
||
217 | ]; |
||
218 | |||
219 | $comments = $this->BlogArticlesComments |
||
220 | ->find() |
||
221 | ->where([ |
||
222 | 'BlogArticlesComments.article_id' => $article->id |
||
223 | ]) |
||
224 | ->contain([ |
||
225 | 'Users' => function ($q) { |
||
226 | return $q->find('medium'); |
||
227 | } |
||
228 | ]) |
||
229 | ->order([ |
||
230 | 'BlogArticlesComments.created' => 'asc' |
||
231 | ]); |
||
232 | |||
233 | $comments = $this->paginate($comments); |
||
234 | |||
235 | //Select the like for the current auth user. |
||
236 | $this->loadModel('BlogArticlesLikes'); |
||
237 | $like = $this->BlogArticlesLikes |
||
238 | ->find() |
||
239 | ->where([ |
||
240 | 'user_id' => ($this->Auth->user()) ? $this->Auth->user('id') : null, |
||
241 | 'article_id' => $article->id |
||
242 | ]) |
||
243 | ->first(); |
||
244 | |||
245 | //Build the newEntity for the comment form. |
||
246 | $formComments = $this->BlogArticlesComments->newEntity(); |
||
247 | |||
248 | //Search related articles |
||
249 | $keywords = preg_split("/([\s,\W])+/", $article->title); |
||
250 | |||
251 | $query = $this->BlogArticles->find(); |
||
252 | $query |
||
253 | ->contain([ |
||
254 | 'BlogCategories', |
||
255 | ]); |
||
256 | |||
257 | foreach ($keywords as $keyword) { |
||
258 | $query->orWhere(function ($exp, $q) use ($keyword) { |
||
259 | return $exp->like('BlogArticles.title', '%' . $keyword . '%'); |
||
260 | }); |
||
261 | } |
||
262 | |||
263 | $articles = $query->andWhere([ |
||
264 | 'BlogArticles.is_display' => 1, |
||
265 | 'BlogArticles.id !=' => $article->id |
||
266 | ]); |
||
267 | |||
268 | //Current user. |
||
269 | $this->loadModel('Users'); |
||
270 | $currentUser = $this->Users |
||
271 | ->find() |
||
272 | ->contain([ |
||
273 | 'Groups' => function ($q) { |
||
274 | return $q->select(['id', 'is_staff']); |
||
275 | } |
||
276 | ]) |
||
277 | ->where([ |
||
278 | 'Users.id' => $this->Auth->user('id') |
||
279 | ]) |
||
280 | ->select(['id', 'group_id']) |
||
281 | ->first(); |
||
282 | |||
283 | $this->set(compact('article', 'formComments', 'comments', 'like', 'articles', 'currentUser', 'hasVoted')); |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * Quote a message. |
||
288 | * |
||
289 | * @param int $articleId Id of the article where is the message to quote. |
||
290 | * @param int $commentId Id of the message to quote. |
||
291 | * |
||
292 | * @throws \Cake\Network\Exception\NotFoundException |
||
293 | * |
||
294 | * @return mixed |
||
295 | */ |
||
296 | public function quote($articleId = null, $commentId = null) |
||
297 | { |
||
298 | if (!$this->request->is('ajax')) { |
||
299 | throw new NotFoundException(); |
||
300 | } |
||
301 | |||
302 | $this->loadModel('BlogArticlesComments'); |
||
303 | |||
304 | $comment = $this->BlogArticlesComments |
||
305 | ->find() |
||
306 | ->where([ |
||
307 | 'BlogArticlesComments.article_id' => $articleId, |
||
308 | 'BlogArticlesComments.id' => $commentId |
||
309 | ]) |
||
310 | ->contain([ |
||
311 | 'Users' => function ($q) { |
||
312 | return $q->find('short'); |
||
313 | } |
||
314 | ]) |
||
315 | ->first(); |
||
316 | |||
317 | $json = []; |
||
318 | |||
319 | if (!is_null($comment)) { |
||
320 | $comment->toArray(); |
||
321 | |||
322 | $url = Router::url(['action' => 'go', $comment->id]); |
||
323 | $text = __("has said :"); |
||
324 | |||
325 | //Build the quote. |
||
326 | $json['comment'] = <<<EOT |
||
327 | <div> |
||
328 | <div> |
||
329 | <a href="{$url}"> |
||
330 | <strong>{$comment->user->full_name} {$text}</strong> |
||
331 | </a> |
||
332 | </div> |
||
333 | <blockquote> |
||
334 | $comment->content |
||
335 | </blockquote> |
||
336 | </div><p> </p><p> </p> |
||
337 | EOT; |
||
338 | |||
339 | $json['error'] = false; |
||
340 | |||
341 | $this->set(compact('json')); |
||
342 | } else { |
||
343 | $json['comment'] = __("This comment doesn't exist."); |
||
344 | $json['error'] = true; |
||
345 | |||
346 | $this->set(compact('json')); |
||
347 | } |
||
348 | |||
349 | //Send response in JSON. |
||
350 | $this->set('_serialize', 'json'); |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * Redirect an user to an article, page and comment. |
||
355 | * |
||
356 | * @param int $commentId Id of the comment. |
||
357 | * |
||
358 | * @return \Cake\Network\Response |
||
359 | */ |
||
360 | View Code Duplication | public function go($commentId = null) |
|
361 | { |
||
362 | $this->loadModel('BlogArticlesComments'); |
||
363 | |||
364 | $comment = $this->BlogArticlesComments |
||
365 | ->find() |
||
366 | ->contain([ |
||
367 | 'BlogArticles' |
||
368 | ]) |
||
369 | ->where([ |
||
370 | 'BlogArticlesComments.id' => $commentId |
||
371 | ]) |
||
372 | ->first(); |
||
373 | |||
374 | if (is_null($comment)) { |
||
375 | $this->Flash->error(__("This comment doesn't exist or has been deleted.")); |
||
376 | |||
377 | return $this->redirect(['action' => 'index']); |
||
378 | } |
||
379 | |||
380 | $comment->toArray(); |
||
381 | |||
382 | //Count the number of message before this message. |
||
383 | $messagesBefore = $this->BlogArticlesComments |
||
384 | ->find() |
||
385 | ->where([ |
||
386 | 'BlogArticlesComments.article_id' => $comment->article_id, |
||
387 | 'BlogArticlesComments.created <' => $comment->created |
||
388 | ]) |
||
389 | ->count(); |
||
390 | |||
391 | //Get the number of messages per page. |
||
392 | $messagesPerPage = Configure::read('Blog.comment_per_page'); |
||
393 | |||
394 | //Calculate the page. |
||
395 | $page = floor($messagesBefore / $messagesPerPage) + 1; |
||
396 | |||
397 | $page = ($page > 1) ? $page : 1; |
||
398 | |||
399 | //Redirect the user. |
||
400 | return $this->redirect([ |
||
401 | '_name' => 'blog-article', |
||
402 | 'slug' => $comment->blog_article->title, |
||
403 | 'id' => $comment->blog_article->id, |
||
404 | '?' => ['page' => $page], |
||
405 | '#' => 'comment-' . $commentId |
||
406 | ]); |
||
407 | } |
||
408 | |||
409 | /** |
||
410 | * Get all articles by a date formatted to "m-Y". |
||
411 | * |
||
412 | * @param string $date The date of the archive. |
||
413 | * |
||
414 | * @return void |
||
415 | */ |
||
416 | public function archive($date = null) |
||
417 | { |
||
418 | $this->loadModel('BlogArticles'); |
||
419 | |||
420 | $this->paginate = [ |
||
421 | 'maxLimit' => Configure::read('Blog.article_per_page') |
||
422 | ]; |
||
423 | |||
424 | $archives = $this->BlogArticles |
||
425 | ->find() |
||
426 | ->where([ |
||
427 | 'DATE_FORMAT(BlogArticles.created,\'%m-%Y\')' => $date, |
||
428 | 'BlogArticles.is_display' => 1 |
||
429 | ]) |
||
430 | ->contain([ |
||
431 | 'BlogCategories', |
||
432 | 'Users' => function ($q) { |
||
433 | return $q->find('short'); |
||
434 | } |
||
435 | ]) |
||
436 | ->order([ |
||
437 | 'BlogArticles.created' => 'desc' |
||
438 | ]); |
||
439 | |||
440 | $articles = $this->paginate($archives); |
||
441 | |||
442 | $this->set(compact('articles', 'date')); |
||
443 | } |
||
444 | |||
445 | /** |
||
446 | * Search articles. |
||
447 | * |
||
448 | * @return void |
||
449 | */ |
||
450 | public function search() |
||
451 | { |
||
452 | $this->loadModel('BlogArticles'); |
||
453 | |||
454 | //Check the keyword to search. (For pagination) |
||
455 | View Code Duplication | if (!empty($this->request->getData('search'))) { |
|
456 | $keyword = $this->request->getData('search'); |
||
457 | $this->request->session()->write('Search.Blog.Keyword', $keyword); |
||
458 | } else { |
||
459 | if ($this->request->session()->read('Search.Blog.Keyword')) { |
||
460 | $keyword = $this->request->session()->read('Search.Blog.Keyword'); |
||
461 | } else { |
||
462 | $keyword = ''; |
||
463 | } |
||
464 | } |
||
465 | |||
466 | //Pagination |
||
467 | $this->paginate = [ |
||
468 | 'maxLimit' => Configure::read('Blog.article_per_page') |
||
469 | ]; |
||
470 | |||
471 | $articles = $this->BlogArticles |
||
472 | ->find() |
||
473 | ->contain([ |
||
474 | 'Users' => function ($q) { |
||
475 | return $q->find('short'); |
||
476 | } |
||
477 | ]) |
||
478 | ->where([ |
||
479 | 'BlogArticles.is_display' => 1 |
||
480 | ]) |
||
481 | ->andWhere(function ($q) use ($keyword) { |
||
482 | return $q |
||
483 | ->like('title', "%$keyword%"); |
||
484 | }) |
||
485 | ->order([ |
||
486 | 'BlogArticles.created' => 'desc' |
||
487 | ]); |
||
488 | |||
489 | $articles = $this->paginate($articles); |
||
490 | |||
491 | $this->set(compact('articles', 'keyword')); |
||
492 | } |
||
493 | |||
494 | /** |
||
495 | * Like an article. |
||
496 | * |
||
497 | * @param int $articleId Id of the article to like. |
||
498 | * |
||
499 | * @throws \Cake\Network\Exception\NotFoundException When it's not an AJAX request. |
||
500 | * |
||
501 | * @return void |
||
502 | */ |
||
503 | public function articleLike($articleId = null) |
||
504 | { |
||
505 | if (!$this->request->is('ajax')) { |
||
506 | throw new NotFoundException(); |
||
507 | } |
||
508 | |||
509 | //Check if the user hasn't already liked this article. |
||
510 | $this->loadModel('BlogArticlesLikes'); |
||
511 | $checkLike = $this->BlogArticlesLikes |
||
512 | ->find() |
||
513 | ->where([ |
||
514 | 'BlogArticlesLikes.user_id' => $this->Auth->user('id'), |
||
515 | 'BlogArticlesLikes.article_id' => $articleId |
||
516 | ]) |
||
517 | ->first(); |
||
518 | |||
519 | $json = []; |
||
520 | |||
521 | View Code Duplication | if (!is_null($checkLike)) { |
|
522 | $json['message'] = __('You already like this article !'); |
||
523 | $json['error'] = true; |
||
524 | |||
525 | $this->set(compact('json')); |
||
526 | |||
527 | $this->set('_serialize', 'json'); |
||
528 | |||
529 | return; |
||
530 | } |
||
531 | |||
532 | //Check if the article exist. |
||
533 | $this->loadModel('BlogArticles'); |
||
534 | $checkArticle = $this->BlogArticles |
||
535 | ->find() |
||
536 | ->where([ |
||
537 | 'BlogArticles.id' => $articleId, |
||
538 | 'BlogArticles.is_display' => 1 |
||
539 | ]) |
||
540 | ->first(); |
||
541 | |||
542 | View Code Duplication | if (is_null($checkArticle)) { |
|
543 | $json['message'] = __("This article doesn't exist !"); |
||
544 | $json['error'] = true; |
||
545 | |||
546 | $this->set(compact('json')); |
||
547 | $this->set('_serialize', 'json'); |
||
548 | |||
549 | return; |
||
550 | } |
||
551 | |||
552 | //Prepare data to be saved. |
||
553 | $data = []; |
||
554 | $data['BlogArticlesLikes']['user_id'] = $this->Auth->user('id'); |
||
555 | $data['BlogArticlesLikes']['article_id'] = $articleId; |
||
556 | |||
557 | $like = $this->BlogArticlesLikes->newEntity($data); |
||
558 | |||
559 | if ($this->BlogArticlesLikes->save($like)) { |
||
560 | //Update the Statistics |
||
561 | $this->eventManager()->attach(new Statistics()); |
||
562 | $event = new Event('Model.BlogArticlesLikes.new'); |
||
563 | $this->eventManager()->dispatch($event); |
||
564 | |||
565 | $json['message'] = __('Thanks for {0} this article ! ', "<i class='fa fa-heart text-danger'></i>"); |
||
566 | $json['title'] = __('You {0} this article.', "<i class='fa fa-heart text-danger'></i>"); |
||
567 | $json['url'] = Router::url( |
||
568 | [ |
||
569 | 'action' => 'articleUnlike', |
||
570 | $articleId |
||
571 | ] |
||
572 | ); |
||
573 | $json['error'] = false; |
||
574 | } else { |
||
575 | $json['message'] = __('An error occurred, please try again later.'); |
||
576 | $json['error'] = true; |
||
577 | } |
||
578 | |||
579 | $this->set(compact('json')); |
||
580 | |||
581 | $this->set('_serialize', 'json'); |
||
582 | } |
||
583 | |||
584 | /** |
||
585 | * Unlike an article. |
||
586 | * |
||
587 | * @param int|null $articleId Id of the article to like. |
||
588 | * |
||
589 | * @throws \Cake\Network\Exception\NotFoundException When it's not an AJAX request. |
||
590 | * |
||
591 | * @return void |
||
592 | */ |
||
593 | public function articleUnlike($articleId = null) |
||
594 | { |
||
595 | if (!$this->request->is('ajax')) { |
||
596 | throw new NotFoundException(); |
||
597 | } |
||
598 | |||
599 | //Check if the user like this article. |
||
600 | $this->loadModel('BlogArticlesLikes'); |
||
601 | $like = $this->BlogArticlesLikes |
||
602 | ->find() |
||
603 | ->contain([ |
||
604 | 'BlogArticles' |
||
605 | ]) |
||
606 | ->where([ |
||
607 | 'BlogArticlesLikes.user_id' => $this->Auth->user('id'), |
||
608 | 'BlogArticlesLikes.article_id' => $articleId, |
||
609 | 'BlogArticles.is_display' => 1 |
||
610 | ]) |
||
611 | ->first(); |
||
612 | |||
613 | $json = []; |
||
614 | |||
615 | View Code Duplication | if (is_null($like)) { |
|
616 | $json['message'] = __("You don't like this article !"); |
||
617 | $json['error'] = true; |
||
618 | |||
619 | $this->set(compact('json')); |
||
620 | |||
621 | $this->set('_serialize', 'json'); |
||
622 | |||
623 | return; |
||
624 | } |
||
625 | |||
626 | if ($this->BlogArticlesLikes->delete($like)) { |
||
627 | //Update the Statistics |
||
628 | $this->eventManager()->attach(new Statistics()); |
||
629 | $event = new Event('Model.BlogArticlesLikes.new'); |
||
630 | $this->eventManager()->dispatch($event); |
||
631 | |||
632 | $json['url'] = Router::url([ |
||
633 | 'action' => 'articleLike', |
||
634 | $articleId |
||
635 | ]); |
||
636 | $json['title'] = __('Like {0}', "<i class='fa fa-heart text-danger'></i>"); |
||
637 | $json['error'] = false; |
||
638 | } else { |
||
639 | $json['message'] = __('An error occurred, please try again later.'); |
||
640 | $json['error'] = true; |
||
641 | } |
||
642 | |||
643 | $this->set(compact('json')); |
||
644 | |||
645 | $this->set('_serialize', 'json'); |
||
646 | } |
||
647 | |||
648 | /** |
||
649 | * Delete a comment. |
||
650 | * |
||
651 | * @param int $id Id of the comment to delete. |
||
652 | * |
||
653 | * @return \Cake\Network\Response |
||
654 | */ |
||
655 | public function deleteComment($id = null) |
||
656 | { |
||
657 | $this->loadModel('BlogArticlesComments'); |
||
658 | |||
659 | $comment = $this->BlogArticlesComments |
||
660 | ->find() |
||
661 | ->contain([ |
||
662 | 'BlogArticles' |
||
663 | ]) |
||
664 | ->where([ |
||
665 | 'BlogArticlesComments.id' => $id |
||
666 | ]) |
||
667 | ->first(); |
||
668 | |||
669 | if (is_null($comment)) { |
||
670 | $this->Flash->error(__("This comment doesn't exist or has been deleted !")); |
||
671 | |||
672 | return $this->redirect($this->referer()); |
||
673 | } |
||
674 | |||
675 | //Current user. |
||
676 | $this->loadModel('Users'); |
||
677 | $currentUser = $this->Users |
||
678 | ->find() |
||
679 | ->contain([ |
||
680 | 'Groups' => function ($q) { |
||
681 | return $q->select(['id', 'is_staff']); |
||
682 | } |
||
683 | ]) |
||
684 | ->where([ |
||
685 | 'Users.id' => $this->Auth->user('id') |
||
686 | ]) |
||
687 | ->select(['id', 'group_id']) |
||
688 | ->first(); |
||
689 | |||
690 | if ($comment->user_id != $this->Auth->user('id') && !$currentUser->group->is_staff) { |
||
691 | $this->Flash->error(__("You don't have the authorization to delete this comment !")); |
||
692 | |||
693 | return $this->redirect($this->referer()); |
||
694 | } |
||
695 | |||
696 | if ($this->BlogArticlesComments->delete($comment)) { |
||
697 | $this->eventManager()->attach(new Statistics()); |
||
698 | $event = new Event('Model.BlogArticlesComments.new'); |
||
699 | $this->eventManager()->dispatch($event); |
||
700 | |||
701 | $this->Flash->success(__("This comment has been deleted successfully !")); |
||
702 | } |
||
703 | |||
704 | return $this->redirect(['_name' => 'blog-article', 'slug' => $comment->blog_article->title, 'id' => $comment->blog_article->id, '?' => ['page' => $comment->blog_article->last_page]]); |
||
705 | } |
||
706 | |||
707 | /** |
||
708 | * Get the form to edit a comment. |
||
709 | * |
||
710 | * @throws \Cake\Network\Exception\NotFoundException When it's not an AJAX request. |
||
711 | * |
||
712 | * @return void |
||
713 | */ |
||
714 | public function getEditComment() |
||
715 | { |
||
716 | if (!$this->request->is('ajax')) { |
||
717 | throw new NotFoundException(); |
||
718 | } |
||
719 | |||
720 | $this->loadModel('BlogArticlesComments'); |
||
721 | $comment = $this->BlogArticlesComments |
||
722 | ->find() |
||
723 | ->where([ |
||
724 | 'BlogArticlesComments.id' => $this->request->getData('id') |
||
725 | ]) |
||
726 | ->first(); |
||
727 | |||
728 | $json = [ |
||
729 | 'error' => false, |
||
730 | 'errorMessage' => '' |
||
731 | ]; |
||
732 | |||
733 | View Code Duplication | if (is_null($comment)) { |
|
734 | $json['error'] = true; |
||
735 | $json['errorMessage'] = __("This comment doesn't exist or has been deleted !"); |
||
736 | |||
737 | $this->set(compact('json')); |
||
738 | |||
739 | return; |
||
740 | } |
||
741 | |||
742 | //Current user. |
||
743 | $this->loadModel('Users'); |
||
744 | $currentUser = $this->Users |
||
745 | ->find() |
||
746 | ->contain([ |
||
747 | 'Groups' => function ($q) { |
||
748 | return $q->select(['id', 'is_staff']); |
||
749 | } |
||
750 | ]) |
||
751 | ->where([ |
||
752 | 'Users.id' => $this->Auth->user('id') |
||
753 | ]) |
||
754 | ->select(['id', 'group_id']) |
||
755 | ->first(); |
||
756 | |||
757 | if ($comment->user_id != $this->Auth->user('id') && !$currentUser->group->is_staff) { |
||
758 | $json['error'] = true; |
||
759 | $json['errorMessage'] = __("You don't have the authorization to edit this comment !"); |
||
760 | |||
761 | $this->set(compact('json')); |
||
762 | |||
763 | return; |
||
764 | } |
||
765 | |||
766 | $this->set(compact('json', 'comment')); |
||
767 | } |
||
768 | |||
769 | /** |
||
770 | * Edit a comment. |
||
771 | * |
||
772 | * @param int $id Id of the comment. |
||
773 | * |
||
774 | * @throws \Cake\Network\Exception\NotFoundException When it's not a POST request. |
||
775 | * |
||
776 | * @return \Cake\Network\Response |
||
777 | */ |
||
778 | public function editComment($id = null) |
||
779 | { |
||
780 | if (!$this->request->is('post')) { |
||
781 | throw new NotFoundException(); |
||
782 | } |
||
783 | |||
784 | $this->loadModel('BlogArticlesComments'); |
||
785 | |||
786 | $comment = $this->BlogArticlesComments |
||
787 | ->find() |
||
788 | ->contain([ |
||
789 | 'BlogArticles' |
||
790 | ]) |
||
791 | ->where([ |
||
792 | 'BlogArticlesComments.id' => $id |
||
793 | ]) |
||
794 | ->first(); |
||
795 | |||
796 | if (is_null($comment)) { |
||
797 | $this->Flash->error(__("This comment doesn't exist or has been deleted !")); |
||
798 | |||
799 | return $this->redirect($this->referer()); |
||
800 | } |
||
801 | |||
802 | //Current user. |
||
803 | $this->loadModel('Users'); |
||
804 | $currentUser = $this->Users |
||
805 | ->find() |
||
806 | ->contain([ |
||
807 | 'Groups' => function ($q) { |
||
808 | return $q->select(['id', 'is_staff']); |
||
809 | } |
||
810 | ]) |
||
811 | ->where([ |
||
812 | 'Users.id' => $this->Auth->user('id') |
||
813 | ]) |
||
814 | ->select(['id', 'group_id']) |
||
815 | ->first(); |
||
816 | |||
817 | if ($comment->user_id != $this->Auth->user('id') && !$currentUser->group->is_staff) { |
||
818 | $this->Flash->error(__("You don't have the authorization to edit this comment !")); |
||
819 | |||
820 | return $this->redirect($this->referer()); |
||
821 | } |
||
822 | |||
823 | $this->BlogArticlesComments->patchEntity($comment, $this->request->getParsedBody()); |
||
824 | if ($this->BlogArticlesComments->save($comment)) { |
||
825 | $this->Flash->success(__("This comment has been edited successfully !")); |
||
826 | } |
||
827 | |||
828 | return $this->redirect(['action' => 'go', $comment->id]); |
||
829 | } |
||
830 | } |
||
831 |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.