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

categorie::get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 23
rs 9.0856
cc 3
eloc 15
nc 3
nop 1
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\core;
9
10
class categorie
11
{
12
    /** @var \phpbb\db\driver\driver_interface */
13
    protected $db;
14
15
    /** @var \phpbb\config\config */
16
    protected $config;
17
18
    /** @var \phpbb\template\template */
19
    protected $template;
20
21
    /** @var \phpbb\user */
22
    protected $user;
23
24
    /** @var \phpbb\controller\helper */
25
    protected $helper;
26
27
    /** @var \phpbb\request\request */
28
    protected $request;
29
30
    /** @var \phpbb\auth\auth */
31
    protected $auth;
32
33
    /** @var \phpbb\cron\manager */
34
    protected $cron;
35
36
    /** @var \ernadoo\phpbbdirectory\core\helper */
37
    protected $dir_helper;
38
39
40
    /** @var array data */
41
    public $data = [];
42
43
    /**
44
     * Constructor.
45
     *
46
     * @param \phpbb\db\driver\driver_interface   $db         Database object
47
     * @param \phpbb\config\config                $config     Config object
48
     * @param \phpbb\template\template            $template   Template object
49
     * @param \phpbb\user                         $user       User object
50
     * @param \phpbb\controller\helper            $helper     Controller helper object
51
     * @param \phpbb\request\request              $request    Request object
52
     * @param \phpbb\auth\auth                    $auth       Auth object
53
     * @param \phpbb\cron\manager                 $cron       Cron object
54
     * @param \ernadoo\phpbbdirectory\core\helper $dir_helper PhpBB Directory extension helper object
55
     */
56
    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\cron\manager $cron, \ernadoo\phpbbdirectory\core\helper $dir_helper)
57
    {
58
        $this->db = $db;
59
        $this->config = $config;
60
        $this->template = $template;
61
        $this->user = $user;
62
        $this->helper = $helper;
63
        $this->request = $request;
64
        $this->auth = $auth;
65
        $this->cron = $cron;
66
        $this->dir_helper = $dir_helper;
67
    }
68
69
    /**
70
     * Function for get approval setting
71
     * used in edit mode for test the setting of new category's link.
72
     *
73
     * @return bool
74
     */
75
    public function need_approval()
76
    {
77
        return (bool) $this->data['cat_validate'];
78
    }
79
80
    /**
81
     * Generate Jumpbox.
82
     *
83
     * @return null
84
     */
85
    public function make_cat_jumpbox()
86
    {
87
        $sql = 'SELECT cat_id, cat_name, parent_id, left_id, right_id
88
			FROM '.DIR_CAT_TABLE.'
89
			ORDER BY left_id ASC';
90
        $result = $this->db->sql_query($sql, 600);
91
92
        $right = $padding = 0;
93
        $padding_store = ['0' => 0];
94
        $display_jumpbox = false;
95
        $iteration = 0;
96
97
        while ($row = $this->db->sql_fetchrow($result)) {
98
            $display_jumpbox = true;
99
100 View Code Duplication
            if ($row['left_id'] < $right) {
101
                $padding++;
102
                $padding_store[$row['parent_id']] = $padding;
103
            } elseif ($row['left_id'] > $right + 1) {
104
                $padding = (isset($padding_store[$row['parent_id']])) ? $padding_store[$row['parent_id']] : $padding;
105
            }
106
107
            $right = $row['right_id'];
108
109
            $this->template->assign_block_vars('jumpbox_forums', [
110
                'FORUM_ID'         => $row['cat_id'],
111
                'FORUM_NAME'       => $row['cat_name'],
112
                'S_FORUM_COUNT'    => $iteration,
113
                'LINK'             => $this->helper->route('ernadoo_phpbbdirectory_page_controller', ['cat_id' => $row['cat_id']]),
114
            ]);
115
116
            for ($i = 0; $i < $padding; $i++) {
117
                $this->template->assign_block_vars('jumpbox_forums.level', []);
118
            }
119
            $iteration++;
120
        }
121
        $this->db->sql_freeresult($result);
122
        unset($padding_store);
123
124
        $this->template->assign_vars([
125
            'S_DISPLAY_JUMPBOX'            => $display_jumpbox,
126
        ]);
127
    }
128
129
    /**
130
     * Generate a list of directory's categories.
131
     *
132
     * @param int   $select_id Selected category
133
     * @param array $ignore_id Array of ignored categories
134
     *
135
     * @return string $cat_list		html code
136
     */
137
    public function make_cat_select($select_id = 0, $ignore_id = [])
138
    {
139
        $ignore_id = is_array($ignore_id) ? $ignore_id : [$ignore_id];
140
141
        // This query is identical to the jumpbox one
142
        $sql = 'SELECT cat_id, cat_name, parent_id, left_id, right_id
143
			FROM '.DIR_CAT_TABLE.'
144
			ORDER BY left_id ASC';
145
        $result = $this->db->sql_query($sql, 600);
146
147
        $right = 0;
148
        $padding_store = ['0' => ''];
149
        $padding = '';
150
        $cat_list = '';
151
152
        while ($row = $this->db->sql_fetchrow($result)) {
153 View Code Duplication
            if ($row['left_id'] < $right) {
154
                $padding .= '&nbsp; &nbsp;';
155
                $padding_store[$row['parent_id']] = $padding;
156
            } elseif ($row['left_id'] > $right + 1) {
157
                $padding = (isset($padding_store[$row['parent_id']])) ? $padding_store[$row['parent_id']] : '';
158
            }
159
160
            $right = $row['right_id'];
161
            $disabled = false;
162
163
            if (in_array($row['cat_id'], $ignore_id)) {
164
                $disabled = true;
165
            }
166
167
            $selected = (($row['cat_id'] == $select_id) ? ' selected="selected"' : '');
168
            $cat_list .= '<option value="'.$row['cat_id'].'"'.(($disabled) ? ' disabled="disabled" class="disabled-option"' : $selected).'>'.$padding.$row['cat_name'].'</option>';
169
        }
170
        $this->db->sql_freeresult($result);
171
        unset($padding_store);
172
173
        return $cat_list;
174
    }
175
176
    /**
177
     * Display cat or subcat.
178
     *
179
     * @return null
180
     */
181
    public function display()
182
    {
183
        $cat_rows = $subcats = [];
184
        $parent_id = $visible_cats = 0;
185
186
        $sql_array = [
187
            'SELECT'      => 'cat_id, left_id, right_id, parent_id, cat_name, cat_desc, display_subcat_list, cat_desc_uid, cat_desc_bitfield, cat_desc_options, cat_links, cat_icon, cat_count_all',
188
            'FROM'        => [
189
                DIR_CAT_TABLE => '',
190
            ],
191
        ];
192
193
        if (empty($this->data)) {
194
            $root_data = ['cat_id' => 0];
195
            $sql_where = '';
196
        } else {
197
            $root_data = $this->data;
198
            $sql_where = 'left_id > '.$root_data['left_id'].' AND left_id < '.$root_data['right_id'];
199
        }
200
201
        $sql = $this->db->sql_build_query('SELECT', [
202
            'SELECT'      => $sql_array['SELECT'],
203
            'FROM'        => $sql_array['FROM'],
204
205
            'WHERE'        => $sql_where,
206
207
            'ORDER_BY'    => 'left_id',
208
        ]);
209
210
        $result = $this->db->sql_query($sql);
211
212
        $branch_root_id = $root_data['cat_id'];
213
        while ($row = $this->db->sql_fetchrow($result)) {
214
            $dir_cat_id = $row['cat_id'];
215
216
            if ($row['parent_id'] == $root_data['cat_id'] || $row['parent_id'] == $branch_root_id) {
217
                // Direct child of current branch
218
                $parent_id = $dir_cat_id;
219
                $cat_rows[$dir_cat_id] = $row;
220
            } else {
221
                $subcats[$parent_id][$dir_cat_id]['display'] = ($row['display_subcat_list']) ? true : false;
222
                $subcats[$parent_id][$dir_cat_id]['name'] = $row['cat_name'];
223
                $subcats[$parent_id][$dir_cat_id]['links'] = $row['cat_links'];
224
                $subcats[$parent_id][$dir_cat_id]['parent_id'] = $row['parent_id'];
225
            }
226
        }
227
        $this->db->sql_freeresult($result);
228
229
        foreach ($cat_rows as $row) {
230
            $visible_cats++;
231
            $dir_cat_id = $row['cat_id'];
232
233
            $subcats_list = [];
234
235
            // Generate list of subcats if we need to
236
            if (isset($subcats[$dir_cat_id])) {
237
                foreach ($subcats[$dir_cat_id] as $subcat_id => $subcat_row) {
238
                    $row['cat_links'] = ($row['cat_count_all']) ? ($row['cat_links'] + $subcat_row['links']) : $row['cat_links'];
239
240
                    if ($subcat_row['display'] && $subcat_row['parent_id'] == $dir_cat_id) {
241
                        $subcats_list[] = [
242
                            'link'         => $this->helper->route('ernadoo_phpbbdirectory_page_controller', ['cat_id' => (int) $subcat_id]),
243
                            'name'         => $subcat_row['name'],
244
                            'links'        => $subcat_row['links'],
245
                        ];
246
                    } else {
247
                        unset($subcats[$dir_cat_id][$subcat_id]);
248
                    }
249
                }
250
            }
251
252
            $this->template->assign_block_vars('cat', [
253
                'CAT_NAME'                 => $row['cat_name'],
254
                'CAT_DESC'                 => generate_text_for_display($row['cat_desc'], $row['cat_desc_uid'], $row['cat_desc_bitfield'], $row['cat_desc_options']),
255
                'CAT_LINKS'                => $row['cat_links'],
256
                'CAT_IMG'                  => $this->dir_helper->get_img_path('icons', $row['cat_icon']),
257
258
                'U_CAT'                    => $this->helper->route('ernadoo_phpbbdirectory_page_controller', ['cat_id' => (int) $row['cat_id']]),
259
            ]);
260
261
            // Assign subcats loop for style authors
262
            foreach ($subcats_list as $subcat) {
263
                $this->template->assign_block_vars('cat.subcat', [
264
                    'U_CAT'        => $subcat['link'],
265
                    'CAT_NAME'     => $subcat['name'],
266
                    'CAT_LINKS'    => $subcat['links'],
267
                ]);
268
            }
269
        }
270
271
        $this->template->assign_vars([
272
            'S_AUTH_ADD'           => $this->auth->acl_get('u_submit_dir'),
273
            'S_AUTH_SEARCH'        => $this->auth->acl_get('u_search_dir'),
274
            'S_HAS_SUBCAT'         => ($visible_cats) ? true : false,
275
            'S_ROOT'               => empty($this->data),
276
277
            'U_MAKE_SEARCH'        => $this->helper->route('ernadoo_phpbbdirectory_search_controller'),
278
        ]);
279
280
        // Do the categorie Prune thang - cron type job ...
281
        if (!$this->config['use_system_cron']) {
282
            $task = $this->cron->find_task('ernadoo.phpbbdirectory.cron.task.core.prune_categorie');
283
            $task->set_categorie_data($this->data);
284
285
            if ($task->is_ready()) {
286
                $url = $task->get_url();
287
                $this->template->assign_var('RUN_CRON_TASK', '<img src="'.$url.'" width="1" height="1" alt="" />');
288
            }
289
        }
290
    }
291
292
    /**
293
     * Get informations about a cat or subcat.
294
     *
295
     * @param int $cat_id The category ID
296
     *
297
     * @return null|false
298
     */
299
    public function get($cat_id = 0)
300
    {
301
        if ($cat_id) {
302
            $sql_array = [
303
                'SELECT'      => 'c.*, w.notify_status',
304
                'FROM'        => [
305
                        DIR_CAT_TABLE    => 'c', ],
306
                'LEFT_JOIN'    => [
307
                        [
308
                            'FROM'    => [DIR_WATCH_TABLE    => 'w'],
309
                            'ON'      => 'c.cat_id = w.cat_id AND w.user_id = '.(int) $this->user->data['user_id'],
310
                        ],
311
                ],
312
                'WHERE'        => 'c.cat_id = '.(int) $cat_id,
313
            ];
314
            $sql = $this->db->sql_build_query('SELECT', $sql_array);
315
            $result = $this->db->sql_query($sql);
316
            if (!($this->data = $this->db->sql_fetchrow($result))) {
317
                return false;
318
            }
319
            $this->db->sql_freeresult($result);
320
        }
321
    }
322
323
    /**
324
     * Create category navigation links for given category, create parent
325
     * list if currently null, assign basic category info to template.
326
     *
327
     * @param array $dir_cat_data
328
     */
329
    public function generate_dir_nav(&$dir_cat_data)
330
    {
331
        global $phpbb_container;
332
333
        $nestedset_category = $phpbb_container->get('ernadoo.phpbbdirectory.core.nestedset_category');
334
335
        // Get cat parents
336
        $dir_cat_parents = $nestedset_category->get_path_basic_data($dir_cat_data);
337
338
        $microdata_attr = 'data-category-id';
339
340
        // Build navigation links
341
        if (!empty($dir_cat_parents)) {
342
            foreach ($dir_cat_parents as $parent_cat_id => $parent_data) {
343
                $this->template->assign_block_vars('dir_navlinks', [
344
                    'FORUM_NAME'       => $parent_data['cat_name'],
345
                    'FORUM_ID'         => $parent_cat_id,
346
                    'MICRODATA'        => $microdata_attr.'="'.$parent_cat_id.'"',
347
                    'U_VIEW_FORUM'     => $this->helper->route('ernadoo_phpbbdirectory_page_controller', ['cat_id' => (int) $parent_cat_id]),
348
                ]);
349
            }
350
        }
351
352
        $this->template->assign_block_vars('dir_navlinks', [
353
            'FORUM_NAME'       => $dir_cat_data['cat_name'],
354
            'FORUM_ID'         => $dir_cat_data['cat_id'],
355
            'MICRODATA'        => $microdata_attr.'="'.$dir_cat_data['cat_id'].'"',
356
            'U_VIEW_FORUM'     => $this->helper->route('ernadoo_phpbbdirectory_page_controller', ['cat_id' => (int) $dir_cat_data['cat_id']]),
357
        ]);
358
    }
359
360
    /**
361
     * Return good key language.
362
     *
363
     * @param bool $validate True if approbation needed before publication
364
     *
365
     * @throws \phpbb\exception\runtime_exception
366
     *
367
     * @return string Information about approval, depends on user auth level
368
     */
369
    public function dir_submit_type($validate)
370
    {
371
        if ($validate && !$this->auth->acl_get('a_')) {
372
            return $this->user->lang['DIR_SUBMIT_TYPE_1'];
373
        } elseif (!$validate && !$this->auth->acl_get('a_')) {
374
            return $this->user->lang['DIR_SUBMIT_TYPE_2'];
375
        } elseif ($this->auth->acl_get('a_')) {
376
            return $this->user->lang['DIR_SUBMIT_TYPE_3'];
377
        } elseif ($this->auth->acl_get('m_')) {
378
            return $this->user->lang['DIR_SUBMIT_TYPE_4'];
379
        }
380
381
        throw new \phpbb\exception\runtime_exception('DIR_ERROR_SUBMIT_TYPE');
382
    }
383
384
    /**
385
     * Category watching common code.
386
     *
387
     * @param string $mode          Watch or unwatch a category
388
     * @param array  $s_watching    An empty array, passed by reference
389
     * @param int    $user_id       The user ID
390
     * @param int    $cat_id        The category ID
391
     * @param string $notify_status User is watching the category?
392
     *
393
     * @return null|string
394
     */
395
    public function watch_categorie($mode, &$s_watching, $user_id, $cat_id, $notify_status)
396
    {
397
        // Is user watching this thread?
398
        if ($user_id != ANONYMOUS) {
399
            $can_watch = true;
400
401
            if (!is_null($notify_status) && $notify_status !== '') {
402
                if ($mode == 'unwatch') {
403
                    $sql = 'DELETE FROM '.DIR_WATCH_TABLE."
404
						WHERE cat_id = $cat_id
405
							AND user_id = $user_id";
406
                    $this->db->sql_query($sql);
407
408
                    $redirect_url = $this->helper->route('ernadoo_phpbbdirectory_page_controller', ['cat_id' => (int) $cat_id]);
409
                    $message = $this->user->lang['DIR_NOT_WATCHING_CAT'];
410
411 View Code Duplication
                    if (!$this->request->is_ajax()) {
412
                        $message .= '<br /><br />'.$this->user->lang('DIR_CLICK_RETURN_CAT', '<a href="'.$redirect_url.'">', '</a>');
413
                    }
414
415
                    meta_refresh(3, $redirect_url);
416
417
                    return $message;
418
                } else {
419
                    $is_watching = true;
420
421
                    if ($notify_status != NOTIFY_YES) {
422
                        $sql = 'UPDATE '.DIR_WATCH_TABLE.'
423
							SET notify_status = '.NOTIFY_YES."
424
							WHERE cat_id = $cat_id
425
								AND user_id = $user_id";
426
                        $this->db->sql_query($sql);
427
                    }
428
                }
429
            } else {
430
                if ($mode == 'watch') {
431
                    $sql = 'INSERT INTO '.DIR_WATCH_TABLE." (user_id, cat_id, notify_status)
432
						VALUES ($user_id, $cat_id, ".NOTIFY_YES.')';
433
                    $this->db->sql_query($sql);
434
435
                    $redirect_url = $this->helper->route('ernadoo_phpbbdirectory_page_controller', ['cat_id' => (int) $cat_id]);
436
                    $message = $this->user->lang['DIR_ARE_WATCHING_CAT'];
437
438 View Code Duplication
                    if (!$this->request->is_ajax()) {
439
                        $message .= '<br /><br />'.$this->user->lang('DIR_CLICK_RETURN_CAT', '<a href="'.$redirect_url.'">', '</a>');
440
                    }
441
442
                    meta_refresh(3, $redirect_url);
443
444
                    return $message;
445
                } else {
446
                    $is_watching = false;
447
                }
448
            }
449
        } else {
450
            $can_watch = false;
451
            $is_watching = false;
452
        }
453
454
        if ($can_watch) {
455
            $s_watching['link'] = $this->helper->route('ernadoo_phpbbdirectory_suscribe_controller', ['cat_id' => $cat_id, 'mode' => (($is_watching) ? 'unwatch' : 'watch')]);
456
            $s_watching['link_toggle'] = $this->helper->route('ernadoo_phpbbdirectory_suscribe_controller', ['cat_id' => $cat_id, 'mode' => ((!$is_watching) ? 'unwatch' : 'watch')]);
457
            $s_watching['title'] = $this->user->lang[(($is_watching) ? 'DIR_STOP' : 'DIR_START').'_WATCHING_CAT'];
458
            $s_watching['title_toggle'] = $this->user->lang[((!$is_watching) ? 'DIR_STOP' : 'DIR_START').'_WATCHING_CAT'];
459
            $s_watching['is_watching'] = $is_watching;
460
        }
461
    }
462
463
    /**
464
     * Return Category name.
465
     *
466
     * @param int $cat_id The category ID
467
     *
468
     * @return string The category name
469
     */
470
    public static function getname($cat_id)
471
    {
472
        global $db;
473
474
        $sql = 'SELECT cat_name
475
			FROM '.DIR_CAT_TABLE.'
476
			WHERE cat_id = '.(int) $cat_id;
477
        $result = $db->sql_query($sql);
478
        $row = $db->sql_fetchrow($result);
479
480
        if (!empty($row)) {
481
            return $row['cat_name'];
482
        }
483
    }
484
}
485