Skip to content

Commit

Permalink
Reverted Issue #2929 until we can get it working correctly.
Browse files Browse the repository at this point in the history
  • Loading branch information
CaMer0n committed Dec 23, 2017
1 parent a0d5e2d commit 1a30641
Showing 1 changed file with 23 additions and 117 deletions.
140 changes: 23 additions & 117 deletions e107_handlers/admin_ui.php
Original file line number Diff line number Diff line change
Expand Up @@ -3047,126 +3047,40 @@ public function getTreeModel()
}

/**
* Get ordered models by their parents.
*
* Get ordered models by their parents
* add extra
* @lonalore
* @return e_admin_tree_model
*/
public function getTreeModelSorted()
{
$tree = $this->getTreeModel();

// Helper arrays for preparing new tree model.
$models = array();
$levels = array();


$parentField = $this->getSortParent();
$orderField = $this->getSortField();

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

if(!in_array($depth, $levels))
{
$levels[] = $depth;
}
$parent = $model->get($parentField);
$order = $model->get($orderField);

$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;
}
$model->set('_depth', '9999'); // include extra field in output, just as the MySQL function did.

return ($weightA < $weightB) ? -1 : 1;
});

$direction = 'ASC';

if($direction == 'DESC')
{
$models = array_reverse($models, true);
$arr[$id] = $model;
}

// 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');
// usort($arr); array_multisort() ?
// If A is the parent of B or both parents are the same.
if ($idA == $parentB || $parentA == $parentB) {
return 0;
}

return 1;
});
}
$tree->setTree($arr,true); // set the newly ordered tree.

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

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 Expand Up @@ -4289,25 +4203,20 @@ protected function _modifyListQry($raw = false, $isfilter = false, $forceFrom =
{
$qry = $this->parseCustomListQry($listQry);
}
/* elseif($this->sortField && $this->sortParent) // automated 'tree' sorting.
elseif($this->sortField && $this->sortParent && !deftrue('e_DEBUG_TREESORT')) // automated 'tree' sorting.
{
// $qry = $this->getParentChildQry();
// $this->listOrder = '_treesort '; // .$this->sortField;
// $qry = "SELECT SQL_CALC_FOUND_ROWS a. *, CASE WHEN a.".$this->sortParent." = 0 THEN a.".$this->sortField." ELSE b.".$this->sortField." + (( a.".$this->sortField.")/1000) END AS treesort FROM `#".$this->table."` AS a LEFT JOIN `#".$this->table."` AS b ON a.".$this->sortParent." = b.".$this->pid;
$qry = $this->getParentChildQry();
$this->listOrder = '_treesort '; // .$this->sortField;
// $this->orderStep = ($this->orderStep === 1) ? 100 : $this->orderStep;
}*/
}
else
{
$qry = "SELECT SQL_CALC_FOUND_ROWS ".$tableSFields." FROM ".$tableFrom;
}

}

if(empty($this->listOrder) && $this->sortField && $this->sortParent)
{
$this->listOrder = $this->sortField;
}


// group field - currently auto-added only if there are joins
// TODO - groupField property
$groupField = '';
Expand Down Expand Up @@ -6607,15 +6516,12 @@ public function getList($ajax = false, $view='default')
$request = $controller->getRequest();
$id = $this->getElementId();
$tree = $options = array();
$tree[$id] = $controller->getTreeModel();

if($view === 'default' && $controller->getSortParent() && $controller->getSortField()) // parent / child sorted tree.
{
e107::getDebug()->log("getTreeModelSorted");
$tree[$id] = $controller->getTreeModelSorted();
}
else

if(deftrue('e_DEBUG_TREESORT') && $view === 'default')
{
$tree[$id] = $controller->getTreeModel();
$controller->getTreeModelSorted();
}

// if going through confirm screen - no JS confirm
Expand Down

0 comments on commit 1a30641

Please sign in to comment.