Passed
Push — Comments ( b886a4...7c68ce )
by Stone
02:19
created

Comments::pendingComments()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 2
dl 0
loc 16
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace App\Controllers\Admin;
4
5
use App\Models\CommentModel;
6
use App\Models\UserModel;
7
use Core\AdminController;
8
use Core\Constant;
9
use Core\Container;
10
use Core\Traits\StringFunctions;
11
12
class Comments extends AdminController{
13
14
    use StringFunctions;
1 ignored issue
show
Bug introduced by
The trait Core\Traits\StringFunctions requires the property $childNodes which is not provided by App\Controllers\Admin\Comments.
Loading history...
15
16
    protected $siteConfig;
17
    protected $pagination;
18
19
    private $commentModel;
20
21
    public function __construct(Container $container)
22
    {
23
        $this->loadModules[] = 'SiteConfig';
24
        $this->loadModules[] = 'pagination';
25
        parent::__construct($container);
26
        $this->commentModel = new CommentModel($this->container);
27
28
        $this->data['configs'] = $this->siteConfig->getSiteConfig();
29
    }
30
31
32
    public function viewComments(string $page = "page-1", int $linesPerPage = Constant::LIST_PER_PAGE)
33
    {
34
        $this->onlyAdmin();
35
36
        $totalComments = $this->commentModel->countComments();
37
        $pagination = $this->pagination->getPagination($page, $totalComments, $linesPerPage);
38
39
        if ($linesPerPage !== Constant::LIST_PER_PAGE) {
40
            $this->data['paginationPostsPerPage'] = $linesPerPage;
41
        }
42
43
        $this->data['pagination'] = $pagination;
44
        $this->data["comments"] = $this->commentModel->getCommentsList($pagination["offset"], $linesPerPage);
45
46
47
        $this->renderView('Admin/Comments');
48
    }
49
50
    public function pendingComments(string $page = "page-1", int $linesPerPage = Constant::LIST_PER_PAGE)
51
    {
52
        $this->onlyAdmin();
53
54
        $totalComments = $this->commentModel->countPendingComments();
55
        $pagination = $this->pagination->getPagination($page, $totalComments, $linesPerPage);
56
57
        if ($linesPerPage !== Constant::LIST_PER_PAGE) {
58
            $this->data['paginationPostsPerPage'] = $linesPerPage;
59
        }
60
61
        $this->data['pagination'] = $pagination;
62
        $this->data["comments"] = $this->commentModel->getPendingCommentsList($pagination["offset"], $linesPerPage);
63
        $this->data['pendingView'] = true;
64
65
        $this->renderView('Admin/Comments');
66
67
    }
68
69
    public function moderateComment(int $commentId)
70
    {
71
        $this->onlyAdmin();
72
73
        $comment = $this->commentModel->getCommentById($commentId);
74
75
        $userModel = new UserModel($this->container);
76
        $user = $userModel->getUserDetailsById($comment->idusers);
77
78
        $this->data["comment"] = $comment;
79
        $this->data["commenter"] = $user;
80
81
        $this->renderView('Admin/ViewComment');
82
    }
83
84
    public function delete(int $commentId)
85
    {
86
        $this->onlyAdmin();
87
        $removedComment = $this->commentModel->delete($commentId);
88
89
        if ($removedComment) {
90
            $this->alertBox->setAlert("Comment  deleted");
91
        }
92
93
        $refererUrl = $this->request->getReferer();
94
        $baseUrl = $this->request->getBaseUrl();
95
        $redirectUrl = $this->removeFromBeginning($refererUrl, $baseUrl);
0 ignored issues
show
Bug introduced by
It seems like $refererUrl can also be of type null; however, parameter $string of App\Controllers\Admin\Co...::removeFromBeginning() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

95
        $redirectUrl = $this->removeFromBeginning(/** @scrutinizer ignore-type */ $refererUrl, $baseUrl);
Loading history...
96
97
        $this->response->redirect($redirectUrl);
98
99
    }
100
101
    public function update()
102
    {
103
        $this->onlyAdmin();
104
        $this->onlyPost();
105
106
        $comment = $this->container->getRequest()->getDataFull();
107
108
        //$this->debug->dump($comment);
109
110
        $commentId = $comment["idcomments"];
111
        //Sanity check on ID
112
        if (!$this->isInt($commentId)) {
113
            throw new \ErrorException("invalid comment ID");
114
        }
115
116
        //update comment
117
        if($this->commentModel->update($commentId, $comment["commentTextArea"], $comment["commentApproved"]))
118
        {
119
            $this->alertBox->setAlert("Comment updated");
120
        }
121
122
        $this->response->redirect("/admin/comments/moderate-comment/".$commentId);
123
    }
124
}