Passed
Push — master ( 08b589...961b46 )
by Michael
08:46
created

Tree::getChildTreeArray()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 13
nc 6
nop 4
dl 0
loc 19
rs 9.8333
c 1
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Wflinks;
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
24
/**
25
 * Abstract base class for forms
26
 *
27
 * @author     Kazumi Ono <[email protected]>
28
 * @author     John Neill <[email protected]>
29
 * @copyright  copyright (c) XOOPS.org
30
 * @package    kernel
31
 * @subpackage XoopsTree
32
 * @access     public
33
 */
34
class Tree
35
{
36
    public $table; //table with parent-child structure
37
    public $id; //name of unique id for records in table $table
38
    public $pid; // name of parent id used in table $table
39
    public $order; //specifies the order of query results
40
    public $title; // name of a field in table $table which will be used when  selection box and paths are generated
41
    public $db;
42
43
    //constructor of class XoopsTree
44
    //sets the names of table, unique id, and parend id
45
46
    /**
47
     * WflinksXoopsTree constructor.
48
     * @param $table_name
49
     * @param $id_name
50
     * @param $pid_name
51
     */
52
    public function __construct($table_name, $id_name, $pid_name)
53
    {
54
        $trace = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 1);
0 ignored issues
show
Unused Code introduced by
The assignment to $trace is dead and can be removed.
Loading history...
55
        //        $GLOBALS['xoopsLogger']->addDeprecated("Class '" . __CLASS__ . "' is deprecated, check 'XoopsObjectTree' in tree.php" . ". Called from {$trace[0]['file']}line {$trace[0]['line']}");
56
        $this->db    = \XoopsDatabaseFactory::getDatabaseConnection();
57
        $this->table = $table_name;
58
        $this->id    = $id_name;
59
        $this->pid   = $pid_name;
60
    }
61
62
    // returns an array of first child objects for a given id($sel_id)
63
64
    /**
65
     * @param        $sel_id
66
     * @param string $order
67
     * @return array
68
     */
69
    public function getFirstChild($sel_id, $order = '')
70
    {
71
        $sel_id = (int)$sel_id;
72
        $arr    = [];
73
        $sql    = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . ' ';
74
        if ('' != $order) {
75
            $sql .= " ORDER BY $order";
76
        }
77
        $result = $this->db->query($sql);
78
        $count  = $this->db->getRowsNum($result);
79
        if (0 == $count) {
80
            return $arr;
81
        }
82
        while (false !== ($myrow = $this->db->fetchArray($result))) {
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
    /**
92
     * @param $sel_id
93
     * @return array
94
     */
95
    public function getFirstChildId($sel_id)
96
    {
97
        $sel_id  = (int)$sel_id;
98
        $idarray = [];
99
        $result  = $this->db->query('SELECT ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . '');
100
        $count   = $this->db->getRowsNum($result);
101
        if (0 == $count) {
102
            return $idarray;
103
        }
104
        while (list($id) = $this->db->fetchRow($result)) {
105
            $idarray[] = $id;
106
        }
107
108
        return $idarray;
109
    }
110
111
    //returns an array of ALL child ids for a given id($sel_id)
112
113
    /**
114
     * @param        $sel_id
115
     * @param string $order
116
     * @param array  $idarray
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
     * @return array
146
     */
147
    public function getAllParentId($sel_id, $order = '', $idarray = [])
148
    {
149
        $sel_id = (int)$sel_id;
150
        $sql    = 'SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . $sel_id . '';
151
        if ('' !== $order) {
152
            $sql .= " ORDER BY $order";
153
        }
154
        $result = $this->db->query($sql);
155
        list($r_id) = $this->db->fetchRow($result);
156
        if (0 == $r_id) {
157
            return $idarray;
158
        }
159
        $idarray[] = $r_id;
160
        $idarray   = $this->getAllParentId($r_id, $order, $idarray);
161
162
        return $idarray;
163
    }
164
165
    //generates path from the root id to a given id($sel_id)
166
    // the path is delimetered with "/"
167
168
    /**
169
     * @param        $sel_id
170
     * @param        $title
171
     * @param string $path
172
     * @return string
173
     */
174
    public function getPathFromId($sel_id, $title, $path = '')
175
    {
176
        $sel_id = (int)$sel_id;
177
        $result = $this->db->query('SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id");
178
        if (0 == $this->db->getRowsNum($result)) {
179
            return $path;
180
        }
181
        list($parentid, $name) = $this->db->fetchRow($result);
182
        $myts = \MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $myts is dead and can be removed.
Loading history...
183
        $name = htmlspecialchars($name);
184
        $path = '/' . $name . $path . '';
185
        if (0 == $parentid) {
186
            return $path;
187
        }
188
        $path = $this->getPathFromId($parentid, $title, $path);
189
190
        return $path;
191
    }
192
193
    //makes a nicely ordered selection box
194
    //$preset_id is used to specify a preselected item
195
    //set $none to 1 to add a option with value 0
196
197
    /**
198
     * @param        $title
199
     * @param string $order
200
     * @param int    $preset_id
201
     * @param int    $none
202
     * @param string $sel_name
203
     * @param string $onchange
204
     */
205
    public function makeMySelBox($title, $order = '', $preset_id = 0, $none = 0, $sel_name = '', $onchange = '')
206
    {
207
        if ('' === $sel_name) {
208
            $sel_name = $this->id;
209
        }
210
        $myts = \MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $myts is dead and can be removed.
Loading history...
211
        echo "<select name='" . $sel_name . "'";
212
        if ('' !== $onchange) {
213
            echo " onchange='" . $onchange . "'";
214
        }
215
        echo ">\n";
216
        $sql = 'SELECT ' . $this->id . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=0';
217
        if ('' !== $order) {
218
            $sql .= " ORDER BY $order";
219
        }
220
        $result = $this->db->query($sql);
221
        if ($none) {
222
            echo "<option value='0'>----</option>\n";
223
        }
224
        while (list($catid, $name) = $this->db->fetchRow($result)) {
225
            $sel = '';
226
            if ($catid == $preset_id) {
227
                $sel = ' selected';
228
            }
229
            echo "<option value='$catid'$sel>$name</option>\n";
230
            $sel = '';
231
            $arr = $this->getChildTreeArray($catid, $order);
232
            foreach ($arr as $option) {
233
                $option['prefix'] = \str_replace('.', '--', $option['prefix']);
234
                $catpath          = $option['prefix'] . '&nbsp;' . htmlspecialchars($option[$title]);
235
                if ($option[$this->id] == $preset_id) {
236
                    $sel = ' selected';
237
                }
238
                echo "<option value='" . $option[$this->id] . "'$sel>$catpath</option>\n";
239
                $sel = '';
240
            }
241
        }
242
        echo "</select>\n";
243
    }
244
245
    //generates nicely formatted linked path from the root id to a given id
246
247
    /**
248
     * @param        $sel_id
249
     * @param        $title
250
     * @param        $funcURL
251
     * @param string $path
252
     * @return string
253
     */
254
    public function getNicePathFromId($sel_id, $title, $funcURL, $path = '')
255
    {
256
        $path   = !empty($path) ? '&nbsp;:&nbsp;' . $path : $path;
257
        $sel_id = (int)$sel_id;
258
        $sql    = 'SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id";
259
        $result = $this->db->query($sql);
260
        if (0 == $this->db->getRowsNum($result)) {
261
            return $path;
262
        }
263
        list($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);
266
        $path = "<a href='" . $funcURL . '&amp;' . $this->id . '=' . $sel_id . "'>" . $name . '</a>' . $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        $sel_id
280
     * @param string $path
281
     * @return string
282
     */
283
    public function getIdPathFromId($sel_id, $path = '')
284
    {
285
        $sel_id = (int)$sel_id;
286
        $result = $this->db->query('SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id");
287
        if (0 == $this->db->getRowsNum($result)) {
288
            return $path;
289
        }
290
        list($parentid) = $this->db->fetchRow($result);
291
        $path = '/' . $sel_id . $path . '';
292
        if (0 == $parentid) {
293
            return $path;
294
        }
295
        $path = $this->getIdPathFromId($parentid, $path);
296
297
        return $path;
298
    }
299
300
    /**
301
     * Enter description here...
302
     *
303
     * @param int|unknown_type    $sel_id
304
     * @param string|unknown_type $order
0 ignored issues
show
Bug introduced by
The type XoopsModules\Wflinks\unknown_type 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...
305
     * @param array|unknown_type  $parray
306
     * @return mixed
307
     */
308
    public function getAllChild($sel_id = 0, $order = '', $parray = [])
309
    {
310
        $sel_id = (int)$sel_id;
311
        $sql    = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . ' ';
312
        if ('' != $order) {
313
            $sql .= " ORDER BY $order";
314
        }
315
        $result = $this->db->query($sql);
316
        $count  = $this->db->getRowsNum($result);
317
        if (0 == $count) {
318
            return $parray;
319
        }
320
        while (false !== ($row = $this->db->fetchArray($result))) {
321
            $parray[] = $row;
322
            $parray   = $this->getAllChild($row[$this->id], $order, $parray);
323
        }
324
325
        return $parray;
326
    }
327
328
    /**
329
     * Enter description here...
330
     *
331
     * @param int|unknown_type    $sel_id
332
     * @param string|unknown_type $order
333
     * @param array|unknown_type  $parray
334
     * @param string|unknown_type $r_prefix
335
     * @return mixed
336
     */
337
    public function getChildTreeArray($sel_id = 0, $order = '', $parray = [], $r_prefix = '')
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
        $count  = $this->db->getRowsNum($result);
346
        if (0 == $count) {
347
            return $parray;
348
        }
349
        while (false !== ($row = $this->db->fetchArray($result))) {
350
            $row['prefix'] = $r_prefix . '.';
351
            $parray[]      = $row;
352
            $parray        = $this->getChildTreeArray($row[$this->id], $order, $parray, $row['prefix']);
353
        }
354
355
        return $parray;
356
    }
357
}
358