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

get_cattree_nodes_ordered()

Description

This function returns an array of category tree nodes in the hierarchical sequence defined in manage options

Parameters

ColumnTypeDefaultDescription
$treefield array - the category tree field to be processed
$resource integer null - the resource against which to check for selected nodes - optional
$allnodes array false - is true if all nodes in the structure are returned, false if only selected nodes are returned

Return

array $flatnodes - the array of nodes returned in correct hierarchical order

Location

include/node_functions.php lines 2076 to 2140

Definition

 
function get_cattree_nodes_ordered($treefield$resource=null$allnodes=false) {
    
$sql_query "SELECT n.ref, n.resource_type_field, n.name, coalesce(n.parent, 0) parent, n.order_by, n.active, rn.resource FROM node n ";
    if (
$allnodes)
        {
        
$sql_query .= " LEFT OUTER ";
        }
    
$sql_query .=  "JOIN resource_node rn on rn.resource = ? and rn.node = n.ref WHERE n.resource_type_field=? order by n.parent, n.order_by";

    
$nodeentries ps_query($sql_query, array("i", (int) $resource"i", (int) $treefield));

    
# Any node that doesn't have a parent in the nodes supplied becomes a parent in this context as its real parent might not have been selected.
    # For example, when viewing options set when $category_tree_add_parents=false
    # Needed for sorting below to ensure the container "ROOT" has child items to return.
    
$selected_nodes array_column($nodeentries'ref');
    for (
$n=0$n count($nodeentries); ++$n)
        {
        if (
$nodeentries[$n]['parent'] !== && !in_array($nodeentries[$n]['parent'], $selected_nodes))
            {
            
$nodeentries[$n]['parent'] = 0;
            }
        }

    
# Category trees have no container root, so create one to carry all top level category tree nodes which don't have a parent
    
$rootnode cattree_node_creator(00"ROOT"null0null, array(), 1);

    
$nodeswithpointers = array(=> &$rootnode);

    foreach(
$nodeentries as $nodeentry) {
        
$ref $nodeentry['ref'];
        
$resource_type_field $nodeentry['resource_type_field'];
        
$name $nodeentry['name'];
        
$parent $nodeentry['parent'];
        
$order_by $nodeentry['order_by'];
        
$resource $nodeentry['resource'];
        
$active $nodeentry['active'] ?? 1;

        
# Save the current node prior to establishing the pointer which can null the current node
        
$savednode=null;
        if (isset(
$nodeswithpointers[$ref])) {
            
$savednode $nodeswithpointers[$ref];
        }
        
        
# Establish a pointer so that this node will be a child of its parent node
        #  This means that the current node entry will be "added" to the children of the parent entry
        
$nodeswithpointers[$ref] = &$nodeswithpointers[$parent]['children'][];
        
        
# Create an entry for the current node with any existing children at this point 
        
$existingchildren = array();
        if (
$savednode && isset($savednode['children'])) {
            
$existingchildren $savednode['children'];
        }   
        
$nodeswithpointers[$ref] = cattree_node_creator($ref$resource_type_field$name$parent$order_by$resource$existingchildren$active);
    }

    
# Flatten the tree starting at the root                                                          
    
$flatnodes cattree_node_flatten($rootnode);

    
$returned_nodes=array();
    foreach(
$flatnodes as $flatnode) {
        if (
$allnodes || $flatnode['resource']!='') {
            
$returned_nodes[$flatnode['ref']]=$flatnode;
        }
    }
    return 
$returned_nodes;
}

This article was last updated 13th December 2024 20:35 Europe/London time based on the source file dated 11th December 2024 22:35 Europe/London time.