-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsTreeWidget.php
63 lines (59 loc) · 2.09 KB
/
JsTreeWidget.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
52
53
54
55
56
57
58
59
60
61
62
63
<?php
class JsTreeWidget extends CWidget {
private $tree = [];
private $treeHtml = '';
public $plugins = [];
public $jstreeId = '';
public $selected = [];
public $data = [];
public function init() {
parent::init();
if(empty($this->jstreeId)) {
throw new Exception('Идентификатор дерева должен быть указан');
}
$this->plugins = array_map(function($x) {return '"' . $x . '"';}, $this->plugins);
$tree = $this->buildTree($this->data);
$this->treeHtml = $this->buildHtmlTree($tree);
}
private function buildTree(array $elements, $parentId = 0, $callback = null) {
$branch = [];
foreach ($elements as $element) {
if ($element['parent_id'] == $parentId) {
$children = $this->buildTree($elements, $element['id'], $callback);
if ($children) {
$element['children'] = $children;
}
$branch[] = $element;
}
}
if(is_null($callback) == false) {
$children = $this->$callback($branch, $parentId);
if(empty($children) == false) {
$branch = array_merge($branch, $children);
}
}
return $branch;
}
private function buildHtmlTree($tree) {
$html = '<ul>';
foreach ($tree as $leaf) {
$html .= '<li
data-id="' . $leaf['id'] . '"
data-parent-id="' . $leaf['parent_id']. '"><a href="#">' . $leaf['title'] . '</a>';
if(isset($leaf['children']) && is_array($leaf['children']) == true) {
$html .= $this->buildHtmlTree($leaf['children']);
}
$html .= '</li>';
}
$html .= '</ul>';
return $html;
}
public function run() {
$this->render('jsTree', [
'htmlTree' => $this->treeHtml,
'plugins' => implode(',', $this->plugins),
'jstreeId' => $this->jstreeId,
'selected' => $this->selected
]);
}
}