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 1865 to 1916

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]))
            {
            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 20th January 2021 19:35 Europe/London time based on the source file dated 20th January 2021 11:50 Europe/London time.