Passed
Pull Request — master (#18)
by Michael
02:59
created

PostHandler::_delete()   F

Complexity

Conditions 30
Paths 1653

Size

Total Lines 130
Code Lines 85

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 30
eloc 85
c 1
b 0
f 0
nc 1653
nop 2
dl 0
loc 130
rs 0

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
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
 * @package
19
 * @since
20
 * @author       XOOPS Development Team, phppp (D.J., [email protected])
21
 */
22
23
use XoopsModules\Newbb;
24
use XoopsModules\Xoopspoll;
25
26
27
28
\defined('NEWBB_FUNCTIONS_INI') || require XOOPS_ROOT_PATH . '/modules/newbb/include/functions.ini.php';
29
//newbb_load_object();
30
31
/**
32
 * Class PostHandler
33
 */
34
//class PostHandler extends ArtObjectHandler
35
class PostHandler extends \XoopsPersistableObjectHandler
36
{
37
    /**
38
     * @param null|\XoopsDatabase $db
39
     */
40
    public function __construct(\XoopsDatabase $db = null)
41
    {
42
        parent::__construct($db, 'bb_posts', Post::class, 'post_id', 'subject');
43
    }
44
45
    /**
46
     * @param mixed|null $id
47
     * @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...
48
     * @return null|\XoopsObject
49
     */
50
    public function get($id = null, $fields = null)
51
    {
52
        $id    = (int)$id;
53
        $post  = null;
54
        $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;
55
        $array = $this->db->fetchArray($this->db->query($sql));
56
        if ($array) {
57
            $post = $this->create(false);
58
            $post->assignVars($array);
59
        }
60
61
        return $post;
62
    }
63
64
    /**
65
     * @param int $topic_id
66
     * @param int $limit
67
     * @param int $approved
68
     * @return array
69
     */
70
    //    public function &getByLimit($limit = 0, $start = 0, CriteriaElement $criteria = null, $fields = null, $asObject = true)
71
    public function &getByLimit($topic_id, $limit, $approved = 1)
72
    {
73
        $sql    = 'SELECT p.*, t.*, tp.topic_status FROM '
74
                  . $this->db->prefix('bb_posts')
75
                  . ' p LEFT JOIN '
76
                  . $this->db->prefix('bb_posts_text')
77
                  . ' t ON p.post_id=t.post_id LEFT JOIN '
78
                  . $this->db->prefix('bb_topics')
79
                  . ' tp ON tp.topic_id=p.topic_id WHERE p.topic_id='
80
                  . $topic_id
81
                  . ' AND p.approved ='
82
                  . $approved
83
                  . ' ORDER BY p.post_time DESC';
84
        $result = $this->db->query($sql, $limit, 0);
85
        $ret    = [];
86
        while (false !== ($myrow = $this->db->fetchArray($result))) {
87
            $post = $this->create(false);
88
            $post->assignVars($myrow);
89
90
            $ret[$myrow['post_id']] = $post;
91
            unset($post);
92
        }
93
94
        return $ret;
95
    }
96
97
    /**
98
     * @param $post
99
     * @return mixed
100
     */
101
    public function getPostForPDF($post)
102
    {
103
        return $post->getPostBody(true);
104
    }
105
106
    /**
107
     * @param $post
108
     * @return mixed
109
     */
110
    public function getPostForPrint($post)
111
    {
112
        return $post->getPostBody();
113
    }
114
115
    /**
116
     * @param       $post
117
     * @param bool  $force
118
     * @return bool
119
     */
120
    public function approve(&$post, $force = false)
121
    {
122
        if (empty($post)) {
123
            return false;
124
        }
125
        if (\is_numeric($post)) {
126
            $post = $this->get($post);
127
        }
128
        $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...
129
130
        $wasApproved = $post->getVar('approved');
131
        // irmtfan approve post if the approved = 0 (pending) or -1 (deleted)
132
        if (empty($force) && $wasApproved > 0) {
133
            return true;
134
        }
135
        $post->setVar('approved', 1);
136
        $this->insert($post, true);
137
138
        /** @var Newbb\TopicHandler $topicHandler */
139
        $topicHandler = Newbb\Helper::getInstance()->getHandler('Topic');
140
        $topic_obj    = $topicHandler->get($post->getVar('topic_id'));
141
        if ($topic_obj->getVar('topic_last_post_id') < $post->getVar('post_id')) {
142
            $topic_obj->setVar('topic_last_post_id', $post->getVar('post_id'));
143
        }
144
        if ($post->isTopic()) {
145
            $topic_obj->setVar('approved', 1);
146
        } else {
147
            $topic_obj->setVar('topic_replies', $topic_obj->getVar('topic_replies') + 1);
148
        }
149
        $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

149
        $topicHandler->insert(/** @scrutinizer ignore-type */ $topic_obj, true);
Loading history...
150
151
        /** @var Newbb\ForumHandler $forumHandler */
152
        $forumHandler = Newbb\Helper::getInstance()->getHandler('Forum');
153
        $forum_obj    = $forumHandler->get($post->getVar('forum_id'));
154
        if ($forum_obj->getVar('forum_last_post_id') < $post->getVar('post_id')) {
155
            $forum_obj->setVar('forum_last_post_id', $post->getVar('post_id'));
156
        }
157
        $forum_obj->setVar('forum_posts', $forum_obj->getVar('forum_posts') + 1);
158
        if ($post->isTopic()) {
159
            $forum_obj->setVar('forum_topics', $forum_obj->getVar('forum_topics') + 1);
160
        }
161
        $forumHandler->insert($forum_obj, true);
162
163
        // Update user stats
164
        if ($post->getVar('uid') > 0) {
165
            $memberHandler = \xoops_getHandler('member');
166
            $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

166
            /** @scrutinizer ignore-call */ 
167
            $poster        = $memberHandler->getUser($post->getVar('uid'));
Loading history...
167
            if (\is_object($poster) && $post->getVar('uid') === $poster->getVar('uid')) {
168
                $poster->setVar('posts', $poster->getVar('posts') + 1);
169
                $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

169
                /** @scrutinizer ignore-call */ 
170
                $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...
170
                unset($poster);
171
            }
172
        }
173
174
        // Update forum stats
175
        $statsHandler = Newbb\Helper::getInstance()->getHandler('Stats');
176
        $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

176
        $statsHandler->/** @scrutinizer ignore-call */ 
177
                       update($post->getVar('forum_id'), 'post');
Loading history...
177
        if ($post->isTopic()) {
178
            $statsHandler->update($post->getVar('forum_id'), 'topic');
179
        }
180
181
        return true;
182
    }
183
184
    /**
185
     * @param \XoopsObject $post
186
     * @param bool         $force
187
     * @return bool
188
     */
189
    public function insert(\XoopsObject $post, $force = true)
190
    {
191
        // Set the post time
192
        // The time should be 'publish' time. To be adjusted later
193
        if (!$post->getVar('post_time')) {
194
            $post->setVar('post_time', \time());
195
        }
196
197
        /** @var Newbb\TopicHandler $topicHandler */
198
        $topicHandler = Newbb\Helper::getInstance()->getHandler('Topic');
199
        // Verify the topic ID
200
        $topic_id = $post->getVar('topic_id');
201
        if ($topic_id) {
202
            $topic_obj = $topicHandler->get($topic_id);
203
            // Invalid topic OR the topic is no approved and the post is not top post
204
            if (!$topic_obj//            || (!$post->isTopic() && $topic_obj->getVar("approved") < 1)
205
            ) {
206
                return false;
207
            }
208
        }
209
        if (empty($topic_id)) {
210
            $post->setVar('topic_id', 0);
211
            $post->setVar('pid', 0);
212
            $post->setNew();
213
            $topic_obj = $topicHandler->create();
214
        }
215
        $textHandler    = Newbb\Helper::getInstance()->getHandler('Text');
216
        $post_text_vars = ['post_text', 'post_edit', 'dohtml', 'doxcode', 'dosmiley', 'doimage', 'dobr'];
217
        if ($post->isNew()) {
218
            if (!$topic_id = $post->getVar('topic_id')) {
219
                $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...
220
                $topic_obj->setVar('topic_poster', $post->getVar('uid'));
221
                $topic_obj->setVar('forum_id', $post->getVar('forum_id'));
222
                $topic_obj->setVar('topic_time', $post->getVar('post_time'));
223
                $topic_obj->setVar('poster_name', $post->getVar('poster_name'), true);
224
                $topic_obj->setVar('approved', $post->getVar('approved'), true);
225
226
                if (!$topic_id = $topicHandler->insert($topic_obj, $force)) {
227
                    $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

227
                    $post->/** @scrutinizer ignore-call */ 
228
                           deleteAttachment();
Loading history...
228
                    $post->setErrors('insert topic error');
229
230
                    //xoops_error($topic_obj->getErrors());
231
                    return false;
232
                }
233
                $post->setVar('topic_id', $topic_id);
234
235
                $pid = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $pid is dead and can be removed.
Loading history...
236
                $post->setVar('pid', 0);
237
            } elseif (!$post->getVar('pid')) {
238
                $pid = $topicHandler->getTopPostId($topic_id);
239
                $post->setVar('pid', $pid);
240
            }
241
242
            $text_obj = $textHandler->create();
243
            foreach ($post_text_vars as $key) {
244
                $text_obj->vars[$key] = $post->vars[$key];
245
            }
246
            $post->destroyVars($post_text_vars);
247
            if (!$post_id = parent::insert($post, $force)) {
248
                return false;
249
            }
250
            $post->unsetNew();
251
252
            $text_obj->setVar('post_id', $post_id);
253
            if (!$textHandler->insert($text_obj, $force)) {
254
                $this->delete($post);
255
                $post->setErrors('post text insert error');
256
257
                //xoops_error($text_obj->getErrors());
258
                return false;
259
            }
260
            if ($post->getVar('approved') > 0) {
261
                $this->approve($post, true);
262
            }
263
            $post->setVar('post_id', $post_id);
264
        } else {
265
            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

265
            if ($post->/** @scrutinizer ignore-call */ isTopic()) {
Loading history...
266
                if ($post->getVar('subject') !== $topic_obj->getVar('topic_title')) {
267
                    $topic_obj->setVar('topic_title', $post->getVar('subject', 'n'));
268
                }
269
                if ($post->getVar('approved') !== $topic_obj->getVar('approved')) {
270
                    $topic_obj->setVar('approved', $post->getVar('approved'));
271
                }
272
                $topic_obj->setDirty();
273
                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...
274
                    $post->setErrors('update topic error');
275
276
                    //                    xoops_error($topic_obj->getErrors());
277
                    return false;
278
                }
279
            }
280
            $text_obj = $textHandler->get($post->getVar('post_id'));
281
            $text_obj->setDirty();
282
            foreach ($post_text_vars as $key) {
283
                $text_obj->vars[$key] = $post->vars[$key];
284
            }
285
            $post->destroyVars($post_text_vars);
286
            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...
287
                //                xoops_error($post->getErrors());
288
                return false;
289
            }
290
            $post->unsetNew();
291
292
            if (!$textHandler->insert($text_obj, $force)) {
293
                $post->setErrors('update post text error');
294
295
                //                xoops_error($text_obj->getErrors());
296
                return false;
297
            }
298
        }
299
300
        return $post->getVar('post_id');
301
    }
302
303
    /**
304
     * @param \XoopsObject $post
305
     * @param bool         $isDeleteOne
306
     * @param bool         $force
307
     * @return bool
308
     */
309
    public function delete($post, $isDeleteOne = true, $force = false)
310
    {
311
        $retVal = false;
312
        if (($post instanceof Post) && ($post->getVar('post_id') > 0)) {
313
            if ($isDeleteOne) {
314
                if ($post->isTopic()) {
315
                    $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

315
                    $criteria = new \CriteriaCompo(new \Criteria('topic_id', /** @scrutinizer ignore-type */ $post->getVar('topic_id')));
Loading history...
316
                    $criteria->add(new \Criteria('approved', 1));
317
                    $criteria->add(new \Criteria('pid', 0, '>'));
318
                    if (!($this->getPostCount($criteria) > 0)) {
319
                        $retVal = $this->_delete($post, $force);
320
                    }
321
                } else {
322
                    $retVal = $this->_delete($post, $force);
323
                }
324
            } else { // want to delete multiple posts
325
                //@TODO: test replacement of XoopsTree with XoopsObjectTree
326
                require_once $GLOBALS['xoops']->path('class/tree.php');
327
                // get tree with this object as the root
328
                $myObjTree = new \XoopsObjectTree($this->getAll(), 'post_id', 'pid', $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 $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

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

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

411
                        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...
introduced by
$poll_moduleHandler is never a sub-type of XoopsModuleHandler.
Loading history...
412
                            $pollHandler = Xoopspoll\Helper::getInstance()->getHandler('Poll');
413
                            if (false !== $pollHandler->deleteAll(new \Criteria('poll_id', $poll_id, '='))) {
414
                                $optionHandler = Xoopspoll\Helper::getInstance()->getHandler('Option');
415
                                $optionHandler->deleteAll(new \Criteria('poll_id', $poll_id, '='));
416
                                $logHandler = Xoopspoll\Helper::getInstance()->getHandler('Log');
417
                                $logHandler->deleteAll(new \Criteria('poll_id', $poll_id, '='));
418
                                \xoops_comment_delete($GLOBALS['xoopsModule']->getVar('mid'), $poll_id);
419
                            }
420
                        } else {
421
                            $poll_moduleHandler = $moduleHandler->getByDirname('umfrage');
422
                            if (($poll_moduleHandler instanceof \XoopsModuleHandler)
0 ignored issues
show
introduced by
$poll_moduleHandler is never a sub-type of XoopsModuleHandler.
Loading history...
423
                                && $poll_moduleHandler->isactive()) {
424
                                require_once $GLOBALS['xoops']->path('modules/umfrage/class/umfrage.php');
425
                                require_once $GLOBALS['xoops']->path('modules/umfrage/class/umfrageoption.php');
426
                                require_once $GLOBALS['xoops']->path('modules/umfrage/class/umfragelog.php');
427
                                require_once $GLOBALS['xoops']->path('modules/umfrage/class/umfragerenderer.php');
428
429
                                $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...
430
                                if (false !== $poll->delete()) {
431
                                    \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...
432
                                    \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...
433
                                    \xoops_comment_delete($GLOBALS['xoopsModule']->getVar('mid'), $poll_id);
434
                                }
435
                            }
436
                        }
437
                    }
438
                }
439
440
                $sql = \sprintf('DELETE FROM `%s` WHERE topic_id = %u', $this->db->prefix('bb_topics'), $post->getVar('topic_id'));
441
                if (!$result = $this->db->queryF($sql)) {
442
                    //                  xoops_error($this->db->error());
443
                }
444
                $sql = \sprintf('DELETE FROM `%s` WHERE topic_id = %u', $this->db->prefix('bb_votedata'), $post->getVar('topic_id'));
445
                if (!$result = $this->db->queryF($sql)) {
446
                    //                  xoops_error($this->db->error());
447
                }
448
            }
449
        } else {
450
            $sql = 'UPDATE '
451
                   . $this->db->prefix('bb_topics')
452
                   . ' t '
453
                   . 'LEFT JOIN '
454
                   . $this->db->prefix('bb_posts')
455
                   . ' p ON p.topic_id = t.topic_id '
456
                   . 'SET t.topic_last_post_id = p.post_id '
457
                   . 'WHERE t.topic_last_post_id = '
458
                   . $post->getVar('post_id')
459
                   . ' '
460
                   . 'AND p.post_id = (SELECT MAX(post_id) FROM '
461
                   . $this->db->prefix('bb_posts')
462
                   . ' '
463
                   . 'WHERE topic_id=t.topic_id)';
464
            if (!$result = $this->db->queryF($sql)) {
465
                //@TODO: add error checking here
466
            }
467
        }
468
469
        $postcount_toupdate = $post->getVar('approved');
470
471
        if ($postcount_toupdate > 0) {
472
            // Update user stats
473
            if ($post->getVar('uid') > 0) {
474
                $memberHandler = \xoops_getHandler('member');
475
                $poster        = $memberHandler->getUser($post->getVar('uid'));
476
                if (\is_object($poster) && $post->getVar('uid') === $poster->getVar('uid')) {
477
                    $poster->setVar('posts', $poster->getVar('posts') - 1);
478
                    $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...
479
                    unset($poster);
480
                }
481
            }
482
            // irmtfan - just update the pid for approved posts when the post is not topic (pid=0)
483
            if (!$post->isTopic()) {
484
                $sql = 'UPDATE ' . $this->db->prefix('bb_posts') . ' SET pid = ' . $post->getVar('pid') . ' WHERE approved=1 AND pid=' . $post->getVar('post_id');
485
                if (!$result = $this->db->queryF($sql)) {
486
                    //                  xoops_error($this->db->error());
487
                }
488
            }
489
        }
490
491
        return true;
492
    }
493
494
    // START irmtfan enhance getPostCount when there is join (read_mode = 2)
495
496
    /**
497
     * @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...
498
     * @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...
499
     * @return int|null
500
     */
501
    public function getPostCount($criteria = null, $join = null)
502
    {
503
        // If not join get the count from XOOPS/class/model/stats as before
504
        if (empty($join)) {
505
            return $this->getCount($criteria);
506
        }
507
508
        $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';
509
        // LEFT JOIN
510
        $sql .= $join;
511
        // WHERE
512
        if (isset($criteria) && $criteria instanceof \CriteriaElement) {
513
            $sql .= ' ' . $criteria->renderWhere();
0 ignored issues
show
Bug introduced by
The method renderWhere() does not exist on CriteriaElement. Did you maybe mean render()? ( Ignorable by Annotation )

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

513
            $sql .= ' ' . $criteria->/** @scrutinizer ignore-call */ renderWhere();

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...
514
        }
515
        if (!$result = $this->db->query($sql)) {
516
            //            xoops_error($this->db->error().'<br>'.$sql);
517
            return null;
518
        }
519
        $myrow = $this->db->fetchArray($result);
520
        $count = $myrow['count'];
521
522
        return $count;
523
    }
524
525
    // END irmtfan enhance getPostCount when there is join (read_mode = 2)
526
527
    /*
528
     *@TODO: combining viewtopic.php
529
     */
530
531
    /**
532
     * @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...
533
     * @param int  $limit
534
     * @param int  $start
535
     * @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...
536
     * @return array
537
     */
538
    public function &getPostsByLimit($criteria = null, $limit = 1, $start = 0, $join = null)
539
    {
540
        $ret = [];
541
        $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';
542
        if (!empty($join)) {
543
            $sql .= (0 === mb_strpos($join, ' ')) ? $join : ' ' . $join;
544
        }
545
        if (isset($criteria) && $criteria instanceof \CriteriaElement) {
546
            $sql .= ' ' . $criteria->renderWhere();
547
            if ('' !== $criteria->getSort()) {
548
                $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
549
            }
550
        }
551
        $result = $this->db->query($sql, (int)$limit, (int)$start);
552
        if (!$result) {
553
            //            xoops_error($this->db->error());
554
            return $ret;
555
        }
556
        while (false !== ($myrow = $this->db->fetchArray($result))) {
557
            $post = $this->create(false);
558
            $post->assignVars($myrow);
559
            $ret[$myrow['post_id']] = $post;
560
            unset($post);
561
        }
562
563
        return $ret;
564
    }
565
566
    /**
567
     * @return bool
568
     */
569
    public function synchronization()
570
    {
571
        //      $this->cleanOrphan();
572
        return true;
573
    }
574
575
    /**
576
     * clean orphan items from database
577
     *
578
     * @return bool true on success
579
     */
580
    public function cleanOrphan()
581
    {
582
        $this->deleteAll(new \Criteria('post_time', 0), true, true);
583
        parent::cleanOrphan($this->db->prefix('bb_topics'), 'topic_id');
584
        parent::cleanOrphan($this->db->prefix('bb_posts_text'), 'post_id');
585
586
        if ($this->mysql_major_version() >= 4) { /* for MySQL 4.1+ */
587
            $sql = 'DELETE FROM ' . $this->db->prefix('bb_posts_text') . ' ' . 'WHERE (post_id NOT IN ( SELECT DISTINCT post_id FROM ' . $this->table . ') )';
588
        } else { /* for 4.0+ */
589
            /* */
590
            $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)';
591
592
            // Alternative for 4.1+
593
            /*
594
            $sql = "DELETE bb FROM ".$this->db->prefix("bb_posts_text")." AS bb" . " "
595
                       . "LEFT JOIN ".$this->table." AS aa ON bb.post_id = aa.post_id " . " "
596
                       . "WHERE (aa.post_id IS NULL)";
597
            */
598
        }
599
        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...
600
            //            xoops_error($this->db->error());
601
            return false;
602
        }
603
604
        return true;
605
    }
606
607
    /**
608
     * clean expired objects from database
609
     *
610
     * @param int $expire time limit for expiration
611
     * @return bool true on success
612
     */
613
    public function cleanExpires($expire = 0)
614
    {
615
        // irmtfan if 0 no cleanup look include/plugin.php
616
        if (!\func_num_args()) {
617
            $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

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