XoopsTopic::makeTopicSelBox()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 4
1
<?php
2
/**
3
 * XOOPS news topic
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @copyright       (c) 2000-2025 XOOPS Project (https://xoops.org)
13
 * @license             GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
14
 * @package             kernel
15
 * @since               2.0.0
16
 * @author              Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/
17
 * @deprecated
18
 */
19
20
defined('XOOPS_ROOT_PATH') || exit('Restricted access');
21
22
$GLOBALS['xoopsLogger']->addDeprecated("'/class/xoopstopic.php' is deprecated since XOOPS 2.5.4, please create your own class instead.");
23
24
include_once XOOPS_ROOT_PATH . '/class/xoopstree.php';
25
26
/**
27
 * Class XoopsTopic
28
 */
29
class XoopsTopic
30
{
31
    /**
32
     * @var \XoopsMySQLDatabase
33
     */
34
    public $db;
35
    public $table;
36
    public $topic_id;
37
    public $topic_pid;
38
    public $topic_title;
39
    public $topic_imgurl;
40
    public $prefix; // only used in topic tree
41
    public $use_permission = false;
42
    public $mid; // module id used for setting permission
43
44
    /**
45
     * @param     $table
46
     * @param int|array $topicid
47
     */
48
    public function __construct($table, $topicid = 0)
49
    {
50
        $this->db    = XoopsDatabaseFactory::getDatabaseConnection();
51
        $this->table = $table;
52
        if (is_array($topicid)) {
53
            $this->makeTopic($topicid);
54
        } elseif ($topicid != 0) {
55
            $this->getTopic((int) $topicid);
56
        } else {
57
            $this->topic_id = $topicid;
58
        }
59
    }
60
61
    /**
62
     * @param $value
63
     */
64
    public function setTopicTitle($value)
65
    {
66
        $this->topic_title = $value;
67
    }
68
69
    /**
70
     * @param $value
71
     */
72
    public function setTopicImgurl($value)
73
    {
74
        $this->topic_imgurl = $value;
75
    }
76
77
    /**
78
     * @param $value
79
     */
80
    public function setTopicPid($value)
81
    {
82
        $this->topic_pid = $value;
83
    }
84
85
    /**
86
     * @param $topicid
87
     */
88
    public function getTopic($topicid)
89
    {
90
        $topicid = (int) $topicid;
91
        $sql     = 'SELECT * FROM ' . $this->table . ' WHERE topic_id=' . $topicid . '';
92
        $result = $this->db->query($sql);
93
        if (!$this->db->isResultSet($result)) {
94
            throw new \RuntimeException(
95
                \sprintf(_DB_QUERY_ERROR, $sql) . $this->db->error(),
96
                E_USER_ERROR,
97
            );
98
        }
99
        $array   = $this->db->fetchArray($result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::fetchArray() does only seem to accept mysqli_result, 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

99
        $array   = $this->db->fetchArray(/** @scrutinizer ignore-type */ $result);
Loading history...
100
        $this->makeTopic($array);
101
    }
102
103
    /**
104
     * @param $array
105
     */
106
    public function makeTopic($array)
107
    {
108
        foreach ($array as $key => $value) {
109
            $this->$key = $value;
110
        }
111
    }
112
113
    /**
114
     * @param $mid
115
     */
116
    public function usePermission($mid)
117
    {
118
        $this->mid            = $mid;
119
        $this->use_permission = true;
120
    }
121
122
    /**
123
     * @return bool
124
     */
125
    public function store()
126
    {
127
        $myts   = \MyTextSanitizer::getInstance();
128
        $title  = '';
129
        $imgurl = '';
130
        if (isset($this->topic_title) && $this->topic_title != '') {
131
            $title = $myts->addSlashes($this->topic_title);
132
        }
133
        if (isset($this->topic_imgurl) && $this->topic_imgurl != '') {
134
            $imgurl = $myts->addSlashes($this->topic_imgurl);
135
        }
136
        if (!isset($this->topic_pid) || !is_numeric($this->topic_pid)) {
137
            $this->topic_pid = 0;
138
        }
139
        if (empty($this->topic_id)) {
140
            $this->topic_id = $this->db->genId($this->table . '_topic_id_seq');
141
            $sql            = sprintf("INSERT INTO %s (topic_id, topic_pid, topic_imgurl, topic_title) VALUES (%u, %u, '%s', '%s')", $this->table, $this->topic_id, $this->topic_pid, $imgurl, $title);
142
        } else {
143
            $sql = sprintf("UPDATE %s SET topic_pid = %u, topic_imgurl = '%s', topic_title = '%s' WHERE topic_id = %u", $this->table, $this->topic_pid, $imgurl, $title, $this->topic_id);
144
        }
145
        if (!$result = $this->db->query($sql)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
146
            ErrorHandler::show('0022');
147
        }
148
        if ($this->use_permission == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
149
            if (empty($this->topic_id)) {
150
                $this->topic_id = $this->db->getInsertId();
151
            }
152
            $xt            = new XoopsTree($this->table, 'topic_id', 'topic_pid');
153
            $parent_topics = $xt->getAllParentId($this->topic_id);
154
            if (!empty($this->m_groups) && \is_array($this->m_groups)) {
0 ignored issues
show
Bug Best Practice introduced by
The property m_groups does not exist on XoopsTopic. Did you maybe forget to declare it?
Loading history...
155
                foreach ($this->m_groups as $m_g) {
156
                    $moderate_topics = XoopsPerms::getPermitted($this->mid, 'ModInTopic', $m_g);
0 ignored issues
show
Bug introduced by
The type XoopsPerms 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...
157
                    $add             = true;
158
                    // only grant this permission when the group has this permission in all parent topics of the created topic
159
                    foreach ($parent_topics as $p_topic) {
160
                        if (!in_array($p_topic, $moderate_topics)) {
161
                            $add = false;
162
                            continue;
163
                        }
164
                    }
165
                    if ($add == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
166
                        $xp = new XoopsPerms();
167
                        $xp->setModuleId($this->mid);
168
                        $xp->setName('ModInTopic');
169
                        $xp->setItemId($this->topic_id);
170
                        $xp->store();
171
                        $xp->addGroup($m_g);
172
                    }
173
                }
174
            }
175
            if (!empty($this->s_groups) && \is_array($this->s_groups)) {
0 ignored issues
show
Bug Best Practice introduced by
The property s_groups does not exist on XoopsTopic. Did you maybe forget to declare it?
Loading history...
176
                foreach ($s_groups as $s_g) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $s_groups seems to be never defined.
Loading history...
177
                    $submit_topics = XoopsPerms::getPermitted($this->mid, 'SubmitInTopic', $s_g);
178
                    $add           = true;
179
                    foreach ($parent_topics as $p_topic) {
180
                        if (!in_array($p_topic, $submit_topics)) {
181
                            $add = false;
182
                            continue;
183
                        }
184
                    }
185
                    if ($add == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
186
                        $xp = new XoopsPerms();
187
                        $xp->setModuleId($this->mid);
188
                        $xp->setName('SubmitInTopic');
189
                        $xp->setItemId($this->topic_id);
190
                        $xp->store();
191
                        $xp->addGroup($s_g);
192
                    }
193
                }
194
            }
195
            if (!empty($this->r_groups) && \is_array($this->r_groups)) {
0 ignored issues
show
Bug Best Practice introduced by
The property r_groups does not exist on XoopsTopic. Did you maybe forget to declare it?
Loading history...
196
                foreach ($r_groups as $r_g) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $r_groups seems to be never defined.
Loading history...
197
                    $read_topics = XoopsPerms::getPermitted($this->mid, 'ReadInTopic', $r_g);
198
                    $add         = true;
199
                    foreach ($parent_topics as $p_topic) {
200
                        if (!in_array($p_topic, $read_topics)) {
201
                            $add = false;
202
                            continue;
203
                        }
204
                    }
205
                    if ($add == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
206
                        $xp = new XoopsPerms();
207
                        $xp->setModuleId($this->mid);
208
                        $xp->setName('ReadInTopic');
209
                        $xp->setItemId($this->topic_id);
210
                        $xp->store();
211
                        $xp->addGroup($r_g);
212
                    }
213
                }
214
            }
215
        }
216
217
        return true;
218
    }
219
220
    public function delete()
221
    {
222
        $sql = sprintf('DELETE FROM %s WHERE topic_id = %u', $this->table, $this->topic_id);
223
        $this->db->query($sql);
224
    }
225
226
    /**
227
     * @return int
228
     */
229
    public function topic_id()
230
    {
231
        return $this->topic_id;
232
    }
233
234
    public function topic_pid()
235
    {
236
        return $this->topic_pid;
237
    }
238
239
    /**
240
     * @param string $format
241
     *
242
     * @return mixed
243
     */
244
    public function topic_title($format = 'S')
245
    {
246
        $myts = \MyTextSanitizer::getInstance();
247
        switch ($format) {
248
            case 'S':
249
            case 'E':
250
                $title = $myts->htmlSpecialChars($this->topic_title);
251
                break;
252
            case 'P':
253
            case 'F':
254
                $title = $myts->htmlSpecialChars($this->topic_title);
255
                break;
256
        }
257
258
        return $title;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $title does not seem to be defined for all execution paths leading up to this point.
Loading history...
259
    }
260
261
    /**
262
     * @param string $format
263
     *
264
     * @return mixed
265
     */
266
    public function topic_imgurl($format = 'S')
267
    {
268
        $myts = \MyTextSanitizer::getInstance();
269
        switch ($format) {
270
            case 'S':
271
            case 'E':
272
                $imgurl = $myts->htmlSpecialChars($this->topic_imgurl);
273
                break;
274
            case 'P':
275
            case 'F':
276
                $imgurl = $myts->htmlSpecialChars($this->topic_imgurl);
277
                break;
278
        }
279
280
        return $imgurl;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $imgurl does not seem to be defined for all execution paths leading up to this point.
Loading history...
281
    }
282
283
    public function prefix()
284
    {
285
        return $this->prefix ?? null;
286
    }
287
288
    /**
289
     * @return array
290
     */
291
    public function getFirstChildTopics()
292
    {
293
        $ret       = [];
294
        $xt        = new XoopsTree($this->table, 'topic_id', 'topic_pid');
295
        $topic_arr = $xt->getFirstChild($this->topic_id, 'topic_title');
296
        if (!empty($topic_arr) && \is_array($topic_arr)) {
297
            foreach ($topic_arr as $topic) {
298
                $ret[] = new XoopsTopic($this->table, $topic);
299
            }
300
        }
301
302
        return $ret;
303
    }
304
305
    /**
306
     * @return array
307
     */
308
    public function getAllChildTopics()
309
    {
310
        $ret       = [];
311
        $xt        = new XoopsTree($this->table, 'topic_id', 'topic_pid');
312
        $topic_arr = $xt->getAllChild($this->topic_id, 'topic_title');
313
        if (!empty($topic_arr) && \is_array($topic_arr)) {
314
            foreach ($topic_arr as $topic) {
315
                $ret[] = new XoopsTopic($this->table, $topic);
316
            }
317
        }
318
319
        return $ret;
320
    }
321
322
    /**
323
     * @return array
324
     */
325
    public function getChildTopicsTreeArray()
326
    {
327
        $ret       = [];
328
        $xt        = new XoopsTree($this->table, 'topic_id', 'topic_pid');
329
        $topic_arr = $xt->getChildTreeArray($this->topic_id, 'topic_title');
330
        if (!empty($topic_arr) && \is_array($topic_arr)) {
331
            foreach ($topic_arr as $topic) {
332
                $ret[] = new XoopsTopic($this->table, $topic);
333
            }
334
        }
335
336
        return $ret;
337
    }
338
339
    /**
340
     * @param int    $none
341
     * @param        $seltopic
342
     * @param string $selname
343
     * @param string $onchange
344
     */
345
    public function makeTopicSelBox($none = 0, $seltopic = -1, $selname = '', $onchange = '')
346
    {
347
        $xt = new XoopsTree($this->table, 'topic_id', 'topic_pid');
348
        if ($seltopic != -1) {
349
            $xt->makeMySelBox('topic_title', 'topic_title', $seltopic, $none, $selname, $onchange);
350
        } elseif (!empty($this->topic_id)) {
351
            $xt->makeMySelBox('topic_title', 'topic_title', $this->topic_id, $none, $selname, $onchange);
352
        } else {
353
            $xt->makeMySelBox('topic_title', 'topic_title', 0, $none, $selname, $onchange);
354
        }
355
    }
356
357
    //generates nicely formatted linked path from the root id to a given id
358
    /**
359
     * @param $funcURL
360
     *
361
     * @return string
362
     */
363
    public function getNiceTopicPathFromId($funcURL)
364
    {
365
        $xt  = new XoopsTree($this->table, 'topic_id', 'topic_pid');
366
        $ret = $xt->getNicePathFromId($this->topic_id, 'topic_title', $funcURL);
367
368
        return $ret;
369
    }
370
371
    /**
372
     * @return array
373
     */
374
    public function getAllChildTopicsId()
375
    {
376
        $xt  = new XoopsTree($this->table, 'topic_id', 'topic_pid');
377
        $ret = $xt->getAllChildId($this->topic_id, 'topic_title');
378
379
        return $ret;
380
    }
381
382
    /**
383
     * @return array
384
     */
385
    public function getTopicsList()
386
    {
387
        $sql = 'SELECT topic_id, topic_pid, topic_title FROM ' . $this->table;
388
        $result = $this->db->query($sql);
389
        if (!$this->db->isResultSet($result)) {
390
            throw new \RuntimeException(
391
                \sprintf(_DB_QUERY_ERROR, $sql) . $this->db->error(),
392
                E_USER_ERROR,
393
            );
394
        }
395
        $ret    = [];
396
        $myts   = \MyTextSanitizer::getInstance();
397
        while (false !== ($myrow = $this->db->fetchArray($result))) {
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::fetchArray() does only seem to accept mysqli_result, 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

397
        while (false !== ($myrow = $this->db->fetchArray(/** @scrutinizer ignore-type */ $result))) {
Loading history...
398
            $ret[$myrow['topic_id']] = ['title' => $myts->htmlSpecialChars($myrow['topic_title']), 'pid' => $myrow['topic_pid']];
399
        }
400
401
        return $ret;
402
    }
403
404
    /**
405
     * @param $pid
406
     * @param $title
407
     *
408
     * @return bool
409
     */
410
    public function topicExists($pid, $title)
411
    {
412
        $sql = 'SELECT COUNT(*) from ' . $this->table . ' WHERE topic_pid = ' . (int) $pid . " AND topic_title = '" . trim($title) . "'";
413
        $result  = $this->db->query($sql);
414
        if (!$this->db->isResultSet($result)) {
415
            throw new \RuntimeException(
416
                \sprintf(_DB_QUERY_ERROR, $sql) . $this->db->error(),
417
                E_USER_ERROR,
418
            );
419
        }
420
        [$count] = $this->db->fetchRow($result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::fetchRow() does only seem to accept mysqli_result, 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

420
        [$count] = $this->db->fetchRow(/** @scrutinizer ignore-type */ $result);
Loading history...
421
        if ($count > 0) {
422
            return true;
423
        } else {
424
            return false;
425
        }
426
    }
427
}
428