PostHandler::insert()   F
last analyzed

Complexity

Conditions 20
Paths 474

Size

Total Lines 112
Code Lines 70

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 20
eloc 70
c 1
b 0
f 0
nc 474
nop 2
dl 0
loc 112
rs 0.7305

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
/**
16
 * @copyright    {@link https://xoops.org/ XOOPS Project}
17
 * @license      {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
18
 * @author       XOOPS Development Team, phppp (D.J., [email protected])
19
 */
20
21
use XoopsModules\Newbb;
22
use XoopsModules\Xoopspoll;
23
24
\defined('NEWBB_FUNCTIONS_INI') || require XOOPS_ROOT_PATH . '/modules/newbb/include/functions.ini.php';
25
//newbb_load_object();
26
27
/**
28
 * Class PostHandler
29
 */
30
//class PostHandler extends ArtObjectHandler
31
class PostHandler extends \XoopsPersistableObjectHandler
32
{
33
    /**
34
     * @param null|\XoopsDatabase $db
35
     */
36
    public function __construct(\XoopsDatabase $db = null)
37
    {
38
        parent::__construct($db, 'bb_posts', Post::class, 'post_id', 'subject');
39
    }
40
41
    /**
42
     * @param mixed|null $id
43
     * @param null       $fields
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $fields is correct as it would always require null to be passed?
Loading history...
44
     * @return null|\XoopsObject
45
     */
46
    public function get($id = null, $fields = null)
47
    {
48
        $id    = (int)$id;
49
        $post  = null;
50
        $sql   = 'SELECT p.*, t.* FROM ' . $this->db->prefix('bb_posts') . ' p LEFT JOIN ' . $this->db->prefix('bb_posts_text') . ' t ON p.post_id=t.post_id WHERE p.post_id=' . $id;
51
        $array = $this->db->fetchArray($this->db->query($sql));
52
        if ($array) {
53
            $post = $this->create(false);
54
            $post->assignVars($array);
55
        }
56
57
        return $post;
58
    }
59
60
    /**
61
     * @param int $topic_id
62
     * @param int $limit
63
     * @param int $approved
64
     * @return array
65
     */
66
    //    public function &getByLimit($limit = 0, $start = 0, CriteriaElement $criteria = null, $fields = null, $asObject = true)
67
    public function &getByLimit($topic_id, $limit, $approved = 1)
68
    {
69
        $sql    = 'SELECT p.*, t.*, tp.topic_status FROM '
70
                  . $this->db->prefix('bb_posts')
71
                  . ' p LEFT JOIN '
72
                  . $this->db->prefix('bb_posts_text')
73
                  . ' t ON p.post_id=t.post_id LEFT JOIN '
74
                  . $this->db->prefix('bb_topics')
75
                  . ' tp ON tp.topic_id=p.topic_id WHERE p.topic_id='
76
                  . $topic_id
77
                  . ' AND p.approved ='
78
                  . $approved
79
                  . ' ORDER BY p.post_time DESC';
80
        $result = $this->db->query($sql, $limit, 0);
81
        $ret    = [];
82
        if ($result) {
83
            while (false !== ($myrow = $this->db->fetchArray($result))) {
84
                $post = $this->create(false);
85
                $post->assignVars($myrow);
86
87
                $ret[$myrow['post_id']] = $post;
88
                unset($post);
89
            }
90
        }
91
92
        return $ret;
93
    }
94
95
    /**
96
     * @param $post
97
     * @return mixed
98
     */
99
    public function getPostForPDF($post)
100
    {
101
        return $post->getPostBody(true);
102
    }
103
104
    /**
105
     * @param $post
106
     * @return mixed
107
     */
108
    public function getPostForPrint($post)
109
    {
110
        return $post->getPostBody();
111
    }
112
113
    /**
114
     * @param mixed $post
115
     * @param bool  $force
116
     * @return bool
117
     */
118
    public function approve(&$post, $force = false)
119
    {
120
        if (empty($post)) {
121
            return false;
122
        }
123
        if (\is_numeric($post)) {
124
            $post = $this->get($post);
125
        }
126
        $post_id = $post->getVar('post_id');
0 ignored issues
show
Unused Code introduced by
The assignment to $post_id is dead and can be removed.
Loading history...
127
128
        $wasApproved = $post->getVar('approved');
129
        // irmtfan approve post if the approved = 0 (pending) or -1 (deleted)
130
        if (empty($force) && $wasApproved > 0) {
131
            return true;
132
        }
133
        $post->setVar('approved', 1);
134
        $this->insert($post, true);
0 ignored issues
show
Bug introduced by
It seems like $post can also be of type null; however, parameter $object of XoopsModules\Newbb\PostHandler::insert() does only seem to accept XoopsObject, 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

134
        $this->insert(/** @scrutinizer ignore-type */ $post, true);
Loading history...
135
136
        /** @var Newbb\TopicHandler $topicHandler */
137
        $topicHandler = Newbb\Helper::getInstance()->getHandler('Topic');
138
        $topic_obj    = $topicHandler->get($post->getVar('topic_id'));
139
        if ($topic_obj->getVar('topic_last_post_id') < $post->getVar('post_id')) {
140
            $topic_obj->setVar('topic_last_post_id', $post->getVar('post_id'));
141
        }
142
        if ($post->isTopic()) {
0 ignored issues
show
Bug introduced by
The method isTopic() does not exist on XoopsObject. It seems like you code against a sub-type of XoopsObject such as Post or XoopsModules\Newbb\Post. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

142
        if ($post->/** @scrutinizer ignore-call */ isTopic()) {
Loading history...
143
            $topic_obj->setVar('approved', 1);
144
        } else {
145
            $topic_obj->setVar('topic_replies', $topic_obj->getVar('topic_replies') + 1);
146
        }
147
        $topicHandler->insert($topic_obj, true);
0 ignored issues
show
Bug introduced by
It seems like $topic_obj can also be of type null; however, parameter $object of XoopsModules\Newbb\TopicHandler::insert() does only seem to accept XoopsObject, 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

147
        $topicHandler->insert(/** @scrutinizer ignore-type */ $topic_obj, true);
Loading history...
148
149
        /** @var Newbb\ForumHandler $forumHandler */
150
        $forumHandler = Newbb\Helper::getInstance()->getHandler('Forum');
151
        $forum_obj    = $forumHandler->get($post->getVar('forum_id'));
152
        if ($forum_obj->getVar('forum_last_post_id') < $post->getVar('post_id')) {
153
            $forum_obj->setVar('forum_last_post_id', $post->getVar('post_id'));
154
        }
155
        $forum_obj->setVar('forum_posts', $forum_obj->getVar('forum_posts') + 1);
156
        if ($post->isTopic()) {
157
            $forum_obj->setVar('forum_topics', $forum_obj->getVar('forum_topics') + 1);
158
        }
159
        $forumHandler->insert($forum_obj, true);
160
161
        // Update user stats
162
        if ($post->getVar('uid') > 0) {
163
            $memberHandler = \xoops_getHandler('member');
164
            $poster        = $memberHandler->getUser($post->getVar('uid'));
0 ignored issues
show
Bug introduced by
The method getUser() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsAvatarHandler or XoopsPersistableObjectHandler. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

164
            /** @scrutinizer ignore-call */ 
165
            $poster        = $memberHandler->getUser($post->getVar('uid'));
Loading history...
165
            if (\is_object($poster) && $post->getVar('uid') === $poster->getVar('uid')) {
166
                $poster->setVar('posts', $poster->getVar('posts') + 1);
167
                $res = $memberHandler->insertUser($poster, true);
0 ignored issues
show
Bug introduced by
The method insertUser() does not exist on XoopsObjectHandler. Did you maybe mean insert()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

167
                /** @scrutinizer ignore-call */ 
168
                $res = $memberHandler->insertUser($poster, true);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Unused Code introduced by
The assignment to $res is dead and can be removed.
Loading history...
168
                unset($poster);
169
            }
170
        }
171
172
        // Update forum stats
173
        $statsHandler = Newbb\Helper::getInstance()->getHandler('Stats');
174
        $statsHandler->update($post->getVar('forum_id'), 'post');
0 ignored issues
show
Bug introduced by
The method update() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsPersistableObjectHandler. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

174
        $statsHandler->/** @scrutinizer ignore-call */ 
175
                       update($post->getVar('forum_id'), 'post');
Loading history...
175
        if ($post->isTopic()) {
176
            $statsHandler->update($post->getVar('forum_id'), 'topic');
177
        }
178
179
        return true;
180
    }
181
182
    /**
183
     * @param \XoopsObject $post
184
     * @param bool $force
185
     * @return bool
186
     */
187
    public function insert(\XoopsObject $post, $force = true)
188
    {
189
        // Set the post time
190
        // The time should be 'publish' time. To be adjusted later
191
        if (!$post->getVar('post_time')) {
192
            $post->setVar('post_time', \time());
193
        }
194
195
        /** @var Newbb\TopicHandler $topicHandler */
196
        $topicHandler = Newbb\Helper::getInstance()->getHandler('Topic');
197
        // Verify the topic ID
198
        $topic_id = $post->getVar('topic_id');
199
        if ($topic_id) {
200
            $topic_obj = $topicHandler->get($topic_id);
201
            // Invalid topic OR the topic is no approved and the post is not top post
202
            if (!$topic_obj//            || (!$post->isTopic() && $topic_obj->getVar("approved") < 1)
203
            ) {
204
                return false;
205
            }
206
        }
207
        if (empty($topic_id)) {
208
            $post->setVar('topic_id', 0);
209
            $post->setVar('pid', 0);
210
            $post->setNew();
211
            $topic_obj = $topicHandler->create();
212
        }
213
        $textHandler    = Newbb\Helper::getInstance()->getHandler('Text');
214
        $post_text_vars = ['post_text', 'post_edit', 'dohtml', 'doxcode', 'dosmiley', 'doimage', 'dobr'];
215
        if ($post->isNew()) {
216
            if (!$topic_id = $post->getVar('topic_id')) {
217
                $topic_obj->setVar('topic_title', $post->getVar('subject', 'n'));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $topic_obj does not seem to be defined for all execution paths leading up to this point.
Loading history...
218
                $topic_obj->setVar('topic_poster', $post->getVar('uid'));
219
                $topic_obj->setVar('forum_id', $post->getVar('forum_id'));
220
                $topic_obj->setVar('topic_time', $post->getVar('post_time'));
221
                $topic_obj->setVar('poster_name', $post->getVar('poster_name'), true);
222
                $topic_obj->setVar('approved', $post->getVar('approved'), true);
223
224
                if (!$topic_id = $topicHandler->insert($topic_obj, $force)) {
225
                    $post->deleteAttachment();
0 ignored issues
show
Bug introduced by
The method deleteAttachment() does not exist on XoopsObject. It seems like you code against a sub-type of XoopsObject such as Post or XoopsModules\Newbb\Post. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

225
                    $post->/** @scrutinizer ignore-call */ 
226
                           deleteAttachment();
Loading history...
226
                    $post->setErrors('insert topic error');
227
228
                    //xoops_error($topic_obj->getErrors());
229
                    return false;
230
                }
231
                $post->setVar('topic_id', $topic_id);
232
233
                $pid = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $pid is dead and can be removed.
Loading history...
234
                $post->setVar('pid', 0);
235
            } elseif (!$post->getVar('pid')) {
236
                $pid = $topicHandler->getTopPostId($topic_id);
237
                $post->setVar('pid', $pid);
238
            }
239
240
            $text_obj = $textHandler->create();
241
            foreach ($post_text_vars as $key) {
242
                $text_obj->vars[$key] = $post->vars[$key];
243
            }
244
            $post->destroyVars($post_text_vars);
245
            if (!$post_id = parent::insert($post, $force)) {
246
                return false;
247
            }
248
            $post->unsetNew();
249
250
            $text_obj->setVar('post_id', $post_id);
251
            if (!$textHandler->insert($text_obj, $force)) {
252
                $this->delete($post);
253
                $post->setErrors('post text insert error');
254
255
                //xoops_error($text_obj->getErrors());
256
                return false;
257
            }
258
            if ($post->getVar('approved') > 0) {
259
                $this->approve($post, true);
260
            }
261
            $post->setVar('post_id', $post_id);
262
        } else {
263
            if ($post->isTopic()) {
264
                if ($post->getVar('subject') !== $topic_obj->getVar('topic_title')) {
265
                    $topic_obj->setVar('topic_title', $post->getVar('subject', 'n'));
266
                }
267
                if ($post->getVar('approved') !== $topic_obj->getVar('approved')) {
268
                    $topic_obj->setVar('approved', $post->getVar('approved'));
269
                }
270
                $topic_obj->setDirty();
271
                if (!$result = $topicHandler->insert($topic_obj, $force)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
272
                    $post->setErrors('update topic error');
273
274
                    //                    xoops_error($topic_obj->getErrors());
275
                    return false;
276
                }
277
            }
278
            $text_obj = $textHandler->get($post->getVar('post_id'));
279
            $text_obj->setDirty();
280
            foreach ($post_text_vars as $key) {
281
                $text_obj->vars[$key] = $post->vars[$key];
282
            }
283
            $post->destroyVars($post_text_vars);
284
            if (!$post_id = parent::insert($post, $force)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $post_id is dead and can be removed.
Loading history...
285
                //                xoops_error($post->getErrors());
286
                return false;
287
            }
288
            $post->unsetNew();
289
290
            if (!$textHandler->insert($text_obj, $force)) {
291
                $post->setErrors('update post text error');
292
293
                //                xoops_error($text_obj->getErrors());
294
                return false;
295
            }
296
        }
297
298
        return $post->getVar('post_id');
299
    }
300
301
    /**
302
     * @param \XoopsObject $post
303
     * @param bool         $isDeleteOne
304
     * @param bool         $force
305
     * @return bool
306
     */
307
    public function delete($post, $isDeleteOne = true, $force = false)
308
    {
309
        $retVal = false;
310
        if (($post instanceof Post) && ($post->getVar('post_id') > 0)) {
311
            if ($isDeleteOne) {
312
                if ($post->isTopic()) {
313
                    $criteria = new \CriteriaCompo(new \Criteria('topic_id', $post->getVar('topic_id')));
0 ignored issues
show
Bug introduced by
It seems like $post->getVar('topic_id') can also be of type array and array; however, parameter $value of Criteria::__construct() 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

313
                    $criteria = new \CriteriaCompo(new \Criteria('topic_id', /** @scrutinizer ignore-type */ $post->getVar('topic_id')));
Loading history...
314
                    $criteria->add(new \Criteria('approved', 1));
315
                    $criteria->add(new \Criteria('pid', 0, '>'));
316
                    if (!($this->getPostCount($criteria) > 0)) {
317
                        $retVal = $this->_delete($post, $force);
318
                    }
319
                } else {
320
                    $retVal = $this->_delete($post, $force);
321
                }
322
            } else { // want to delete multiple posts
323
                //@TODO: test replacement of XoopsTree with XoopsObjectTree
324
                require_once $GLOBALS['xoops']->path('class/tree.php');
325
                // get tree with this object as the root
326
                $myObjTree = new \XoopsObjectTree($this->getAll(), 'post_id', 'pid', $post->getVar('post_id'));
0 ignored issues
show
Unused Code introduced by
The assignment to $myObjTree is dead and can be removed.
Loading history...
Bug introduced by
It seems like $post->getVar('post_id') can also be of type array and array; however, parameter $rootId of XoopsObjectTree::__construct() 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

326
                $myObjTree = new \XoopsObjectTree($this->getAll(), 'post_id', 'pid', /** @scrutinizer ignore-type */ $post->getVar('post_id'));
Loading history...
327
                $arr       = $myObjtree->getAllChild(); // get all children of this object
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $myObjtree does not exist. Did you maybe mean $myObjTree?
Loading history...
328
                /*
329
                                require_once $GLOBALS['xoops']->path("class/xoopstree.php");
330
                                $mytree = new \XoopsTree($this->db->prefix("bb_posts"), "post_id", "pid");
331
                                $arr = $mytree->getAllChild($post->getVar('post_id'));
332
                */
333
                // irmtfan - delete children in a reverse order
334
                $success = true;
335
                for ($i = \count($arr) - 1; $i >= 0; $i--) {
336
                    $childpost = $this->create(false);
337
                    $childpost->assignVars($arr[$i]);
338
                    $thisSuccess = $this->_delete($childpost, $force);
339
                    $success     = $success && $thisSuccess;
340
                    unset($childpost);
341
                }
342
                if ($success) {
343
                    // if we successfully deleted all children then try and delete this post
344
                    $retVal = $this->_delete($post, $force);
345
                } else {
346
                    // did not successfully delete all children so don't delete this post
347
                    $retVal = false;
348
                }
349
            }
350
        }
351
352
        return $retVal;
353
    }
354
355
    /**
356
     * @param       $post
357
     * @param bool  $force
358
     * @return bool
359
     */
360
    private function _delete($post, $force = false)
361
    {
362
        if ((!$post instanceof Post) || (0 === $post->getVar('post_id'))) {
363
            return false;
364
        }
365
366
        /* Set active post as deleted */
367
        if (empty($force) && ($post->getVar('approved') > 0)) {
368
            $sql = 'UPDATE ' . $this->db->prefix('bb_posts') . ' SET approved = -1 WHERE post_id = ' . $post->getVar('post_id');
369
            if (!$result = $this->db->queryF($sql)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
370
                //@TODO: add error check here
371
            }
372
        } else { /* delete pending post directly */
373
            $sql = \sprintf('DELETE FROM `%s` WHERE post_id = %u', $this->db->prefix('bb_posts'), $post->getVar('post_id'));
0 ignored issues
show
Bug introduced by
It seems like $post->getVar('post_id') can also be of type array and array; however, parameter $values of sprintf() does only seem to accept double|integer|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

373
            $sql = \sprintf('DELETE FROM `%s` WHERE post_id = %u', $this->db->prefix('bb_posts'), /** @scrutinizer ignore-type */ $post->getVar('post_id'));
Loading history...
374
            if (!$result = $this->db->queryF($sql)) {
375
                $post->setErrors('delte post error: ' . $sql);
376
377
                return false;
378
            }
379
            $post->deleteAttachment();
380
381
            $sql = \sprintf('DELETE FROM `%s` WHERE post_id = %u', $this->db->prefix('bb_posts_text'), $post->getVar('post_id'));
382
            if (!$result = $this->db->queryF($sql)) {
383
                $post->setErrors('Could not remove post text: ' . $sql);
384
385
                return false;
386
            }
387
        }
388
389
        if ($post->isTopic()) {
390
            /** @var Newbb\TopicHandler $topicHandler */
391
            $topicHandler = Newbb\Helper::getInstance()->getHandler('Topic');
392
            $topic_obj    = $topicHandler->get($post->getVar('topic_id'));
393
            if (empty($force) && \is_object($topic_obj) && $topic_obj->getVar('approved') > 0) {
394
                $topiccount_toupdate = 1;
0 ignored issues
show
Unused Code introduced by
The assignment to $topiccount_toupdate is dead and can be removed.
Loading history...
395
                $topic_obj->setVar('approved', -1);
396
                $topicHandler->insert($topic_obj);
397
                \xoops_notification_deletebyitem($GLOBALS['xoopsModule']->getVar('mid'), 'thread', $post->getVar('topic_id'));
398
            } else {
399
                if (\is_object($topic_obj)) {
400
                    if ($topic_obj->getVar('approved') > 0) {
401
                        \xoops_notification_deletebyitem($GLOBALS['xoopsModule']->getVar('mid'), 'thread', $post->getVar('topic_id'));
402
                    }
403
404
                    $poll_id = $topic_obj->getVar('poll_id');
405
                    /** @var \XoopsModuleHandler $moduleHandler */
406
                    $moduleHandler = \xoops_getHandler('module');
407
                    if ($poll_id > 0) {
408
                        $poll_moduleHandler = $moduleHandler->getByDirname('xoopspoll');
409
                        if (($poll_moduleHandler instanceof \XoopsModuleHandler) && $poll_moduleHandler->isactive()) {
0 ignored issues
show
introduced by
$poll_moduleHandler is never a sub-type of XoopsModuleHandler.
Loading history...
Bug introduced by
The method isactive() does not exist on XoopsModuleHandler. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

409
                        if (($poll_moduleHandler instanceof \XoopsModuleHandler) && $poll_moduleHandler->/** @scrutinizer ignore-call */ isactive()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
410
                            $pollHandler = Xoopspoll\Helper::getInstance()->getHandler('Poll');
411
                            if (false !== $pollHandler->deleteAll(new \Criteria('poll_id', $poll_id, '='))) {
412
                                $optionHandler = Xoopspoll\Helper::getInstance()->getHandler('Option');
413
                                $optionHandler->deleteAll(new \Criteria('poll_id', $poll_id, '='));
414
                                $logHandler = Xoopspoll\Helper::getInstance()->getHandler('Log');
415
                                $logHandler->deleteAll(new \Criteria('poll_id', $poll_id, '='));
416
                                \xoops_comment_delete($GLOBALS['xoopsModule']->getVar('mid'), $poll_id);
417
                            }
418
                        } else {
419
                            $poll_moduleHandler = $moduleHandler->getByDirname('umfrage');
420
                            if (($poll_moduleHandler instanceof \XoopsModuleHandler)
0 ignored issues
show
introduced by
$poll_moduleHandler is never a sub-type of XoopsModuleHandler.
Loading history...
421
                                && $poll_moduleHandler->isactive()) {
422
                                require_once $GLOBALS['xoops']->path('modules/umfrage/class/umfrage.php');
423
                                require_once $GLOBALS['xoops']->path('modules/umfrage/class/umfrageoption.php');
424
                                require_once $GLOBALS['xoops']->path('modules/umfrage/class/umfragelog.php');
425
                                require_once $GLOBALS['xoops']->path('modules/umfrage/class/umfragerenderer.php');
426
427
                                $poll = new \Umfrage($poll_id);
0 ignored issues
show
Bug introduced by
The type Umfrage was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
428
                                if (false !== $poll->delete()) {
429
                                    (new \UmfrageOption())->deleteByPollId($poll_id);
0 ignored issues
show
Bug introduced by
The type UmfrageOption was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
430
                                    (new \UmfrageLog())->deleteByPollId($poll_id);
0 ignored issues
show
Bug introduced by
The type UmfrageLog was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
431
                                    \xoops_comment_delete($GLOBALS['xoopsModule']->getVar('mid'), $poll_id);
432
                                }
433
                            }
434
                        }
435
                    }
436
                }
437
438
                $sql = \sprintf('DELETE FROM `%s` WHERE topic_id = %u', $this->db->prefix('bb_topics'), $post->getVar('topic_id'));
439
                if (!$result = $this->db->queryF($sql)) {
440
                    //                  xoops_error($this->db->error());
441
                }
442
                $sql = \sprintf('DELETE FROM `%s` WHERE topic_id = %u', $this->db->prefix('bb_votedata'), $post->getVar('topic_id'));
443
                if (!$result = $this->db->queryF($sql)) {
444
                    //                  xoops_error($this->db->error());
445
                }
446
            }
447
        } else {
448
            $sql = 'UPDATE '
449
                   . $this->db->prefix('bb_topics')
450
                   . ' t '
451
                   . 'LEFT JOIN '
452
                   . $this->db->prefix('bb_posts')
453
                   . ' p ON p.topic_id = t.topic_id '
454
                   . 'SET t.topic_last_post_id = p.post_id '
455
                   . 'WHERE t.topic_last_post_id = '
456
                   . $post->getVar('post_id')
457
                   . ' '
458
                   . 'AND p.post_id = (SELECT MAX(post_id) FROM '
459
                   . $this->db->prefix('bb_posts')
460
                   . ' '
461
                   . 'WHERE topic_id=t.topic_id)';
462
            if (!$result = $this->db->queryF($sql)) {
463
                //@TODO: add error checking here
464
            }
465
        }
466
467
        $postcount_toupdate = $post->getVar('approved');
468
469
        if ($postcount_toupdate > 0) {
470
            // Update user stats
471
            if ($post->getVar('uid') > 0) {
472
                $memberHandler = \xoops_getHandler('member');
473
                $poster        = $memberHandler->getUser($post->getVar('uid'));
474
                if (\is_object($poster) && $post->getVar('uid') === $poster->getVar('uid')) {
475
                    $poster->setVar('posts', $poster->getVar('posts') - 1);
476
                    $res = $memberHandler->insertUser($poster, true);
0 ignored issues
show
Unused Code introduced by
The assignment to $res is dead and can be removed.
Loading history...
477
                    unset($poster);
478
                }
479
            }
480
            // irmtfan - just update the pid for approved posts when the post is not topic (pid=0)
481
            if (!$post->isTopic()) {
482
                $sql = 'UPDATE ' . $this->db->prefix('bb_posts') . ' SET pid = ' . $post->getVar('pid') . ' WHERE approved=1 AND pid=' . $post->getVar('post_id');
483
                if (!$result = $this->db->queryF($sql)) {
484
                    //                  xoops_error($this->db->error());
485
                }
486
            }
487
        }
488
489
        return true;
490
    }
491
492
    // START irmtfan enhance getPostCount when there is join (read_mode = 2)
493
494
    /**
495
     * @param null $criteria
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $criteria is correct as it would always require null to be passed?
Loading history...
496
     * @param null $join
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $join is correct as it would always require null to be passed?
Loading history...
497
     * @return int|null
498
     */
499
    public function getPostCount($criteria = null, $join = null)
500
    {
501
        // If not join get the count from XOOPS/class/model/stats as before
502
        if (empty($join)) {
503
            return $this->getCount($criteria);
504
        }
505
506
        $sql = 'SELECT COUNT(*) AS count' . ' ' . 'FROM ' . $this->db->prefix('bb_posts') . ' AS p' . ' ' . 'LEFT JOIN ' . $this->db->prefix('bb_posts_text') . ' ' . 'AS t ON t.post_id = p.post_id';
507
        // LEFT JOIN
508
        $sql .= $join;
509
        // WHERE
510
        if (\is_object($criteria) && \is_subclass_of($criteria, \CriteriaElement::class)) {
511
            $sql .= ' ' . $criteria->renderWhere();
512
        }
513
        if (!$result = $this->db->query($sql)) {
514
            //            xoops_error($this->db->error().'<br>'.$sql);
515
            return null;
516
        }
517
        $myrow = $this->db->fetchArray($result);
518
        $count = $myrow['count'];
519
520
        return $count;
521
    }
522
523
    // END irmtfan enhance getPostCount when there is join (read_mode = 2)
524
525
    /*
526
     *@TODO: combining viewtopic.php
527
     */
528
529
    /**
530
     * @param null $criteria
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $criteria is correct as it would always require null to be passed?
Loading history...
531
     * @param int  $limit
532
     * @param int  $start
533
     * @param null $join
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $join is correct as it would always require null to be passed?
Loading history...
534
     * @return array
535
     */
536
    public function &getPostsByLimit($criteria = null, $limit = 1, $start = 0, $join = null)
537
    {
538
        $ret = [];
539
        $sql = 'SELECT p.*, t.* ' . 'FROM ' . $this->db->prefix('bb_posts') . ' AS p ' . 'LEFT JOIN ' . $this->db->prefix('bb_posts_text') . ' AS t ON t.post_id = p.post_id';
540
        if (!empty($join)) {
541
            $sql .= (0 === mb_strpos($join, ' ')) ? $join : ' ' . $join;
542
        }
543
        if (\is_object($criteria) && \is_subclass_of($criteria, \CriteriaElement::class)) {
544
            $sql .= ' ' . $criteria->renderWhere();
545
            if ('' !== $criteria->getSort()) {
546
                $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
547
            }
548
        }
549
        $result = $this->db->query($sql, (int)$limit, (int)$start);
550
        if ($result) {
551
            while (false !== ($myrow = $this->db->fetchArray($result))) {
552
                $post = $this->create(false);
553
                $post->assignVars($myrow);
554
                $ret[$myrow['post_id']] = $post;
555
                unset($post);
556
            }
557
        }
558
559
        return $ret;
560
    }
561
562
    /**
563
     * @return bool
564
     */
565
    public function synchronization()
566
    {
567
        //      $this->cleanOrphan();
568
        return true;
569
    }
570
571
    /**
572
     * clean orphan items from database
573
     *
574
     * @return bool true on success
575
     */
576
    public function cleanOrphan()
577
    {
578
        $this->deleteAll(new \Criteria('post_time', 0), true, true);
579
        parent::cleanOrphan($this->db->prefix('bb_topics'), 'topic_id');
580
        parent::cleanOrphan($this->db->prefix('bb_posts_text'), 'post_id');
581
582
        if ($this->mysql_major_version() >= 4) { /* for MySQL 4.1+ */
583
            $sql = 'DELETE FROM ' . $this->db->prefix('bb_posts_text') . ' ' . 'WHERE (post_id NOT IN ( SELECT DISTINCT post_id FROM ' . $this->table . ') )';
584
        } else { /* for 4.0+ */
585
            $sql = 'DELETE ' . $this->db->prefix('bb_posts_text') . ' FROM ' . $this->db->prefix('bb_posts_text') . ' ' . 'LEFT JOIN ' . $this->table . ' AS aa ON ' . $this->db->prefix('bb_posts_text') . '.post_id = aa.post_id ' . ' ' . 'WHERE (aa.post_id IS NULL)';
586
587
            // Alternative for 4.1+
588
            /*
589
            $sql = "DELETE bb FROM ".$this->db->prefix("bb_posts_text")." AS bb" . " "
590
                       . "LEFT JOIN ".$this->table." AS aa ON bb.post_id = aa.post_id " . " "
591
                       . "WHERE (aa.post_id IS NULL)";
592
            */
593
        }
594
        if (!$result = $this->db->queryF($sql)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
595
            //            xoops_error($this->db->error());
596
            return false;
597
        }
598
599
        return true;
600
    }
601
602
    /**
603
     * clean expired objects from database
604
     *
605
     * @param int $expire time limit for expiration
606
     * @return bool true on success
607
     */
608
    public function cleanExpires($expire = 0)
609
    {
610
        // irmtfan if 0 no cleanup look include/plugin.php
611
        if (!\func_num_args()) {
612
            $newbbConfig = newbb_load_config();
0 ignored issues
show
Bug introduced by
The function newbb_load_config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

612
            $newbbConfig = /** @scrutinizer ignore-call */ newbb_load_config();
Loading history...
613
            $expire      = isset($newbbConfig['pending_expire']) ? (int)$newbbConfig['pending_expire'] : 7;
614
            $expire      = $expire * 24 * 3600; // days to seconds
615
        }
616
        if (empty($expire)) {
617
            return false;
618
        }
619
        $crit_expire = new \CriteriaCompo(new \Criteria('approved', 0, '<='));
620
        //        if (!empty($expire)) {
621
        $crit_expire->add(new \Criteria('post_time', \time() - (int)$expire, '<'));
622
623
        //        }
624
        return $this->deleteAll($crit_expire, true/*, true*/);
625
    }
626
}
627