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

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 6378 to 6467

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'];
        
$smartsearch_archives $smartsearch['archive'];
        
$search_all_archives $smartsearch_archives === 'all';

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

        
$startTime microtime(true);
        global 
$smartsearch_accessoverride;

        
$search_all_workflow_states_original =  $GLOBALS['search_all_workflow_states'];
        if (
$search_all_archives) {
            
# Search saved for all states when $search_all_workflow_states was true so make sure we always apply it for the search.
            
$GLOBALS['search_all_workflow_states'] = true;
        }

        
$results do_search($smartsearch['search'], $smartsearch['restypes'], "relevance"$smartsearch_archives$result_limit"desc"$smartsearch_accessoverride$smartsearch['starsearch'], falsefalse""falsetruefalsefalsefalsenulltrue);

        
$GLOBALS['search_all_workflow_states'] = $search_all_workflow_states_original;

        
# 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...");

            if (
$smartsearch_archives !== '') {
                
$smartsearch_archives explode(","$smartsearch_archives);

                for (
$n 0$n $count_results$n++) {
                    if (
$search_all_archives) {
                        
add_resource_to_collection($results_contents_add[$n], $collectiontrue);
                    } else {
                        
# 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], $collectiontrue);
                        }
                    }
                }
            }
        }

            
$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], $collectiontrue);
            }
        }
            
$endTime microtime(true);
            
$elapsed $endTime $startTime;
            
debug("smart_collections" . (($smart_collections_async) ? "_async:" ":") . $elapsed seconds for " $smartsearch['search']);
    }
}

This article was last updated 11th February 2025 18:35 Europe/London time based on the source file dated 11th February 2025 11:40 Europe/London time.