Passed
Pull Request — master (#61)
by Stone
08:36 queued 05:08
created

Comments::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 8
rs 10
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;
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
     * View all the comments with pagination
33
     * @param string $page
34
     * @param int $linesPerPage
35
     * @throws \ReflectionException
36
     * @throws \Twig_Error_Loader
37
     * @throws \Twig_Error_Runtime
38
     * @throws \Twig_Error_Syntax
39
     */
40
    public function viewComments(string $page = "page-1", int $linesPerPage = Constant::LIST_PER_PAGE)
41
    {
42
        $this->onlyAdmin();
43
44
        $totalComments = $this->commentModel->countComments();
45
        $pagination = $this->pagination->getPagination($page, $totalComments, $linesPerPage);
46
47
        if ($linesPerPage !== Constant::LIST_PER_PAGE) {
48
            $this->data['paginationPostsPerPage'] = $linesPerPage;
49
        }
50
51
        $this->data['pagination'] = $pagination;
52
        $this->data["comments"] = $this->commentModel->getCommentsList($pagination["offset"], $linesPerPage);
53
54
55
        $this->renderView('Admin/Comments');
56
    }
57
58
    /**
59
     * View all the pending comments with pagination
60
     * @param string $page
61
     * @param int $linesPerPage
62
     * @throws \ReflectionException
63
     * @throws \Twig_Error_Loader
64
     * @throws \Twig_Error_Runtime
65
     * @throws \Twig_Error_Syntax
66
     */
67
    public function pendingComments(string $page = "page-1", int $linesPerPage = Constant::LIST_PER_PAGE)
68
    {
69
        $this->onlyAdmin();
70
71
        $totalComments = $this->commentModel->countPendingComments();
72
        $pagination = $this->pagination->getPagination($page, $totalComments, $linesPerPage);
73
74
        if ($linesPerPage !== Constant::LIST_PER_PAGE) {
75
            $this->data['paginationPostsPerPage'] = $linesPerPage;
76
        }
77
78
        $this->data['pagination'] = $pagination;
79
        $this->data["comments"] = $this->commentModel->getPendingCommentsList($pagination["offset"], $linesPerPage);
80
        $this->data['pendingView'] = true;
81
82
        $this->renderView('Admin/Comments');
83
84
    }
85
86
    /**
87
     * View the moderate comment page with a specific comment
88
     * @param int $commentId
89
     * @throws \ReflectionException
90
     * @throws \Twig_Error_Loader
91
     * @throws \Twig_Error_Runtime
92
     * @throws \Twig_Error_Syntax
93
     */
94
    public function moderateComment(int $commentId)
95
    {
96
        $this->onlyAdmin();
97
98
        $comment = $this->commentModel->getCommentById($commentId);
99
100
        $userModel = new UserModel($this->container);
101
        $user = $userModel->getUserDetailsById($comment->idusers);
102
103
        $this->data["comment"] = $comment;
104
        $this->data["commenter"] = $user;
105
106
        $this->renderView('Admin/ViewComment');
107
    }
108
109
    /**
110
     * Delete a comment
111
     * @param int $commentId
112
     * @throws \Exception
113
     */
114
    public function delete(int $commentId)
115
    {
116
        $this->onlyAdmin();
117
        $removedComment = $this->commentModel->delete($commentId);
118
119
        if ($removedComment) {
120
            $this->alertBox->setAlert("Comment  deleted");
121
        }
122
123
        $refererUrl = $this->request->getReferer();
124
        if($refererUrl === "") //referer can return null, set default
125
        {
126
            $refererUrl = "admin/comments/view-comments";
127
        }
128
        $baseUrl = $this->request->getBaseUrl();
129
        $redirectUrl = $this->removeFromBeginning($refererUrl, $baseUrl);
130
131
        $this->response->redirect($redirectUrl);
132
133
    }
134
135
    /**
136
     * Update a comment via post
137
     * @throws \ErrorException
138
     */
139
    public function update()
140
    {
141
        $this->onlyAdmin();
142
        $this->onlyPost();
143
144
        $comment = $this->container->getRequest()->getDataFull();
145
146
        //$this->debug->dump($comment);
147
148
        $commentId = $comment["idcomments"];
149
        //Sanity check on ID
150
        if (!$this->isInt($commentId)) {
151
            throw new \ErrorException("invalid comment ID");
152
        }
153
154
        //update comment
155
        if($this->commentModel->update($commentId, $comment["commentTextArea"], $comment["commentApproved"]))
156
        {
157
            $this->alertBox->setAlert("Comment updated");
158
        }
159
160
        $this->response->redirect("/admin/comments/moderate-comment/".$commentId);
161
    }
162
}