Collections functions
Encryption functions
General functions
Render functions
Theme permission functions
Video 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 2026 to 2183

Definition

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

    global 
$CACHE_FC_RESOURCES$themes_simple_images;
    
$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];
        }

    
$c_ref_escaped escape_check($c["ref"]);
    
$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);
    
$all_fcs = (isset($ctx["all_fcs"]) && is_array($ctx["all_fcs"]) ? $ctx["all_fcs"] : array());

    
// 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 (ie 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_joins = array();
    
$rca_where '';
    
$fc_permissions_where '';
    if(!
checkperm("v"))
        {
        global 
$usergroup$userref;
        
$rca_joins = array(
            
sprintf('LEFT JOIN resource_custom_access AS rca_u ON r.ref = rca_u.resource AND rca_u.user = \'%s\' AND (rca_u.user_expires IS NULL OR rca_u.user_expires > now())'escape_check($userref)),
            
sprintf('LEFT JOIN resource_custom_access AS rca_ug ON r.ref = rca_ug.resource AND rca_ug.usergroup = \'%s\''escape_check($usergroup)),
        );
        
$rca_where sprintf(
            
'AND (r.access < %1$s OR (r.access IN (%1$s, %2$s) AND ((rca_ug.access IS NOT NULL AND rca_ug.access < %1$s) OR (rca_u.access IS NOT NULL AND rca_u.access < %1$s))))',
            
RESOURCE_ACCESS_CONFIDENTIAL,
            
RESOURCE_ACCESS_CUSTOM_GROUP);
        
$fc_permissions_where featured_collections_permissions_filter_sql("AND""c.ref");
        }

    if(
$use_thumbnail_selection_method && isset($c["thumbnail_selection_method"]) && isset($c["bg_img_resource_ref"]))
        {
        global 
$FEATURED_COLLECTION_BG_IMG_SELECTION_OPTIONS$theme_images_number;

        if(
$c["thumbnail_selection_method"] == $FEATURED_COLLECTION_BG_IMG_SELECTION_OPTIONS["no_image"])
            {
            return array();
            }
        else if(
$c["thumbnail_selection_method"] == $FEATURED_COLLECTION_BG_IMG_SELECTION_OPTIONS["manual"])
            {
            
$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' %s"
,
                
implode(" "$rca_joins),
                
escape_check($c["bg_img_resource_ref"]),
                
$rca_where);
            }
        
// For most_popular_image & most_popular_images we change the limit only if it hasn't been provided by the context.
        
else if($c["thumbnail_selection_method"] == $FEATURED_COLLECTION_BG_IMG_SELECTION_OPTIONS["most_popular_image"] && is_null($limit))
            {
            
$limit 1;
            }
        else if(
$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 = '{$c_ref_escaped}' AND c.`type` = " COLLECTION_TYPE_FEATURED,
    );

    if(
is_featured_collection_category($c))
        {
        
$all_fcs sql_query("SELECT ref, parent FROM collection WHERE `type`='" 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(
count($fcresources) < $themes_simple_images && !$colstack->isEmpty())
            {
            
$checkfc $colstack->pop();
            if(!
in_array($checkfc,$all_fcs_rp))
                {
                
$subfcimages get_collection_resources($checkfc);        
                if(
is_array($subfcimages) && count($subfcimages) > 0)
                    {
                    
$fcresources array_merge($fcresources,$subfcimages);                
                    }
                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);
                }
            }
        
$subquery["where"] = "WHERE r.ref IN ('" implode("','",$fcresources) . "')";
        }

    
$subquery["join"] = implode(" "$subquery["join"]);
    
$subquery["where"] .= {$rca_where} {$fc_permissions_where}";

    
$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),
        (isset(
$union) ? $union ''),
        
sql_limit(null$limit)
    );

    
$fc_resources sql_array($sql"themeimage");

    
$CACHE_FC_RESOURCES[$cache_id] = $fc_resources;

    return 
$fc_resources;
    }

This article was last updated 18th January 2021 11:05 Europe/London time based on the source file dated 13th January 2021 13:50 Europe/London time.