Path: blob/master/web-gui/buildyourownbotnet/assets/js/aci-tree/php/Tree.php
1293 views
<?php12// the base class to return data in JSON for aciTree3// as you can see, keep it simple it's the best way to do things :D4// note: you'll need PHP >= 5.2 to run this56abstract class Tree {78/**9* Get tree branch as KEY => ITEM.10* @param string $parentId - if NULL then it's the root11* @return array12*/13abstract public function branch($parentId = null);1415/**16* Get item properties (all default properties must be defined).17* @param string $itemId18* @return array19*/20public function itemProps($itemId) {21return array(22'inode' => null, // NULL = maybe a folder, TRUE - is a folder, FALSE - it's not a folder23'open' => false, // should open folder?24'icon' => null // icon CSS class name (if any) can be ARRAY [name, background X, background Y] (in this order)25);26}2728private function _json($parentId, Array &$json, $children) {29$branch = $this->branch($parentId);30foreach ($branch as $id => $label) {31$props = $this->itemProps($id);32$list = array();33if ($children) {34$this->_json($id, $list, $children);35if (count($list) == 0) {36$props['inode'] = false;37}38}39$json[] = array_merge(array(40'id' => $id,41'label' => $label,42'branch' => $list43), $props);44}45}4647/**48* Output tree JSON (array of id/item/props/items with props.inode depending if it's a TREE folder or not).49* @param string $parentId50* @param bool $children - include children?51*/52public function json($parentId, $children = false) {53$json = array();54$this->_json($parentId, $json, $children);55header('Content-type: application/json; charset=UTF-8');56header('Vary: Accept-Encoding');57echo json_encode($json);58die;59}6061}626364