Completed
Pull Request — master (#25)
by Erwan
04:33
created

comments::__construct()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 31
Code Lines 23

Duplication

Lines 4
Ratio 12.9 %

Importance

Changes 4
Bugs 2 Features 0
Metric Value
c 4
b 2
f 0
dl 4
loc 31
rs 8.5806
cc 4
eloc 23
nc 2
nop 13

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
* phpBB Directory extension for the phpBB Forum Software package.
4
*
5
* @copyright (c) 2014 ErnadoO <http://www.phpbb-services.com>
6
* @license GNU General Public License, version 2 (GPL-2.0)
7
*/
8
namespace ernadoo\phpbbdirectory\controller;
9
10
class comments
11
{
12
    private $captcha;
13
    private $s_comment;
14
    private $s_hidden_fields = [];
15
16
    /** @var \phpbb\db\driver\driver_interface */
17
    protected $db;
18
19
    /** @var \phpbb\config\config */
20
    protected $config;
21
22
    /** @var \phpbb\template\template */
23
    protected $template;
24
25
    /** @var \phpbb\user */
26
    protected $user;
27
28
    /** @var \phpbb\controller\helper */
29
    protected $helper;
30
31
    /** @var \phpbb\request\request */
32
    protected $request;
33
34
    /** @var \phpbb\auth\auth */
35
    protected $auth;
36
37
    /** @var \phpbb\pagination */
38
    protected $pagination;
39
40
    /** @var \phpbb\captcha\factory */
41
    protected $captcha_factory;
42
43
    /** @var \ernadoo\phpbbdirectory\core\categorie */
44
    protected $categorie;
45
46
    /** @var \ernadoo\phpbbdirectory\core\comment */
47
    protected $comment;
48
49
    /** @var string phpBB root path */
50
    protected $root_path;
51
52
    /** @var string phpEx */
53
    protected $php_ext;
54
55
    /**
56
     * Constructor.
57
     *
58
     * @param \phpbb\db\driver\driver_interface      $db              Database object
59
     * @param \phpbb\config\config                   $config          Config object
60
     * @param \phpbb\template\template               $template        Template object
61
     * @param \phpbb\user                            $user            User object
62
     * @param \phpbb\controller\helper               $helper          Controller helper object
63
     * @param \phpbb\request\request                 $request         Request object
64
     * @param \phpbb\auth\auth                       $auth            Auth object
65
     * @param \phpbb\pagination                      $pagination      Pagination object
66
     * @param \phpbb\captcha\factory                 $captcha_factory Captcha object
67
     * @param \ernadoo\phpbbdirectory\core\categorie $categorie       PhpBB Directory extension categorie object
68
     * @param \ernadoo\phpbbdirectory\core\comment   $comment         PhpBB Directory extension comment object
69
     * @param string                                 $root_path       phpBB root path
70
     * @param string                                 $php_ext         phpEx
71
     */
72
    public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\config\config $config, \phpbb\template\template $template, \phpbb\user $user, \phpbb\controller\helper $helper, \phpbb\request\request $request, \phpbb\auth\auth $auth, \phpbb\pagination $pagination, \phpbb\captcha\factory $captcha_factory, \ernadoo\phpbbdirectory\core\categorie $categorie, \ernadoo\phpbbdirectory\core\comment $comment, $root_path, $php_ext)
73
    {
74
        $this->db = $db;
75
        $this->config = $config;
76
        $this->template = $template;
77
        $this->user = $user;
78
        $this->helper = $helper;
79
        $this->request = $request;
80
        $this->auth = $auth;
81
        $this->pagination = $pagination;
82
        $this->captcha_factory = $captcha_factory;
83
        $this->categorie = $categorie;
84
        $this->comment = $comment;
85
        $this->root_path = $root_path;
86
        $this->php_ext = $php_ext;
87
88
        $this->user->add_lang_ext('ernadoo/phpbbdirectory', 'directory');
89
        $user->add_lang(['ucp', 'posting']);
90
91
        $this->template->assign_vars([
92
            'S_PHPBB_DIRECTORY'                => true,
93
            'DIRECTORY_TRANSLATION_INFO'       => (!empty($user->lang['DIRECTORY_TRANSLATION_INFO'])) ? $user->lang['DIRECTORY_TRANSLATION_INFO'] : '',
94
            'S_SIMPLE_MESSAGE'                 => true,
95
        ]);
96
97
        // The CAPTCHA kicks in here. We can't help that the information gets lost on language change.
98 View Code Duplication
        if (!$this->user->data['is_registered'] && $this->config['dir_visual_confirm']) {
99
            $this->captcha = $this->captcha_factory->get_instance($this->config['captcha_plugin']);
100
            $this->captcha->init(CONFIRM_POST);
101
        }
102
    }
103
104
    /**
105
     * Populate form when an error occurred.
106
     *
107
     * @param int $link_id    The link ID
108
     * @param int $comment_id The comment ID
109
     *
110
     * @throws \phpbb\exception\http_exception
111
     *
112
     * @return \Symfony\Component\HttpFoundation\Response A Symfony Response object
113
     */
114
    public function delete_comment($link_id, $comment_id)
115
    {
116
        $this->_check_comments_enable($link_id);
117
118
        if ($this->request->is_set_post('cancel')) {
119
            $redirect = $this->helper->route('ernadoo_phpbbdirectory_comment_view_controller', ['link_id' => (int) $link_id]);
120
            redirect($redirect);
121
        }
122
123
        $sql = 'SELECT *
124
			FROM '.DIR_COMMENT_TABLE.'
125
			WHERE comment_id = '.(int) $comment_id;
126
        $result = $this->db->sql_query($sql);
127
        $value = $this->db->sql_fetchrow($result);
128
129 View Code Duplication
        if (!$this->user->data['is_registered'] || !$this->auth->acl_get('m_delete_comment_dir') && (!$this->auth->acl_get('u_delete_comment_dir') || $this->user->data['user_id'] != $value['comment_user_id'])) {
130
            throw new \phpbb\exception\http_exception(403, 'DIR_ERROR_NOT_AUTH');
131
        }
132
133
        if (confirm_box(true)) {
134
            $this->comment->del($link_id, $comment_id);
135
136
            $meta_info = $this->helper->route('ernadoo_phpbbdirectory_comment_view_controller', ['link_id' => (int) $link_id]);
137
            meta_refresh(3, $meta_info);
138
            $message = $this->user->lang['DIR_COMMENT_DELETE_OK'];
139
            $message = $message.'<br /><br />'.$this->user->lang('DIR_CLICK_RETURN_COMMENT', '<a href="'.$meta_info.'">', '</a>');
140
141
            return $this->helper->message($message);
142
        } else {
143
            confirm_box(false, 'DIR_COMMENT_DELETE');
144
        }
145
    }
146
147
    /**
148
     * Edit a comment.
149
     *
150
     * @param int $link_id    The category ID
151
     * @param int $comment_id The comment ID
152
     *
153
     * @throws \phpbb\exception\http_exception
154
     *
155
     * @return null|\Symfony\Component\HttpFoundation\Response A Symfony Response object
156
     */
157
    public function edit_comment($link_id, $comment_id)
158
    {
159
        $this->_check_comments_enable($link_id);
160
161
        $sql = 'SELECT *
162
			FROM '.DIR_COMMENT_TABLE.'
163
			WHERE comment_id = '.(int) $comment_id;
164
        $result = $this->db->sql_query($sql);
165
        $value = $this->db->sql_fetchrow($result);
166
167 View Code Duplication
        if (!$this->user->data['is_registered'] || !$this->auth->acl_get('m_edit_comment_dir') && (!$this->auth->acl_get('u_edit_comment_dir') || $this->user->data['user_id'] != $value['comment_user_id'])) {
168
            throw new \phpbb\exception\http_exception(403, 'DIR_ERROR_NOT_AUTH');
169
        }
170
171
        $comment_text = generate_text_for_edit($value['comment_text'], $value['comment_uid'], $value['comment_flags']);
172
        $this->s_comment = $comment_text['text'];
173
174
        $submit = $this->request->is_set_post('update_comment') ? true : false;
175
176
        // If form is done
177
        if ($submit) {
178
            return $this->_data_processing($link_id, $comment_id, 'edit');
179
        }
180
181
        return $this->view($link_id, 1, 'edit');
182
    }
183
184
    /**
185
     * Post a new comment.
186
     *
187
     * @param int $link_id The category ID
188
     *
189
     * @throws \phpbb\exception\http_exception
190
     *
191
     * @return null
192
     */
193
    public function new_comment($link_id)
194
    {
195
        $this->_check_comments_enable($link_id);
196
197
        if (!$this->auth->acl_get('u_comment_dir')) {
198
            throw new \phpbb\exception\http_exception(403, 'DIR_ERROR_NOT_AUTH');
199
        }
200
201
        $submit = $this->request->is_set_post('submit_comment') ? true : false;
202
        $refresh = $this->request->is_set_post('refresh_vc') ? true : false;
203
204
        // If form is done
205
        if ($submit || $refresh) {
206
            return $this->_data_processing($link_id);
207
        } else {
208
            $redirect = $this->helper->route('ernadoo_phpbbdirectory_comment_view_controller', ['link_id' => (int) $link_id]);
209
            redirect($redirect);
210
        }
211
    }
212
213
    /**
214
     * Display popup comment.
215
     *
216
     * @param int    $link_id The category ID
217
     * @param int    $page    Page number taken from the URL
218
     * @param string $mode    add|edit
219
     *
220
     * @throws \phpbb\exception\http_exception
221
     *
222
     * @return \Symfony\Component\HttpFoundation\Response A Symfony Response object
223
     */
224
    public function view($link_id, $page, $mode = 'new')
225
    {
226
        $this->_check_comments_enable($link_id);
227
228
        $comment_id = $this->request->variable('c', 0);
229
        $view = $this->request->variable('view', '');
230
        $start = ($page - 1) * $this->config['dir_comments_per_page'];
231
232
        $this->s_hidden_fields = array_merge($this->s_hidden_fields, ['page' => $page]);
233
234
        $this->_populate_form($link_id, $mode);
235
236
        $sql = 'SELECT COUNT(comment_id) AS nb_comments
237
			FROM '.DIR_COMMENT_TABLE.'
238
			WHERE comment_link_id = '.(int) $link_id;
239
        $result = $this->db->sql_query($sql);
240
        $nb_comments = (int) $this->db->sql_fetchfield('nb_comments');
241
        $this->db->sql_freeresult($result);
242
243
        // Make sure $start is set to the last page if it exceeds the amount
244
        $start = $this->pagination->validate_start($start, $this->config['dir_comments_per_page'], $nb_comments);
245
246
        $sql_array = [
247
            'SELECT'      => 'a.comment_id, a.comment_user_id, a. comment_user_ip, a.comment_date, a.comment_text, a.comment_uid, a.comment_bitfield, a.comment_flags, u.username, u.user_id, u.user_colour, z.foe',
248
            'FROM'        => [
249
                    DIR_COMMENT_TABLE    => 'a', ],
250
            'LEFT_JOIN'    => [
251
                    [
252
                        'FROM'    => [USERS_TABLE => 'u'],
253
                        'ON'      => 'a.comment_user_id = u.user_id',
254
                    ],
255
                    [
256
                        'FROM'    => [ZEBRA_TABLE => 'z'],
257
                        'ON'      => 'z.user_id = '.$this->user->data['user_id'].' AND z.zebra_id = a.comment_user_id',
258
                    ],
259
            ],
260
            'WHERE'        => 'a.comment_link_id = '.(int) $link_id,
261
            'ORDER_BY'     => 'a.comment_date DESC', ];
262
        $sql = $this->db->sql_build_query('SELECT', $sql_array);
263
        $result = $this->db->sql_query_limit($sql, $this->config['dir_comments_per_page'], $start);
264
265
        $have_result = false;
266
267
        while ($comments = $this->db->sql_fetchrow($result)) {
268
            $have_result = true;
269
270
            $edit_allowed = ($this->user->data['is_registered'] && ($this->auth->acl_get('m_edit_comment_dir') || (
271
                $this->user->data['user_id'] == $comments['comment_user_id'] &&
272
                $this->auth->acl_get('u_edit_comment_dir')
273
            )));
274
275
            $delete_allowed = ($this->user->data['is_registered'] && ($this->auth->acl_get('m_delete_comment_dir') || (
276
                $this->user->data['user_id'] == $comments['comment_user_id'] &&
277
                $this->auth->acl_get('u_delete_comment_dir')
278
            )));
279
280
            $this->template->assign_block_vars('comment', [
281
                'MINI_POST_IMG'        => $this->user->img('icon_post_target', 'POST'),
282
                'S_USER'               => get_username_string('full', $comments['comment_user_id'], $comments['username'], $comments['user_colour']),
283
                'S_USER_IP'            => $comments['comment_user_ip'],
284
                'S_DATE'               => $this->user->format_date($comments['comment_date']),
285
                'S_COMMENT'            => generate_text_for_display($comments['comment_text'], $comments['comment_uid'], $comments['comment_bitfield'], $comments['comment_flags']),
286
                'S_ID'                 => $comments['comment_id'],
287
288
                'U_EDIT'              => ($edit_allowed) ? $this->helper->route('ernadoo_phpbbdirectory_comment_edit_controller', ['link_id' => (int) $link_id, 'comment_id' => (int) $comments['comment_id']]) : '',
289
                'U_DELETE'            => ($delete_allowed) ? $this->helper->route('ernadoo_phpbbdirectory_comment_delete_controller', ['link_id' => (int) $link_id, 'comment_id' => (int) $comments['comment_id'], '_referer' => $this->helper->get_current_url()]) : '',
290
291
                'S_IGNORE_POST'        => ($comments['foe'] && ($view != 'show' || $comment_id != $comments['comment_id'])) ? true : false,
292
                'L_IGNORE_POST'        => ($comments['foe']) ? $this->user->lang('POST_BY_FOE', get_username_string('full', $comments['comment_user_id'], $comments['username'], $comments['user_colour']), '<a href="'.$this->helper->url('directory/link/'.$link_id.'/comment'.(($page > 1) ? '/'.$page : '').'?view=show#c'.(int) $comments['comment_id']).'">', '</a>') : '',
293
                'L_POST_DISPLAY'       => ($comments['foe']) ? $this->user->lang('POST_DISPLAY', '<a class="display_post" data-post-id="'.$comments['comment_id'].'" href="'.$this->helper->url('directory/link/'.$link_id.'/comment'.(($page > 1) ? '/'.$page : '').'?c='.(int) $comments['comment_id'].'&view=show#c'.(int) $comments['comment_id']).'">', '</a>') : '',
294
295
                'S_INFO'            => $this->auth->acl_get('m_info'),
296
            ]);
297
        }
298
299
        $base_url = [
300
            'routes'    => 'ernadoo_phpbbdirectory_comment_view_controller',
301
            'params'    => ['link_id' => (int) $link_id],
302
        ];
303
304
        $this->pagination->generate_template_pagination($base_url, 'pagination', 'page', $nb_comments, $this->config['dir_comments_per_page'], $start);
305
306
        $this->template->assign_vars([
307
            'TOTAL_COMMENTS'       => $this->user->lang('DIR_NB_COMMS', (int) $nb_comments),
308
            'S_HAVE_RESULT'        => $have_result ? true : false,
309
        ]);
310
311
        return $this->helper->render('comments.html', $this->user->lang['DIR_COMMENT_TITLE']);
312
    }
313
314
    /**
315
     * Routine.
316
     *
317
     * @param int    $link_id    The link ID
318
     * @param int    $comment_id The comment ID
319
     * @param string $mode       new|edit
320
     *
321
     * @return \Symfony\Component\HttpFoundation\Response A Symfony Response object
322
     */
323
    private function _data_processing($link_id, $comment_id = 0, $mode = 'new')
324
    {
325
        if (!check_form_key('dir_form_comment')) {
326
            return $this->helper->message('FORM_INVALID');
327
        }
328
329
        $this->s_comment = $this->request->variable('message', '', true);
330
331
        if (!function_exists('validate_data')) {
332
            include $this->root_path.'includes/functions_user.'.$this->php_ext;
333
        }
334
335
        $error = validate_data(
336
            [
337
                'reply' => $this->s_comment, ],
338
            [
339
                'reply' => [
340
                    ['string', false, 1, $this->config['dir_length_comments']],
341
                ],
342
            ]
343
        );
344
345
        $error = array_map([$this->user, 'lang'], $error);
346
347 View Code Duplication
        if (!$this->user->data['is_registered'] && $this->config['dir_visual_confirm']) {
348
            $vc_response = $this->captcha->validate();
349
            if ($vc_response !== false) {
350
                $error[] = $vc_response;
351
            }
352
353
            if ($this->config['dir_visual_confirm_max_attempts'] && $this->captcha->get_attempt_count() > $this->config['dir_visual_confirm_max_attempts']) {
354
                $error[] = $this->user->lang['TOO_MANY_ADDS'];
355
            }
356
        }
357
358
        if (!$error) {
359
            $uid = $bitfield = $flags = '';
360
            generate_text_for_storage($this->s_comment, $uid, $bitfield, $flags, (bool) $this->config['dir_allow_bbcode'], (bool) $this->config['dir_allow_links'], (bool) $this->config['dir_allow_smilies']);
361
362
            $data_edit = [
363
                'comment_text'         => $this->s_comment,
364
                'comment_uid'          => $uid,
365
                'comment_flags'        => $flags,
366
                'comment_bitfield'     => $bitfield,
367
            ];
368
369
            if ($mode == 'edit') {
370
                $this->comment->edit($data_edit, $comment_id);
371
            } else {
372
                $data_add = [
373
                    'comment_link_id'     => (int) $link_id,
374
                    'comment_date'        => time(),
375
                    'comment_user_id'     => $this->user->data['user_id'],
376
                    'comment_user_ip'     => $this->user->ip,
377
                ];
378
379
                $data_add = array_merge($data_edit, $data_add);
380
381
                $this->comment->add($data_add);
382
            }
383
384
            $meta_info = $this->helper->route('ernadoo_phpbbdirectory_comment_view_controller', ['link_id' => (int) $link_id]);
385
            meta_refresh(3, $meta_info);
386
            $message = $this->user->lang['DIR_'.strtoupper($mode).'_COMMENT_OK'];
387
            $message = $message.'<br /><br />'.$this->user->lang('DIR_CLICK_RETURN_COMMENT', '<a href="'.$meta_info.'">', '</a>');
388
389
            return $this->helper->message($message);
390
        } else {
391
            $this->template->assign_vars([
392
                'ERROR'    => (count($error)) ? implode('<br />', $error) : '',
393
            ]);
394
395
            return $this->view($link_id, $this->request->variable('page', 1), $mode);
396
        }
397
    }
398
399
    /**
400
     * Check if comments are enable in a category.
401
     *
402
     * @param int $link_id The link ID
403
     *
404
     * @throws \phpbb\exception\http_exception
405
     *
406
     * @return null Retun null if comments are allowed, http_exception if not
407
     */
408
    private function _check_comments_enable($link_id)
409
    {
410
        $sql = 'SELECT link_cat
411
			FROM '.DIR_LINK_TABLE.'
412
			WHERE link_id = '.(int) $link_id;
413
        $result = $this->db->sql_query($sql);
414
        $cat_id = (int) $this->db->sql_fetchfield('link_cat');
415
        $this->db->sql_freeresult($result);
416
417
        if ($cat_id) {
418
            $this->categorie->get($cat_id);
419
420
            if (!$this->categorie->data['cat_allow_comments']) {
421
                throw new \phpbb\exception\http_exception(403, 'DIR_ERROR_NOT_AUTH');
422
            }
423
        } else {
424
            throw new \phpbb\exception\http_exception(404, 'DIR_ERROR_NO_LINKS');
425
        }
426
    }
427
428
    /**
429
     * Populate form when an error occurred.
430
     *
431
     * @param int    $link_id The link ID
432
     * @param string $mode    add|edit
433
     *
434
     * @return null
435
     */
436
    private function _populate_form($link_id, $mode)
437
    {
438 View Code Duplication
        if (!$this->user->data['is_registered'] && $this->config['dir_visual_confirm'] && $mode != 'edit') {
439
            $this->s_hidden_fields = array_merge($this->s_hidden_fields, $this->captcha->get_hidden_fields());
440
441
            $this->template->assign_vars([
442
                'S_CONFIRM_CODE'          => true,
443
                'CAPTCHA_TEMPLATE'        => $this->captcha->get_template(),
444
            ]);
445
        }
446
447
        if (!function_exists('generate_smilies')) {
448
            include $this->root_path.'includes/functions_posting.'.$this->php_ext;
449
        }
450
        if (!function_exists('display_custom_bbcodes')) {
451
            include $this->root_path.'includes/functions_display.'.$this->php_ext;
452
        }
453
454
        generate_smilies('inline', 0);
455
        display_custom_bbcodes();
456
        add_form_key('dir_form_comment');
457
458
        $this->template->assign_vars([
459
            'S_AUTH_COMM'        => $this->auth->acl_get('u_comment_dir'),
460
461
            'BBCODE_STATUS'        => ($this->config['dir_allow_bbcode']) ? $this->user->lang('BBCODE_IS_ON', '<a href="'.append_sid($this->root_path."faq.$this->php_ext", 'mode=bbcode').'">', '</a>') : $this->user->lang('BBCODE_IS_OFF', '<a href="'.append_sid($this->root_path."faq.$this->php_ext", 'mode=bbcode').'">', '</a>'),
462
            'IMG_STATUS'           => ($this->config['dir_allow_bbcode']) ? $this->user->lang['IMAGES_ARE_ON'] : $this->user->lang['IMAGES_ARE_OFF'],
463
            'SMILIES_STATUS'       => ($this->config['dir_allow_smilies']) ? $this->user->lang['SMILIES_ARE_ON'] : $this->user->lang['SMILIES_ARE_OFF'],
464
            'URL_STATUS'           => ($this->config['dir_allow_links']) ? $this->user->lang['URL_IS_ON'] : $this->user->lang['URL_IS_OFF'],
465
            'FLASH_STATUS'         => ($this->config['dir_allow_bbcode'] && $this->config['dir_allow_flash']) ? $this->user->lang['FLASH_IS_ON'] : $this->user->lang['FLASH_IS_OFF'],
466
467
            'L_DIR_REPLY_EXP'    => $this->user->lang('DIR_REPLY_EXP', $this->config['dir_length_comments']),
468
469
            'S_COMMENT'        => isset($this->s_comment) ? $this->s_comment : '',
470
471
            'S_BBCODE_ALLOWED'    => (bool) $this->config['dir_allow_bbcode'],
472
            'S_BBCODE_IMG'        => (bool) $this->config['dir_allow_bbcode'],
473
            'S_BBCODE_FLASH'      => ($this->config['dir_allow_bbcode'] && $this->config['dir_allow_flash']) ? true : false,
474
            'S_BBCODE_QUOTE'      => true,
475
            'S_LINKS_ALLOWED'     => (bool) $this->config['dir_allow_links'],
476
            'S_SMILIES_ALLOWED'   => (bool) $this->config['dir_allow_smilies'],
477
478
            'S_HIDDEN_FIELDS'      => build_hidden_fields($this->s_hidden_fields),
479
            'S_BUTTON_NAME'        => ($mode == 'edit') ? 'update_comment' : 'submit_comment',
480
            'S_POST_ACTION'        => ($mode == 'edit') ? '' : $this->helper->route('ernadoo_phpbbdirectory_comment_new_controller', ['link_id' => (int) $link_id]),
481
        ]);
482
    }
483
}
484