Collections functions
Encryption functions
General functions
Render functions
Theme permission functions
Video functions
Resource functions

compute_node_branch_path()

Description

Get to the root of the branch starting from a node.

IMPORTANT: the term nodes here is generic, it refers to a tree node structure containing at least ref and parent

Parameters

ColumnTypeDefaultDescription
$nodes array List of nodes to search through (MUST contain elements with at least the "ref" index)
$id integer Node ref we compute the branch path for
$carry array Branch structure data which is carried forward. List of nodes, first item is the ROOT node

Return

array Branch path structure starting from root to the searched node

Location

include/node_functions.php lines 1888 to 1948

Definition

 
function compute_node_branch_path(array $nodesint $id)
    {
    if(empty(
$nodes))
        {
        return array();
        }

    global 
$NODE_BRANCH_PATHS_CACHE;
    
$NODE_BRANCH_PATHS_CACHE = (!is_null($NODE_BRANCH_PATHS_CACHE) && is_array($NODE_BRANCH_PATHS_CACHE) ? $NODE_BRANCH_PATHS_CACHE : array());
    
// create a unique ID for this list of nodes since these can be used for anything
    
$nodes_list_id md5(json_encode($nodes));

    if(isset(
$NODE_BRANCH_PATHS_CACHE[$nodes_list_id][$id]))
        {
        return 
$NODE_BRANCH_PATHS_CACHE[$nodes_list_id][$id];
        }

    
$found_node_index array_search($idarray_column($nodes'ref'));
    if(
$found_node_index === false)
        {
        return array();
        }

    
$node $nodes[$found_node_index];
    
$node_parent = (isset($node["parent"]) && $node["parent"] > ? (int) $node["parent"] : null);

    
$path = array($node);
    while(!
is_null($node_parent))
        {
        
$id $node_parent;
        if(isset(
$NODE_BRANCH_PATHS_CACHE[$nodes_list_id][$id]))
            {
            
# Check the nodes found before returning cached value to handle multiple branches containing the same node e.g. resource in multiple collections.
            
$available_nodes = array();
            foreach (
$NODE_BRANCH_PATHS_CACHE[$nodes_list_id][$id] as $node_available)
                {
                
$available_nodes[]=$node_available['ref'];
                }
            if (
in_array($path[0]['ref'],$available_nodes))
                {
                return 
$NODE_BRANCH_PATHS_CACHE[$nodes_list_id][$id];
                }
            }

        
$found_node_index array_search($idarray_column($nodes'ref'));
        if(
$found_node_index === false)
            {
            break;
            }

        
$node $nodes[$found_node_index];
        
$node_parent = (isset($node["parent"]) && $node["parent"] > ? (int) $node["parent"] : null);

        
$path[] = $node;
        }

    
$path_reverse array_reverse($path);
    
$NODE_BRANCH_PATHS_CACHE[$nodes_list_id][$id] = $path_reverse;

    return 
$path_reverse;
    }

This article was last updated 21st April 2021 18:05 Europe/London time based on the source file dated 29th March 2021 12:15 Europe/London time.