function sortTree($nodeid,$arTree) { $res = array(); for($i=0;$i<sizeof($arTree);$i++) if ($arTree[$i]["parentid"]==$nodeid) { array_push($res,$arTree[$i]); $subres = sortTree($arTree[$i]["id"],$arTree); for ($j=0;$j<sizeof($subres);$j++) array_push($res,$subres[$j]); } return $res; }
/** * Ontology data for deliveries (not results, so use deliveryService->getRootClass) * @throws common_exception_IsAjaxAction */ public function getOntologyData() { if (!tao_helpers_Request::isAjax()) { throw new common_exception_IsAjaxAction(__FUNCTION__); } $options = array('subclasses' => true, 'instances' => true, 'highlightUri' => '', 'chunk' => false, 'offset' => 0, 'limit' => 0); if ($this->hasRequestParameter('loadNode')) { $options['uniqueNode'] = $this->getRequestParameter('loadNode'); } if ($this->hasRequestParameter("selected")) { $options['browse'] = array($this->getRequestParameter("selected")); } if ($this->hasRequestParameter('hideInstances')) { if ((bool) $this->getRequestParameter('hideInstances')) { $options['instances'] = false; } } if ($this->hasRequestParameter('classUri')) { $clazz = $this->getCurrentClass(); $options['chunk'] = !$clazz->equals($this->deliveryService->getRootClass()); } else { $clazz = $this->deliveryService->getRootClass(); } if ($this->hasRequestParameter('offset')) { $options['offset'] = $this->getRequestParameter('offset'); } if ($this->hasRequestParameter('limit')) { $options['limit'] = $this->getRequestParameter('limit'); } //generate the tree from the given parameters $tree = $this->getClassService()->toTree($clazz, $options); $tree = $this->addPermissions($tree); function sortTree(&$tree) { usort($tree, function ($a, $b) { if (isset($a['data']) && isset($b['data'])) { if ($a['type'] != $b['type']) { return $a['type'] == 'class' ? -1 : 1; } else { return strcasecmp($a['data'], $b['data']); } } return 0; }); } if (isset($tree['children'])) { sortTree($tree['children']); } elseif (array_values($tree) === $tree) { //is indexed array sortTree($tree); } //expose the tree $this->returnJson($tree); }
function treeFromList(&$orig_nodelist, $threshold = 0, $return_main_payload = TRUE) { $tree = array(); $nodelist = $orig_nodelist; // index the tree items by their order in $orig_nodelist $ti = 0; foreach ($nodelist as &$node) { $node['__tree_index'] = $ti++; } // Array equivalent of traceEntity() function. $trace = array(); // set kidc and kids only once foreach (array_keys($nodelist) as $nodeid) { $nodelist[$nodeid]['kidc'] = 0; $nodelist[$nodeid]['kids'] = array(); } do { $nextpass = FALSE; foreach (array_keys($nodelist) as $nodeid) { // When adding a node to the working tree, book another // iteration, because the new item could make a way for // others onto the tree. Also remove any item added from // the input list, so iteration base shrinks. // First check if we can assign directly. if ($nodelist[$nodeid]['parent_id'] == NULL) { $tree[$nodeid] = $nodelist[$nodeid]; $trace[$nodeid] = array(); // Trace to root node is empty unset($nodelist[$nodeid]); $nextpass = TRUE; } elseif (isset($trace[$nodelist[$nodeid]['parent_id']])) { // Trace to a node is a trace to its parent plus parent id. $trace[$nodeid] = $trace[$nodelist[$nodeid]['parent_id']]; $trace[$nodeid][] = $nodelist[$nodeid]['parent_id']; pokeNode($tree, $trace[$nodeid], $nodeid, $nodelist[$nodeid], $threshold); // path to any other node is made of all parent nodes plus the added node itself unset($nodelist[$nodeid]); $nextpass = TRUE; } } } while ($nextpass); if (!$return_main_payload) { return $nodelist; } // update each input node with its backtrace route foreach ($trace as $nodeid => $route) { $orig_nodelist[$nodeid]['trace'] = $route; } sortTree($tree, 'treeItemCmp'); // sort the resulting tree by the order in original list return $tree; }