Completed
Push — master ( 196a6a...314b46 )
by Stone
12s
created

Comments::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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