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

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

Return

array Branch path structure starting from root to the searched node (inclusive)

Location

include/node_functions.php lines 2291 to 2346

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];
        }

    
$nodes_ref_list array_column($nodes'ref');

    
$found_node_index array_search($id$nodes_ref_list);
    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))
        {
        
// Parent node path is already known (cached), use it instead of recalculating it
        
if(isset($NODE_BRANCH_PATHS_CACHE[$nodes_list_id][$node_parent]))
            {
            
$parent_branch_reversed array_reverse($NODE_BRANCH_PATHS_CACHE[$nodes_list_id][$node_parent]);
            
$path array_merge($path$parent_branch_reversed);
            break;
            }

        
$found_node_index array_search($node_parent$nodes_ref_list);
        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 17th November 2024 15:35 Europe/London time based on the source file dated 28th October 2024 11:15 Europe/London time.