Passed
Push — master ( 52863f...37ba64 )
by Alexey
04:34
created

Tree::ul()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 25
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 20
nc 8
nop 6
dl 0
loc 25
rs 8.5806
c 0
b 0
f 0
1
<?php
2
/**
3
 * Tree
4
 *
5
 * @author Alexey Krupskiy <[email protected]>
6
 * @link http://inji.ru/
7
 * @copyright 2015 Alexey Krupskiy
8
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
9
 */
10
11
namespace Ui;
12
13
class Tree extends \Object {
14
15
    /**
16
     * Function for generate item body html
17
     *
18
     * @var closure|null
19
     */
20
    public $itemBodyFn = null;
21
22
    /**
23
     * Function for generate item body html
24
     *
25
     * @var closure|null
26
     */
27
    public $itemActiveCheck = null;
28
29
    /**
30
     * Active item class name
31
     *
32
     * @var string
33
     */
34
    public $itemActiveClass = 'active';
35
36
    public function __construct() {
37
        
38
    }
39
40
    /**
41
     * Draw tree
42
     * 
43
     * @param Model|string $objectRoot
44
     * @param integer $maxDeep
45
     * @param array $order
46
     * @return integer
47
     */
48
    public function draw($objectRoot, $maxDeep = 0, $order = []) {
49
        return Tree::ul($objectRoot, $maxDeep, $this->itemBodyFn, $order, $this->itemActiveCheck, $this->itemActiveClass);
0 ignored issues
show
Bug introduced by
It seems like $this->itemActiveCheck can also be of type null; however, Ui\Tree::ul() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
50
    }
51
52
    /**
53
     * Start generating items tree from root item
54
     * item must has parent_id col for generating tree by this coll
55
     * 
56
     * @param Model|string $objectRoot
57
     * @param integer $maxDeep
58
     * @param closure|null $hrefFunc
59
     * @param array $order
60
     * @return integer
61
     */
62
    public static function ul($objectRoot, $maxDeep = 0, $hrefFunc = null, $order = [], $activeFunc = '', $activeClass = 'active') {
63
        $count = 0;
64
        if (!$hrefFunc) {
65
            $hrefFunc = function($object) {
66
                return "<a href='#'> {$object->name()}</a>";
67
            };
68
        }
69
        ?>
70
        <ul class="treeview" data-col='tree_path'>
71
            <?php
72
            if (is_string($objectRoot)) {
73
                $items = $objectRoot::getList(['where' => ['parent_id', 0]]);
74
            } else {
75
                $class = get_class($objectRoot);
76
                $items = $class::getList(['where' => ['parent_id', $objectRoot->pk()], 'order' => $order]);
77
            }
78
            $count += count($items);
79
            foreach ($items as $objectChild) {
80
                $count += static::showLi($objectChild, 1, $maxDeep, $hrefFunc, $order, $activeFunc, $activeClass);
81
            }
82
            ?>
83
        </ul>
84
        <?php
85
        return $count;
86
    }
87
88
    public static function showLi($object, $deep = 1, $maxDeep = 0, $hrefFunc = null, $order = [], $activeFunc = '', $activeClass = 'active') {
89
        $count = 0;
90
        $isset = false;
91
        $class = get_class($object);
92
        $item = $hrefFunc ? $hrefFunc($object) : "<a href='#'> {$object->name()}</a> ";
93
        $attributes = [];
94
        if ($activeFunc && $activeFunc($object)) {
95
            $attributes['class'] = $activeClass;
96
        }
97
98
        if (is_array($item)) {
99
            $attributes = $item['attributes'];
100
            $item = $item['text'];
101
        }
102
        if (!isset($attributes['id'])) {
103
            $attributes['id'] = str_replace('\\', '_', get_class($object)) . "-{$object->pk()}";
104
        }
105
        if (!$maxDeep || $deep < $maxDeep) {
106
            $items = $class::getList(['where' => ['parent_id', $object->pk()], 'order' => $order]);
107
            $count += count($items);
108
            foreach ($items as $objectChild) {
109
                if (!$isset) {
110
                    $isset = true;
111
                    if ($activeFunc && $activeFunc($objectChild)) {
112
                        $attributes['class'] = $activeClass;
113
                    }
114
                    echo \Html::el('li', $attributes, $item, true);
115
                    echo '<ul>';
116
                }
117
                $count += static::showLi($objectChild, $deep + 1, $maxDeep, $hrefFunc, $order, $activeFunc, $activeClass);
118
            }
119
        }
120
        if ($isset) {
121
            echo '</ul></li>';
122
        } else {
123
            echo \Html::el('li', $attributes, $item);
124
        }
125
        return $count;
126
    }
127
}