Collections functions
General functions
Render functions
Theme permission functions
Resource functions

add_resource_nodes()

Description

Add nodes in array to resource

Parameters

ColumnTypeDefaultDescription
$resourceid integer Resource ID to add nodes to
$nodes array array Array of node IDs to add
$checkperms boolean true Check permissions before adding?
$logthis boolean true Log this? Log entries are ideally added when more data on all the changes made is available to make reverts easier.

Return

boolean

Location

include/node_functions.php lines 1247 to 1318

Definition

 
function add_resource_nodes(int $resourceid,$nodes=array(), $checkperms true$logthis=true)
    {
    global 
$userref;
    if(!
is_array($nodes) && (string)(int)$nodes != $nodes)
        {return 
false;}

    
$sql '';
    
$sql_params = [];

    
# check $nodes array values are positive integers and valid for int type node db field
    
$options_db_int = [ 'options' => [ 'min_range' => 1,   'max_range' => 2147483647] ];
    foreach(
$nodes as $node)
        {
        if (!
filter_var($nodeFILTER_VALIDATE_INT$options_db_int))
            {
            return 
false;
            }

        
$sql .= ',(?, ?)';
        
$sql_params[] = 'i';
        
$sql_params[] = $resourceid;
        
$sql_params[] = 'i';
        
$sql_params[] = $node;
        }
    
$sql ltrim($sql',');

    if(
$checkperms && (PHP_SAPI != 'cli' || defined("RS_TEST_MODE")))
        {
        
// Need to check user has permissions to add nodes (unless running from any CLI script other than unit tests)
        
$resourcedata get_resource_data($resourceid);

        if (!
$resourcedata)
            {
            return 
false;
            }
        
        
$access get_edit_access($resourceid,$resourcedata["archive"],false,$resourcedata);
        if(!
$access)
            {return 
false;}

        if(
$resourcedata["lock_user"] > && $resourcedata["lock_user"] != $userref)
            {
            
$error get_resource_lock_message($resourcedata["lock_user"]);
            return 
false;
            }
        }
    if(!
is_array($nodes))
        {
$nodes=array($nodes);}

    
ps_query("INSERT INTO resource_node(resource, node) VALUES {$sql} ON DUPLICATE KEY UPDATE hit_count=hit_count"$sql_params);

    if(
$logthis)
        {
        
$field_nodes_arr = array();
        foreach (
$nodes as $node)
            {
            
$nodedata = array();
            
get_node($node$nodedata);
            if (
$nodedata)
                {
                
$field_nodes_arr[$nodedata["resource_type_field"]][] = $nodedata["name"];
                }
            }
        
        foreach (
$field_nodes_arr as $key => $value)
            {
            
resource_log($resourceid,"e",$key,"","","," implode(",",$value));
            }
        }

    return 
true;
    }

This article was last updated 20th May 2022 08:35 Europe/London time based on the source file dated 6th April 2022 16:35 Europe/London time.