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

update_related_resource()

Description

Update related resources - add new related resource(s) or delete existing

Parameters

ColumnTypeDefaultDescription
$ref int ID of primary resource
$related
$add boolean true Add relationship? If false this will delete the specified relationships
related int|array Resource ID or array of resource IDs to link to current resource

Return

boolean

Location

include/resource_functions.php lines 6811 to 6887

Definition

 
function update_related_resource($ref,$related,$add=true)
    {
    if (!
is_int_loose($ref) || (!is_int_loose($related) && !is_array($related)))
        {
        return 
false;
        }
    if(
is_array($related))
        {
        
$related array_filter($related,"is_int_loose");
        }
    else
        {
        
$related = array((int)$related);
        }

    if (
count($related) == 0)
        {
        return 
false;
        }

    
// Check edit access
    
$access get_edit_access($ref);
    if(!
$access)
        {
        
debug('Failed to update related resources for ref ' $ref ' - no edit access to this resource');
        return 
false;
        }
    foreach(
$related as $relate)
        {
        
$access get_edit_access($relate);
        if(!
$access)
            {
            
debug('Failed to update related resources for ref ' $ref ' - user cannot edit ref ' $relate);
            return 
false;
            }
        }

    
// This params array can be used for both SELECT and DELETE
    
$relatedparams array_merge(["i",$ref],ps_param_fill($related,"i"),ps_param_fill($related,"i"),["i",$ref]);

    
$query "SELECT resource, related FROM resource_related  WHERE (resource = ? AND related IN (" ps_param_insert(count($related)) . "))
      OR (resource IN (" 
ps_param_insert(count($related)) . ") AND related = ?)";
    
$currentlyrelated ps_query($query,$relatedparams);

    
// Create array of all related resources
    
$currentlyrelated_arr array_unique(array_merge(
        
array_column($currentlyrelated,"related"),
        
array_column($currentlyrelated,"resource")
        ));

    if(
count($currentlyrelated_arr) > && !$add)
        {
        
// Relationships exist and we want to remove
        
$query "DELETE FROM resource_related  WHERE (resource = ? AND related IN (" ps_param_insert(count($related)) . "))
        OR (resource IN (" 
ps_param_insert(count($related)) . ") AND related = ?)";
        
ps_query($query,$relatedparams);
        }
    elseif(
$add)
        {
        
$newrelated = array();
        foreach(
$related as $torelate)
            {
            if(!
in_array($torelate$currentlyrelated_arr) && $torelate != $ref)
                {
                
$newrelated[] = $torelate;
                }
            }
        if(
count($newrelated) > 0)
            {
            
ps_query("INSERT INTO resource_related (resource,related)
                            VALUES ('" 
$ref "','" .
                                   
implode("'),('" $ref "','",$newrelated) .
                                   
"')");
            }
        }
    return 
true;
    }

This article was last updated 4th December 2024 08:35 Europe/London time based on the source file dated 28th November 2024 12:10 Europe/London time.