1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-06 14:46:56 +02:00

Merge pull request #2929 from lonalore/AdminUI

Admin ui parent/child list sorting.
This commit is contained in:
Cameron
2017-12-21 16:01:23 -08:00
committed by GitHub

View File

@@ -3047,40 +3047,124 @@ class e_admin_controller_ui extends e_admin_controller
} }
/** /**
* Get ordered models by their parents * Get ordered models by their parents.
* add extra *
* @lonalore
* @return e_admin_tree_model * @return e_admin_tree_model
*/ */
public function getTreeModelSorted() public function getTreeModelSorted()
{ {
$tree = $this->getTreeModel(); $tree = $this->getTreeModel();
$parentField = $this->getSortParent(); // Helper arrays for preparing new tree model.
$orderField = $this->getSortField(); $models = array();
$levels = array();
$arr = array(); // Calculate depth for each model.
foreach ($tree->getTree() as $id => $model) /** @var e_admin_model $model */
foreach($tree->getTree() as $id => $model)
{ {
$parent = $model->get($parentField); $depth = $this->calculateModelDepth($model);
$order = $model->get($orderField);
$model->set('_depth', '9999'); // include extra field in output, just as the MySQL function did. if(!in_array($depth, $levels))
{
$levels[] = $depth;
}
$model->set('_depth', $depth);
$arr[$id] = $model; $model->set('_id', $id);
$models[$id] = $model;
} }
// First, we sort models by $sortField.
uasort($models, function($modelA, $modelB) {
$sortField = $this->getSortField();
// usort($arr); array_multisort() ? /** @var e_admin_model $modelA */
/** @var e_admin_model $modelB */
$tree->setTree($arr,true); // set the newly ordered tree. $weightA = (int) $modelA->get($sortField);
$weightB = (int) $modelB->get($sortField);
var_dump($arr); if ($weightA == $weightB) {
return 0;
}
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)
{
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($models, true);
return $this->_tree_model; 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. * @lonalore - found online.