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

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 standard "Default Collection" if one doesn't exist

Return

array

Location

include/collections_functions.php lines 16 to 204

Definition

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

    
$condsql "";
    
$condparams = [];
    
$keysql "";
    
$keyparams = [];
    
$extrasql "";
    
$extraparams = [];
    
$sort strtoupper($sort) == "ASC" "ASC" "DESC";

    if (
$find=="!shared")
        {
        
# only return shared collections
        
$condsql " WHERE (c.`type` = ? OR c.ref IN (SELECT DISTINCT collection FROM user_collection WHERE user<>? UNION SELECT DISTINCT collection FROM external_access_keys))";
        
$condparams = array("i",COLLECTION_TYPE_PUBLIC,"i",$user);
        }
    elseif (
strlen($find)==&& !is_numeric($find))
        {
        
# A-Z search
        
$condsql=" WHERE c.name LIKE ?";
        
$condparams = array("s",$find "%");
        }
    elseif (
strlen($find)>|| is_numeric($find))
        {  
        
$keywords=split_keywords($find);
        
$keysql="";
        
$keyparams = array();
        for (
$n=0;$n<count($keywords);$n++)
            {
            
$keyref=resolve_keyword($keywords[$n],false);
            if(
$keyref === false)
                {
                continue;
                }

            
$keysql.=" JOIN collection_keyword k" $n " ON k" $n ".collection=ref AND (k" $n ".keyword=?)";
            
$keyparams array_merge($keyparams, ['i'$keyref]);
            }
        }

    
$validtypes = [COLLECTION_TYPE_STANDARDCOLLECTION_TYPE_PUBLICCOLLECTION_TYPE_REQUEST];
    if(
$themes_in_my_collections)
        {
        
$validtypes[] = COLLECTION_TYPE_FEATURED;
        }
    
$condsql .= $condsql == "" "WHERE" " AND";
    
$condsql .= " c.`type` IN (" ps_param_insert(count($validtypes)) . ")";
    
$condparams =  array_merge($condparams,ps_param_fill($validtypes,"i"));

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

    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('' == $condsql)
            {
            
$extrasql " WHERE ";
            }
        else
            {
            
$extrasql .= " AND ";
            }

        
$extrasql .= " (c.session_id=?)";
        
$extraparams = array("i",$rs_session);
        }
   
    
$order_sort="";
    
$validsort =  array("name","ref","user","created","public","home_page_publish","type","parent");
    if (
$order_by!="name" && in_array(strtolower($order_by),$validsort))
        {
        
$order_sort=" ORDER BY $order_by $sort";
        }

    
// Control the selected columns. $query_select_columns is for the outer SQL and $collection_select_columns
    // is for the inner one. Both have some extra columns from the user & resource table.
    
$collection_table_columns = [
        
'ref',
        
'name',
        
'user',
        
'created',
        
'public',
        
'allow_changes',
        
'cant_delete',
        
'keywords',
        
'savedsearch',
        
'home_page_publish',
        
'home_page_text',
        
'home_page_image',
        
'session_id',
        
'description',
        
'type',
        
'parent',
        
'thumbnail_selection_method',
        
'bg_img_resource_ref',
        
'order_by',
    ];
    
$query_select_columns implode(', '$collection_table_columns) . ', username, fullname, count';
    
$collection_select_columns = [];
    foreach(
$collection_table_columns as $column_name)
        {
        
$collection_select_columns[] = "c.{$column_name}";
        }
    
$collection_select_columns implode(', '$collection_select_columns) . ', u.username, u.fullname, count(r.resource) AS count';

    
$query "SELECT {$query_select_columns}
                FROM (
                         SELECT 
{$collection_select_columns}
                           FROM user AS u
                           JOIN collection AS c ON u.ref = c.user AND c.user = ?
                LEFT OUTER JOIN collection_resource AS r ON c.ref = r.collection
                       
$condsql 
                       
$extrasql
                       GROUP BY c.ref
        
                          UNION
                         SELECT 
{$collection_select_columns}
                           FROM user_collection AS uc
                           JOIN collection AS c ON uc.collection = c.ref AND uc.user = ? AND c.user <> ?
                LEFT OUTER JOIN collection_resource AS r ON c.ref = r.collection
                      LEFT JOIN user AS u ON c.user = u.ref
                       
$condsql
                       GROUP BY c.ref
        
                          UNION
                         SELECT 
{$collection_select_columns}
                           FROM usergroup_collection AS gc
                           JOIN collection AS c ON gc.collection = c.ref AND gc.usergroup = ? AND c.user <> ?
                LEFT OUTER JOIN collection_resource AS r ON c.ref = r.collection
                      LEFT JOIN user AS u ON c.user = u.ref
                       
$condsql
                        GROUP BY c.ref
        ) AS clist
        
$keysql
        GROUP BY ref 
$order_sort";

    
$queryparams array_merge(
        array(
"i",$user),
        
$condparams,
        
$extraparams,
        array(
"i"$user,"i"$user),
        
$condparams,
        array(
"i"$usergroup,"i",$user),
        
$condparams,
        
$keyparams
    
);

    
$return ps_query($query,$queryparams,'collection_access' $user);

    if (
$order_by=="name")
        {
        if (
$sort=="ASC"){usort($return'collections_comparator');}
        elseif (
$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;
        
$usercollection=create_collection ($user,"Default Collection",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 4th December 2024 08:35 Europe/London time based on the source file dated 27th November 2024 15:30 Europe/London time.