From c0f66a45fc2095877bd0792917725446a701346d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=B3na=20Lore?= Date: Thu, 21 Dec 2017 21:54:06 +0100 Subject: [PATCH 1/3] "_depth" calculation for admin_ui --- e107_handlers/admin_ui.php | 69 ++++++++++++++++++++++++++++---------- 1 file changed, 51 insertions(+), 18 deletions(-) diff --git a/e107_handlers/admin_ui.php b/e107_handlers/admin_ui.php index 1670b36a0..a5250c15e 100644 --- a/e107_handlers/admin_ui.php +++ b/e107_handlers/admin_ui.php @@ -3047,40 +3047,73 @@ class e_admin_controller_ui extends e_admin_controller } /** - * Get ordered models by their parents - * add extra - * @lonalore + * Get ordered models by their parents. + * * @return e_admin_tree_model */ public function getTreeModelSorted() { $tree = $this->getTreeModel(); - $parentField = $this->getSortParent(); - $orderField = $this->getSortField(); + // New tree model. + $_tree = array(); - $arr = array(); - foreach ($tree->getTree() as $id => $model) + /** @var e_admin_model $model */ + foreach($tree->getTree() as $id => $model) { - $parent = $model->get($parentField); - $order = $model->get($orderField); - - $model->set('_depth', '9999'); // include extra field in output, just as the MySQL function did. - - - $arr[$id] = $model; + $depth = $this->calculateModelDepth($model); + $model->set('_depth', $depth); + $_tree[$id] = $model; } + // usort($arr); array_multisort() ? - // usort($arr); array_multisort() ? + // Set the newly ordered tree. + $tree->setTree($_tree, true); - $tree->setTree($arr,true); // set the newly ordered tree. - - var_dump($arr); + print_a($_tree); return $this->_tree_model; } + /** + * Calculates '_depth' property for the given model. + * + * @param e_admin_model $model + * Admin model we want to get '_depth' property for. + * + * @return int + * Depth for the e_admin_model object. + */ + public function calculateModelDepth($model) + { + $parentField = $this->getSortParent(); + $parentID = (int) $model->get($parentField); + + // Default depth. + $depth = 1; + + // If no parent. + if($parentID === 0) + { + return $depth; + } + + $tree = $this->getTreeModel(); + + /** @var e_admin_model $_model */ + foreach($tree->getTree() as $id => $_model) + { + if($id == $parentID) + { + $depth += $this->calculateModelDepth($_model); + break; + } + } + + return $depth; + } + /** * @lonalore - found online. From 1b39eae368cb1049eaa021f858fd8a98c67ace44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=B3na=20Lore?= Date: Fri, 22 Dec 2017 00:39:01 +0100 Subject: [PATCH 2/3] getTreeModelSorted() --- e107_handlers/admin_ui.php | 58 +++++++++++++++++++++++++++++++++----- 1 file changed, 51 insertions(+), 7 deletions(-) diff --git a/e107_handlers/admin_ui.php b/e107_handlers/admin_ui.php index a5250c15e..79e29fc8c 100644 --- a/e107_handlers/admin_ui.php +++ b/e107_handlers/admin_ui.php @@ -3055,23 +3055,67 @@ class e_admin_controller_ui extends e_admin_controller { $tree = $this->getTreeModel(); - // New tree model. - $_tree = array(); + // Helper arrays for preparing new tree model. + $models = array(); + $levels = array(); + // Calculate depth for each model. /** @var e_admin_model $model */ foreach($tree->getTree() as $id => $model) { $depth = $this->calculateModelDepth($model); + + if(!in_array($depth, $levels)) + { + $levels[] = $depth; + } + $model->set('_depth', $depth); - $_tree[$id] = $model; + $model->set('_id', $id); + $models[$id] = $model; } - // usort($arr); array_multisort() ? + // First, we sort models by $sortField. + uasort($models, function($modelA, $modelB) { + $sortField = $this->getSortField(); + + /** @var e_admin_model $modelA */ + /** @var e_admin_model $modelB */ + + $weightA = (int) $modelA->get($sortField); + $weightB = (int) $modelB->get($sortField); + + if ($weightA == $weightB) { + return 0; + } + + return ($weightA < $weightB) ? -1 : 1; + }); + + // Now, we sort models by hierarchy. + foreach($levels as $level) + { + uasort($models, function($modelA, $modelB) { + $parentField = $this->getSortParent(); + + /** @var e_admin_model $modelA */ + /** @var e_admin_model $modelB */ + + $parentA = (int) $modelA->get($parentField); + $parentB = (int) $modelB->get($parentField); + $idA = (int) $modelA->get('_id'); + + // If A is the parent of B or both parents are the same. + if ($idA == $parentB || $parentA == $parentB) { + return 0; + } + + return 1; + }); + } // Set the newly ordered tree. - $tree->setTree($_tree, true); - - print_a($_tree); + $tree->setTree($models, true); return $this->_tree_model; } From 4ef9e7893520639dbcec07a92c3798f00e0a46d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=B3na=20Lore?= Date: Fri, 22 Dec 2017 00:51:44 +0100 Subject: [PATCH 3/3] Ability to use ASC/DESC direction in getTreeModelSorted() --- e107_handlers/admin_ui.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/e107_handlers/admin_ui.php b/e107_handlers/admin_ui.php index 79e29fc8c..585700722 100644 --- a/e107_handlers/admin_ui.php +++ b/e107_handlers/admin_ui.php @@ -3092,6 +3092,13 @@ class e_admin_controller_ui extends e_admin_controller return ($weightA < $weightB) ? -1 : 1; }); + $direction = 'ASC'; + + if($direction == 'DESC') + { + $models = array_reverse($models, true); + } + // Now, we sort models by hierarchy. foreach($levels as $level) {