Collections functions
General functions
Render functions
Theme permission 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 6055 to 6125

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

    
// Check edit access
    
$access get_edit_access($ref);
    if(!
$access)
        {
        return 
false;
        }
    foreach(
$related as $relate)
        {
        
$access get_edit_access($relate);
        if(!
$access)
            {
            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);
        }
    else if(
$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 5th October 2022 02:05 Europe/London time based on the source file dated 30th September 2022 14:20 Europe/London time.