ReadtopicHandler::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Newbb;
4
5
/*
6
 * You may not change or alter any portion of this comment or credits
7
 * of supporting developers from this source code or any supporting source code
8
 * which is considered copyrighted (c) material of the original comment or credit authors.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
//  ------------------------------------------------------------------------ //
15
//  Author: phppp (D.J., [email protected])                                  //
16
//  URL: https://xoops.org                                                    //
17
//  Project: Article Project                                                 //
18
//  ------------------------------------------------------------------------ //
19
20
use XoopsModules\Newbb;
21
22
require_once __DIR__ . '/Read.php';
23
24
/**
25
 * A handler for read/unread handling
26
 *
27
 *
28
 * @author        D.J. (phppp, https://xoopsforge.com)
29
 * @copyright     copyright (c) 2005 XOOPS.org
30
 */
31
32
/**
33
 * Class ReadtopicHandler
34
 */
35
class ReadtopicHandler extends Newbb\ReadHandler
36
{
37
    /**
38
     * maximum records per forum for one user.
39
     * assigned from $GLOBALS['xoopsModuleConfig']["read_items"]
40
     *
41
     * @var int
42
     * @readonly
43
     */
44
    private int $items_per_forum;
45
46
    /**
47
     * @param \XoopsDatabase|null $db
48
     */
49
    public function __construct(\XoopsDatabase $db = null)
50
    {
51
        parent::__construct($db, 'topic');
0 ignored issues
show
Bug introduced by
It seems like $db can also be of type null; however, parameter $db of XoopsModules\Newbb\ReadHandler::__construct() does only seem to accept XoopsMySQLDatabase, 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

51
        parent::__construct(/** @scrutinizer ignore-type */ $db, 'topic');
Loading history...
52
        $newbbConfig           = \newbbLoadConfig();
53
        $this->items_per_forum = isset($newbbConfig['read_items']) ? (int)$newbbConfig['read_items'] : 100;
54
    }
55
56
    /**
57
     * clean orphan items from database
58
     *
59
     * @param string $table_link
60
     * @param string $field_link
61
     * @param string $field_object
62
     * @return bool   true on success
63
     */
64
    public function cleanOrphan($table_link = '', $field_link = '', $field_object = ''): bool //cleanOrphan()
65
    {
66
        parent::cleanOrphan($this->db->prefix('newbb_posts'), 'post_id');
67
68
        return parent::cleanOrphan($this->db->prefix('newbb_topics'), 'topic_id', 'read_item');
69
    }
70
71
    /**
72
     * Clear garbage
73
     *
74
     * Delete all expired and duplicated records
75
     */
76
    public function clearGarbage(): bool
77
    {
78
        parent::clearGarbage();
79
80
        // TODO: clearItemsExceedMaximumItemsPerForum
81
        return true;
82
    }
83
84
    /**
85
     * @param int      $status
86
     * @param int      $forum_id
87
     * @param int|null $uid
88
     * @return bool
89
     */
90
    public function setReadItems(int $status = 0, int $forum_id = 0, int $uid = null): bool
91
    {
92
        if (empty($this->mode)) {
93
            return true;
94
        }
95
96
        if (1 == $this->mode) {
97
            return $this->setReadItemsCookie($status, $forum_id);
98
        }
99
100
        return $this->setReadItemsDb($status, $forum_id, $uid);
0 ignored issues
show
Bug introduced by
It seems like $uid can also be of type null; however, parameter $uid of XoopsModules\Newbb\Readt...ndler::setReadItemsDb() does only seem to accept integer, 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

100
        return $this->setReadItemsDb($status, $forum_id, /** @scrutinizer ignore-type */ $uid);
Loading history...
101
    }
102
103
    /**
104
     * @param int $status
105
     * @param int $forum_id
106
     * @return bool
107
     */
108
    public function setReadItemsCookie(int $status, int $forum_id): bool
109
    {
110
        $cookie_name = 'LT';
111
        $cookie_vars = \newbbGetCookie($cookie_name, true);
112
113
        /** @var TopicHandler $itemHandler */
114
        $itemHandler = Helper::getInstance()->getHandler('Topic');
115
        $criteria    = new \CriteriaCompo(new \Criteria('forum_id', $forum_id));
116
        $criteria->setSort('topic_last_post_id');
117
        $criteria->setOrder('DESC');
118
        $criteria->setLimit($this->items_per_forum);
119
        $items = $itemHandler->getIds($criteria);
120
121
        foreach ($items as $var) {
122
            if (empty($status)) {
123
                if (isset($cookie_vars[$var])) {
124
                    unset($cookie_vars[$var]);
125
                }
126
            } else {
127
                $cookie_vars[$var] = \time(); /*$items[$var]*/
128
            }
129
        }
130
        \newbbSetCookie($cookie_name, $cookie_vars);
131
132
        return true;
133
    }
134
135
    /**
136
     * @param int $status
137
     * @param int $forum_id
138
     * @param int $uid
139
     * @return bool
140
     */
141
    public function setReadItemsDb(int $status, int $forum_id, int $uid): bool
142
    {
143
        if (empty($uid)) {
144
            if (\is_object($GLOBALS['xoopsUser'])) {
145
                $uid = $GLOBALS['xoopsUser']->getVar('uid');
146
            } else {
147
                return false;
148
            }
149
        }
150
151
        /** @var TopicHandler $itemHandler */
152
        $itemHandler    = Helper::getInstance()->getHandler('Topic');
153
        $criteria_topic = new \CriteriaCompo(new \Criteria('forum_id', $forum_id));
154
        $criteria_topic->setSort('topic_last_post_id');
155
        $criteria_topic->setOrder('DESC');
156
        $criteria_topic->setLimit($this->items_per_forum);
157
        $criteria_sticky = new \CriteriaCompo(new \Criteria('forum_id', $forum_id));
158
        $criteria_sticky->add(new \Criteria('topic_sticky', '1'));
159
160
        if (empty($status)) {
161
            $items_id  = $itemHandler->getIds($criteria_topic);
162
            $sticky_id = $itemHandler->getIds($criteria_sticky);
163
            $items     = $items_id + $sticky_id;
164
            $criteria  = new \CriteriaCompo(new \Criteria('uid', $uid));
165
            $criteria->add(new \Criteria('read_item', '(' . \implode(', ', $items) . ')', 'IN'));
166
            $this->deleteAll($criteria, true);
167
168
            return true;
169
        }
170
171
        $itemsObject  = $itemHandler->getAll($criteria_topic, ['topic_last_post_id']);
172
        $stickyObject = $itemHandler->getAll($criteria_sticky, ['topic_last_post_id']);
173
        $itemsObject  += $stickyObject;
174
        $items        = [];
175
        foreach (\array_keys($itemsObject) as $key) {
176
            $items[$key] = $itemsObject[$key]->getVar('topic_last_post_id');
177
        }
178
        unset($itemsObject, $stickyObject);
179
180
        foreach (\array_keys($items) as $key) {
181
            $this->setReadDb($key, $items[$key], $uid);
182
        }
183
184
        return true;
185
    }
186
187
    /**
188
     * @return void
189
     */
190
    public function synchronization(): void
191
    {
192
        //        return;
193
    }
194
}
195