Passed
Push — master ( 6d0bec...700d12 )
by Michael
05:09 queued 02:42
created

XoopsTree   A

Complexity

Total Complexity 42

Size/Duplication

Total Lines 330
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 42
eloc 146
c 2
b 0
f 0
dl 0
loc 330
rs 9.0399

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getIdPathFromId() 0 15 3
A getPathFromId() 0 17 3
A getAllParentId() 0 16 3
B makeMySelBox() 0 38 9
A getNicePathFromId() 0 19 4
A getChildTreeArray() 0 19 4
A __construct() 0 9 1
A getFirstChildId() 0 14 3
A getFirstChild() 0 18 4
A getAllChildId() 0 18 4
A getAllChild() 0 18 4

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
namespace XoopsModules\News;
4
5
/**
6
 * XOOPS tree handler
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
 * @package         kernel
18
 * @since           2.0.0
19
 * @author          Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/
20
 */
21
22
/**
23
 * Abstract base class for forms
24
 *
25
 * @author     Kazumi Ono <[email protected]>
26
 * @author     John Neill <[email protected]>
27
 * @copyright  copyright (c) XOOPS.org
28
 * @package    kernel
29
 * @subpackage XoopsTree
30
 * @access     public
31
 */
32
class XoopsTree
33
{
34
    public $table; //table with parent-child structure
35
    public $id; //name of unique id for records in table $table
36
    public $pid; // name of parent id used in table $table
37
    public $order; //specifies the order of query results
38
    public $title; // name of a field in table $table which will be used when  selection box and paths are generated
39
    public $db;
40
    //constructor of class XoopsTree
41
    //sets the names of table, unique id, and parend id
42
43
    /**
44
     * @param $table_name
45
     * @param $id_name
46
     * @param $pid_name
47
     */
48
    public function __construct($table_name, $id_name, $pid_name)
49
    {
50
        $trace = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 1);
51
        $GLOBALS['xoopsLogger']->addDeprecated("Class '" . __CLASS__ . "' is deprecated, check 'XoopsObjectTree' in tree.php" . ". Called from {$trace[0]['file']}line {$trace[0]['line']}");
52
        /** @var \XoopsMySQLDatabase $db */
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
    /**
62
     * @param        $sel_id
63
     * @param string $order
64
     *
65
     * @return array
66
     */
67
    public function getFirstChild($sel_id, $order = '')
68
    {
69
        $sel_id = (int)$sel_id;
70
        $arr    = [];
71
        $sql    = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . '';
72
        if ('' !== $order) {
73
            $sql .= " ORDER BY $order";
74
        }
75
        $result = $this->db->query($sql);
76
        $count  = $this->db->getRowsNum($result);
77
        if (0 == $count) {
78
            return $arr;
79
        }
80
        while (false !== ($myrow = $this->db->fetchArray($result))) {
81
            $arr[] = $myrow;
82
        }
83
84
        return $arr;
85
    }
86
87
    // returns an array of all FIRST child ids of a given id($sel_id)
88
89
    /**
90
     * @param $sel_id
91
     *
92
     * @return array
93
     */
94
    public function getFirstChildId($sel_id)
95
    {
96
        $sel_id  = (int)$sel_id;
97
        $idarray = [];
98
        $result  = $this->db->query('SELECT ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . '');
99
        $count   = $this->db->getRowsNum($result);
100
        if (0 == $count) {
101
            return $idarray;
102
        }
103
        while (list($id) = $this->db->fetchRow($result)) {
104
            $idarray[] = $id;
105
        }
106
107
        return $idarray;
108
    }
109
110
    //returns an array of ALL child ids for a given id($sel_id)
111
112
    /**
113
     * @param        $sel_id
114
     * @param string $order
115
     * @param array  $idarray
116
     *
117
     * @return array
118
     */
119
    public function getAllChildId($sel_id, $order = '', $idarray = [])
120
    {
121
        $sel_id = (int)$sel_id;
122
        $sql    = 'SELECT ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . '';
123
        if ('' !== $order) {
124
            $sql .= " ORDER BY $order";
125
        }
126
        $result = $this->db->query($sql);
127
        $count  = $this->db->getRowsNum($result);
128
        if (0 == $count) {
129
            return $idarray;
130
        }
131
        while (list($r_id) = $this->db->fetchRow($result)) {
132
            $idarray[] = $r_id;
133
            $idarray   = $this->getAllChildId($r_id, $order, $idarray);
134
        }
135
136
        return $idarray;
137
    }
138
139
    //returns an array of ALL parent ids for a given id($sel_id)
140
141
    /**
142
     * @param        $sel_id
143
     * @param string $order
144
     * @param array  $idarray
145
     *
146
     * @return array
147
     */
148
    public function getAllParentId($sel_id, $order = '', $idarray = [])
149
    {
150
        $sel_id = (int)$sel_id;
151
        $sql    = 'SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . $sel_id . '';
152
        if ('' !== $order) {
153
            $sql .= " ORDER BY $order";
154
        }
155
        $result = $this->db->query($sql);
156
        [$r_id] = $this->db->fetchRow($result);
157
        if (0 == $r_id) {
158
            return $idarray;
159
        }
160
        $idarray[] = $r_id;
161
        $idarray   = $this->getAllParentId($r_id, $order, $idarray);
162
163
        return $idarray;
164
    }
165
166
    //generates path from the root id to a given id($sel_id)
167
    // the path is delimetered with "/"
168
169
    /**
170
     * @param        $sel_id
171
     * @param        $title
172
     * @param string $path
173
     *
174
     * @return string
175
     */
176
    public function getPathFromId($sel_id, $title, $path = '')
177
    {
178
        $sel_id = (int)$sel_id;
179
        $result = $this->db->query('SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id");
180
        if (0 == $this->db->getRowsNum($result)) {
181
            return $path;
182
        }
183
        [$parentid, $name] = $this->db->fetchRow($result);
184
        $myts = \MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $myts is dead and can be removed.
Loading history...
185
        $name = htmlspecialchars($name, ENT_QUOTES | ENT_HTML5);
186
        $path = '/' . $name . $path . '';
187
        if (0 == $parentid) {
188
            return $path;
189
        }
190
        $path = $this->getPathFromId($parentid, $title, $path);
191
192
        return $path;
193
    }
194
195
    //makes a nicely ordered selection box
196
    //$preset_id is used to specify a preselected item
197
    //set $none to 1 to add a option with value 0
198
199
    /**
200
     * @param        $title
201
     * @param string $order
202
     * @param int    $preset_id
203
     * @param int    $none
204
     * @param string $sel_name
205
     * @param string $onchange
206
     */
207
    public function makeMySelBox($title, $order = '', $preset_id = 0, $none = 0, $sel_name = '', $onchange = '')
208
    {
209
        if ('' == $sel_name) {
210
            $sel_name = $this->id;
211
        }
212
        $myts = \MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $myts is dead and can be removed.
Loading history...
213
        echo "<select name='" . $sel_name . "'";
214
        if ('' !== $onchange) {
215
            echo " onchange='" . $onchange . "'";
216
        }
217
        echo ">\n";
218
        $sql = 'SELECT ' . $this->id . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=0';
219
        if ('' !== $order) {
220
            $sql .= " ORDER BY $order";
221
        }
222
        $result = $this->db->query($sql);
223
        if ($none) {
224
            echo "<option value='0'>----</option>\n";
225
        }
226
        while (list($catid, $name) = $this->db->fetchRow($result)) {
227
            $sel = '';
228
            if ($catid == $preset_id) {
229
                $sel = ' selected';
230
            }
231
            echo "<option value='$catid'$sel>$name</option>\n";
232
            $sel = '';
233
            $arr = $this->getChildTreeArray($catid, $order);
234
            foreach ($arr as $option) {
235
                $option['prefix'] = \str_replace('.', '--', $option['prefix']);
236
                $catpath          = $option['prefix'] . '&nbsp;' . htmlspecialchars($option[$title], ENT_QUOTES | ENT_HTML5);
237
                if ($option[$this->id] == $preset_id) {
238
                    $sel = ' selected';
239
                }
240
                echo "<option value='" . $option[$this->id] . "'$sel>$catpath</option>\n";
241
                $sel = '';
242
            }
243
        }
244
        echo "</select>\n";
245
    }
246
247
    //generates nicely formatted linked path from the root id to a given id
248
249
    /**
250
     * @param        $sel_id
251
     * @param        $title
252
     * @param        $funcURL
253
     * @param string $path
254
     *
255
     * @return string
256
     */
257
    public function getNicePathFromId($sel_id, $title, $funcURL, $path = '')
258
    {
259
        $path   = !empty($path) ? '&nbsp;:&nbsp;' . $path : $path;
260
        $sel_id = (int)$sel_id;
261
        $sql    = 'SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id";
262
        $result = $this->db->query($sql);
263
        if (0 == $this->db->getRowsNum($result)) {
264
            return $path;
265
        }
266
        [$parentid, $name] = $this->db->fetchRow($result);
267
        $myts = \MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $myts is dead and can be removed.
Loading history...
268
        $name = htmlspecialchars($name, ENT_QUOTES | ENT_HTML5);
269
        $path = "<a href='" . $funcURL . '&amp;' . $this->id . '=' . $sel_id . "'>" . $name . '</a>' . $path . '';
270
        if (0 == $parentid) {
271
            return $path;
272
        }
273
        $path = $this->getNicePathFromId($parentid, $title, $funcURL, $path);
274
275
        return $path;
276
    }
277
278
    //generates id path from the root id to a given id
279
    // the path is delimetered with "/"
280
281
    /**
282
     * @param        $sel_id
283
     * @param string $path
284
     *
285
     * @return string
286
     */
287
    public function getIdPathFromId($sel_id, $path = '')
288
    {
289
        $sel_id = (int)$sel_id;
290
        $result = $this->db->query('SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id");
291
        if (0 == $this->db->getRowsNum($result)) {
292
            return $path;
293
        }
294
        [$parentid] = $this->db->fetchRow($result);
295
        $path = '/' . $sel_id . $path . '';
296
        if (0 == $parentid) {
297
            return $path;
298
        }
299
        $path = $this->getIdPathFromId($parentid, $path);
300
301
        return $path;
302
    }
303
304
    /**
305
     * Enter description here...
306
     *
307
     * @param int|mixed    $sel_id
308
     * @param string|mixed $order
309
     * @param array|mixed  $parray
310
     *
311
     * @return mixed
312
     */
313
    public function getAllChild($sel_id = 0, $order = '', $parray = [])
314
    {
315
        $sel_id = (int)$sel_id;
316
        $sql    = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . '';
317
        if ('' !== $order) {
318
            $sql .= " ORDER BY $order";
319
        }
320
        $result = $this->db->query($sql);
321
        $count  = $this->db->getRowsNum($result);
322
        if (0 == $count) {
323
            return $parray;
324
        }
325
        while (false !== ($row = $this->db->fetchArray($result))) {
326
            $parray[] = $row;
327
            $parray   = $this->getAllChild($row[$this->id], $order, $parray);
328
        }
329
330
        return $parray;
331
    }
332
333
    /**
334
     * Enter description here...
335
     *
336
     * @param int|mixed    $sel_id
337
     * @param string|mixed $order
338
     * @param array|mixed  $parray
339
     * @param string|mixed $r_prefix
340
     *
341
     * @return mixed
342
     */
343
    public function getChildTreeArray($sel_id = 0, $order = '', $parray = [], $r_prefix = '')
344
    {
345
        $sel_id = (int)$sel_id;
346
        $sql    = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . '';
347
        if ('' !== $order) {
348
            $sql .= " ORDER BY $order";
349
        }
350
        $result = $this->db->query($sql);
351
        $count  = $this->db->getRowsNum($result);
352
        if (0 == $count) {
353
            return $parray;
354
        }
355
        while (false !== ($row = $this->db->fetchArray($result))) {
356
            $row['prefix'] = $r_prefix . '.';
357
            $parray[]      = $row;
358
            $parray        = $this->getChildTreeArray($row[$this->id], $order, $parray, $row['prefix']);
359
        }
360
361
        return $parray;
362
    }
363
}
364