Collections functions
General functions
Node functions
Render functions
Theme permission functions
User functions
Resource 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 6562 to 6638

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 19th March 2024 03:05 Europe/London time based on the source file dated 11th March 2024 14:25 Europe/London time.