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); |
|
|
|
|
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
|
|
|
} |
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:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.