Coding standards
Security in ResourceSpace
Developer reference
Database
Action functions
Admin functions
Ajax functions
Annotation functions
API functions
Collections functions
Comment functions
Config functions
CSV export functions
Dash functions
Debug functions
Encryption functions
Facial recognition functions
File functions
General functions
Language functions
Log functions
Login functions
Message functions
Migration functions
Node functions
PDF functions
Plugin functions
Render functions
Reporting functions
Request functions
Research functions
Slideshow functions
Theme permission functions
User functions
Video functions
Database functions
Metadata functions
Resource functions
Search functions
Map functions
Job functions
Tab functions
Test 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
$sort_order integer null Sort order of resource in collection

Return

boolean | string

Location

include/collections_functions.php lines 356 to 499

Definition

 
function add_resource_to_collection(
    
$resource,
    
$collection,
    
$smartadd=false,
    
$size="",
    
$addtype="",
    
bool $col_access_control null,
    array 
$external_shares null,
    
string $search '',
    
int $sort_order null
)
    {
    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)
        {
        
$collection_data get_collection($collectiontrue);

        
// 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 = [];
        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 $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, sortorder) VALUES (?, ?, ?)',
                [
'i'$collection'i'$resource'i'$sort_order ?: 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);
            }
        
        if(
$collection_data !== false && $collection_data['type'] != COLLECTION_TYPE_SELECTION) {
            
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 4th December 2024 08:35 Europe/London time based on the source file dated 27th November 2024 15:30 Europe/London time.