Collections functions
General functions
Render functions
Theme permission functions
Resource functions

update_smart_collection()

Description

Update a smart collection with or without the $smart_collections_async option.

Parameters

ColumnTypeDefaultDescription
$smartsearch_ref int Id of 'savedsearch'.

Return

void

Location

include/collections_functions.php lines 6863 to 6949

Definition

 
function update_smart_collection(int $smartsearch_ref)
    {
    if (
$smartsearch_ref == 0)
        {
        return;
        }
    
$smartsearch=ps_query("select search, collection, restypes, starsearch, archive, created, result_limit from collection_savedsearch where ref = ?", ['i'$smartsearch_ref]);
    global 
$smart_collections_async;

    if (isset(
$smartsearch[0]['search']))
        {
        
$smartsearch=$smartsearch[0];
        
$collection $smartsearch['collection'];

        
# Option to limit results;
        
$result_limit=$smartsearch["result_limit"]; if ($result_limit=="" || $result_limit==0) {$result_limit=-1;}

        
$startTime microtime(true);
        global 
$smartsearch_accessoverride;
        
$results=do_search($smartsearch['search'], $smartsearch['restypes'], "relevance"$smartsearch['archive'],$result_limit,"desc",$smartsearch_accessoverride,$smartsearch['starsearch'],false,false,"",false,true,false,false,false,null,true);
        
//$startTime = microtime(true); 
        # results is a list of the current search without any restrictions
        # we need to compare against the current collection contents to minimize inserts and deletions
        
$current_contents=ps_array("select resource value from collection_resource where collection= ?", ['i'$collection]);

        
$results_contents=array();
        
$counter=0;
        if (!empty(
$results)&&is_array($results))
            {
            foreach(
$results as $results_item)
                { 
                if (isset(
$results_item['ref']))
                    {
                    
$results_contents[]=$results_item['ref'];
                    
$counter++;
                    if (
$counter>=$result_limit && $result_limit!=-1)
                        {    
                        break;
                        }
                    }
                }
            }

        
$results_contents_add array_values(array_diff($results_contents$current_contents));
        
$current_contents_remove array_values(array_diff($current_contents$results_contents));

        
$count_results=count($results_contents_add);
        if (
$count_results>0)
            {
            
# Add any new resources
            
debug"smart_collections" . (($smart_collections_async) ? "_async:" ":") . " Adding $count_results resources to collection...");

            
# Selected archive states returned as a string
            
$smartsearch_archives=ps_value("select archive AS value from collection_savedsearch where ref= ?", ['i'$smartsearch_ref],"");

            if(isset(
$smartsearch_archives))
                {
                
$smartsearch_archives=explode(",",$smartsearch_archives);

                for (
$n=0;$n<$count_results;$n++)
                    {
                    
# Check the resource archive state    
                    
$archivestatus=ps_value("SELECT archive AS value FROM resource WHERE ref = ?",["i",$results_contents_add[$n]],"");
                
                    if (
in_array($archivestatus$smartsearch_archives))
                        {
                        
add_resource_to_collection($results_contents_add[$n],$collection,true);
                        }
                    }
                }
            }

            
$count_contents=count($current_contents_remove);
            if (
$count_contents>0)
                {
                
# Remove any resources no longer present.
                
debug"smart_collections" . (($smart_collections_async) ? "_async:" ":") . " Removing $count_contents resources...");
                for (
$n=0;$n<$count_contents;$n++)
                    {                
                    
remove_resource_from_collection($current_contents_remove[$n],$collection,true);
                    }
                }
            
$endTime microtime(true);  
            
$elapsed $endTime $startTime;
            
debug("smart_collections" . (($smart_collections_async) ? "_async:" ":") . $elapsed seconds for " $smartsearch['search']);
        }
    }

This article was last updated 31st January 2023 21:05 Europe/London time based on the source file dated 6th January 2023 19:05 Europe/London time.