Collections functions
Encryption functions
General functions
Render functions
Theme permission functions
Video functions
Resource functions

search_public_collections()

Description

Performs a search for featured collections / public collections.

Parameters

ColumnTypeDefaultDescription
$search string ""
$order_by string "name"
$sort string "ASC"
$exclude_themes boolean true
$exclude_public boolean false
$include_resources boolean false
$override_group_restrict boolean false
$search_user_collections boolean false
$fetchrows integer -1

Return

array

Location

include/collections_functions.php lines 676 to 818

Definition

 
function search_public_collections($search=""$order_by="name"$sort="ASC"$exclude_themes=true$exclude_public=false$include_resources=false$override_group_restrict=false$search_user_collections=false$fetchrows=-1)
    {
    global 
$userref;

    
$keysql "";
    
$sql "";

    
// Validate sort & order_by
    
$sort = (in_array($sort, array("ASC""DESC")) ? $sort "ASC");
    
$valid_order_bys = array("fullname""name""ref""count""type""created");
    
$order_by = (in_array($order_by$valid_order_bys) ? $order_by "name");

    
# Keywords searching?
    
$keywords=split_keywords($search);  
    if (
strlen($search)==&& !is_numeric($search)) 
        {
        
# A-Z search
        
$sql="and c.name like '" escape_check($search) . "%'";
        }
    elseif (
substr($search,0,16)=="collectiontitle:")
        {
        
# A-Z specific title search
        
        
$newsearch="";
        for (
$n=0;$n<count($keywords);$n++)
            {
               if (
substr($keywords[$n],0,16)=="collectiontitle:"$newsearch.=" ".substr($keywords[$n],16);    // wildcard * - %
            
}

        
$newsearch strpos($newsearch,'*')===false '%' trim($newsearch) . '%' str_replace('*''%'trim($newsearch));
        
$sql="and c.name like '$newsearch'";
            
        }
    if (
strlen($search)>|| is_numeric($search))
        {  
        
        
$keyrefs=array();
        for (
$n=0;$n<count($keywords);$n++)
            {
            if (
substr($keywords[$n],0,16)!="collectiontitle:")
                {
                if (
substr($keywords[$n],0,16)=="collectionowner:"
                    {
                    
$keywords[$n]=substr($keywords[$n],16);
                    
$keyref=$keywords[$n];
                       
$sql.=" and (u.username rlike '$keyref' or u.fullname rlike '$keyref')";    
                    }
                elseif (
substr($keywords[$n],0,19)=="collectionownerref:"
                    {
                    
$keywords[$n]=substr($keywords[$n],19);
                    
$keyref=$keywords[$n];
                       
$sql.=" and (c.user='$keyref')";
                    } 
                else
                    {
                    if (
substr($keywords[$n],0,19)=="collectionkeywords:"$keywords[$n]=substr($keywords[$n],19);
            
# Support field specific matching - discard the field identifier as not appropriate for collection searches.
            
if (strpos($keywords[$n],":")!==false) {$keywords[$n]=substr($keywords[$n],strpos($keywords[$n],":")+1);}
                    
$keyref=resolve_keyword($keywords[$n],false);
                    if (
$keyref!==false) {$keyrefs[]=$keyref;}
                    
$keysql .= " JOIN collection_keyword AS k" $n " ON k" $n ".collection = c.ref AND (k" $n ".keyword = '$keyref')";
                    }
                
//$keysql="or keyword in (" . join (",",$keyrefs) . ")";
                
}
            }
        
        global 
$search_public_collections_ref;
        if (
$search_public_collections_ref && is_numeric($search)){$spcr="or c.ref='" escape_check($search) . "'";} else {$spcr="";}    
        
//$sql.="and (c.name rlike '%$search%' or u.username rlike '%$search%' or u.fullname rlike '%$search%' $spcr )";
        
}
    
    
# Restrict to parent, child and sibling groups?
    
global $public_collections_confine_group,$userref,$usergroup;
    if (
$public_collections_confine_group && !$override_group_restrict)
        {
        
# Form a list of all applicable groups
        
$groups=array($usergroup); # Start with user's own group
        
$groups=array_merge($groups,sql_array("select ref value from usergroup where parent='" escape_check($usergroup) . "'")); # Children
        
$groups=array_merge($groups,sql_array("select parent value from usergroup where ref='" escape_check($usergroup) . "'")); # Parent
        
$groups=array_merge($groups,sql_array("select ref value from usergroup where parent<>0 and parent=(select parent from usergroup where ref='" escape_check($usergroup) . "')")); # Siblings (same parent)
        
        
$sql.=" and u.usergroup in ('" join ("','",$groups) . "')";
        }

    
$filter_by_user = ($search_user_collections "AND c.user = '" escape_check($userref) . "'" "");

    
// Add extra elements to the SELECT statement if needed
    
$select_extra "";
    if(
$include_resources)
        {
        
$select_extra .= ", count(DISTINCT cr.resource) AS count";
        }

    
// Filter by type (public/featured collections)
    
$public_type_filter_sql "c.`type` = " COLLECTION_TYPE_PUBLIC;
    
$featured_type_filter_sql sprintf(
        
"(c.`type` = %s %s)",
        
COLLECTION_TYPE_FEATURED,
        
trim(featured_collections_permissions_filter_sql("AND""c.ref"))
    );
    if(
$exclude_themes)
        {
        
$featured_type_filter_sql "";
        }
    else if(
$exclude_public && !$search_user_collections)
        {
        
$public_type_filter_sql "";
        }
    
$type_filter_sql sprintf(
        (
$public_type_filter_sql != "" && $featured_type_filter_sql != "" "(%s%s)" "%s%s"),
        
$public_type_filter_sql,
        (
$public_type_filter_sql != "" && $featured_type_filter_sql != "" " OR {$featured_type_filter_sql}$featured_type_filter_sql)
    );

    
$main_sql sprintf(
        
"SELECT *
           FROM (
                         SELECT DISTINCT c.*,
                                u.username,
                                u.fullname,
                                if(c.`type` = %s AND count(DISTINCT cr.resource) = 0, true, false) AS is_featured_collection_category
                                %s
                           FROM collection AS c
                      LEFT JOIN collection_resource AS cr ON c.ref = cr.collection
                LEFT OUTER JOIN user AS u ON c.user = u.ref
                LEFT OUTER JOIN collection_keyword AS k ON c.ref = k.collection
                          %s # keysql
                          WHERE %s # type_filter_sql
                            %s
                       GROUP BY c.ref
                       ORDER BY %s
           ) AS pfcs
          WHERE (pfcs.`type` = 4 OR (pfcs.`type` = 3 AND pfcs.is_featured_collection_category = false))"
,
        
COLLECTION_TYPE_FEATURED,
        
$select_extra,
        
$keysql,
        
$type_filter_sql,
        
$filter_by_user $sql# extra filters
        
"{$order_by} {$sort}"
    
);

    return 
sql_query($main_sql''$fetchrows);
    }

This article was last updated 24th November 2020 18:05 Europe/London time based on the source file dated 23rd November 2020 16:00 Europe/London time.