Collections functions
General functions
Node functions
Render functions
Theme permission functions
User functions
Resource functions

get_featured_collection_resources()

Description

Get featured collection resources (including from child nodes). For normal FCs this is using the collection_resource table.
For FC categories, this will check within normal FCs contained by that category. Normally used in combination with
generate_featured_collection_image_urls() but useful to determine if a FC category is full of empty FCs.

information should take precedence over internal logic (e.g determining the result limit)

Parameters

ColumnTypeDefaultDescription
$c array Collection data structure similar to the one returned by {@see get_featured_collections()}
$ctx array Extra context used to get FC resources (e.g smart FC?, limit on number of resources returned). Context

Return

array

Location

include/collections_functions.php lines 2826 to 3018

Definition

 
function get_featured_collection_resources(array $c, array $ctx)
    {
    global 
$usergroup$userref$CACHE_FC_RESOURCES$themes_simple_images;
    global 
$FEATURED_COLLECTION_BG_IMG_SELECTION_OPTIONS$theme_images_number;

    if(!isset(
$c["ref"]) || !is_int((int) $c["ref"]))
        {
        return array();
        }

    
$CACHE_FC_RESOURCES = (!is_null($CACHE_FC_RESOURCES) && is_array($CACHE_FC_RESOURCES) ? $CACHE_FC_RESOURCES : array());
    
// create a unique ID for this result set as the context for the same FC may differ
    
$cache_id $c["ref"] . md5(json_encode($ctx));
    if(isset(
$CACHE_FC_RESOURCES[$cache_id]))
        {
        return 
$CACHE_FC_RESOURCES[$cache_id];
        }

    
$limit = (isset($ctx["limit"]) && (int) $ctx["limit"] > ? (int) $ctx["limit"] : null);
    
$use_thumbnail_selection_method = (isset($ctx["use_thumbnail_selection_method"]) ? (bool) $ctx["use_thumbnail_selection_method"] : false);

    
// Smart FCs
    
if(isset($ctx["smart"]) && $ctx["smart"] === true)
        {
        
// Root smart FCs don't have an image (legacy reasons)
        
if(is_null($c["parent"]))
            {
            return array();
            }

        
$node_search NODE_TOKEN_PREFIX $c['ref'];
        
$limit = (!is_null($limit) ? $limit 1);

        
// Access control is still in place (i.e. permissions are honoured)
        
$smart_fc_resources do_search($node_search'''hit_count'0$limit'desc'false0falsefalse''truefalsetrue);
        
$smart_fc_resources = (is_array($smart_fc_resources) ? array_column($smart_fc_resources"ref") : array());

        
$CACHE_FC_RESOURCES[$cache_id] = $smart_fc_resources;
        return 
$smart_fc_resources;
        }

    
// Access control
    
$rca_where ''$rca_where_params = array();
    
$rca_joins = array();$rca_join_params = array();
    
$fc_permissions_where '';$fc_permissions_where_params = [];
    
$union="";$unionparams=[];
    if(!
checkperm("v"))
        {
        
// Add joins for user and group custom access
        
$rca_joins[] = 'LEFT JOIN resource_custom_access AS rca_u ON r.ref = rca_u.resource AND rca_u.user = ? AND (rca_u.user_expires IS NULL OR rca_u.user_expires > now())';
        
$rca_join_params [] = "i"$rca_join_params [] = $userref;

        
$rca_joins[] = 'LEFT JOIN resource_custom_access AS rca_ug ON r.ref = rca_ug.resource AND rca_ug.usergroup = ?';
        
$rca_join_params [] = "i"$rca_join_params [] = $usergroup;

        
$rca_where 'AND (r.access < ? OR (r.access IN (?, ?) AND ((rca_ug.access IS NOT NULL AND rca_ug.access < ?) OR (rca_u.access IS NOT NULL AND rca_u.access < ?))))';
        
$rca_where_params = array("i"RESOURCE_ACCESS_CONFIDENTIAL"i"RESOURCE_ACCESS_CONFIDENTIAL"i"RESOURCE_ACCESS_CUSTOM_GROUP"i"RESOURCE_ACCESS_CONFIDENTIAL"i"RESOURCE_ACCESS_CONFIDENTIAL);

        
$fcf_sql featured_collections_permissions_filter_sql("AND""c.ref");
        if(
is_array($fcf_sql))
            {
            
$fc_permissions_where "AND (c.`type` = ? " $fcf_sql[0] . ")";
            
$fc_permissions_where_params array_merge(["i",COLLECTION_TYPE_FEATURED],$fcf_sql[1]);
            }
        }

    if(
$use_thumbnail_selection_method && isset($c["thumbnail_selection_method"]))
        {
        if(
$c["thumbnail_selection_method"] == $FEATURED_COLLECTION_BG_IMG_SELECTION_OPTIONS["no_image"])
            {
            return array();
            }
        elseif(
$c["thumbnail_selection_method"] == $FEATURED_COLLECTION_BG_IMG_SELECTION_OPTIONS["manual"] && isset($c["bg_img_resource_ref"]))
            {
            
$limit 1;
            
$union sprintf("
                UNION SELECT ref, 1 AS use_as_theme_thumbnail, r.hit_count FROM resource AS r %s WHERE r.ref = ? %s"
,
                
implode(" "$rca_joins),
                
$rca_where);

            
$unionparams array_merge($rca_join_params, ["i",$c["bg_img_resource_ref"]], $rca_where_params);
            }
        
// For most_popular_image & most_popular_images we change the limit only if it hasn't been provided by the context.
        
elseif($c["thumbnail_selection_method"] == $FEATURED_COLLECTION_BG_IMG_SELECTION_OPTIONS["most_popular_image"] && is_null($limit))
            {
            
$limit 1;
            }
        elseif(
$c["thumbnail_selection_method"] == $FEATURED_COLLECTION_BG_IMG_SELECTION_OPTIONS["most_popular_images"] && is_null($limit))
            {
            
$limit $theme_images_number;
            }
        }

    
// A SQL statement. Each array index represents a different SQL clause.
    
$subquery = array(
        
"select" => "SELECT r.ref, cr.use_as_theme_thumbnail, r.hit_count",
        
"from" => "FROM collection AS c",
        
"join" => array_merge(
            array(
                
"JOIN collection_resource AS cr ON cr.collection = c.ref",
                
"JOIN resource AS r ON r.ref = cr.resource AND r.archive = 0 AND r.ref > 0",
            ),
            
$rca_joins
        
),
        
"where" => "WHERE c.ref = ? AND c.`type` = ?",
    );
    
$subquery_params array_merge($rca_join_params, array("i"$c["ref"], "i"COLLECTION_TYPE_FEATURED), $rca_where_params);

    if(
is_featured_collection_category($c))
        {
        
$all_fcs ps_query("SELECT ref, parent FROM collection WHERE `type`=?", array("i",COLLECTION_TYPE_FEATURED), "featured_collections");
        
$all_fcs_rp array_column($all_fcs'parent','ref');

        
// Array to hold resources
        
$fcresources=array();

        
// Create stack of collections to search 
        // (not a queue as we want to get to the lowest child collections first where the resources are)
        
$colstack = new SplStack(); // 
        
$children array_keys($all_fcs_rp,$c["ref"]);
        foreach(
$children as $child_fc)
            {
            
$colstack->push($child_fc);
            }

        while((
is_null($limit) || count($fcresources) < $limit) && !$colstack->isEmpty())
            {
            
$checkfc $colstack->pop();
            if(!
in_array($checkfc,$all_fcs_rp))
                {
                
$subfcimages get_collection_resources($checkfc);
                if(
is_array($subfcimages) && count($subfcimages) > 0)
                    {
                    
// The join defined above specifically excludes any resources that are not in the active archive state,
                    // for the limiting via $ctx to function correctly we'll need to check for each resources state before adding it  to fcresources
                    
$resources get_resource_data_batch($subfcimages);
                    
$fcresources array_merge($fcresources,array_column(array_filter($resources, function($r){return $r['archive'] == "0";}), 'ref'));
                    } 
                continue;
                }
     
            
// Either a parent FC or no results, add sub fcs to stack
            
$children array_keys($all_fcs_rp,$checkfc);
            foreach(
$children as $child_fc)
                {
                
$colstack->push($child_fc);
                }
            }
        
$fcrescount count($fcresources);
        if(
$fcrescount 0)
            {
            
$chunks = [$fcresources];
            
// Large numbers of query parameters can cause errors so chunking may be required for larger collections.
            
if($fcrescount 20000)
                {
                
$chunks array_chunk($fcresources20000);
                }              
            
$fc_resources = [];  
            
$subquery["join"] = implode(" "$subquery["join"]);
            foreach(
$chunks as $fcresources)
                {
                
$subquery["where"] = " WHERE r.ref IN (" ps_param_insert(count($fcresources)) . ")";
                
$subquery_params array_merge($rca_join_params,ps_param_fill($fcresources,"i"), $rca_where_params);
                
$subquery["where"] .= {$rca_where} {$fc_permissions_where}";
                
$subquery_params array_merge($subquery_params,$fc_permissions_where_params);
                
                
$sql sprintf("SELECT DISTINCT ti.ref AS `value`, ti.use_as_theme_thumbnail, ti.hit_count FROM (%s %s) AS ti ORDER BY ti.use_as_theme_thumbnail DESC, ti.hit_count DESC, ti.ref DESC %s",
                    
implode(" "$subquery),
                    
$union,
                    
sql_limit(null$limit)
                );
            
                
$fc_resources array_merge($fc_resourcesps_array($sql,array_merge($subquery_params,$unionparams),"themeimage"));
                }
                
$CACHE_FC_RESOURCES[$cache_id] = $fc_resources;
                return 
$fc_resources;
            }
        }

    
$subquery["join"] = implode(" "$subquery["join"]);
    
$subquery["where"] .= {$rca_where} {$fc_permissions_where}";
    
$subquery_params array_merge($subquery_params,$fc_permissions_where_params);
    
    
$sql sprintf("SELECT DISTINCT ti.ref AS `value`, ti.use_as_theme_thumbnail, ti.hit_count FROM (%s %s) AS ti ORDER BY ti.use_as_theme_thumbnail DESC, ti.hit_count DESC, ti.ref DESC %s",
        
implode(" "$subquery),
        
$union,
        
sql_limit(null$limit)
    );

    
$fc_resources ps_array($sql,array_merge($subquery_params,$unionparams),"themeimage");
    
$CACHE_FC_RESOURCES[$cache_id] = $fc_resources;
    return 
$fc_resources;
    }

This article was last updated 19th March 2024 06:05 Europe/London time based on the source file dated 15th March 2024 17:00 Europe/London time.