Coding standards
Security in ResourceSpace
Developer reference
Database
Action functions
Admin functions
Ajax functions
Annotation functions
API functions
Collections functions
Comment functions
Config functions
CSV export functions
Dash functions
Debug functions
Encryption functions
Facial recognition functions
File functions
General functions
Language functions
Log functions
Login functions
Message functions
Migration functions
Node functions
PDF functions
Plugin functions
Render functions
Reporting functions
Request functions
Research functions
Slideshow functions
Theme permission functions
User functions
Video functions
Database functions
Metadata functions
Resource functions
Search functions
Map functions
Job functions
Tab functions
Test 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 2687 to 2756

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"];
        if (!isset(
$orderednodes[$n]["translated_name"]) || is_i18n_language_string($orderednodes[$n]["translated_name"]))
            {
            
// Where translated_path is in i18n format, add_sql_node_language() couldn't find a match with the current language. Translate it to get default language.
            
$orderednodes[$n]["translated_path"] = i18n_get_translated($orderednodes[$n]["name"]);
            }
        else
            {
            
$orderednodes[$n]["translated_path"] = $orderednodes[$n]["translated_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"];
                    if (!isset(
$children[$c]["translated_name"]) || is_i18n_language_string($children[$c]["translated_name"]))
                        {
                        
// Where translated_path is in i18n format, add_sql_node_language() couldn't find a match with the current language. Translate it to get default language.
                        
$children[$c]["translated_path"] = $orderednodes[$n]["translated_path"] . "/" .  i18n_get_translated($children[$c]["name"]);
                        }
                    else
                        {
                        
$children[$c]["translated_path"] = $orderednodes[$n]["translated_path"] . "/" .  ($children[$c]["translated_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 4th December 2024 08:35 Europe/London time based on the source file dated 28th November 2024 12:10 Europe/London time.