Tree::makeMySelBox()   B
last analyzed

Complexity

Conditions 9
Paths 112

Size

Total Lines 38
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 28
nc 112
nop 6
dl 0
loc 38
rs 7.9555
c 0
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
 * 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 Tree
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
     * WflinksXoopsTree constructor.
45
     * @param $table_name
46
     * @param $id_name
47
     * @param $pid_name
48
     */
49
    public function __construct($table_name, $id_name, $pid_name)
50
    {
51
        $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...
52
        //        $GLOBALS['xoopsLogger']->addDeprecated("Class '" . __CLASS__ . "' is deprecated, check 'XoopsObjectTree' in tree.php" . ". Called from {$trace[0]['file']}line {$trace[0]['line']}");
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
     * @return array
65
     */
66
    public function getFirstChild($sel_id, $order = '')
67
    {
68
        $sel_id = (int)$sel_id;
69
        $arr    = [];
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
        $count  = $this->db->getRowsNum($result);
76
        if (0 == $count) {
77
            return $arr;
78
        }
79
        while (false !== ($myrow = $this->db->fetchArray($result))) {
80
            $arr[] = $myrow;
81
        }
82
83
        return $arr;
84
    }
85
86
    // returns an array of all FIRST child ids of a given id($sel_id)
87
88
    /**
89
     * @param $sel_id
90
     * @return array
91
     */
92
    public function getFirstChildId($sel_id)
93
    {
94
        $sel_id  = (int)$sel_id;
95
        $idarray = [];
96
        $result  = $this->db->query('SELECT ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . '');
97
        $count   = $this->db->getRowsNum($result);
98
        if (0 == $count) {
99
            return $idarray;
100
        }
101
        while (list($id) = $this->db->fetchRow($result)) {
102
            $idarray[] = $id;
103
        }
104
105
        return $idarray;
106
    }
107
108
    //returns an array of ALL child ids for a given id($sel_id)
109
110
    /**
111
     * @param        $sel_id
112
     * @param string $order
113
     * @param array  $idarray
114
     * @return array
115
     */
116
    public function getAllChildId($sel_id, $order = '', $idarray = [])
117
    {
118
        $sel_id = (int)$sel_id;
119
        $sql    = 'SELECT ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . '';
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($sel_id)
137
138
    /**
139
     * @param        $sel_id
140
     * @param string $order
141
     * @param array  $idarray
142
     * @return array
143
     */
144
    public function getAllParentId($sel_id, $order = '', $idarray = [])
145
    {
146
        $sel_id = (int)$sel_id;
147
        $sql    = 'SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . $sel_id . '';
148
        if ('' !== $order) {
149
            $sql .= " ORDER BY $order";
150
        }
151
        $result = $this->db->query($sql);
152
        list($r_id) = $this->db->fetchRow($result);
153
        if (0 == $r_id) {
154
            return $idarray;
155
        }
156
        $idarray[] = $r_id;
157
        $idarray   = $this->getAllParentId($r_id, $order, $idarray);
158
159
        return $idarray;
160
    }
161
162
    //generates path from the root id to a given id($sel_id)
163
    // the path is delimetered with "/"
164
165
    /**
166
     * @param        $sel_id
167
     * @param        $title
168
     * @param string $path
169
     * @return string
170
     */
171
    public function getPathFromId($sel_id, $title, $path = '')
172
    {
173
        $sel_id = (int)$sel_id;
174
        $result = $this->db->query('SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id");
175
        if (0 == $this->db->getRowsNum($result)) {
176
            return $path;
177
        }
178
        list($parentid, $name) = $this->db->fetchRow($result);
179
        $myts = \MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $myts is dead and can be removed.
Loading history...
180
        $name = htmlspecialchars($name, ENT_QUOTES | ENT_HTML5);
181
        $path = '/' . $name . $path . '';
182
        if (0 == $parentid) {
183
            return $path;
184
        }
185
        $path = $this->getPathFromId($parentid, $title, $path);
186
187
        return $path;
188
    }
189
190
    //makes a nicely ordered selection box
191
    //$preset_id is used to specify a preselected item
192
    //set $none to 1 to add a option with value 0
193
194
    /**
195
     * @param        $title
196
     * @param string $order
197
     * @param int    $preset_id
198
     * @param int    $none
199
     * @param string $sel_name
200
     * @param string $onchange
201
     */
202
    public function makeMySelBox($title, $order = '', $preset_id = 0, $none = 0, $sel_name = '', $onchange = '')
203
    {
204
        if ('' === $sel_name) {
205
            $sel_name = $this->id;
206
        }
207
        $myts = \MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $myts is dead and can be removed.
Loading history...
208
        echo "<select name='" . $sel_name . "'";
209
        if ('' !== $onchange) {
210
            echo " onchange='" . $onchange . "'";
211
        }
212
        echo ">\n";
213
        $sql = 'SELECT ' . $this->id . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=0';
214
        if ('' !== $order) {
215
            $sql .= " ORDER BY $order";
216
        }
217
        $result = $this->db->query($sql);
218
        if ($none) {
219
            echo "<option value='0'>----</option>\n";
220
        }
221
        while (list($catid, $name) = $this->db->fetchRow($result)) {
222
            $sel = '';
223
            if ($catid == $preset_id) {
224
                $sel = ' selected';
225
            }
226
            echo "<option value='$catid'$sel>$name</option>\n";
227
            $sel = '';
228
            $arr = $this->getChildTreeArray($catid, $order);
229
            foreach ($arr as $option) {
230
                $option['prefix'] = \str_replace('.', '--', $option['prefix']);
231
                $catpath          = $option['prefix'] . '&nbsp;' . htmlspecialchars($option[$title], ENT_QUOTES | ENT_HTML5);
232
                if ($option[$this->id] == $preset_id) {
233
                    $sel = ' selected';
234
                }
235
                echo "<option value='" . $option[$this->id] . "'$sel>$catpath</option>\n";
236
                $sel = '';
237
            }
238
        }
239
        echo "</select>\n";
240
    }
241
242
    //generates nicely formatted linked path from the root id to a given id
243
244
    /**
245
     * @param        $sel_id
246
     * @param        $title
247
     * @param        $funcURL
248
     * @param string $path
249
     * @return string
250
     */
251
    public function getNicePathFromId($sel_id, $title, $funcURL, $path = '')
252
    {
253
        $path   = !empty($path) ? '&nbsp;:&nbsp;' . $path : $path;
254
        $sel_id = (int)$sel_id;
255
        $sql    = 'SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id";
256
        $result = $this->db->query($sql);
257
        if (0 == $this->db->getRowsNum($result)) {
258
            return $path;
259
        }
260
        list($parentid, $name) = $this->db->fetchRow($result);
261
        $myts = \MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $myts is dead and can be removed.
Loading history...
262
        $name = htmlspecialchars($name, ENT_QUOTES | ENT_HTML5);
263
        $path = "<a href='" . $funcURL . '&amp;' . $this->id . '=' . $sel_id . "'>" . $name . '</a>' . $path . '';
264
        if (0 == $parentid) {
265
            return $path;
266
        }
267
        $path = $this->getNicePathFromId($parentid, $title, $funcURL, $path);
268
269
        return $path;
270
    }
271
272
    //generates id path from the root id to a given id
273
    // the path is delimetered with "/"
274
275
    /**
276
     * @param        $sel_id
277
     * @param string $path
278
     * @return string
279
     */
280
    public function getIdPathFromId($sel_id, $path = '')
281
    {
282
        $sel_id = (int)$sel_id;
283
        $result = $this->db->query('SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id");
284
        if (0 == $this->db->getRowsNum($result)) {
285
            return $path;
286
        }
287
        list($parentid) = $this->db->fetchRow($result);
288
        $path = '/' . $sel_id . $path . '';
289
        if (0 == $parentid) {
290
            return $path;
291
        }
292
        $path = $this->getIdPathFromId($parentid, $path);
293
294
        return $path;
295
    }
296
297
    /**
298
     * Enter description here...
299
     *
300
     * @param int|unknown_type    $sel_id
301
     * @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...
302
     * @param array|unknown_type  $parray
303
     * @return mixed
304
     */
305
    public function getAllChild($sel_id = 0, $order = '', $parray = [])
306
    {
307
        $sel_id = (int)$sel_id;
308
        $sql    = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . ' ';
309
        if ('' != $order) {
310
            $sql .= " ORDER BY $order";
311
        }
312
        $result = $this->db->query($sql);
313
        $count  = $this->db->getRowsNum($result);
314
        if (0 == $count) {
315
            return $parray;
316
        }
317
        while (false !== ($row = $this->db->fetchArray($result))) {
318
            $parray[] = $row;
319
            $parray   = $this->getAllChild($row[$this->id], $order, $parray);
320
        }
321
322
        return $parray;
323
    }
324
325
    /**
326
     * Enter description here...
327
     *
328
     * @param int|unknown_type    $sel_id
329
     * @param string|unknown_type $order
330
     * @param array|unknown_type  $parray
331
     * @param string|unknown_type $r_prefix
332
     * @return mixed
333
     */
334
    public function getChildTreeArray($sel_id = 0, $order = '', $parray = [], $r_prefix = '')
335
    {
336
        $sel_id = (int)$sel_id;
337
        $sql    = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . ' ';
338
        if ('' != $order) {
339
            $sql .= " ORDER BY $order";
340
        }
341
        $result = $this->db->query($sql);
342
        $count  = $this->db->getRowsNum($result);
343
        if (0 == $count) {
344
            return $parray;
345
        }
346
        while (false !== ($row = $this->db->fetchArray($result))) {
347
            $row['prefix'] = $r_prefix . '.';
348
            $parray[]      = $row;
349
            $parray        = $this->getChildTreeArray($row[$this->id], $order, $parray, $row['prefix']);
350
        }
351
352
        return $parray;
353
    }
354
}
355