Passed
Pull Request — master (#27)
by Michael
29:26 queued 12:14
created

XoopsTopic::makeTopic()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 2
nc 2
nop 1
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\News;
4
5
/**
6
 * XOOPS news topic
7
 *
8
 * You may not change or alter any portion of this comment or credits
9
 * of supporting developers from this source code or any supporting source code
10
 * which is considered copyrighted (c) material of the original comment or credit authors.
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
 *
15
 * @copyright       XOOPS Project (https://xoops.org)
16
 * @license         GNU GPL 2 (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
17
 * @since           2.0.0
18
 * @author          Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/
19
 * @deprecated
20
 */
21
22
//$GLOBALS['xoopsLogger']->addDeprecated("'/class/xoopstopic.php' is deprecated since XOOPS 2.5.4, please create your own class instead.");
23
24
// require_once XOOPS_ROOT_PATH . '/modules/news/class/xoopstree.php';
25
26
use MyTextSanitizer;
27
use XoopsDatabaseFactory;
28
use XoopsModules\News;
29
use XoopsPerms;
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...
30
31
/**
32
 * Class XoopsTopic
33
 */
34
class XoopsTopic
35
{
36
    public $table;
37
    public $topic_id;
38
    public $topic_pid;
39
    public $topic_title;
40
    public $topic_imgurl;
41
    public $prefix; // only used in topic tree
42
    public $use_permission = false;
43
    public $mid; // module id used for setting permission
44
45
    /**
46
     * @param     $table
47
     * @param int $topicid
48
     */
49
    public function __construct($table, $topicid = 0)
50
    {
51
        /** @var \XoopsMySQLDatabase $db */
52
        $this->db    = XoopsDatabaseFactory::getDatabaseConnection();
0 ignored issues
show
Bug Best Practice introduced by
The property db does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
53
        $this->table = $table;
54
        if (\is_array($topicid)) {
0 ignored issues
show
introduced by
The condition is_array($topicid) is always false.
Loading history...
55
            $this->makeTopic($topicid);
56
        } elseif (0 != $topicid) {
57
            $this->getTopic((int)$topicid);
58
        } else {
59
            $this->topic_id = $topicid;
60
        }
61
    }
62
63
    /**
64
     * @param $value
65
     */
66
    public function setTopicTitle($value): void
67
    {
68
        $this->topic_title = $value;
69
    }
70
71
    /**
72
     * @param $value
73
     */
74
    public function setTopicImgurl($value): void
75
    {
76
        $this->topic_imgurl = $value;
77
    }
78
79
    /**
80
     * @param $value
81
     */
82
    public function setTopicPid($value): void
83
    {
84
        $this->topic_pid = $value;
85
    }
86
87
    /**
88
     * @param $topicid
89
     */
90
    public function getTopic($topicid): void
91
    {
92
        $topicid = (int)$topicid;
93
        $sql     = 'SELECT * FROM ' . $this->table . ' WHERE topic_id=' . $topicid;
94
        $array   = $this->db->fetchArray($this->db->query($sql));
95
        $this->makeTopic($array);
96
    }
97
98
    /**
99
     * @param $array
100
     */
101
    public function makeTopic($array): void
102
    {
103
        foreach ($array as $key => $value) {
104
            $this->$key = $value;
105
        }
106
    }
107
108
    /**
109
     * @param $mid
110
     */
111
    public function usePermission($mid): void
112
    {
113
        $this->mid            = $mid;
114
        $this->use_permission = true;
115
    }
116
117
    /**
118
     * @return bool
119
     */
120
    public function store()
121
    {
122
        $myts   = MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $myts is dead and can be removed.
Loading history...
123
        $title  = '';
124
        $imgurl = '';
125
        if (isset($this->topic_title) && '' !== $this->topic_title) {
126
            $title = $GLOBALS['xoopsDB']->escape($this->topic_title);
127
        }
128
        if (isset($this->topic_imgurl) && '' !== $this->topic_imgurl) {
129
            $imgurl = $GLOBALS['xoopsDB']->escape($this->topic_imgurl);
130
        }
131
        if (!isset($this->topic_pid) || !\is_numeric($this->topic_pid)) {
132
            $this->topic_pid = 0;
133
        }
134
        if (empty($this->topic_id)) {
135
            $this->topic_id = $this->db->genId($this->table . '_topic_id_seq');
136
            $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);
137
        } else {
138
            $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);
139
        }
140
        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...
141
            \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR);
142
        }
143
        if ($this->use_permission) {
144
            if (empty($this->topic_id)) {
145
                $this->topic_id = $this->db->getInsertId();
146
            }
147
            $xt            = new \XoopsTree($this->table, 'topic_id', 'topic_pid');
148
            $parent_topics = $xt->getAllParentId($this->topic_id);
149
            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 XoopsModules\News\XoopsTopic. Did you maybe forget to declare it?
Loading history...
150
                foreach ($this->m_groups as $m_g) {
151
                    $moderate_topics = XoopsPerms::getPermitted($this->mid, 'ModInTopic', $m_g);
152
                    $add             = true;
153
                    // only grant this permission when the group has this permission in all parent topics of the created topic
154
                    foreach ($parent_topics as $p_topic) {
155
                        if (!\in_array($p_topic, $moderate_topics, true)) {
156
                            $add = false;
157
                            continue;
158
                        }
159
                    }
160
                    if ($add) {
161
                        $xp = new XoopsPerms();
162
                        $xp->setModuleId($this->mid);
163
                        $xp->setName('ModInTopic');
164
                        $xp->setItemId($this->topic_id);
165
                        $xp->store();
166
                        $xp->addGroup($m_g);
167
                    }
168
                }
169
            }
170
            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 XoopsModules\News\XoopsTopic. Did you maybe forget to declare it?
Loading history...
171
                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...
172
                    $submit_topics = XoopsPerms::getPermitted($this->mid, 'SubmitInTopic', $s_g);
173
                    $add           = true;
174
                    foreach ($parent_topics as $p_topic) {
175
                        if (!\in_array($p_topic, $submit_topics, true)) {
176
                            $add = false;
177
                            continue;
178
                        }
179
                    }
180
                    if ($add) {
181
                        $xp = new XoopsPerms();
182
                        $xp->setModuleId($this->mid);
183
                        $xp->setName('SubmitInTopic');
184
                        $xp->setItemId($this->topic_id);
185
                        $xp->store();
186
                        $xp->addGroup($s_g);
187
                    }
188
                }
189
            }
190
            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 XoopsModules\News\XoopsTopic. Did you maybe forget to declare it?
Loading history...
191
                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...
192
                    $read_topics = XoopsPerms::getPermitted($this->mid, 'ReadInTopic', $r_g);
193
                    $add         = true;
194
                    foreach ($parent_topics as $p_topic) {
195
                        if (!\in_array($p_topic, $read_topics, true)) {
196
                            $add = false;
197
                            continue;
198
                        }
199
                    }
200
                    if ($add) {
201
                        $xp = new XoopsPerms();
202
                        $xp->setModuleId($this->mid);
203
                        $xp->setName('ReadInTopic');
204
                        $xp->setItemId($this->topic_id);
205
                        $xp->store();
206
                        $xp->addGroup($r_g);
207
                    }
208
                }
209
            }
210
        }
211
212
        return true;
213
    }
214
215
    public function delete(): void
216
    {
217
        $sql = \sprintf('DELETE FROM `%s` WHERE topic_id = %u', $this->table, $this->topic_id);
218
        $this->db->query($sql);
219
    }
220
221
    /**
222
     * @return int
223
     */
224
    public function topic_id()
225
    {
226
        return $this->topic_id;
227
    }
228
229
    public function topic_pid()
230
    {
231
        return $this->topic_pid;
232
    }
233
234
    /**
235
     * @param string $format
236
     *
237
     * @return mixed
238
     */
239
    public function topic_title($format = 'S')
240
    {
241
        $myts = MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $myts is dead and can be removed.
Loading history...
242
        switch ($format) {
243
            case 'S':
244
            case 'E':
245
                $title = \htmlspecialchars($this->topic_title, \ENT_QUOTES | \ENT_HTML5);
246
                break;
247
            case 'P':
248
            case 'F':
249
                $title = \htmlspecialchars($this->topic_title, \ENT_QUOTES | \ENT_HTML5);
250
                break;
251
        }
252
253
        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...
254
    }
255
256
    /**
257
     * @param string $format
258
     *
259
     * @return mixed
260
     */
261
    public function topic_imgurl($format = 'S')
262
    {
263
        $myts = MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $myts is dead and can be removed.
Loading history...
264
        switch ($format) {
265
            case 'S':
266
            case 'E':
267
                $imgurl = \htmlspecialchars($this->topic_imgurl, \ENT_QUOTES | \ENT_HTML5);
268
                break;
269
            case 'P':
270
            case 'F':
271
                $imgurl = \htmlspecialchars($this->topic_imgurl, \ENT_QUOTES | \ENT_HTML5);
272
                break;
273
        }
274
275
        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...
276
    }
277
278
    /**
279
     * @return null
280
     */
281
    public function prefix()
282
    {
283
        return $this->prefix ?? null;
284
    }
285
286
    /**
287
     * @return array
288
     */
289
    public function getFirstChildTopics()
290
    {
291
        $ret       = [];
292
        $xt        = new \XoopsTree($this->table, 'topic_id', 'topic_pid');
293
        $topic_arr = $xt->getFirstChild($this->topic_id, 'topic_title');
294
        if (\is_array($topic_arr) && \count($topic_arr)) {
295
            foreach ($topic_arr as $topic) {
296
                $ret[] = new self($this->table, $topic);
297
            }
298
        }
299
300
        return $ret;
301
    }
302
303
    /**
304
     * @return array
305
     */
306
    public function getAllChildTopics()
307
    {
308
        $ret       = [];
309
        $xt        = new \XoopsTree($this->table, 'topic_id', 'topic_pid');
310
        $topic_arr = $xt->getAllChild($this->topic_id, 'topic_title');
311
        if (\is_array($topic_arr) && \count($topic_arr)) {
312
            foreach ($topic_arr as $topic) {
313
                $ret[] = new self($this->table, $topic);
314
            }
315
        }
316
317
        return $ret;
318
    }
319
320
    /**
321
     * @return array
322
     */
323
    public function getChildTopicsTreeArray()
324
    {
325
        $ret       = [];
326
        $xt        = new \XoopsTree($this->table, 'topic_id', 'topic_pid');
327
        $topic_arr = $xt->getChildTreeArray($this->topic_id, 'topic_title');
328
        if (\is_array($topic_arr) && \count($topic_arr)) {
329
            foreach ($topic_arr as $topic) {
330
                $ret[] = new self($this->table, $topic);
331
            }
332
        }
333
334
        return $ret;
335
    }
336
337
    /**
338
     * @param int    $none
339
     * @param        $seltopic
340
     * @param string $selname
341
     * @param string $onchange
342
     */
343
    public function makeTopicSelBox($none = 0, $seltopic = -1, $selname = '', $onchange = ''): void
344
    {
345
        $xt = new \XoopsModules\News\ObjectTree($this->table, 'topic_id', 'topic_pid');
346
        if (-1 != $seltopic) {
347
            $xt->makeMySelBox('topic_title', 'topic_title', $seltopic, $none, $selname, $onchange);
0 ignored issues
show
Bug introduced by
The method makeMySelBox() does not exist on XoopsModules\News\ObjectTree. Did you maybe mean makeSelBox()? ( Ignorable by Annotation )

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

347
            $xt->/** @scrutinizer ignore-call */ 
348
                 makeMySelBox('topic_title', 'topic_title', $seltopic, $none, $selname, $onchange);

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...
348
        } elseif (!empty($this->topic_id)) {
349
            $xt->makeMySelBox('topic_title', 'topic_title', $this->topic_id, $none, $selname, $onchange);
350
        } else {
351
            $xt->makeMySelBox('topic_title', 'topic_title', 0, $none, $selname, $onchange);
352
        }
353
    }
354
355
    //generates nicely formatted linked path from the root id to a given id
356
357
    /**
358
     * @param $funcURL
359
     *
360
     * @return mixed
361
     */
362
    public function getNiceTopicPathFromId($funcURL)
363
    {
364
        $xt  = new \XoopsModules\News\ObjectTree($this->table, 'topic_id', 'topic_pid');
365
        $ret = $xt->getNicePathFromId($this->topic_id, 'topic_title', $funcURL);
0 ignored issues
show
Bug introduced by
The method getNicePathFromId() does not exist on XoopsModules\News\ObjectTree. ( Ignorable by Annotation )

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

365
        /** @scrutinizer ignore-call */ 
366
        $ret = $xt->getNicePathFromId($this->topic_id, 'topic_title', $funcURL);

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...
366
367
        return $ret;
368
    }
369
370
    /**
371
     * @return mixed
372
     */
373
    public function getAllChildTopicsId()
374
    {
375
        $xt  = new \XoopsModules\News\ObjectTree($this->table, 'topic_id', 'topic_pid');
376
        $ret = $xt->getAllChildId($this->topic_id, 'topic_title');
0 ignored issues
show
Bug introduced by
The method getAllChildId() does not exist on XoopsModules\News\ObjectTree. Did you maybe mean getAllChild()? ( Ignorable by Annotation )

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

376
        /** @scrutinizer ignore-call */ 
377
        $ret = $xt->getAllChildId($this->topic_id, 'topic_title');

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...
377
378
        return $ret;
379
    }
380
381
    /**
382
     * @return array
383
     */
384
    public function getTopicsList()
385
    {
386
        $ret    = [];
387
        $result = $this->db->query('SELECT topic_id, topic_pid, topic_title FROM ' . $this->table);
388
        if ($result) {
389
            $myts = MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $myts is dead and can be removed.
Loading history...
390
            while (false !== ($myrow = $this->db->fetchArray($result))) {
391
                $ret[$myrow['topic_id']] = [
392
                    'title' => \htmlspecialchars($myrow['topic_title'], \ENT_QUOTES | \ENT_HTML5),
393
                    'pid'   => $myrow['topic_pid'],
394
                ];
395
            }
396
        }
397
398
        return $ret;
399
    }
400
401
    /**
402
     * @param $pid
403
     * @param $title
404
     *
405
     * @return bool
406
     */
407
    public function topicExists($pid, $title)
408
    {
409
        $sql = 'SELECT COUNT(*) FROM ' . $this->table . ' WHERE topic_pid = ' . (int)$pid . " AND topic_title = '" . \trim($title) . "'";
410
        $rs  = $this->db->query($sql);
411
        [$count] = $this->db->fetchRow($rs);
412
        if ($count > 0) {
413
            return true;
414
        }
415
416
        return false;
417
    }
418
}
419