Collections functions
General functions
Node functions
Render functions
Theme permission functions
User 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 1293 to 1368

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 19th March 2024 05:35 Europe/London time based on the source file dated 15th March 2024 17:00 Europe/London time.