From f2c136bae34cb090ced8e59b4fba300cec337314 Mon Sep 17 00:00:00 2001 From: mokaddem Date: Tue, 21 Apr 2020 08:30:07 +0200 Subject: [PATCH] chg: [galaxy:fork_tree] Moved generation in the model --- app/Controller/GalaxiesController.php | 26 +--------------------- app/Model/Galaxy.php | 32 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 25 deletions(-) diff --git a/app/Controller/GalaxiesController.php b/app/Controller/GalaxiesController.php index baf8c7447..d49e54184 100644 --- a/app/Controller/GalaxiesController.php +++ b/app/Controller/GalaxiesController.php @@ -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()); } diff --git a/app/Model/Galaxy.php b/app/Model/Galaxy.php index 2939f391c..14e5ba4f9 100644 --- a/app/Model/Galaxy.php +++ b/app/Model/Galaxy.php @@ -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; + } }