1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-01 04:10:38 +02:00

PR #3016: Reduced Cognitive Complexity

This commit is contained in:
Nick Liu
2018-01-29 02:47:38 -06:00
parent 8102917903
commit 0a88fd1194

41
e107_handlers/model_class.php Executable file → Normal file
View File

@@ -3394,25 +3394,38 @@ class e_tree_model extends e_front_model
while(!empty($nodes))
{
$node = &$nodes[0];
array_shift($nodes);
foreach($rows as $key => $row)
{
$rowParentID = (int) $row[$sort_parent];
$nodeID = (int) $node[$primary_field];
if($rowParentID === $nodeID)
{
$node['_children'][] = &$row;
unset($rows[$key]);
$nodes[] = &$row;
unset($row);
}
}
self::moveRowsToTreeNodes($nodes, $rows, $primary_field, $sort_parent);
}
return array(0 => $root);
}
/**
* Put rows with parent matching the ID of the first node into the next node's children
* @param array &$nodes Current queue of nodes, the first of which may have children added to it
* @param array &rows The remaining rows that have yet to be converted into children of nodes
* @param string $primary_field The field name of the primary key (matches children to parents)
* @param string $sort_parent The field name whose value is the parent ID
* @returns null
*/
private static function moveRowsToTreeNodes(&$nodes, &$rows, $primary_field, $sort_parent)
{
$node = &$nodes[0];
array_shift($nodes);
foreach($rows as $key => $row)
{
$nodeID = (int) $node[$primary_field];
$rowParentID = (int) $row[$sort_parent];
if($nodeID === $rowParentID)
{
$node['_children'][] = &$row;
unset($rows[$key]);
$nodes[] = &$row;
unset($row);
}
}
}
/**
* Flattens a tree into a depth-first array, sorting each node by a field's values
* @param array $tree Tree with child nodes under the "_children" key