Collections functions
Encryption functions
General functions
Render functions
Theme permission functions
Video 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 6219 to 6285

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;
            }
        }
    
$currentlyrelated=sql_query("SELECT resource, related 
                                   FROM resource_related 
                                  WHERE (resource='
$ref' AND related IN ('" implode("','",$related) . "'))
                                     OR (resource IN ('" 
implode("','",$related) . "') AND related='$ref')");  
    
    
// 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
        
sql_query("DELETE FROM resource_related
                         WHERE (resource='
$ref' AND related IN ('" implode("','",$related) . "'))
                            OR (resource IN ('" 
implode("','",$related) . "') AND related='$ref')");
        }
    else
        {
        
$newrelated = array();
        foreach(
$related as $torelate)
            {
            if(!
in_array($torelate$currentlyrelated_arr) && $torelate != $ref)
                {
                
$newrelated[] = $torelate;
                }
            }
        if(
count($newrelated) > 0)
            {
            
sql_query("INSERT INTO resource_related (resource,related)
                            VALUES ('" 
$ref "','" 
                                   
implode("'),('" $ref "','",$newrelated) .
                                   
"')");
            }
        }
    return 
true;
    }

This article was last updated 14th May 2021 22:35 Europe/London time based on the source file dated 11th May 2021 10:25 Europe/London time.