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

get_tree_strings()

Description

This function returns an array of strings that represent the full paths to each tree node passed

Parameters

ColumnTypeDefaultDescription
$resource_nodes array - node tree to parse
$allnodes array false - include paths to all nodes -if false will just include the paths to the end leaf nodes

Return

array $nodestrings - array of strings for all nodes passed in correct hierarchical order

Location

include/node_functions.php lines 2159 to 2232

Definition

 
function get_tree_strings($resource_nodes,$allnodes false)
    {
    global 
$category_tree_add_parents;
    
// Arrange all passed nodes with parents first so that unnecessary paths can be removed
    
$orderednodes = array();
    
$orderednoderefs = array();
    
// Array with node ids as indexes to ease parent tracking
    
$treenodes = array();

    while(
count($resource_nodes) > 0)
        {
        
$todocount count($resource_nodes);
        for(
$n=0;$n $todocount;$n++)
            {    
            if(
                
in_array($resource_nodes[$n]["parent"],array_column($resource_nodes,"ref"))
                &&
                !
in_array($resource_nodes[$n]["parent"],array_column($orderednodes,"ref"))
                &&
                
$resource_nodes[$n]["parent"] != $resource_nodes[$n]["ref"// Cater for potential misconfiguration where parent==self (possibly a legacy from pre-nodes tree config)
                
)
                {
                
// Don't add yet, add once parent has been added
                // By continuing, the resource_nodes array is unchanged, so array column does not need to be reestablished
                
continue;
                }
            
$orderednodes[] = $resource_nodes[$n];
            
$orderednoderefs[] = $resource_nodes[$n]["parent"];
            
$treenodes[$resource_nodes[$n]["ref"]] = $resource_nodes[$n];
            unset(
$resource_nodes[$n]);
            }
        
$resource_nodes array_values($resource_nodes);
        }

    
// Create an array of all branch nodes for each node
    
$nodestrings = array();

    foreach(
$orderednodes as $resource_node)
        {
        
$node_parts = array();
        
// Create an array to hold all the node names, including all parents
        
$node_parts[$resource_node["ref"]] = array();
        
$node_parts[$resource_node["ref"]][] = i18n_get_translated($resource_node["name"]);
        
$nodeparent $resource_node["parent"];
        while(
$nodeparent != "" && isset($treenodes[$nodeparent]))
            {
            if (
$nodeparent == $resource_node["ref"]) { break; } // Cater for potential misconfiguration where parent==self
            
$node_parts[$resource_node["ref"]][] = i18n_get_translated($treenodes[$nodeparent]["name"]);
            
$nodeparent $treenodes[$nodeparent]["parent"];
            }

        
// Create string representation, reversing the order so parents come first
        
$fullpath "";
        for(
$n=count($node_parts[$resource_node["ref"]])-1;$n>=0;$n--)
            {
            
$fullpath .= $node_parts[$resource_node["ref"]][$n];
            if(!
$allnodes)
                {
                
$duplicatepath array_search($fullpath,$nodestrings);                 

                if(
$duplicatepath !== false)
                    {
                    unset(
$nodestrings[$duplicatepath]);
                    }          
                }
            if(
$n>0)
                {
                
$fullpath .= "/";
                }
            }
        
$nodestrings[$resource_node["ref"]] = $fullpath;
        }
    return 
$nodestrings;
    }

This article was last updated 5th October 2022 15:35 Europe/London time based on the source file dated 3rd October 2022 13:50 Europe/London time.