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

get_user_collections()

Description

Return all collections belonging to or shared with $user

Parameters

ColumnTypeDefaultDescription
$user integer
$find string "" A search string
$order_by string "name" Column to sort by
$sort string "ASC" ASC or DESC sort order
$fetchrows integer -1 How many rows to fetch
$auto_create boolean true Create a default My Collection if one doesn't exist

Return

void

Location

include/collections_functions.php lines 16 to 159

Definition

 
function get_user_collections($user,$find="",$order_by="name",$sort="ASC",$fetchrows=-1,$auto_create=true)
    {
    global 
$usergroup;

    
$sql "";
    
$keysql "";
    
$extrasql "";

    if (
$find=="!shared")
        {
        
# only return shared collections
        
$sql=" where (c.`type` = " COLLECTION_TYPE_PUBLIC " or c.ref in (select distinct collection from user_collection where user<>'" escape_check($user) . "' union select distinct collection from external_access_keys))";                
        }
    elseif (
strlen($find)==&& !is_numeric($find))
        {
        
# A-Z search
        
$sql=" where c.name like '" escape_check($find) . "%'";
        }
    elseif (
strlen($find)>|| is_numeric($find))
        {  
        
$keywords=split_keywords($find);
        
$keyrefs=array();
        
$keysql="";
        for (
$n=0;$n<count($keywords);$n++)
            {
            
$keyref=resolve_keyword($keywords[$n],false);
            if (
$keyref!==false) {$keyrefs[]=$keyref;}

            
$keysql.=" join collection_keyword k" $n " on k" $n ".collection=ref and (k" $n ".keyword='$keyref')";    
            
//$keysql="or keyword in (" . join (",",$keyrefs) . ")";
            
}

 
        
//$sql.="and (c.name rlike '$search' or u.username rlike '$search' or u.fullname rlike '$search' $spcr )";
        
}

    
// Type filter
    
global $themes_in_my_collections;
    
$sql .= sprintf(
        
"%s c.`type` IN (%s, %s%s)",
        (
$sql == "" "WHERE" " AND"),
        
COLLECTION_TYPE_STANDARD,
        
COLLECTION_TYPE_PUBLIC,
        (
$themes_in_my_collections ", " COLLECTION_TYPE_FEATURED ""));

    if(
$themes_in_my_collections)
        {
        
// If we show featured collections, remove the categories
        
$keysql .= sprintf(
            
" WHERE (clist.`type` IN (%s, %s) OR (clist.`type` = %s AND clist.`count` > 0))",
            
COLLECTION_TYPE_STANDARD,
            
COLLECTION_TYPE_PUBLIC,
            
COLLECTION_TYPE_FEATURED);
        }

    global 
$anonymous_login,$username,$anonymous_user_session_collection;

    if(isset(
$anonymous_login) && ($username==$anonymous_login) && $anonymous_user_session_collection)
        {
        
// Anonymous user - only get the user's own collections that are for this session - although we can still join to 
        // get collections that have been specifically shared with the anonymous user 
        
if('' == $sql)
            {
            
$extrasql " where ";
            }
        else
            {
            
$extrasql .= " and ";
            }

        global 
$rs_session;

        
$extrasql .= " (c.session_id='{$rs_session}')";
        }

   
    
$order_sort="";
    if (
$order_by!="name"){$order_sort=" order by $order_by $sort";}

    
$return sprintf(
        
'SELECT * FROM (
                         SELECT c.*, u.username, u.fullname, count(r.resource) AS count
                           FROM user AS u
                           JOIN collection AS c ON u.ref = c.user AND c.user = \'%1$s\'
                LEFT OUTER JOIN collection_resource AS r ON c.ref = r.collection
                          %2$s %3$s
                       GROUP BY c.ref
        
                          UNION
                         SELECT c.*, u.username, u.fullname, count(r.resource) AS count
                           FROM user_collection AS uc
                           JOIN collection AS c ON uc.collection = c.ref AND uc.user = \'%1$s\' AND c.user <> \'%1$s\'
                LEFT OUTER JOIN collection_resource AS r ON c.ref = r.collection
                      LEFT JOIN user AS u ON c.user = u.ref
                          %2$s
                       GROUP BY c.ref
        
                          UNION
                         SELECT c.*, u.username, u.fullname, count(r.resource) AS count
                           FROM usergroup_collection AS gc
                           JOIN collection AS c ON gc.collection = c.ref AND gc.usergroup = \'%4$s\' AND c.user <> \'%1$s\'
                LEFT OUTER JOIN collection_resource AS r ON c.ref = r.collection
                      LEFT JOIN user AS u ON c.user = u.ref
                          %2$s
                        GROUP BY c.ref
        ) AS clist
        %5$s
        GROUP BY ref %6$s'
,
        
escape_check($user), # %1$s
        
$sql# %2$s
        
$extrasql# %3$s
        
escape_check($usergroup), # %4$s
        
$keysql# %5$s
        
$order_sort # %6$s
    
);
    
$return sql_query($return);
    
    if (
$order_by=="name"){
        if (
$sort=="ASC"){usort($return'collections_comparator');}
        else if (
$sort=="DESC"){usort($return,'collections_comparator_desc');}
    }
    
    
// To keep Default Collection creation consistent: Check that user has at least one collection of his/her own  (not if collection result is empty, which may include shares), 
    
$hasown=false;
    for (
$n=0;$n<count($return);$n++){
        if (
$return[$n]['user']==$user){
            
$hasown=true;
        }
    }

    if (!
$hasown && $auto_create && $find==""# User has no collections of their own, and this is not a search. Make a new 'Default Collection'
        
{
        
# No collections of one's own? The user must have at least one Default Collection
        
global $usercollection;
        
$name=get_mycollection_name($user);
        
$usercollection=create_collection ($user,$name,0,1); // make not deletable
        
set_user_collection($user,$usercollection);
        
        
# Recurse to send the updated collection list.
        
return get_user_collections($user,$find,$order_by,$sort,$fetchrows,false);
        }

    return 
$return;
    }

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