chg: [galaxy:fork_tree] Moved generation in the model

pull/6120/head
mokaddem 2020-04-21 08:30:07 +02:00
parent 7ffcd86eae
commit f2c136bae3
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
2 changed files with 33 additions and 25 deletions

View File

@ -428,31 +428,7 @@ class GalaxiesController extends AppController
'recursive' => -1,
'conditions' => array('Galaxy.id' => $galaxyId)
));
$tree = array();
$lookup = array();
foreach ($clusters as $i => $cluster) {
$clusters[$i]['children'] = array();
$lookup[$cluster['GalaxyCluster']['id']] = &$clusters[$i];
}
foreach ($clusters as $i => $cluster) {
if (!empty($cluster['GalaxyCluster']['extended_from'])) {
$parent = $cluster['GalaxyCluster']['extended_from'];
$lookup[$parent['GalaxyCluster']['id']]['children'][] = &$clusters[$i];
} else {
$tree[] = &$clusters[$i];
}
}
foreach($tree as $i => $node) {
if (empty($node['children'])) {
unset($tree[$i]);
}
}
$tree = array(array(
'Galaxy' => $galaxy['Galaxy'],
'children' => array_values($tree)
));
$tree = $this->Galaxy->generateForkTree($clusters, $galaxy, $pruneRootLeaves=true);
if ($this->_isRest()) {
return $this->RestResponse->viewData($tree, $this->response->type());
}

View File

@ -510,4 +510,36 @@ class Galaxy extends AppModel
}
}
}
public function generateForkTree($clusters, $galaxy, $pruneRootLeaves=true)
{
$tree = array();
$lookup = array();
foreach ($clusters as $i => $cluster) {
$clusters[$i]['children'] = array();
$lookup[$cluster['GalaxyCluster']['id']] = &$clusters[$i];
}
foreach ($clusters as $i => $cluster) {
if (!empty($cluster['GalaxyCluster']['extended_from'])) {
$parent = $cluster['GalaxyCluster']['extended_from'];
$lookup[$parent['GalaxyCluster']['id']]['children'][] = &$clusters[$i];
} else {
$tree[] = &$clusters[$i];
}
}
if ($pruneRootLeaves) {
foreach($tree as $i => $node) {
if (empty($node['children'])) {
unset($tree[$i]);
}
}
}
$tree = array(array(
'Galaxy' => $galaxy['Galaxy'],
'children' => array_values($tree)
));
return $tree;
}
}