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

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 2971 to 3019

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 17th November 2024 15:35 Europe/London time based on the source file dated 28th October 2024 11:15 Europe/London time.