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

toggle_active_state_for_nodes()

Description

Toggle nodes' active state

Parameters

ColumnTypeDefaultDescription
$refs: array
'ref'
'asc'
''
[FIELD_TYPE_CATEGORY_TREE]
'ref';
$refs list Node IDs

Return

array<int, 0|1>

Location

include/node_functions.php lines 2928 to 2976

Definition

 
function toggle_active_state_for_nodes(array $refs): array
    {
    
$refs_chunked db_chunk_id_list($refs);
    
$rtfs_tree array_column(get_resource_type_fields('''ref''asc''', [FIELD_TYPE_CATEGORY_TREE]), 'ref');
    
$tree_nodes_by_rtf = [];
    
$nodes_new_state = [];

    
db_begin_transaction('toggle_node_active_state');

    foreach (
$refs_chunked as $refs_chunk) {
        
$nodes get_nodes_by_refs($refs_chunk);
        
$nodes_to_toggle = [];

        foreach (
$nodes as $node) {
            if (
in_array($node['resource_type_field'], $rtfs_tree)) {
                
// Build a list of nodes, grouped by resource type fields because changes to a tree need to follow its structure
                
$tree_nodes_by_rtf[$node['resource_type_field']][] = $node['ref'];
            } else {
                
// Simple fixed list fields get toggled straight away
                
$nodes_to_toggle[] = $node['ref'];
            }
        }

        if (
$nodes_to_toggle !== []) {
            
ps_query(
                
sprintf(
                    
'UPDATE node AS n
                    INNER JOIN resource_type_field AS rtf ON n.resource_type_field = rtf.ref
                        SET n.`active` = IF(n.`active` = 1, 0, 1)
                        WHERE n.`ref` IN (%s)
                        AND rtf.`type` IN (%s)'
,
                    
ps_param_insert(count($nodes_to_toggle)),
                    
ps_param_insert(count($GLOBALS['FIXED_LIST_FIELD_TYPES']))
                ),
                
array_merge(ps_param_fill($nodes_to_toggle'i'), ps_param_fill($GLOBALS['FIXED_LIST_FIELD_TYPES'], 'i'))
            );
            
$nodes_new_state += array_column(get_nodes_by_refs($nodes_to_toggle), 'active''ref');
        }
    }

    
db_end_transaction('toggle_node_active_state');

    foreach (
$tree_nodes_by_rtf as $rtf => $nodes_list) {
        
$nodes_new_state += toggle_category_tree_nodes_active_state($rtf$nodes_list);
    }

    
clear_query_cache('schema');
    return 
$nodes_new_state;
    }

This article was last updated 29th May 2024 08:35 Europe/London time based on the source file dated 14th May 2024 18:25 Europe/London time.