Tree   A
last analyzed

Complexity

Total Complexity 42

Size/Duplication

Total Lines 324
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 142
c 0
b 0
f 0
dl 0
loc 324
rs 9.0399
wmc 42

11 Methods

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

How to fix   Complexity   

Complex Class

Complex classes like Tree 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 Tree, and based on these observations, apply Extract Interface, too.

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