Passed
Push — master ( 2ecc51...c1b656 )
by Michael
04:26 queued 02:02
created

efqTree::getUnformattedPathFromId()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 5
nop 3
dl 0
loc 22
rs 8.9197
c 0
b 0
f 0
1
<?php
2
/*
3
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright    XOOPS Project https://xoops.org/
14
 * @license      GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
15
 * @package
16
 * @since
17
 * @author       XOOPS Development Team, Kazumi Ono (AKA onokazu)
18
 */
19
20
// Hacks provided by: wtravel                //
21
//  e-mail: [email protected]            //
22
// ------------------------------------------------------------------------- //
23
24
class efqTree
25
{
26
    public $table;
27
    //table with parent-child structure
28
    public $id;
29
    //name of unique id for records in table $table
30
    public $pid;
31
    // name of parent id used in table $table
32
    public $order;
33
    //specifies the order of query results
34
    public $title;
35
    // name of a field in table $table which will be used when  selection box and paths are generated
36
    public $db;
37
    public $dirid;
38
39
    //constructor of class XoopsTree
40
    //sets the names of table, unique id, and parend id
41
    /**
42
     * efqTree constructor.
43
     * @param $table_name
44
     * @param $id_name
45
     * @param $pid_name
46
     */
47
    public function __construct($table_name, $id_name, $pid_name)
48
    {
49
        $this->db    = XoopsDatabaseFactory::getDatabaseConnection();
0 ignored issues
show
Bug introduced by
The type XoopsDatabaseFactory 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...
50
        $this->table = $table_name;
51
        $this->id    = $id_name;
52
        $this->pid   = $pid_name;
53
        $this->dirid = 0;
54
    }
55
56
    /**
57
     * @param int $dirid
58
     */
59
    public function setDir($dirid = 0)
60
    {
61
        $this->dirid = $dirid;
62
    }
63
64
    // returns an array of first child objects for a given id($sel_id)
65
66
    /**
67
     * @param        $sel_id
68
     * @param string $order
69
     * @return array
70
     */
71
    public function getFirstChild($sel_id, $order = '')
72
    {
73
        $arr = array();
74
        $sql = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . '';
75
        if ($order !== '') {
76
            $sql .= " ORDER BY $order";
77
        }
78
        $result = $this->db->query($sql);
79
        $count  = $this->db->getRowsNum($result);
80
        if ($count == 0) {
81
            return $arr;
82
        }
83
        while ($myrow = $this->db->fetchArray($result)) {
84
            array_push($arr, $myrow);
85
        }
86
87
        return $arr;
88
    }
89
90
    // returns an array of all FIRST child ids of a given id($sel_id)
91
92
    /**
93
     * @param $sel_id
94
     * @return array
95
     */
96
    public function getFirstChildId($sel_id)
97
    {
98
        $idarray = array();
99
        $result  = $this->db->query('SELECT ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . '');
100
        $count   = $this->db->getRowsNum($result);
101
        if ($count == 0) {
102
            return $idarray;
103
        }
104
        while (list($id) = $this->db->fetchRow($result)) {
105
            array_push($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 View Code Duplication
    public function getAllChildId($sel_id, $order = '', $idarray = array())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
    {
121
        $sql = 'SELECT ' . $this->id . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . '';
122
        if ($order !== '') {
123
            $sql .= " ORDER BY $order";
124
        }
125
        $result = $this->db->query($sql);
126
        $count  = $this->db->getRowsNum($result);
127
        if ($count == 0) {
128
            return $idarray;
129
        }
130
        while (list($r_id) = $this->db->fetchRow($result)) {
131
            array_push($idarray, $r_id);
132
            $idarray = $this->getAllChildId($r_id, $order, $idarray);
133
        }
134
135
        return $idarray;
136
    }
137
138
    //returns an array of ALL parent ids for a given id($sel_id)
139
140
    /**
141
     * @param        $sel_id
142
     * @param string $order
143
     * @param array  $idarray
144
     * @return array
145
     */
146
    public function getAllParentId($sel_id, $order = '', $idarray = array())
147
    {
148
        $sql = 'SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . '=' . $sel_id . '';
149
        if ($order !== '') {
150
            $sql .= " ORDER BY $order";
151
        }
152
        $result = $this->db->query($sql);
153
        list($r_id) = $this->db->fetchRow($result);
154
        if ($r_id == 0) {
155
            return $idarray;
156
        }
157
        array_push($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($sel_id)
164
    // the path is delimetered with "/"
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
        $result = $this->db->query('SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id");
174
        if ($this->db->getRowsNum($result) == 0) {
175
            return $path;
176
        }
177
        list($parentid, $name) = $this->db->fetchRow($result);
178
        $myts = MyTextSanitizer::getInstance();
0 ignored issues
show
Bug introduced by
The type MyTextSanitizer 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...
179
        $name = $myts->htmlSpecialChars($name);
180
        $path = '/' . $name . $path . '';
181
        if ($parentid == 0) {
182
            return $path;
183
        }
184
        $path = $this->getPathFromId($parentid, $title, $path);
185
186
        return $path;
187
    }
188
189
    //makes a nicely ordered selection box
190
    //$preset_id is used to specify a preselected item
191
    //set $none to 1 to add a option with value 0
192
    /**
193
     * @param        $title
194
     * @param string $order
195
     * @param int    $preset_id
196
     * @param int    $none
197
     * @param string $sel_name
198
     * @param string $onchange
199
     * @param int    $multiple
200
     */
201
    public function makeMySelBox($title, $order = '', $preset_id = 0, $none = 0, $sel_name = '', $onchange = '', $multiple = 0)
202
    {
203
        if ($sel_name === '') {
204
            $sel_name = $this->id;
205
        }
206
        $myts = MyTextSanitizer::getInstance();
207
208
        $sql = 'SELECT ' . $this->id . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->pid . '=0';
209
        if ($this->dirid != 0) {
210
            $sql .= " AND dirid=$this->dirid";
211
        }
212
        if ($order !== '') {
213
            $sql .= " ORDER BY $order";
214
        }
215
        //echo 'test';
216
        //echo $sql;
217
        $result = $this->db->query($sql);
218
219
        echo "<select name='" . $sel_name . '\'';
220
        if ($onchange !== '') {
221
            echo " onchange='" . $onchange . '\'';
222
        }
223
        if ($multiple != 0) {
224
            echo ' multiple="multiple"';
225
        }
226
        echo ">\n";
227
228
        if ($none) {
229
            echo "<option value='0'>----</option>\n";
230
        }
231
        while (list($catid, $name) = $this->db->fetchRow($result)) {
232
            $sel = '';
233
            if ($catid == $preset_id) {
234
                $sel = " selected='selected'";
235
            }
236
            echo "<option value='$catid'$sel>$name</option>\n";
237
            $sel = '';
238
            $arr = $this->getChildTreeArray($catid, $order);
239
            foreach ($arr as $option) {
240
                $option['prefix'] = str_replace('.', '--', $option['prefix']);
241
                $catpath          = $option['prefix'] . '&nbsp;' . $myts->htmlSpecialChars($option[$title]);
242
                if ($option[$this->id] == $preset_id) {
243
                    $sel = " selected='selected'";
244
                }
245
                echo "<option value='" . $option[$this->id] . "'$sel>$catpath</option>\n";
246
                $sel = '';
247
            }
248
        }
249
        echo "</select>\n";
250
    }
251
252
    //generates nicely formatted linked path from the root id to a given id
253
254
    /**
255
     * @param        $sel_id
256
     * @param        $title
257
     * @param        $funcURL
258
     * @param string $path
259
     * @param string $outputvar
260
     * @return string
261
     */
262
    public function getNicePathFromId($sel_id, $title, $funcURL, $path = '', $outputvar = 'catid')
263
    {
264
        $sql    = 'SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id";
265
        $result = $this->db->query($sql);
266
        if ($this->db->getRowsNum($result) == 0) {
267
            return $path;
268
        }
269
        list($parentid, $name) = $this->db->fetchRow($result);
270
        $myts = MyTextSanitizer::getInstance();
271
        $name = $myts->htmlSpecialChars($name);
272
        $path = "<a href='" . $funcURL . '&' . $outputvar . '=' . $sel_id . '\'>' . $name . '</a>&nbsp;:&nbsp;' . $path . '';
273
        if ($parentid == 0) {
274
            return $path;
275
        }
276
        $path = $this->getNicePathFromId($parentid, $title, $funcURL, $path, $outputvar);
277
278
        return $path;
279
    }
280
281
    //generates nicely formatted path from the root id to a given id
282
283
    /**
284
     * @param        $sel_id
285
     * @param        $title
286
     * @param string $path
287
     * @return string
288
     */
289
    public function getUnformattedPathFromId($sel_id, $title, $path = '')
290
    {
291
        $sql    = 'SELECT ' . $this->pid . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id";
292
        $result = $this->db->query($sql);
293
        if ($this->db->getRowsNum($result) == 0) {
294
            return $path;
295
        }
296
        list($parentid, $name) = $this->db->fetchRow($result);
297
        $myts = MyTextSanitizer::getInstance();
298
        $name = $myts->htmlSpecialChars($name);
299
        if ($path !== '') {
300
            $path = '' . $name . '&nbsp;:&nbsp;' . $path . '';
301
        } else {
302
            $path = '' . $name . '';
303
        }
304
305
        if ($parentid == 0) {
306
            return $path;
307
        }
308
        $path = $this->getUnformattedPathFromId($parentid, $title, $path);
309
310
        return $path;
311
    }
312
313
    //generates id path from the root id to a given id
314
    // the path is delimetered with "/"
315
    /**
316
     * @param        $sel_id
317
     * @param string $path
318
     * @return string
319
     */
320
    public function getIdPathFromId($sel_id, $path = '')
321
    {
322
        $result = $this->db->query('SELECT ' . $this->pid . ' FROM ' . $this->table . ' WHERE ' . $this->id . "=$sel_id");
323
        if ($this->db->getRowsNum($result) == 0) {
324
            return $path;
325
        }
326
        list($parentid) = $this->db->fetchRow($result);
327
        $path = '/' . $sel_id . $path . '';
328
        if ($parentid == 0) {
329
            return $path;
330
        }
331
        $path = $this->getIdPathFromId($parentid, $path);
332
333
        return $path;
334
    }
335
336
    /**
337
     * @param int    $sel_id
338
     * @param string $order
339
     * @param array  $parray
340
     * @return array
341
     */
342 View Code Duplication
    public function getAllChild($sel_id = 0, $order = '', $parray = array())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
343
    {
344
        $sql = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . '';
345
        if ($order !== '') {
346
            $sql .= " ORDER BY $order";
347
        }
348
        $result = $this->db->query($sql);
349
        $count  = $this->db->getRowsNum($result);
350
        if ($count == 0) {
351
            return $parray;
352
        }
353
        while ($row = $this->db->fetchArray($result)) {
354
            array_push($parray, $row);
355
            $parray = $this->getAllChild($row[$this->id], $order, $parray);
356
        }
357
358
        return $parray;
359
    }
360
361
    /**
362
     * @param int    $sel_id
363
     * @param string $order
364
     * @param array  $parray
365
     * @param string $r_prefix
366
     * @return array
367
     */
368
    public function getChildTreeArray($sel_id = 0, $order = '', $parray = array(), $r_prefix = '')
369
    {
370
        $sql = 'SELECT * FROM ' . $this->table . ' WHERE ' . $this->pid . '=' . $sel_id . '';
371
        if ($order !== '') {
372
            $sql .= " ORDER BY $order";
373
        }
374
        $result = $this->db->query($sql);
375
        $count  = $this->db->getRowsNum($result);
376
        if ($count == 0) {
377
            return $parray;
378
        }
379
        while ($row = $this->db->fetchArray($result)) {
380
            $row['prefix'] = $r_prefix . '.';
381
            array_push($parray, $row);
382
            $parray = $this->getChildTreeArray($row[$this->id], $order, $parray, $row['prefix']);
383
        }
384
385
        return $parray;
386
    }
387
}
388