Collections functions
General functions
Node functions
Render functions
Theme permission functions
User functions
Resource functions

order_tree_nodes()

Description

Order array of tree nodes into logical order - Each parent followed by its child nodes, all following order_by

Parameters

ColumnTypeDefaultDescription
$nodes array Array of detailed nodes

Return

array Full nodes in order

Location

include/node_functions.php lines 2659 to 2712

Definition

 
function order_tree_nodes($nodes)
    {
    if(
count($nodes)==0)
        {
        return [];
        }
    
// Find parent nodes first
    
$parents array_column($nodes,"parent");
    
$toplevels min($parents) > $parents : [0];
    
$orderednodes array_values(array_filter($nodes,function($node) use ($toplevels){return in_array((int)$node["parent"],$toplevels);}));
    
usort($orderednodes,'node_orderby_comparator');
    for(
$n=0;$n count($orderednodes);$n++)
        {
        
$orderednodes[$n]["path"] = $orderednodes[$n]["name"];
        
$orderednodes[$n]["translated_path"] = $orderednodes[$n]["translated_name"] ?? i18n_get_translated($orderednodes[$n]["name"]);
        }

    
// Find child nodes
    
$parents_processed = [];
    while(
count($nodes) > 0)
        {
        
// Loop to find children
        
for($n=0;$n count($orderednodes);$n++)
            {
            if(!
in_array($orderednodes[$n]["ref"],$parents_processed))
                {
                
// Add the children of this node with the the path added (relative to paremnt)
                
$children array_filter($nodes,function($node) use($orderednodes,$n){return (int)$node["parent"] == $orderednodes[$n]["ref"];});
                
// Set order
                
uasort($children,"node_orderby_comparator");
                
$children array_values($children);
                for(
$c=0;$c count($children);$c++)
                    {
                    
$children[$c]["path"] = $orderednodes[$n]["path"] . "/" .  $children[$c]["name"];
                    
$children[$c]["translated_path"] = $orderednodes[$n]["translated_path"] . "/" .  ($children[$c]["translated_name"] ?? i18n_get_translated($children[$c]["name"]));
                    
// Insert the child after the parent and any nodes with a lower order_by value
                    
array_splice($orderednodes$n+1+$c0,  [$children[$c]]);
                    
// Remove child from $treenodes
                    
$pos array_search($children[$c]["ref"],array_column($nodes,"ref"));
                    unset(
$nodes[$pos]);
                    
$nodes array_values($nodes);
                    }
                
$parents_processed[] = $orderednodes[$n]["ref"];
                }
            else
                {
                
$pos array_search($orderednodes[$n]["ref"],array_column($nodes,"ref"));
                unset(
$nodes[$pos]);
                }
            }
        
$nodes array_values($nodes);
        }
    return 
$orderednodes;
    }

This article was last updated 27th April 2024 10:05 Europe/London time based on the source file dated 15th April 2024 11:30 Europe/London time.