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_saved_search_items()

Description

Adds all the resources in the provided search to $collection

Parameters

ColumnTypeDefaultDescription
$collection integer
$search string ""
$restypes string ""
$archivesearch string ""
$order_by string "relevance"
$sort string "desc"
$daylimit string ""
$res_access int "" The ID of the resource access level
$editable_only boolean false If true then only editable resources will be added

Return

boolean

Location

include/collections_functions.php lines 2583 to 2757

Definition

 
function add_saved_search_items($collection$search ""$restypes ""$archivesearch ""
                                
$order_by "relevance"$sort "desc"$daylimit ""$res_access "",$editable_only=false)
    {
    if((string)(int)
$collection != $collection)
        {
        
// Not an integer
        
return false;
        }
    
    global 
$collection_share_warning$collection_allow_not_approved_share$userref$collection_block_restypes$search_all_workflow_states;
    
    
# Adds resources from a search to the collection.
    
if($search_all_workflow_states && != $archivesearch)
        {
        
$search_all_workflow_states false;
        }
   
    
$results=do_search($search$restypes$order_by$archivesearch,[0,-1],$sort,false,DEPRECATED_STARSEARCH,false,false,$daylimit,false,true,false,$editable_only,false,$res_access);

    if(!
is_array($results) || (isset($results["total"]) && $results["total"] == 0))
        {
        return 
false;
        }

    
// To maintain current collection order but add the search items in the correct order we must first move the existing collection resources out the way
    
$searchcount $results["total"];
    if(
$searchcount 0)
        {
        
ps_query("UPDATE collection_resource SET sortorder = if(isnull(sortorder), ?,sortorder + ?) WHERE collection= ?",
            [
            
'i'$searchcount,
            
'i'$searchcount,
            
'i'$collection
            
]
        );
        }

    
// 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 add a further entry
    # for this specific resource, and warn the user that this has happened.
    
$keys array_merge(
        
get_external_shares([
            
'share_collection' => $collection,
            
'share_type' => 0,
            
'ignore_permissions' => true,
        ]),
        
$fc_branch_path_keys
    
);
    
$resourcesnotadded = array(); # record the resources that are not added so we can display to the user
    
$blockedtypes = array();# Record the resource types that are not added 

    
foreach($results["data"] as $result)
        {
        
$resource=$result["ref"];
        
$archivestatus=$result["archive"];
        
        if(
in_array($result["resource_type"],$collection_block_restypes))
            {
            
$blockedtypes[] = $result["resource_type"];
            continue;
            }

        if (
count($keys)>0)
            {
            if ( (
$archivestatus && !$collection_allow_not_approved_share) || !can_share_resource($resource) )
                {
                
$resourcesnotadded[$resource] = $result;
                continue;
                }

            for (
$n=0;$n<count($keys);$n++)
                {
                
$sql '';
                
$params = [];
                if(
$keys[$n]["expires"]==''){$sql .= 'NULL, ';}
                else
                    {
                    
$sql .= '?, ';
                    
$params[] = 's'$params[] = $keys[$n]["expires"];
                    }
                if(
$keys[$n]["usergroup"]==''){$sql .= 'NULL';}
                else
                    {
                    
$sql .= '?';
                    
$params[] = 'i'$params[] = $keys[$n]["usergroup"];
                    }
                
# Insert a new access key entry for this resource/collection.
                
ps_query("INSERT INTO external_access_keys(resource,access_key,user,collection,date,access,password_hash,expires,usergroup) VALUES (?, ?, ?, ?,NOW(), ?, ?, {$sql})",
                    
array_merge([
                    
'i'$resource,
                    
's'$keys[$n]["access_key"],
                    
'i'$userref,
                    
'i'$collection,
                    
's'$keys[$n]["access"],
                    
's'$keys[$n]["password_hash"]
                    ], 
$params)
                );
                
#log this
                
collection_log($collection,LOG_CODE_COLLECTION_SHARED_RESOURCE_WITH,$resource$keys[$n]["access_key"]);
                
                
# Set the flag so a warning appears.
                
$collection_share_warning=true;
                }
            }
        }

    if (
is_array($results["data"]))
        {       
        
$modifyNotAdded hook('modifynotaddedsearchitems''', array($results["data"], $resourcesnotadded));
        if (
is_array($modifyNotAdded)) {
            
$resourcesnotadded $modifyNotAdded;
        }
        
$n 0;
        foreach(
$results["data"] as $result)
            {
            
$resource=$result["ref"];
            if (!isset(
$resourcesnotadded[$resource]) && !in_array($result["resource_type"],$collection_block_restypes))
                {
                
ps_query("DELETE FROM collection_resource WHERE resource=? AND collection=?",array("i",$resource,"i",$collection));
                
ps_query("INSERT INTO collection_resource(resource,collection,sortorder) VALUES (?,?,?)",array("i",$resource,"i",$collection,"s",$n));
                
                
#log this
                
collection_log($collection,LOG_CODE_COLLECTION_ADDED_RESOURCE,$resource);
                
$n++;
                }
            }
        }

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

    if (!empty(
$resourcesnotadded) || count($blockedtypes)>0)
        {
        
# Translate to titles only for displaying them to the user
        
global $view_title_field;
        
$titles = array();
        foreach (
$resourcesnotadded as $resource)
            {
            
$titles[] = i18n_get_translated($resource['field' $view_title_field]);
            }
        if(
count($blockedtypes)>0)
            {
            
$blocked_restypes=array_unique($blockedtypes);
            
// Return a list of blocked resouce types
            
$titles["blockedtypes"]=$blocked_restypes;
            }
        return 
$titles;
        }
        
    return array();
    }

This article was last updated 8th December 2024 10:05 Europe/London time based on the source file dated 4th December 2024 16:35 Europe/London time.