Collections functions
Encryption functions
General functions
Render functions
Theme permission functions
Video 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 1768 to 1835

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();
    
// 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"))
                )
                {
                
// Don't add yet, add once parent has been added
                
continue;
                }
            
$orderednodes[] = $resource_nodes[$n];
            
$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]))
            {
            
$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 3rd December 2020 13:35 Europe/London time based on the source file dated 30th November 2020 09:45 Europe/London time.