|
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
|
|
|
public static function ul($objectRoot, $maxDeep = 0, $hrefFunc = null) |
|
16
|
|
|
{ |
|
17
|
|
|
$count = 0; |
|
18
|
|
|
if (!$hrefFunc) { |
|
19
|
|
|
$hrefFunc = function($object) { |
|
20
|
|
|
echo "<a href='#'> {$object->name()}</a>"; |
|
21
|
|
|
}; |
|
22
|
|
|
} |
|
23
|
|
|
?> |
|
24
|
|
|
<ul class="treeview" data-col='tree_path'> |
|
25
|
|
|
<?php |
|
26
|
|
|
if (is_string($objectRoot)) { |
|
27
|
|
|
$items = $objectRoot::getList(['where' => ['parent_id', 0]]); |
|
28
|
|
|
} else { |
|
29
|
|
|
$class = get_class($objectRoot); |
|
30
|
|
|
$items = $class::getList(['where' => ['parent_id', $objectRoot->pk()]]); |
|
31
|
|
|
} |
|
32
|
|
|
$count += count($items); |
|
33
|
|
|
foreach ($items as $objectChild) { |
|
34
|
|
|
$count+=static::showLi($objectChild, 1, $maxDeep, $hrefFunc); |
|
35
|
|
|
} |
|
36
|
|
|
?> |
|
37
|
|
|
</ul> |
|
38
|
|
|
<?php |
|
39
|
|
|
return $count; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public static function showLi($object, $deep = 1, $maxDeep = 0, $hrefFunc = null) |
|
43
|
|
|
{ |
|
44
|
|
|
$count = 0; |
|
45
|
|
|
$isset = false; |
|
46
|
|
|
$class = get_class($object); |
|
47
|
|
|
if (!$maxDeep || $deep < $maxDeep) { |
|
48
|
|
|
$items = $class::getList(['where' => ['parent_id', $object->pk()]]); |
|
49
|
|
|
$count += count($items); |
|
50
|
|
|
foreach ($items as $objectChild) { |
|
51
|
|
View Code Duplication |
if (!$isset) { |
|
|
|
|
|
|
52
|
|
|
$isset = true; |
|
53
|
|
|
?> |
|
54
|
|
|
<li id='<?= str_replace('\\', '_', get_class($object)) . "-{$object->pk()}"; ?>'> |
|
55
|
|
|
<?= $hrefFunc ? $hrefFunc($object) : "<a href='#'> {$object->name()}</a> "; ?> |
|
56
|
|
|
<ul> |
|
57
|
|
|
<?php |
|
58
|
|
|
} |
|
59
|
|
|
$count+=static::showLi($objectChild, $deep + 1, $maxDeep, $hrefFunc); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
View Code Duplication |
if ($isset) { |
|
|
|
|
|
|
63
|
|
|
?> |
|
64
|
|
|
</ul> |
|
65
|
|
|
</li> |
|
66
|
|
|
<?php |
|
67
|
|
|
} else { |
|
68
|
|
|
?> |
|
69
|
|
|
<li id='<?= str_replace('\\', '_', get_class($object)) . "-{$object->pk()}"; ?>'> |
|
70
|
|
|
<?= $hrefFunc ? $hrefFunc($object) : "<a href='#'> {$object->name()}</a> "; ?> |
|
71
|
|
|
</li> |
|
72
|
|
|
<?php |
|
73
|
|
|
} |
|
74
|
|
|
return $count; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
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.