Passed
Push — Comments ( 3ea17b...2fd76b )
by Stone
02:01
created

Comment::loadComments()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 0
dl 0
loc 21
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace App\Controllers\Ajax;
4
5
use App\Models\CommentModel;
6
use Core\AjaxController;
7
use Core\Container;
8
9
class Comment extends AjaxController
10
{
11
    private $commentModel;
12
13
    public function __construct(Container $container)
14
    {
15
        parent::__construct($container);
16
        $this->commentModel = new CommentModel($this->container);
17
    }
18
19
    /**
20
     * Update the approved status of a comment
21
     * @throws \Core\JsonException
22
     */
23
    public function modifyApproved()
24
    {
25
        $this->onlyAdmin();
26
        $this->onlyPost();
27
        $state = (bool)($this->request->getData("state") === 'true');
28
        $commentId = (int)$this->request->getData("commentId");
29
30
        $result = array();
31
        $result["success"] = $this->commentModel->setApproved(!$state, $commentId);
32
        $result["state"] = !$state;
33
        $result["commentId"] = $commentId;
34
        echo json_encode($result);
35
    }
36
37
    public function loadComments()
38
    {
39
        $commentOffset = (int)$this->request->getData("commentOffset");
40
        $postId = (int)$this->request->getData("postId");
41
42
        $result = array();
43
        $result["success"]=false;
44
45
        $data["comments"] = $this->commentModel->getCommentsListOnPost($postId, $commentOffset);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
46
        if($data["comments"])
47
        {
48
            $result["success"]=true;
49
            $twig = $this->container->getTemplate();
50
            $html = $twig->render('Includes/LoadComments.twig', $data);
51
52
            $result["html"] = $html;
53
            $result["commentOffset"] = $commentOffset + count($data["comments"]);
54
        }
55
56
57
        echo json_encode($result);
58
    }
59
60
}