Collections functions
General functions
Node functions
Render functions
Theme permission functions
User functions
Resource functions

toggle_category_tree_nodes_active_state()

Description

Toggle category tree nodes' active state

Parameters

ColumnTypeDefaultDescription
$rtf int Resource type field ID
$node_refs: array
null
array_flip$node_refs;
$node_refs list

Return

array<int, 0|1>

Location

include/node_functions.php lines 2988 to 3055

Definition

 
function toggle_category_tree_nodes_active_state(int $rtf, array $node_refs): array {
    
$is_not_active = fn(int $active): bool => $active === 0;
    
$nodes_to_enable = [];
    
$nodes_to_disable = [];

    
$rtf_nodes get_cattree_nodes_ordered($rtfnulltrue);
    
// Remove the fake "root" node which get_cattree_nodes_ordered() is adding since we won't be
    // using get_cattree_node_strings() with it.
    
array_shift($rtf_nodes);
    
$rtf_nodes_indexed array_column($rtf_nodesnull'ref');
    
$nodes_ordered array_keys(array_intersect_key($rtf_nodes_indexedarray_flip($node_refs)));

    foreach (
$nodes_ordered as $node_ref) {
        if (
in_array($node_ref$nodes_to_disable)) {
            
// Child node disabled previously by a parent - no need to carry on processing it (already done because of 
            // a parents' change on its path)
            
continue;
        }

        
$node $rtf_nodes_indexed[$node_ref];
        
$node_is_active $node['active'] === 1;

        
// Check that no parent on this branch path is disabled
        
$node_branch array_column(
            
compute_node_branch_path(array_values($rtf_nodes_indexed), $node['ref']),
            
'active',
            
'ref'
        
);
        
array_pop($node_branch);
        if (
array_filter($node_branch$is_not_active) !== []) {
            
// Node should already be disabled, add it to the list so we can return it
            
$nodes_to_disable[] = $node['ref'];
            continue;
        }

        if (
$node_is_active) {
            
// Disable - this MUST propagate to its children (if any)
            
$branch_refs array_column(
                
cattree_node_flatten(
                    
array_merge(
                        
$node,
                        [
                            
'resource' => null# Fake it for cattree_node_flatten()
                            
'children' => get_node_tree($node['ref'], $rtf_nodes_indexed),
                        ]
                    )
                ),
                
'ref'
            
);

            
$nodes_to_disable array_merge($nodes_to_disable$branch_refs);
            foreach (
$branch_refs as $path_node_ref) {
                
$rtf_nodes_indexed[$path_node_ref]['active'] = 0;
            }
        } else {
            
// Activate - this MUST NOT propagate to its children (if any)
            
$nodes_to_enable[] = $node_ref;

            
// Pretend this has been done in case there's going to be a child node in the queue waiting to be toggled 
            
$rtf_nodes_indexed[$node_ref]['active'] = 1;
        }
    }

    
update_node_active_state($nodes_to_enabletrue);
    
update_node_active_state($nodes_to_disablefalse);
    
    return 
array_column(get_nodes_by_refs(array_merge($nodes_to_enable$nodes_to_disable)), 'active''ref');
}

This article was last updated 29th April 2024 21:35 Europe/London time based on the source file dated 15th April 2024 11:30 Europe/London time.