PostHandler::cleanExpires()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 17
rs 9.9666
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
38
39
\defined('NEWBB_FUNCTIONS_INI') || require $GLOBALS['xoops']->path('modules/newbb/include/functions.ini.php');
40
41
/**
42
 * Class PostHandler
43
 */
44
class PostHandler extends \XoopsPersistableObjectHandler
45
{
46
    /**
47
     * @param \XoopsDatabase|null $db
48
     */
49
    public function __construct(\XoopsDatabase $db = null)
50
    {
51
        parent::__construct($db, 'newbb_posts', Post::class, 'post_id', 'subject');
52
    }
53
54
    /**
55
     * @param mixed $id
56
     * @param null  $var
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $var is correct as it would always require null to be passed?
Loading history...
57
     * @return null|\XoopsObject
58
     */
59
    public function get($id = null, $var = null) //get($id)
60
    {
61
        $id    = (int)$id;
62
        $post  = null;
63
        $sql   = 'SELECT p.*, t.* FROM ' . $this->db->prefix('newbb_posts') . ' p LEFT JOIN ' . $this->db->prefix('newbb_posts_text') . ' t ON p.post_id=t.post_id WHERE p.post_id=' . $id;
64
        $array = $this->db->fetchArray($this->db->query($sql));
65
        if ($array) {
66
            $post = $this->create(false);
67
            $post->assignVars($array);
68
        }
69
70
        return $post;
71
    }
72
73
    /**
74
     * @param int              $limit
75
     * @param int              $start
76
     * @param \CriteriaElement $criteria
77
     * @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...
78
     * @param bool             $asObject
79
     * @param int              $topic_id
80
     * @param int              $approved
81
     * @return array
82
     */
83
    //    public function getByLimit($topic_id, $limit, $approved = 1)
84
    public function &getByLimit(
85
        $limit = 0,
86
        $start = 0,
87
        \CriteriaElement $criteria = null,
88
        $fields = null,
89
        $asObject = true,
90
        $topic_id = 0,
91
        $approved = 1
92
    ) {
93
        $sql    = 'SELECT p.*, t.*, tp.topic_status FROM '
94
                  . $this->db->prefix('newbb_posts')
95
                  . ' p LEFT JOIN '
96
                  . $this->db->prefix('newbb_posts_text')
97
                  . ' t ON p.post_id=t.post_id LEFT JOIN '
98
                  . $this->db->prefix('newbb_topics')
99
                  . ' tp ON tp.topic_id=p.topic_id WHERE p.topic_id='
100
                  . $topic_id
101
                  . ' AND p.approved ='
102
                  . $approved
103
                  . ' ORDER BY p.post_time DESC';
104
        $result = $this->db->query($sql, $limit, 0);
105
        $ret    = [];
106
        while (false !== ($myrow = $this->db->fetchArray($result))) {
107
            $post = $this->create(false);
108
            $post->assignVars($myrow);
109
110
            $ret[$myrow['post_id']] = $post;
111
            unset($post);
112
        }
113
114
        return $ret;
115
    }
116
117
    /**
118
     * @param Post $post
119
     * @return mixed
120
     */
121
    public function getPostForPDF($post)
122
    {
123
        return $post->getPostBody(true);
0 ignored issues
show
Unused Code introduced by
The call to XoopsModules\Newbb\Post::getPostBody() has too many arguments starting with true. ( Ignorable by Annotation )

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

123
        return $post->/** @scrutinizer ignore-call */ getPostBody(true);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
124
    }
125
126
    /**
127
     * @param Post $post
128
     * @return mixed
129
     */
130
    public function getPostForPrint($post)
131
    {
132
        return $post->getPostBody();
133
    }
134
135
    /**
136
     * @param int|Post|\XoopsObject $post
137
     * @param bool                  $force
138
     * @return bool
139
     */
140
    public function approve(&$post, $force = false)
141
    {
142
        if (empty($post)) {
143
            return false;
144
        }
145
        if (\is_numeric($post)) {
146
            $post = $this->get($post);
147
        }
148
149
        $wasApproved = $post->getVar('approved');
150
        // irmtfan approve post if the approved = 0 (pending) or -1 (deleted)
151
        if (empty($force) && $wasApproved > 0) {
152
            return true;
153
        }
154
        $post->setVar('approved', 1);
155
        $this->insert($post, true);
0 ignored issues
show
Bug introduced by
It seems like $post can also be of type null; however, parameter $post 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

155
        $this->insert(/** @scrutinizer ignore-type */ $post, true);
Loading history...
156
157
        /** @var Newbb\TopicHandler $topicHandler */
158
        $topicHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Topic');
159
        $topicObject  = $topicHandler->get($post->getVar('topic_id'));
160
        if ($topicObject->getVar('topic_last_post_id') < $post->getVar('post_id')) {
161
            $topicObject->setVar('topic_last_post_id', $post->getVar('post_id'));
162
        }
163
        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 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

163
        if ($post->/** @scrutinizer ignore-call */ isTopic()) {
Loading history...
164
            $topicObject->setVar('approved', 1);
165
        } else {
166
            $topicObject->setVar('topic_replies', $topicObject->getVar('topic_replies') + 1);
167
        }
168
        $topicHandler->insert($topicObject, true);
0 ignored issues
show
Bug introduced by
It seems like $topicObject 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

168
        $topicHandler->insert(/** @scrutinizer ignore-type */ $topicObject, true);
Loading history...
169
170
        /** @var Newbb\ForumHandler $forumHandler */
171
        $forumHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Forum');
172
        $forumObject  = $forumHandler->get($post->getVar('forum_id'));
173
        if ($forumObject->getVar('forum_last_post_id') < $post->getVar('post_id')) {
174
            $forumObject->setVar('forum_last_post_id', $post->getVar('post_id'));
175
        }
176
        $forumObject->setVar('forum_posts', $forumObject->getVar('forum_posts') + 1);
177
        if ($post->isTopic()) {
178
            $forumObject->setVar('forum_topics', $forumObject->getVar('forum_topics') + 1);
179
        }
180
        $forumHandler->insert($forumObject, true);
181
182
        // Update user stats
183
        if ($post->getVar('uid') > 0) {
184
            /** @var \XoopsMemberHandler $memberHandler */
185
            $memberHandler = \xoops_getHandler('member');
186
            $poster        = $memberHandler->getUser($post->getVar('uid'));
187
            if (\is_object($poster) && $post->getVar('uid') == $poster->getVar('uid')) {
188
                $poster->setVar('posts', $poster->getVar('posts') + 1);
189
                $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...
190
                unset($poster);
191
            }
192
        }
193
194
        // Update forum stats
195
        /** @var StatsHandler $statsHandler */
196
        $statsHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Stats');
197
        $statsHandler->update($post->getVar('forum_id'), 'post');
198
        if ($post->isTopic()) {
199
            $statsHandler->update($post->getVar('forum_id'), 'topic');
200
        }
201
202
        return true;
203
    }
204
205
    /**
206
     * @param \XoopsObject $post
207
     * @param bool         $force
208
     * @return bool
209
     */
210
    public function insert(\XoopsObject $post, $force = true) //insert(&$post, $force = true)
211
    {
212
        $topicObject = null;
213
        // Set the post time
214
        // The time should be "publish" time. To be adjusted later
215
        if (!$post->getVar('post_time')) {
216
            $post->setVar('post_time', \time());
217
        }
218
219
        /** @var Newbb\TopicHandler $topicHandler */
220
        $topicHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Topic');
221
        // Verify the topic ID
222
        $topic_id = $post->getVar('topic_id');
223
        if ($topic_id) {
224
            $topicObject = $topicHandler->get($topic_id);
225
            // Invalid topic OR the topic is no approved and the post is not top post
226
            if (!$topicObject//    || (!$post->isTopic() && $topicObject->getVar("approved") < 1)
227
            ) {
228
                return false;
229
            }
230
        }
231
        if (empty($topic_id)) {
232
            $post->setVar('topic_id', 0);
233
            $post->setVar('pid', 0);
234
            $post->setNew();
235
            $topicObject = $topicHandler->create();
236
        }
237
        $textHandler    = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Text');
238
        $post_text_vars = ['post_text', 'post_edit', 'dohtml', 'doxcode', 'dosmiley', 'doimage', 'dobr'];
239
        if ($post->isNew()) {
240
            if (!$topic_id = $post->getVar('topic_id')) {
241
                $topicObject->setVar('topic_title', $post->getVar('subject', 'n'));
242
                $topicObject->setVar('topic_poster', $post->getVar('uid'));
243
                $topicObject->setVar('forum_id', $post->getVar('forum_id'));
244
                $topicObject->setVar('topic_time', $post->getVar('post_time'));
245
                $topicObject->setVar('poster_name', $post->getVar('poster_name'));
246
                $topicObject->setVar('approved', $post->getVar('approved'));
247
248
                if (!$topic_id = $topicHandler->insert($topicObject, $force)) {
0 ignored issues
show
Bug introduced by
It seems like $topicObject 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

248
                if (!$topic_id = $topicHandler->insert(/** @scrutinizer ignore-type */ $topicObject, $force)) {
Loading history...
249
                    $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 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

249
                    $post->/** @scrutinizer ignore-call */ 
250
                           deleteAttachment();
Loading history...
250
                    $post->setErrors('insert topic error');
251
252
                    //xoops_error($topicObject->getErrors());
253
                    return false;
254
                }
255
                $post->setVar('topic_id', $topic_id);
256
257
                $pid = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $pid is dead and can be removed.
Loading history...
258
                $post->setVar('pid', 0);
259
            } elseif (!$post->getVar('pid')) {
260
                $pid = $topicHandler->getTopPostId($topic_id);
261
                $post->setVar('pid', $pid);
262
            }
263
264
            $textObject = $textHandler->create();
265
            foreach ($post_text_vars as $key) {
266
                $textObject->vars[$key] = $post->vars[$key];
267
            }
268
            $post->destroyVars($post_text_vars);
269
270
            //            if (!$post_id = parent::insert($post, $force)) {
271
            //                return false;
272
            //            }
273
274
            if (!$post_id = parent::insert($post, $force)) {
275
                return false;
276
            }
277
            $post->unsetNew();
278
279
            $textObject->setVar('post_id', $post_id);
280
            if (!$textHandler->insert($textObject, $force)) {
281
                $this->delete($post);
282
                $post->setErrors('post text insert error');
283
284
                //xoops_error($textObject->getErrors());
285
                return false;
286
            }
287
            if ($post->getVar('approved') > 0) {
288
                $this->approve($post, true);
289
            }
290
            $post->setVar('post_id', $post_id);
291
        } else {
292
            if ($post->isTopic()) {
293
                if ($post->getVar('subject') !== $topicObject->getVar('topic_title')) {
294
                    $topicObject->setVar('topic_title', $post->getVar('subject', 'n'));
295
                }
296
                if ($post->getVar('approved') !== $topicObject->getVar('approved')) {
297
                    $topicObject->setVar('approved', $post->getVar('approved'));
298
                }
299
                $topicObject->setDirty();
300
                if (!$result = $topicHandler->insert($topicObject, $force)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
301
                    $post->setErrors('update topic error');
302
303
                    //xoops_error($topicObject->getErrors());
304
                    return false;
305
                }
306
            }
307
            $textObject = $textHandler->get($post->getVar('post_id'));
308
            $textObject->setDirty();
309
            foreach ($post_text_vars as $key) {
310
                $textObject->vars[$key] = $post->vars[$key];
311
            }
312
            $post->destroyVars($post_text_vars);
313
            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...
314
                //xoops_error($post->getErrors());
315
                return false;
316
            }
317
            $post->unsetNew();
318
319
            if (!$textHandler->insert($textObject, $force)) {
320
                $post->setErrors('update post text error');
321
322
                //xoops_error($textObject->getErrors());
323
                return false;
324
            }
325
        }
326
327
        return $post->getVar('post_id');
328
    }
329
330
    /**
331
     * @param \XoopsObject|Post $post
332
     * @param bool              $isDeleteOne
333
     * @param bool              $force
334
     * @return bool
335
     */
336
    public function delete(\XoopsObject $post, $isDeleteOne = true, $force = false)
337
    {
338
        if (!\is_object($post) || 0 == $post->getVar('post_id')) {
339
            return false;
340
        }
341
342
        if ($isDeleteOne) {
343
            if ($post->isTopic()) {
344
                $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

344
                $criteria = new \CriteriaCompo(new \Criteria('topic_id', /** @scrutinizer ignore-type */ $post->getVar('topic_id')));
Loading history...
345
                $criteria->add(new \Criteria('approved', 1));
346
                $criteria->add(new \Criteria('pid', 0, '>'));
347
                if ($this->getPostCount($criteria) > 0) {
348
                    return false;
349
                }
350
            }
351
352
            return $this->myDelete($post, $force);
353
        }
354
        require_once $GLOBALS['xoops']->path('class/xoopstree.php');
355
        $mytree = new Newbb\Tree($this->db->prefix('newbb_posts'), 'post_id', 'pid');
356
        $arr    = $mytree->getAllChild($post->getVar('post_id'));
357
        // irmtfan - delete childs in a reverse order
358
        for ($i = \count($arr) - 1; $i >= 0; $i--) {
0 ignored issues
show
Bug introduced by
It seems like $arr can also be of type mixed; however, parameter $var of count() does only seem to accept Countable|array, 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

358
        for ($i = \count(/** @scrutinizer ignore-type */ $arr) - 1; $i >= 0; $i--) {
Loading history...
359
            $childpost = $this->create(false);
360
            $childpost->assignVars($arr[$i]);
361
            $this->myDelete($childpost, $force);
362
            unset($childpost);
363
        }
364
        $this->myDelete($post, $force);
365
366
        return true;
367
    }
368
369
    /**
370
     * @param Post|\XoopsObject $post
371
     * @param bool              $force
372
     * @return bool
373
     */
374
    public function myDelete(Post $post, $force = false)
375
    {
376
        global $xoopsModule;
377
378
        if (!\is_object($post) || 0 == $post->getVar('post_id')) {
379
            return false;
380
        }
381
382
        /* Set active post as deleted */
383
        if ($post->getVar('approved') > 0 && empty($force)) {
384
            $sql = 'UPDATE ' . $this->db->prefix('newbb_posts') . ' SET approved = -1 WHERE post_id = ' . $post->getVar('post_id');
385
            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...
386
            }
387
            /* delete pending post directly */
388
        } else {
389
            $sql = \sprintf('DELETE FROM `%s` WHERE post_id = %u', $this->db->prefix('newbb_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

389
            $sql = \sprintf('DELETE FROM `%s` WHERE post_id = %u', $this->db->prefix('newbb_posts'), /** @scrutinizer ignore-type */ $post->getVar('post_id'));
Loading history...
390
            if (!$result = $this->db->queryF($sql)) {
391
                $post->setErrors('delete post error: ' . $sql);
392
393
                return false;
394
            }
395
            $post->deleteAttachment();
396
397
            $sql = \sprintf('DELETE FROM `%s` WHERE post_id = %u', $this->db->prefix('newbb_posts_text'), $post->getVar('post_id'));
398
            if (!$result = $this->db->queryF($sql)) {
399
                $post->setErrors('Could not remove post text: ' . $sql);
400
401
                return false;
402
            }
403
        }
404
405
        if ($post->isTopic()) {
406
            $topicHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Topic');
407
            /** @var Topic $topicObject */
408
            $topicObject = $topicHandler->get($post->getVar('topic_id'));
409
            if (\is_object($topicObject) && $topicObject->getVar('approved') > 0 && empty($force)) {
410
                $topiccount_toupdate = 1;
0 ignored issues
show
Unused Code introduced by
The assignment to $topiccount_toupdate is dead and can be removed.
Loading history...
411
                $topicObject->setVar('approved', -1);
412
                $topicHandler->insert($topicObject);
413
                \xoops_notification_deletebyitem($xoopsModule->getVar('mid'), 'thread', $post->getVar('topic_id'));
414
            } else {
415
                if (\is_object($topicObject)) {
416
                    if ($topicObject->getVar('approved') > 0) {
417
                        \xoops_notification_deletebyitem($xoopsModule->getVar('mid'), 'thread', $post->getVar('topic_id'));
418
                    }
419
420
                    $poll_id = $topicObject->getVar('poll_id');
421
                    // START irmtfan poll_module
422
                    $topicObject->deletePoll($poll_id);
423
                    // END irmtfan poll_module
424
                }
425
426
                $sql = \sprintf('DELETE FROM `%s` WHERE topic_id = %u', $this->db->prefix('newbb_topics'), $post->getVar('topic_id'));
427
                if (!$result = $this->db->queryF($sql)) {
428
                    //xoops_error($this->db->error());
429
                }
430
                $sql = \sprintf('DELETE FROM `%s` WHERE topic_id = %u', $this->db->prefix('newbb_votedata'), $post->getVar('topic_id'));
431
                if (!$result = $this->db->queryF($sql)) {
432
                    //xoops_error($this->db->error());
433
                }
434
            }
435
        } else {
436
            $sql = 'UPDATE ' . $this->db->prefix('newbb_topics') . ' t
437
                            LEFT JOIN ' . $this->db->prefix('newbb_posts') . ' p ON p.topic_id = t.topic_id
438
                            SET t.topic_last_post_id = p.post_id
439
                            WHERE t.topic_last_post_id = ' . $post->getVar('post_id') . '
440
                                    AND p.post_id = (SELECT MAX(post_id) FROM ' . $this->db->prefix('newbb_posts') . ' WHERE topic_id=t.topic_id)';
441
            if (!$result = $this->db->queryF($sql)) {
442
            }
443
        }
444
445
        $postcount_toupdate = $post->getVar('approved');
446
447
        if ($postcount_toupdate > 0) {
448
            // Update user stats
449
            if ($post->getVar('uid') > 0) {
450
                /** @var \XoopsMemberHandler $memberHandler */
451
                $memberHandler = \xoops_getHandler('member');
452
                $poster        = $memberHandler->getUser($post->getVar('uid'));
453
                if (\is_object($poster) && $post->getVar('uid') == $poster->getVar('uid')) {
454
                    $poster->setVar('posts', $poster->getVar('posts') - 1);
455
                    $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...
456
                    unset($poster);
457
                }
458
            }
459
            // irmtfan - just update the pid for approved posts when the post is not topic (pid=0)
460
            if (!$post->isTopic()) {
461
                $sql = 'UPDATE ' . $this->db->prefix('newbb_posts') . ' SET pid = ' . $post->getVar('pid') . ' WHERE approved=1 AND pid=' . $post->getVar('post_id');
462
                if (!$result = $this->db->queryF($sql)) {
463
                    //xoops_error($this->db->error());
464
                }
465
            }
466
        }
467
468
        return true;
469
    }
470
471
    // START irmtfan enhance getPostCount when there is join (read_mode = 2)
472
473
    /**
474
     * @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...
475
     * @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...
476
     * @return int|null
477
     */
478
    public function getPostCount($criteria = null, $join = null)
479
    {
480
        // if not join get the count from XOOPS/class/model/stats as before
481
        if (empty($join)) {
482
            return parent::getCount($criteria);
483
        }
484
485
        $sql = 'SELECT COUNT(*) as count' . ' FROM ' . $this->db->prefix('newbb_posts') . ' AS p' . ' LEFT JOIN ' . $this->db->prefix('newbb_posts_text') . ' AS t ON t.post_id = p.post_id';
486
        // LEFT JOIN
487
        $sql .= $join;
488
        // WHERE
489
        if (null !== $criteria && $criteria instanceof \CriteriaElement) {
0 ignored issues
show
introduced by
The condition null !== $criteria is always false.
Loading history...
490
            $sql .= ' ' . $criteria->renderWhere();
491
        }
492
        if (!$result = $this->db->query($sql)) {
493
            //xoops_error($this->db->error().'<br>'.$sql);
494
            return null;
495
        }
496
        $myrow = $this->db->fetchArray($result);
497
        $count = $myrow['count'];
498
499
        return $count;
500
    }
501
502
    // END irmtfan enhance getPostCount when there is join (read_mode = 2)
503
    /*
504
     * TODO: combining viewtopic.php
505
     */
506
507
    /**
508
     * @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...
509
     * @param int  $limit
510
     * @param int  $start
511
     * @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...
512
     * @return array
513
     */
514
    public function getPostsByLimit($criteria = null, $limit = 1, $start = 0, $join = null)
515
    {
516
        $ret = [];
517
        $sql = 'SELECT p.*, t.* ' . ' FROM ' . $this->db->prefix('newbb_posts') . ' AS p' . ' LEFT JOIN ' . $this->db->prefix('newbb_posts_text') . ' AS t ON t.post_id = p.post_id';
518
        if (!empty($join)) {
519
            $sql .= $join;
520
        }
521
        if (null !== $criteria && $criteria instanceof \CriteriaElement) {
0 ignored issues
show
introduced by
The condition null !== $criteria is always false.
Loading history...
522
            $sql .= ' ' . $criteria->renderWhere();
523
            if ('' !== $criteria->getSort()) {
524
                $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
525
            }
526
        }
527
        $result = $this->db->query($sql, (int)$limit, (int)$start);
528
        if (!$result) {
529
            //xoops_error($this->db->error());
530
            return $ret;
531
        }
532
        while (false !== ($myrow = $this->db->fetchArray($result))) {
533
            $post = $this->create(false);
534
            $post->assignVars($myrow);
535
            $ret[$myrow['post_id']] = $post;
536
            unset($post);
537
        }
538
539
        return $ret;
540
    }
541
542
    /**
543
     * @return bool
544
     */
545
    public function synchronization()
546
    {
547
        //$this->cleanOrphan();
548
        return true;
549
    }
550
551
    /**
552
     * clean orphan items from database
553
     *
554
     * @param string $table_link
555
     * @param string $field_link
556
     * @param string $field_object
557
     * @return bool   true on success
558
     */
559
    public function cleanOrphan($table_link = '', $field_link = '', $field_object = '') //cleanOrphan()
560
    {
561
        $this->deleteAll(new \Criteria('post_time', 0), true, true);
562
        parent::cleanOrphan($this->db->prefix('newbb_topics'), 'topic_id');
563
        parent::cleanOrphan($this->db->prefix('newbb_posts_text'), 'post_id');
564
565
        $sql = 'DELETE FROM ' . $this->db->prefix('newbb_posts_text') . ' WHERE (post_id NOT IN ( SELECT DISTINCT post_id FROM ' . $this->table . ') )';
566
        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...
567
            //xoops_error($this->db->error());
568
            return false;
569
        }
570
571
        return true;
572
    }
573
574
    /**
575
     * clean expired objects from database
576
     *
577
     * @param int $expire time limit for expiration
578
     * @return bool true on success
579
     */
580
    public function cleanExpires($expire = 0)
581
    {
582
        // irmtfan if 0 no cleanup look include/plugin.php
583
        if (!\func_num_args()) {
584
            $newbbConfig = \newbbLoadConfig();
585
            $expire      = isset($newbbConfig['pending_expire']) ? (int)$newbbConfig['pending_expire'] : 7;
586
            $expire      = $expire * 24 * 3600; // days to seconds
587
        }
588
        if (empty($expire)) {
589
            return false;
590
        }
591
        $crit_expire = new \CriteriaCompo(new \Criteria('approved', 0, '<='));
592
        //if (!empty($expire)) {
593
        $crit_expire->add(new \Criteria('post_time', \time() - (int)$expire, '<'));
594
595
        //}
596
        return $this->deleteAll($crit_expire, true/*, true*/);
597
    }
598
}
599