Passed
Push — master ( 05ee30...e8b784 )
by Michael
05:15
created

ObjectTree::getIdPathFromId()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 16
rs 9.9
cc 3
nc 3
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Adslight;
6
7
/**
8
 * Class ObjectTree
9
 * @package XoopsModules\Adslight
10
 */
11
class ObjectTree extends \XoopsObjectTree
12
{
13
    //generates id path from the root id to a given id
14
    // the path is delimetered with "/"
15
    /**
16
     * @param        $sel_id
17
     * @param string $path
18
     *
19
     * @return string
20
     */
21
    public function getIdPathFromId($sel_id, $path = ''): string
22
    {
23
        global $xoopsDB;
24
        $sel_id = (int)$sel_id;
25
        $result = $xoopsDB->query('SELECT ' . $this->parentId . ' FROM ' . $this->table . ' WHERE ' . $this->myId . "=$sel_id");
0 ignored issues
show
Bug Best Practice introduced by
The property table does not exist on XoopsModules\Adslight\ObjectTree. Since you implemented __get, consider adding a @property annotation.
Loading history...
26
        if (0 == $xoopsDB->getRowsNum($result)) {
27
            return $path;
28
        }
29
        [$parentid] = $xoopsDB->fetchRow($result);
30
        $path = '/' . $sel_id . $path . '';
31
        if (0 == $parentid) {
32
            return $path;
33
        }
34
        $path = $this->getIdPathFromId($parentid, $path);
35
36
        return $path;
37
    }
38
39
    //generates nicely formatted linked path from the root id to a given id
40
41
    /**
42
     * @param        $sel_id
43
     * @param        $title
44
     * @param        $funcURL
45
     * @param string $path
46
     *
47
     * @return string
48
     */
49
    public function getNicePathFromId($sel_id, $title, $funcURL, $path = ''): string
50
    {
51
        global $xoopsDB;
52
        $path   = !empty($path) ? '&nbsp;:&nbsp;' . $path : $path;
53
        $sel_id = (int)$sel_id;
54
        $sql    = 'SELECT ' . $this->parentId . ', ' . $title . ' FROM ' . $this->table . ' WHERE ' . $this->myId . "=$sel_id";
0 ignored issues
show
Bug Best Practice introduced by
The property table does not exist on XoopsModules\Adslight\ObjectTree. Since you implemented __get, consider adding a @property annotation.
Loading history...
55
        $result = $xoopsDB->query($sql);
56
        if (0 == $xoopsDB->getRowsNum($result)) {
57
            return $path;
58
        }
59
        [$parentid, $name] = $xoopsDB->fetchRow($result);
60
        $myts = \MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $myts is dead and can be removed.
Loading history...
61
        $name = \htmlspecialchars($name, \ENT_QUOTES | \ENT_HTML5);
62
        $path = "<a href='" . $funcURL . '&amp;' . $this->myId . '=' . $sel_id . "'>" . $name . '</a>' . $path . '';
63
        if (0 == $parentid) {
64
            return $path;
65
        }
66
        $path = $this->getNicePathFromId($parentid, $title, $funcURL, $path);
67
68
        return $path;
69
    }
70
}
71