Passed
Pull Request — master (#1301)
by Michael
05:46
created

XoopsTree   B

Complexity

Total Complexity 52

Size/Duplication

Total Lines 360
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 172
c 0
b 0
f 0
dl 0
loc 360
rs 7.44
wmc 52

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getFirstChild() 0 21 5
A getAllChildId() 0 21 5
A getFirstChildId() 0 18 4
A getIdPathFromId() 0 20 4
A getPathFromId() 0 22 4
A getAllParentId() 0 20 4
A getChildTreeArray() 0 22 5
C makeMySelBox() 0 41 10
A getAllChild() 0 21 5
A getNicePathFromId() 0 23 5

How to fix   Complexity   

Complex Class

Complex classes like XoopsTree often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use XoopsTree, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * XOOPS tree handler
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-2016 XOOPS Project (www.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
 */
18
19
defined('XOOPS_ROOT_PATH') || exit('Restricted access');
20
21
/**
22
 * Abstract base class for forms
23
 *
24
 * @author              Kazumi Ono <[email protected]>
25
 * @author              John Neill <[email protected]>
26
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
27
 * @package             kernel
28
 * @subpackage          XoopsTree
29
 * @access              public
30
 */
31
class XoopsTree
32
{
33
    public $table; //table with parent-child structure
34
    public $id; //name of unique id for records in table $table
35
    public $pid; // name of parent id used in table $table
36
    public $order; //specifies the order of query results
37
    public $title; // name of a field in table $table which will be used when  selection box and paths are generated
38
    /**
39
     * @var \XoopsMySQLDatabase
40
     */
41
    public $db;
42
43
    //constructor of class XoopsTree
44
    //sets the names of table, unique id, and parend id
45
    /**
46
     * @param $table_name
47
     * @param $id_name
48
     * @param $pid_name
49
     */
50
    public function __construct($table_name, $id_name, $pid_name)
51
    {
52
        $GLOBALS['xoopsLogger']->addDeprecated("Class '" . __CLASS__ . "' is deprecated, check 'XoopsObjectTree' in tree.php");
53
        $this->db = XoopsDatabaseFactory::getDatabaseConnection();
54
        $this->table = $table_name;
55
        $this->id    = $id_name;
56
        $this->pid   = $pid_name;
57
    }
58
59
    // returns an array of first child objects for a given id($sel_id)
60
    /**
61
     * @param        $sel_id
62
     * @param string $order
63
     *
64
     * @return array
65
     */
66
    public function getFirstChild($sel_id, $order = '')
67
    {
68
        $sel_id = (int)$sel_id;
69
        $arr    = array();
70
        $sql    = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . '';
71
        if ($order != '') {
72
            $sql .= " ORDER BY $order";
73
        }
74
        $result = $this->db->query($sql);
75
        if (!$this->db->isResultSet($result)) {
76
            \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR);
77
        }
78
        $count  = $this->db->getRowsNum($result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::getRowsNum() 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

78
        $count  = $this->db->getRowsNum(/** @scrutinizer ignore-type */ $result);
Loading history...
79
        if ($count == 0) {
80
            return $arr;
81
        }
82
        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

82
        while (false !== ($myrow = $this->db->fetchArray(/** @scrutinizer ignore-type */ $result))) {
Loading history...
83
            $arr[] = $myrow;
84
        }
85
86
        return $arr;
87
    }
88
89
    // returns an array of all FIRST child ids of a given id($sel_id)
90
    /**
91
     * @param $sel_id
92
     *
93
     * @return array
94
     */
95
    public function getFirstChildId($sel_id)
96
    {
97
        $sel_id  = (int)$sel_id;
98
        $idarray = array();
99
        $sql  = 'SELECT ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . '';
100
        $result  = $this->db->query($sql);
101
        if (!$this->db->isResultSet($result)) {
102
            \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR);
103
        }
104
        $count   = $this->db->getRowsNum($result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::getRowsNum() 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

104
        $count   = $this->db->getRowsNum(/** @scrutinizer ignore-type */ $result);
Loading history...
105
        if ($count == 0) {
106
            return $idarray;
107
        }
108
        while (false !== (list($id) = $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

108
        while (false !== (list($id) = $this->db->fetchRow(/** @scrutinizer ignore-type */ $result))) {
Loading history...
109
            $idarray[] = $id;
110
        }
111
112
        return $idarray;
113
    }
114
115
    //returns an array of ALL child ids for a given id($sel_id)
116
    /**
117
     * @param        $sel_id
118
     * @param string $order
119
     * @param array  $idarray
120
     *
121
     * @return array
122
     */
123
    public function getAllChildId($sel_id, $order = '', $idarray = array())
124
    {
125
        $sel_id = (int)$sel_id;
126
        $sql    = 'SELECT ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . '';
127
        if ($order != '') {
128
            $sql .= " ORDER BY $order";
129
        }
130
        $result = $this->db->query($sql);
131
        if (!$this->db->isResultSet($result)) {
132
            \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR);
133
        }
134
        $count  = $this->db->getRowsNum($result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::getRowsNum() 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

134
        $count  = $this->db->getRowsNum(/** @scrutinizer ignore-type */ $result);
Loading history...
135
        if ($count == 0) {
136
            return $idarray;
137
        }
138
        while (false !== (list($r_id) = $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

138
        while (false !== (list($r_id) = $this->db->fetchRow(/** @scrutinizer ignore-type */ $result))) {
Loading history...
139
            $idarray[] = $r_id;
140
            $idarray   = $this->getAllChildId($r_id, $order, $idarray);
141
        }
142
143
        return $idarray;
144
    }
145
146
    //returns an array of ALL parent ids for a given id($sel_id)
147
148
    /**
149
     * @param string|int $sel_id
150
     * @param string     $order
151
     * @param array      $idarray
152
     *
153
     * @return array
154
     */
155
    public function getAllParentId($sel_id, $order = '', $idarray = array())
156
    {
157
        $sel_id = (int)$sel_id;
158
        $sql    = 'SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . $sel_id . '';
159
        if ($order != '') {
160
            $sql .= " ORDER BY $order";
161
        }
162
        $result  = $this->db->query($sql);
163
        if (!$this->db->isResultSet($result)) {
164
            \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR);
165
        }
166
        list($r_id) = $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

166
        list($r_id) = $this->db->fetchRow(/** @scrutinizer ignore-type */ $result);
Loading history...
167
        $r_id = (int)$r_id;
168
        if ($r_id === 0) {
169
            return $idarray;
170
        }
171
        $idarray[] = $r_id;
172
        $idarray   = $this->getAllParentId($r_id, $order, $idarray);
173
174
        return $idarray;
175
    }
176
177
    //generates path from the root id to a given id($sel_id)
178
    // the path is delimetered with "/"
179
    /**
180
     * @param string|int $sel_id
181
     * @param string     $title
182
     * @param string     $path
183
     *
184
     * @return string
185
     */
186
    public function getPathFromId($sel_id, $title, $path = '')
187
    {
188
        $sel_id = (int)$sel_id;
189
        $sql = 'SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id";
190
        $result  = $this->db->query($sql);
191
        if (!$this->db->isResultSet($result)) {
192
            \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR);
193
        }
194
        if ($this->db->getRowsNum($result) == 0) {
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::getRowsNum() 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

194
        if ($this->db->getRowsNum(/** @scrutinizer ignore-type */ $result) == 0) {
Loading history...
195
            return $path;
196
        }
197
        list($parentid, $name) = $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

197
        list($parentid, $name) = $this->db->fetchRow(/** @scrutinizer ignore-type */ $result);
Loading history...
198
        $myts = \MyTextSanitizer::getInstance();
199
        $parentid = (int)$parentid;
200
        $name = $myts->htmlSpecialChars($name);
201
        $path = '/' . $name . $path . '';
202
        if ($parentid === 0) {
203
            return $path;
204
        }
205
        $path = $this->getPathFromId($parentid, $title, $path);
206
207
        return $path;
208
    }
209
210
    //makes a nicely ordered selection box
211
    //$preset_id is used to specify a preselected item
212
    //set $none to 1 to add an option with value 0
213
    /**
214
     * @param        $title
215
     * @param string $order
216
     * @param int    $preset_id
217
     * @param int    $none
218
     * @param string $sel_name
219
     * @param string $onchange
220
     */
221
    public function makeMySelBox($title, $order = '', $preset_id = 0, $none = 0, $sel_name = '', $onchange = '')
222
    {
223
        if ($sel_name == '') {
224
            $sel_name = $this->id;
225
        }
226
        $myts = \MyTextSanitizer::getInstance();
227
        echo "<select name='" . $sel_name . "'";
228
        if ($onchange != '') {
229
            echo " onchange='" . $onchange . "'";
230
        }
231
        echo ">\n";
232
        $sql = 'SELECT ' . $this->id . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=0';
233
        if ($order != '') {
234
            $sql .= " ORDER BY $order";
235
        }
236
        $result = $this->db->query($sql);
237
        if (!$this->db->isResultSet($result)) {
238
            \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR);
239
        }
240
        if ($none) {
241
            echo "<option value='0'>----</option>\n";
242
        }
243
        while (false !== (list($catid, $name) = $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

243
        while (false !== (list($catid, $name) = $this->db->fetchRow(/** @scrutinizer ignore-type */ $result))) {
Loading history...
244
            $sel = '';
245
            if ($catid == $preset_id) {
246
                $sel = " selected";
247
            }
248
            echo "<option value='$catid'$sel>$name</option>\n";
249
            $sel = '';
250
            $arr = $this->getChildTreeArray($catid, $order);
251
            foreach ($arr as $option) {
252
                $option['prefix'] = str_replace('.', '--', $option['prefix']);
253
                $catpath          = $option['prefix'] . '&nbsp;' . $myts->htmlSpecialChars($option[$title]);
254
                if ($option[$this->id] == $preset_id) {
255
                    $sel = " selected";
256
                }
257
                echo "<option value='" . $option[$this->id] . "'$sel>$catpath</option>\n";
258
                $sel = '';
259
            }
260
        }
261
        echo "</select>\n";
262
    }
263
264
    //generates nicely formatted linked path from the root id to a given id
265
    /**
266
     * @param string|int    $sel_id
267
     * @param string $title
268
     * @param string $funcURL
269
     * @param string $path
270
     *
271
     * @return string
272
     */
273
    public function getNicePathFromId($sel_id, $title, $funcURL, $path = '')
274
    {
275
        $path   = !empty($path) ? '&nbsp;:&nbsp;' . $path : $path;
276
        $sel_id = (int)$sel_id;
277
        $sql    = 'SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id";
278
         $result  = $this->db->query($sql);
279
        if (!$this->db->isResultSet($result)) {
280
            \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR);
281
        }
282
        if ($this->db->getRowsNum($result) == 0) {
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::getRowsNum() 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

282
        if ($this->db->getRowsNum(/** @scrutinizer ignore-type */ $result) == 0) {
Loading history...
283
            return $path;
284
        }
285
        list($parentid, $name) = $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

285
        list($parentid, $name) = $this->db->fetchRow(/** @scrutinizer ignore-type */ $result);
Loading history...
286
        $myts = \MyTextSanitizer::getInstance();
287
        $name = $myts->htmlSpecialChars($name);
288
        $parentid = (int)$parentid;
289
        $path = "<a href='" . $funcURL . '&amp;' . $this->id . '=' . $sel_id . "'>" . $name . '</a>' . $path . '';
290
        if ($parentid === 0) {
291
            return $path;
292
        }
293
        $path = $this->getNicePathFromId($parentid, $title, $funcURL, $path);
294
295
        return $path;
296
    }
297
298
    //generates id path from the root id to a given id
299
    // the path is delimetered with "/"
300
    /**
301
     * @param string|int $sel_id
302
     * @param string     $path
303
     *
304
     * @return string
305
     */
306
    public function getIdPathFromId($sel_id, $path = '')
307
    {
308
        $sel_id = (int)$sel_id;
309
        $sql    = 'SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id";
310
        $result = $this->db->query($sql);
311
        if (!$this->db->isResultSet($result)) {
312
            \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR);
313
        }
314
        if ($this->db->getRowsNum($result) == 0) {
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::getRowsNum() 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

314
        if ($this->db->getRowsNum(/** @scrutinizer ignore-type */ $result) == 0) {
Loading history...
315
            return $path;
316
        }
317
        list($parentid) = $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

317
        list($parentid) = $this->db->fetchRow(/** @scrutinizer ignore-type */ $result);
Loading history...
318
        $path = '/' . $sel_id . $path . '';
319
        $parentid = (int)$parentid;
320
        if ($parentid === 0) {
321
            return $path;
322
        }
323
        $path = $this->getIdPathFromId($parentid, $path);
324
325
        return $path;
326
    }
327
328
    /**
329
     * Enter description here...
330
     *
331
     * @param int|mixed    $sel_id
332
     * @param string|mixed $order
333
     * @param array|mixed  $parray
334
     *
335
     * @return mixed
336
     */
337
    public function getAllChild($sel_id = 0, $order = '', $parray = array())
338
    {
339
        $sel_id = (int)$sel_id;
340
        $sql    = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . '';
341
        if ($order != '') {
342
            $sql .= " ORDER BY $order";
343
        }
344
        $result = $this->db->query($sql);
345
        if (!$this->db->isResultSet($result)) {
346
            \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR);
347
        }
348
        $count  = $this->db->getRowsNum($result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::getRowsNum() 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

348
        $count  = $this->db->getRowsNum(/** @scrutinizer ignore-type */ $result);
Loading history...
349
        if ($count == 0) {
350
            return $parray;
351
        }
352
        while (false !== ($row = $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

352
        while (false !== ($row = $this->db->fetchArray(/** @scrutinizer ignore-type */ $result))) {
Loading history...
353
            $parray[] = $row;
354
            $parray   = $this->getAllChild($row[$this->id], $order, $parray);
355
        }
356
357
        return $parray;
358
    }
359
360
    /**
361
     * Enter description here...
362
     *
363
     * @param  int|mixed    $sel_id
364
     * @param  string|mixed $order
365
     * @param  array|mixed  $parray
366
     * @param  string|mixed $r_prefix
367
     * @return mixed
368
     */
369
    public function getChildTreeArray($sel_id = 0, $order = '', $parray = array(), $r_prefix = '')
370
    {
371
        $sel_id = (int)$sel_id;
372
        $sql    = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . '';
373
        if ($order != '') {
374
            $sql .= " ORDER BY $order";
375
        }
376
        $result = $this->db->query($sql);
377
        if (!$this->db->isResultSet($result)) {
378
            \trigger_error("Query Failed! SQL: $sql- Error: " . $this->db->error(), E_USER_ERROR);
379
        }
380
        $count  = $this->db->getRowsNum($result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::getRowsNum() 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

380
        $count  = $this->db->getRowsNum(/** @scrutinizer ignore-type */ $result);
Loading history...
381
        if ($count == 0) {
382
            return $parray;
383
        }
384
        while (false !== ($row = $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

384
        while (false !== ($row = $this->db->fetchArray(/** @scrutinizer ignore-type */ $result))) {
Loading history...
385
            $row['prefix'] = $r_prefix . '.';
386
            $parray[]      = $row;
387
            $parray        = $this->getChildTreeArray($row[$this->id], $order, $parray, $row['prefix']);
388
        }
389
390
        return $parray;
391
    }
392
}
393