-
Notifications
You must be signed in to change notification settings - Fork 1
/
Tree.php
51 lines (50 loc) · 1.15 KB
/
Tree.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
namespace yii\dwz;
use yii\helpers\Html;
use yii\dwz\tree\Node\Node;
use yii\dwz\tree\Builder\NodeBuilder;
use yii\dwz\tree\Node\NodeInterface;
use yii\dwz\TreeVisitor;
/**
*
*/
class Tree extends Widget
{
public $items = [];
protected $root;
protected $nodebuilder;
public function init(){
$this->nodebuilder = new NodeBuilder();
$this->nodebuilder->value('root');
$this->initTree($this->items,$this->nodebuilder);
$this->root = $this->nodebuilder->getNode();
}
public function run(){
return $this->getHtmlCode();
}
protected function initTree($items,&$root){
foreach ($items as $v) {
if(isset($v['visible']) && !$v['visible']){
continue;
}
if(isset($v['menus']) && $v['menus']){
$_t = $v['menus'];
unset($v['menus']);
$tree = $root->tree($v);
$this->initTree($_t,$tree);
$tree->end();
continue;
}
$root->leaf($v);
}
}
public function getHtmlCode(){
$visitor = new TreeVisitor();
$res = $this->root->accept($visitor);
if(!isset($this->options['class']))
$this->options['class'] = 'tree treeFolder';
$res[0] = Html::beginTag('ul',$this->options);
$res = implode('', $res);
return $res;
}
}