Collections functions
General functions
Render functions
Theme permission functions
Resource functions

add_resource_to_collection()

Description

Add resource $resource to collection $collection

to allow this function to determine it but it may have performance issues.
to allow this function to determine it but it will affect performance.

Parameters

ColumnTypeDefaultDescription
$resource integer
$collection integer
$smartadd boolean false
$size string ""
$addtype string ""
$col_access_control boolean null Collection access control. Is user allowed to add to it? You can leave it null
$external_shares array null List of external share keys. {@see get_external_shares()}. You can leave it null
$search string '' Optionsl search string. Used to update resource_node hit count

Return

boolean | string

Location

include/collections_functions.php lines 357 to 496

Definition

 
function add_resource_to_collection(
    
$resource,
    
$collection,
    
$smartadd=false,
    
$size="",
    
$addtype="",
    
bool $col_access_control null,
    array 
$external_shares null,
    
string $search ''
)
    {
    global 
$lang;

    if(!
is_int_loose($collection) || !is_int_loose($resource))
        {
        return 
$lang["cantmodifycollection"];
        }

    global 
$collection_allow_not_approved_share$collection_block_restypes;
    
$addpermitted $col_access_control ?? (
        (
collection_writeable($collection) && !is_featured_collection_category_by_children($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=ps_value("SELECT resource_type value FROM resource WHERE ref = ?",["i",$resource],0);
            }
        if(
in_array($addtype,$collection_block_restypes))
            {
            
$addpermitted=false;
            }
        }

    if (
$addpermitted)
        {
        
// If this is a featured collection  apply all the external access keys from the categories which make up its 
        // branch path to prevent breaking existing shares for any of those featured collection categories.
        
$fc_branch_path_keys = [];
        
$collection_data get_collection($collectiontrue);
        if(
$collection_data !== false && $collection_data['type'] === COLLECTION_TYPE_FEATURED)
            {
            
$branch_category_ids array_column(
                
// determine the branch from the parent because the keys for the collection in question will be done below
                
get_featured_collection_category_branch_by_leaf((int)$collection_data['parent'], []),
                
'ref'
            
);
            foreach(
$branch_category_ids as $fc_category_id)
                {
                
$fc_branch_path_keys array_merge(
                    
$fc_branch_path_keys,
                    
get_external_shares([
                        
'share_collection' => $fc_category_id,
                        
'share_type' => 0,
                        
'ignore_permissions' => true
                    
])
                );
                }
            }


        
# 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 array_merge(
            
$external_shares ?? get_external_shares(array("share_collection"=>$collection,"share_type"=>0,"ignore_permissions"=>true)),
            
$fc_branch_path_keys
        
);
        if (
count($keys)>0)
            {
            
$archivestatus=ps_value("SELECT archive AS value FROM resource WHERE ref = ?",["i",$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=ps_array("SELECT DISTINCT expires value FROM external_access_keys WHERE collection = ?",["i",$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;
                
ps_query(
                    
'INSERT INTO external_access_keys(resource, access_key, user, collection, `date`, expires, access, usergroup, password_hash) VALUES (?, ?, ?, ?, now(), ?, ?, ?, ?)',
                    [
                        
'i'$resource,
                        
's'$keys[$n]['access_key'],
                        
'i'$userref,
                        
'i'$collection ?: null,
                        
's'$keys[$n]['expires'] ?: null,
                        
'i'$keys[$n]['access'],
                        
'i'$keys[$n]['usergroup'] ?: null,
                        
's'$keys[$n]['password_hash'] ?: null,
                    ]
                );
                
collection_log($collection,LOG_CODE_COLLECTION_SHARED_RESOURCE_WITH,$resource$keys[$n]["access_key"]);
                }            
            }

        
hook("Addtocollectionsuccess""", array( "resourceId" => $resource"collectionId" => $collection ) );

        if(!
hook("addtocollectionsql""", array( $resource,$collection$size)))
            {
            
ps_query('DELETE FROM collection_resource WHERE collection = ? AND resource = ?', ['i'$collection'i'$resource]);
            
ps_query(
                
'INSERT INTO collection_resource(collection, resource, purchase_size) VALUES (?, ?, ?)',
                [
'i'$collection'i'$resource's'$size ?: null]
            );
            }
        
        
# Update the hitcounts for the search nodes (if search specified)
        
if (strpos($search,NODE_TOKEN_PREFIX) !== false)
            {
            
update_node_hitcount_from_search($resource,$search);
            }
        
        
collection_log($collection,LOG_CODE_COLLECTION_ADDED_RESOURCE,$resource);

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

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

This article was last updated 5th October 2022 03:05 Europe/London time based on the source file dated 4th October 2022 09:50 Europe/London time.