Collections functions
Encryption functions
General functions
Theme permission functions
Resource functions

add_resource_to_collection()

Description

Add resource $resource to collection $collection

Parameters

ColumnTypeDefaultDescription
$resource integer
$collection integer
$smartadd boolean false
$size string ""
$addtype string ""

Return

boolean

Location

include/collections_functions.php lines 254 to 333

Definition

 
function add_resource_to_collection($resource,$collection,$smartadd=false,$size="",$addtype="")
    {
    if((string)(int)
$collection != (string)$collection || (string)(int)$resource != (string)$resource)
        {
        return 
false;
        }

    global 
$collection_allow_not_approved_share$collection_block_restypes;    
    
$addpermitted=collection_writeable($collection) || $smartadd;
    if (
$addpermitted && !$smartadd && (count($collection_block_restypes)>0)) // Can't always block adding resource types since this may be a single resource managed request
        
{
        if(
$addtype=="")
            {
            
$addtype=sql_value("select resource_type value from resource where ref='" escape_check($resource) . "'",0);
            }
        if(
in_array($addtype,$collection_block_restypes))
            {
            
$addpermitted=false;
            }
        }
        
    if (
$addpermitted)    
        {
        
# Check if this collection has already been shared externally. If it has, we must fail if not permitted or add a further entry
        # for this specific resource, and warn the user that this has happened.
        
$keys=get_collection_external_access($collection);
        if (
count($keys)>0)
            {
            
$archivestatus=sql_value("select archive as value from resource where ref='" escape_check($resource) . "'","");
            if (
$archivestatus<&& !$collection_allow_not_approved_share) {global $lang$lang["cantmodifycollection"]=$lang["notapprovedresources"] . $resource;return false;}

            
// Check if user can share externally and has open access. We shouldn't add this if they can't share externally, have restricted access or only been granted access
            
if (!can_share_resource($resource)){return false;}
            
            
# Set the flag so a warning appears.
            
global $collection_share_warning;
            
# Check to see if all shares have expired
            
$expiry_dates=sql_array("select distinct expires value from external_access_keys where collection='" escape_check($collection) . "'");
            
$datetime=time();
            
$collection_share_warning=true;
            foreach(
$expiry_dates as $key => $date)
                {
                if(
$date!="" && $date<$datetime){$collection_share_warning=false;}
                }
            
            for (
$n=0;$n<count($keys);$n++)
                {
                
# Insert a new access key entry for this resource/collection.
                
global $userref;
                
                
sql_query("insert into external_access_keys(resource,access_key,user,collection,date,expires,access,usergroup,password_hash) values ('" escape_check($resource) . "','" escape_check($keys[$n]["access_key"]) . "','$userref','" escape_check($collection) . "',now()," . ($keys[$n]["expires"]==''?'null':"'" escape_check($keys[$n]["expires"]) . "'") . ",'" escape_check($keys[$n]["access"]) . "'," . (($keys[$n]["usergroup"]!="")?"'" escape_check($keys[$n]["usergroup"]) ."'":"NULL") . ",'" $keys[$n]["password_hash"] . "')");
                
                
#log this
                
collection_log($collection,"s",$resource$keys[$n]["access_key"]);
                }
            
            }
        
        
hook("Addtocollectionsuccess""", array( "resourceId" => $resource"collectionId" => $collection ) );
        
        if(!
hook("addtocollectionsql""", array( $resource,$collection$size)))
            {
            
sql_query("delete from collection_resource where resource='" escape_check($resource) . "' and collection='" escape_check($collection) . "'");
            
sql_query("insert into collection_resource(resource,collection,purchase_size) values ('" escape_check($resource) . "','" escape_check($collection) . "','$size')");
            }
        
        
// log this
        
collection_log($collection,"a",$resource);

        
// Clear theme image cache
        
clear_query_cache("themeimage");

        return 
true;
        }
    else
        {
        
hook("Addtocollectionfail""", array( "resourceId" => $resource"collectionId" => $collection ) );
        return 
false;
        }
    }

This article was last updated 10th July 2020 00:35 Europe/London time based on the source file dated 9th July 2020 17:22 Europe/London time.