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
2
3
namespace XoopsModules\Newbb;
4
5
//
6
//  ------------------------------------------------------------------------ //
7
//                XOOPS - PHP Content Management System                      //
8
//                  Copyright (c) 2000-2020 XOOPS.org                        //
9
//                       <https://xoops.org>                             //
10
//  ------------------------------------------------------------------------ //
11
//  This program is free software; you can redistribute it and/or modify     //
12
//  it under the terms of the GNU General Public License as published by     //
13
//  the Free Software Foundation; either version 2 of the License, or        //
14
//  (at your option) any later version.                                      //
15
//                                                                           //
16
//  You may not change or alter any portion of this comment or credits       //
17
//  of supporting developers from this source code or any supporting         //
18
//  source code which is considered copyrighted (c) material of the          //
19
//  original comment or credit authors.                                      //
20
//                                                                           //
21
//  This program is distributed in the hope that it will be useful,          //
22
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
23
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
24
//  GNU General Public License for more details.                             //
25
//                                                                           //
26
//  You should have received a copy of the GNU General Public License        //
27
//  along with this program; if not, write to the Free Software              //
28
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
29
//  ------------------------------------------------------------------------ //
30
//  Author: phppp (D.J., [email protected])                                  //
31
//  URL: https://xoops.org                                                    //
32
//  Project: Article Project                                                 //
33
//  ------------------------------------------------------------------------ //
34
35
use XoopsModules\Newbb;
36
37
require_once __DIR__ . '/Read.php';
38
39
/**
40
 * A handler for read/unread handling
41
 *
42
 * @package       newbb
43
 *
44
 * @author        D.J. (phppp, http://xoopsforge.com)
45
 * @copyright     copyright (c) 2005 XOOPS.org
46
 */
47
48
/**
49
 * Class ReadtopicHandler
50
 */
51
class ReadtopicHandler extends Newbb\ReadHandler
52
{
53
    /**
54
     * maximum records per forum for one user.
55
     * assigned from $GLOBALS['xoopsModuleConfig']["read_items"]
56
     *
57
     * @var integer
58
     */
59
    private $items_per_forum;
60
61
    /**
62
     * @param \XoopsDatabase|null $db
63
     */
64
    public function __construct(\XoopsDatabase $db = null)
65
    {
66
        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 XoopsDatabase, 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

66
        parent::__construct(/** @scrutinizer ignore-type */ $db, 'topic');
Loading history...
67
        $newbbConfig           = \newbbLoadConfig();
68
        $this->items_per_forum = isset($newbbConfig['read_items']) ? (int)$newbbConfig['read_items'] : 100;
69
    }
70
71
    /**
72
     * clean orphan items from database
73
     *
74
     * @param string $table_link
75
     * @param string $field_link
76
     * @param string $field_object
77
     * @return bool   true on success
78
     */
79
    public function cleanOrphan($table_link = '', $field_link = '', $field_object = '') //cleanOrphan()
80
    {
81
        parent::cleanOrphan($this->db->prefix('newbb_posts'), 'post_id');
82
83
        return parent::cleanOrphan($this->db->prefix('newbb_topics'), 'topic_id', 'read_item');
84
    }
85
86
    /**
87
     * Clear garbage
88
     *
89
     * Delete all expired and duplicated records
90
     */
91
    public function clearGarbage()
92
    {
93
        parent::clearGarbage();
94
95
        // TODO: clearItemsExceedMaximumItemsPerForum
96
        return true;
97
    }
98
99
    /**
100
     * @param int  $status
101
     * @param int  $forum_id
102
     * @param null $uid
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $uid is correct as it would always require null to be passed?
Loading history...
103
     * @return bool
104
     */
105
    public function setReadItems($status = 0, $forum_id = 0, $uid = null)
106
    {
107
        if (empty($this->mode)) {
108
            return true;
109
        }
110
111
        if (1 == $this->mode) {
112
            return $this->setReadItemsCookie($status, $forum_id);
113
        }
114
115
        return $this->setReadItemsDb($status, $forum_id, $uid);
116
    }
117
118
    /**
119
     * @param $status
120
     * @param $forum_id
121
     * @return bool
122
     */
123
    public function setReadItemsCookie($status, $forum_id)
124
    {
125
        $cookie_name = 'LT';
126
        $cookie_vars = \newbbGetCookie($cookie_name, true);
127
128
        /** @var Newbb\TopicHandler $itemHandler */
129
        $itemHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Topic');
130
        $criteria    = new \CriteriaCompo(new \Criteria('forum_id', $forum_id));
131
        $criteria->setSort('topic_last_post_id');
132
        $criteria->setOrder('DESC');
133
        $criteria->setLimit($this->items_per_forum);
134
        $items = $itemHandler->getIds($criteria);
135
136
        foreach ($items as $var) {
137
            if (empty($status)) {
138
                if (isset($cookie_vars[$var])) {
139
                    unset($cookie_vars[$var]);
140
                }
141
            } else {
142
                $cookie_vars[$var] = \time() /*$items[$var]*/
143
                ;
144
            }
145
        }
146
        \newbbSetCookie($cookie_name, $cookie_vars);
147
148
        return true;
149
    }
150
151
    /**
152
     * @param $status
153
     * @param $forum_id
154
     * @param $uid
155
     * @return bool
156
     */
157
    public function setReadItemsDb($status, $forum_id, $uid)
158
    {
159
        if (empty($uid)) {
160
            if (\is_object($GLOBALS['xoopsUser'])) {
161
                $uid = $GLOBALS['xoopsUser']->getVar('uid');
162
            } else {
163
                return false;
164
            }
165
        }
166
167
        /** @var Newbb\TopicHandler $itemHandler */
168
        $itemHandler    = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Topic');
169
        $criteria_topic = new \CriteriaCompo(new \Criteria('forum_id', $forum_id));
170
        $criteria_topic->setSort('topic_last_post_id');
171
        $criteria_topic->setOrder('DESC');
172
        $criteria_topic->setLimit($this->items_per_forum);
173
        $criteria_sticky = new \CriteriaCompo(new \Criteria('forum_id', $forum_id));
174
        $criteria_sticky->add(new \Criteria('topic_sticky', 1));
175
176
        if (empty($status)) {
177
            $items_id  = $itemHandler->getIds($criteria_topic);
178
            $sticky_id = $itemHandler->getIds($criteria_sticky);
179
            $items     = $items_id + $sticky_id;
180
            $criteria  = new \CriteriaCompo(new \Criteria('uid', $uid));
181
            $criteria->add(new \Criteria('read_item', '(' . \implode(', ', $items) . ')', 'IN'));
182
            $this->deleteAll($criteria, true);
183
184
            return true;
185
        }
186
187
        $itemsObject  = $itemHandler->getAll($criteria_topic, ['topic_last_post_id']);
188
        $stickyObject = $itemHandler->getAll($criteria_sticky, ['topic_last_post_id']);
189
        $itemsObject  += $stickyObject;
190
        $items        = [];
191
        foreach (\array_keys($itemsObject) as $key) {
192
            $items[$key] = $itemsObject[$key]->getVar('topic_last_post_id');
193
        }
194
        unset($itemsObject, $stickyObject);
195
196
        foreach (\array_keys($items) as $key) {
197
            $this->setReadDb($key, $items[$key], $uid);
198
        }
199
200
        return true;
201
    }
202
203
    /**
204
     * @return bool|void
205
     */
206
    public function synchronization()
207
    {
208
        //        return;
209
    }
210
}
211