Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
malwaredllc
GitHub Repository: malwaredllc/byob
Path: blob/master/web-gui/buildyourownbotnet/assets/js/aci-tree/php/Tree.php
1293 views
1
<?php
2
3
// the base class to return data in JSON for aciTree
4
// as you can see, keep it simple it's the best way to do things :D
5
// note: you'll need PHP >= 5.2 to run this
6
7
abstract class Tree {
8
9
/**
10
* Get tree branch as KEY => ITEM.
11
* @param string $parentId - if NULL then it's the root
12
* @return array
13
*/
14
abstract public function branch($parentId = null);
15
16
/**
17
* Get item properties (all default properties must be defined).
18
* @param string $itemId
19
* @return array
20
*/
21
public function itemProps($itemId) {
22
return array(
23
'inode' => null, // NULL = maybe a folder, TRUE - is a folder, FALSE - it's not a folder
24
'open' => false, // should open folder?
25
'icon' => null // icon CSS class name (if any) can be ARRAY [name, background X, background Y] (in this order)
26
);
27
}
28
29
private function _json($parentId, Array &$json, $children) {
30
$branch = $this->branch($parentId);
31
foreach ($branch as $id => $label) {
32
$props = $this->itemProps($id);
33
$list = array();
34
if ($children) {
35
$this->_json($id, $list, $children);
36
if (count($list) == 0) {
37
$props['inode'] = false;
38
}
39
}
40
$json[] = array_merge(array(
41
'id' => $id,
42
'label' => $label,
43
'branch' => $list
44
), $props);
45
}
46
}
47
48
/**
49
* Output tree JSON (array of id/item/props/items with props.inode depending if it's a TREE folder or not).
50
* @param string $parentId
51
* @param bool $children - include children?
52
*/
53
public function json($parentId, $children = false) {
54
$json = array();
55
$this->_json($parentId, $json, $children);
56
header('Content-type: application/json; charset=UTF-8');
57
header('Vary: Accept-Encoding');
58
echo json_encode($json);
59
die;
60
}
61
62
}
63
64