Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Admin ui #2929

Merged
merged 3 commits into from
Dec 22, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 98 additions & 14 deletions e107_handlers/admin_ui.php
Original file line number Diff line number Diff line change
Expand Up @@ -3047,40 +3047,124 @@ public function getTreeModel()
}

/**
* 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();
// Helper arrays for preparing new tree model.
$models = array();
$levels = array();

$arr = array();
foreach ($tree->getTree() as $id => $model)
// Calculate depth for each model.
/** @var e_admin_model $model */
foreach($tree->getTree() as $id => $model)
{
$parent = $model->get($parentField);
$order = $model->get($orderField);
$depth = $this->calculateModelDepth($model);

if(!in_array($depth, $levels))
{
$levels[] = $depth;
}

$model->set('_depth', '9999'); // include extra field in output, just as the MySQL function did.
$model->set('_depth', $depth);
$model->set('_id', $id);
$models[$id] = $model;
}

// 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;
}

$arr[$id] = $model;
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 */

// usort($arr); array_multisort() ?
$parentA = (int) $modelA->get($parentField);
$parentB = (int) $modelB->get($parentField);
$idA = (int) $modelA->get('_id');

$tree->setTree($arr,true); // set the newly ordered tree.
// If A is the parent of B or both parents are the same.
if ($idA == $parentB || $parentA == $parentB) {
return 0;
}

return 1;
});
}

var_dump($arr);
// Set the newly ordered tree.
$tree->setTree($models, true);

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.
Expand Down