Collections functions
General functions
Render functions
Theme permission functions
Resource 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 2490 to 2628

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,-1,$sort,false,DEPRECATED_STARSEARCH,false,false,$daylimit,false,true,false,$editable_only,false,$res_access);

    if(!
is_array($results) || count($results) == 0)
        {
        return 
false;
        }
        
    
# 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 get_collection_external_access($collection);
    
$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 
    
    // To maintain current collection order but add the search items in the correct order we must first ove the existing collection resoruces out the way
    
$searchcount count($results);
    if(
$searchcount 0)
        {
        
ps_query("UPDATE collection_resource SET sortorder = if(isnull(sortorder), ?,sortorder + ?) WHERE collection= ?",
            [
            
'i'$searchcount,
            
'i'$searchcount,
            
'i'$collection
            
]
        );
        }

    for (
$r=0;$r<$searchcount;$r++)
        {
        
$resource=$results[$r]["ref"];
        
$archivestatus=$results[$r]["archive"];
        
        if(
in_array($results[$r]["resource_type"],$collection_block_restypes))
            {
            
$blockedtypes[] = $results[$r]["resource_type"];
            continue;
            }

        if (
count($keys)>0)
            {            
            if (
$archivestatus<&& !$collection_allow_not_approved_share)
                {
                
$resourcesnotadded[$resource] = $results[$r];
                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[] = 's'$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))
        {        
        
$modifyNotAdded hook('modifynotaddedsearchitems''', array($results$resourcesnotadded));
        if (
is_array($modifyNotAdded))
            
$resourcesnotadded $modifyNotAdded;

        for (
$n=0;$n<$searchcount;$n++)
            {
            
$resource=$results[$n]["ref"];
            if (!isset(
$resourcesnotadded[$resource]) && !in_array($results[$n]["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);
                }
            }
        }

    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;
        }
    if(
count($blockedtypes)>0)
        {
        }
    return array();
    }

This article was last updated 5th October 2022 13:35 Europe/London time based on the source file dated 5th October 2022 10:35 Europe/London time.