Passed
Push — master ( 92128b...d5c2c1 )
by Andreas
17:19
created

_on_initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package net.nehmer.comments
4
 * @author The Midgard Project, http://www.midgard-project.org
5
 * @copyright The Midgard Project, http://www.midgard-project.org
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 */
8
9
use midcom\datamanager\datamanager;
10
use Symfony\Component\HttpFoundation\Request;
11
12
/**
13
 * Comments welcome page handler
14
 *
15
 * @package net.nehmer.comments
16
 */
17
class net_nehmer_comments_handler_admin extends midcom_baseclasses_components_handler
18
{
19
    use net_nehmer_comments_handler;
0 ignored issues
show
introduced by
The trait net_nehmer_comments_handler requires some properties which are not provided by net_nehmer_comments_handler_admin: $auth, $guid, $user
Loading history...
20
21
    /**
22
     * This datamanager instance is used to display an existing comment. only set
23
     * if there are actually comments to display.
24
     *
25
     * @var datamanager
26
     */
27
    private $_display_datamanager;
28
29
    /**
30
     * @var net_nehmer_comments_comment[]
31
     */
32
    private $comments;
33
34
    private $status;
35
36 7
    public function _on_initialize()
37
    {
38 7
        midcom::get()->auth->require_valid_user();
39 7
        $this->_topic->require_do('net.nehmer.comments:moderation');
40 7
    }
41
42
    /**
43
     * Prepares the _display_datamanager member.
44
     */
45
    private function _init_display_datamanager()
46
    {
47
        $this->_display_datamanager = datamanager::from_schemadb($this->_config->get('schemadb'));
0 ignored issues
show
Bug introduced by
It seems like $this->_config->get('schemadb') can also be of type false; however, parameter $path of midcom\datamanager\datamanager::from_schemadb() 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

47
        $this->_display_datamanager = datamanager::from_schemadb(/** @scrutinizer ignore-type */ $this->_config->get('schemadb'));
Loading history...
48
        $this->_request_data['display_datamanager'] = $this->_display_datamanager;
49
    }
50
51 1
    public function _handler_welcome(array &$data)
52
    {
53 1
        $data['topic'] = $this->_topic;
54 1
    }
55
56
    /**
57
     * @param array $data The local request data.
58
     */
59 1
    public function _show_welcome(string $handler_id, array &$data)
60
    {
61 1
        midcom_show_style('admin-start');
62 1
        midcom_show_style('admin-welcome');
63 1
        midcom_show_style('admin-end');
64 1
    }
65
66 6
    private function _load_comments() : array
67
    {
68 6
        $view_status = [];
69 6
        switch ($this->status) {
70 6
            case 'reported_abuse':
71 1
                $this->_request_data['status_to_show'] = 'reported abuse';
72 1
                $view_status[] = net_nehmer_comments_comment::REPORTED_ABUSE;
73 1
                break;
74 5
            case 'abuse':
75 1
                $this->_request_data['status_to_show'] = 'abuse';
76 1
                $view_status[] = net_nehmer_comments_comment::ABUSE;
77 1
                break;
78 4
            case 'junk':
79 1
                $this->_request_data['status_to_show'] = 'junk';
80 1
                $view_status[] = net_nehmer_comments_comment::JUNK;
81 1
                break;
82 3
            case 'latest':
83 1
                $this->_request_data['status_to_show'] = 'latest comments';
84 1
                $view_status[] = net_nehmer_comments_comment::NEW_ANONYMOUS;
85 1
                $view_status[] = net_nehmer_comments_comment::NEW_USER;
86 1
                $view_status[] = net_nehmer_comments_comment::MODERATED;
87 1
                if ($this->_config->get('show_reported_abuse_as_normal')) {
88 1
                    $view_status[] = net_nehmer_comments_comment::REPORTED_ABUSE;
89
                }
90 1
                break;
91 2
            case 'latest_new':
92 1
                $this->_request_data['status_to_show'] = 'latest comments, only new';
93 1
                $view_status[] = net_nehmer_comments_comment::NEW_ANONYMOUS;
94 1
                $view_status[] = net_nehmer_comments_comment::NEW_USER;
95 1
                if ($this->_config->get('show_reported_abuse_as_normal')) {
96 1
                    $view_status[] = net_nehmer_comments_comment::REPORTED_ABUSE;
97
                }
98 1
                break;
99 1
            case 'latest_approved':
100 1
                $this->_request_data['status_to_show'] = 'latest comments, only approved';
101 1
                $view_status[] = net_nehmer_comments_comment::MODERATED;
102 1
                break;
103
        }
104
105 6
        $qb = new org_openpsa_qbpager(net_nehmer_comments_comment::class, 'net_nehmer_comments_comments');
106 6
        $qb->results_per_page = $this->_config->get('items_to_show');
107 6
        $qb->display_pages = $this->_config->get('paging');
108 6
        $qb->add_constraint('status', 'IN', $view_status);
109 6
        $qb->add_order('metadata.revised', 'DESC');
110
111 6
        return $qb->execute();
112
    }
113
114
    /**
115
     * Checks if a button of the admin toolbar was pressed.
116
     */
117
    public function _handler_moderate_ajax(Request $request, string $status, array &$data)
118
    {
119
        if (   !$request->request->has('action')
120
            || !$request->request->has('guid')) {
121
            throw new midcom_error_notfound('Incomplete POST data');
122
        }
123
        if ($request->request->get('action') !== 'action_delete') {
124
            throw new midcom_error_notfound('Unsupported action');
125
        }
126
127
        $comment = new net_nehmer_comments_comment($request->request->get('guid'));
128
        if (!$comment->delete()) {
129
            throw new midcom_error("Failed to delete comment GUID '{$comment->guid}': " . midcom_connection::get_error_string());
130
        }
131
132
        midcom::get()->cache->invalidate($comment->objectguid);
133
134
        $this->status = $status;
135
        if ($comments = $this->_load_comments()) {
136
            $data['comment'] = end($comments);
137
            $this->_init_display_datamanager();
138
        }
139
        midcom::get()->skip_page_style = true;
140
    }
141
142
    /**
143
     * @param array $data The local request data.
144
     */
145
    public function _show_moderate_ajax(string $handler_id, array &$data)
146
    {
147
        if (!empty($data['comment'])) {
148
            $this->_display_datamanager->set_storage($data['comment']);
149
            $data['comment_toolbar'] = $this->populate_post_toolbar($data['comment'], $this->status);
150
            midcom_show_style('admin-comments-item');
151
        }
152
    }
153
154 6
    public function _handler_moderate(string $status, array &$data)
155
    {
156 6
        $this->status = $status;
157
158 6
        if ($this->comments = $this->_load_comments()) {
159
            $this->_init_display_datamanager();
160
        }
161
162 6
        midcom::get()->head->enable_jquery();
163 6
        midcom::get()->head->add_jsfile(MIDCOM_STATIC_URL . '/net.nehmer.comments/moderate.js');
164
165 6
        net_nehmer_comments_viewer::add_head_elements();
166 6
        $this->add_breadcrumb('', $this->_l10n->get($data['status_to_show']));
167 6
    }
168
169
    /**
170
     * @param array $data The local request data.
171
     */
172 6
    public function _show_moderate(string $handler_id, array &$data)
173
    {
174 6
        midcom_show_style('admin-start');
175 6
        if ($this->comments) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->comments of type net_nehmer_comments_comment[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
176
            midcom_show_style('admin-comments-start');
177
            foreach ($this->comments as $comment) {
178
                $data['comment'] = $comment;
179
                $this->_show_moderate_ajax($handler_id, $data);
180
            }
181
            midcom_show_style('admin-comments-end');
182
        }
183 6
        midcom_show_style('admin-end');
184 6
    }
185
}
186