Collections functions
General functions
Node functions
Render functions
Theme permission functions
User functions
Resource 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 2045 to 2109

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

        
# 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 24th May 2024 11:05 Europe/London time based on the source file dated 14th May 2024 18:25 Europe/London time.