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

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 1298 to 1373

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;}

    if (
count($nodes) == 0)
        {
        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"],$resourcedata);
        if(!
$access)
            {return 
false;}

        if(
$resourcedata["lock_user"] > && $resourcedata["lock_user"] != $userref)
            {
            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(NODE_NAME_STRING_SEPARATOR,$value));
            }
        }

    return 
true;
    }

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.