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

save_collection()

Description

Process the save action when saving a collection

Parameters

ColumnTypeDefaultDescription
$ref integer
$coldata array array

Return

false|void

Location

include/collections_functions.php lines 984 to 1243

Definition

 
function save_collection($ref$coldata=array())
    {
    global 
$attach_user_smart_groups;
    
    if (!
is_numeric($ref) || !collection_writeable($ref))
        {
        return 
false;
        }
    
    if(
count($coldata) == 0)
        {
        
// Old way
        
$coldata["name"]            = getval("name","");
        
$coldata["allow_changes"]   = getval("allow_changes","") != "" 0;
        
$coldata["public"]          = getval('public'0true);
        
$coldata["keywords"]        = getval("keywords","");

        if (
checkperm("h"))
            {
            
$coldata["home_page_publish"]   = (getval("home_page_publish","") != "") ? "1" "0";
            
$coldata["home_page_text"]      = getval("home_page_text","");
            if (
getval("home_page_image","") != "")
                {
                
$coldata["home_page_image"] = getval("home_page_image","");
                }
            }
        }
        
    
$oldcoldata get_collection($ref);    

    if (!
hook('modifysavecollection'))
        {
        
$sqlset = array();
        foreach(
$coldata as $colopt => $colset)
            {
            
// Public collection
            
if($colopt == "public" && $colset == 1)
                {
                
$sqlset["type"] = COLLECTION_TYPE_PUBLIC;
                }

            
// "featured_collections_changes" is determined by collection_edit.php page
            // This is meant to override the type if collection has a parent. The order of $coldata elements matters!
            
if($colopt == "featured_collections_changes" && !empty($colset))
                {
                
$sqlset["type"] = COLLECTION_TYPE_FEATURED;
                
$sqlset["parent"] = null;

                if(isset(
$colset["update_parent"]))
                    {
                    
$force_featured_collection_type = isset($colset["force_featured_collection_type"]);

                    
// A FC root category is created directly from the collections_featured.php page so not having a parent, means it's just public
                    
if($colset["update_parent"] == && !$force_featured_collection_type)
                        {
                        
$sqlset["type"] = COLLECTION_TYPE_PUBLIC;
                        }
                    else
                        {
                        
$sqlset["parent"] = (int) $colset["update_parent"];
                        }
                    }

                if(isset(
$colset["thumbnail_selection_method"]))
                    {
                    
$sqlset["thumbnail_selection_method"] = $colset["thumbnail_selection_method"];

                    
// Prevent the parent from being changed if user only modified the thumbnail_selection_method
                    
$sqlset["parent"] = (!isset($colset["update_parent"]) ? $oldcoldata["parent"] : $sqlset["parent"]);
                    }

                
// Prevent unnecessary changes
                
foreach(array("type""parent""thumbnail_selection_method") as $puc_to_prop)
                    {
                    if(isset(
$sqlset[$puc_to_prop]) && $oldcoldata[$puc_to_prop] == $sqlset[$puc_to_prop])
                        {
                        unset(
$sqlset[$puc_to_prop]);
                        }
                    }

                continue;
                }

            if(!isset(
$oldcoldata[$colopt]) || $colset != $oldcoldata[$colopt])
                {
                
$sqlset[$colopt] = $colset;
                }
            }

        
// If collection is set as private by caller code, disable incompatible properties used for COLLECTION_TYPE_FEATURED (set by the user or exsting)
        
if(isset($sqlset["public"]) && $sqlset["public"] == 0)
            {
            
$sqlset["type"] = COLLECTION_TYPE_STANDARD;
            
$sqlset["parent"] = null;
            
$sqlset["thumbnail_selection_method"] = null;
            
$sqlset["bg_img_resource_ref"] = null;
            }

        if(
count($sqlset) > 0)
            {
            
$sqlupdate "";
            
$clear_fc_query_cache false;
            foreach(
$sqlset as $colopt => $colset)
                {
                if(
$sqlupdate != "")
                    {
                    
$sqlupdate .= ", ";    
                    }

                if(
in_array($colopt, array("type""parent""thumbnail_selection_method""bg_img_resource_ref")))
                    {
                    
$clear_fc_query_cache true;
                    }

                if(
in_array($colopt, array("parent""thumbnail_selection_method""bg_img_resource_ref")))
                    {
                    
$sqlupdate .= $colopt " = " sql_null_or_val((string) $colset$colset == 0);
                    continue;
                    }

                
$sqlupdate .= $colopt " = '" escape_check($colset) . "' ";
                }

            
$sql "UPDATE collection SET {$sqlupdate} WHERE ref = '{$ref}'";
            
sql_query($sql);

            if(
$clear_fc_query_cache)
                {
                
clear_query_cache("featured_collections");
                }

            
// Log the changes
            
foreach($sqlset as $colopt => $colset)
                {
                switch(
$colopt)
                    {
                    case 
"public";
                        
collection_log($ref'A'0$colset 'public' 'private');
                    break;    
                    case 
"allow_changes";
                        
collection_log($ref'U'0,  $colset 'true' 'false' );
                    break; 
                    default;
                        
collection_log($ref'e'0,  $colopt  " = " $colset);
                    break;
                    }
                 
                }
            }
        } 
# end replace hook - modifysavecollection

    
index_collection($ref);
  
    
$old_attached_users=sql_array("SELECT user value FROM user_collection WHERE collection='$ref'");
    
$new_attached_users=array();
    
$collection_owner=sql_value("SELECT u.fullname value FROM collection c LEFT JOIN user u on c.user=u.ref WHERE c.ref='$ref'","");
    if(
$collection_owner=='')
        {
        
$collection_owner=sql_value("SELECT u.username value FROM collection c LEFT JOIN user u on c.user=u.ref WHERE c.ref='$ref'","");
        }
    
    
sql_query("delete from user_collection where collection='$ref'");
    
    if (
$attach_user_smart_groups)
        {
        
$old_attached_groups=sql_array("SELECT usergroup value FROM usergroup_collection WHERE collection='$ref'");
        
sql_query("delete from usergroup_collection where collection='$ref'");
        }

    
# If 'users' is specified (i.e. access is private) then rebuild users list
    
$users=getvalescaped("users",false);
    if ((
$users)!="")
        {
        
# Build a new list and insert
        
$users=resolve_userlist_groups($users);
        
$ulist=array_unique(trim_array(explode(",",$users)));
        
$urefs=sql_array("select ref value from user where username in ('" join("','",$ulist) . "')");
        if (
count($urefs)>0)
            {
            
sql_query("insert into user_collection(collection,user) values ($ref," join("),(" $ref ",",$urefs) . ")");
            
$new_attached_users=array_diff($urefs$old_attached_users);
            }
        
#log this
        
collection_log($ref,"S",0join(", ",$ulist));
        
        if(
$attach_user_smart_groups)
            {
            
$groups=resolve_userlist_groups_smart($users);
            
$groupnames='';
            if(
$groups!='')
                {
                
$groups=explode(",",$groups);
                if (
count($groups)>0)
                    { 
                    foreach (
$groups as $group)
                        {
                        
sql_query("insert into usergroup_collection(collection,usergroup) values ('$ref','$group')");
                        
// get the group name
                        
if($groupnames!='')
                            {
                            
$groupnames.=", ";
                            }
                        
$groupnames.=sql_value("select name value from usergroup where ref='{$group}'","");
                        }

                    
$new_attached_groups=array_diff($groups$old_attached_groups);
                    if(!empty(
$new_attached_groups))
                        {
                        foreach(
$new_attached_groups as $newg)
                            {
                            
$group_users=sql_array("SELECT ref value FROM user WHERE usergroup=$newg");
                            
$new_attached_users=array_merge($new_attached_users$group_users);
                            }
                        }
                    }
                
#log this
                
collection_log($ref,"S",0$groupnames);
                }
            }
        
# Send a message to any new attached user
        
if(!empty($new_attached_users))
            {
            global 
$baseurl$lang;

            
$new_attached_users=array_unique($new_attached_users);
            
message_add($new_attached_users,str_replace(array('%user%''%colname%'), array($collection_ownergetvalescaped("name","")), $lang['collectionprivate_attachedusermessage']),$baseurl "/?c=" $ref);
            }
        }

    
# Relate all resources?
    
if (getval("relateall","")!="")
        {
        
relate_all_collection($ref);
        }

    
# Remove all resources?
    
if (getval("removeall","")!="")
        {
        
remove_all_resources_from_collection($ref);
        }
        
    
# Delete all resources?
    
if (getval("deleteall","")!="" && !checkperm("D"))
        {
        if(
allow_multi_edit($ref))
            {
            
delete_resources_in_collection($ref);
            }
        }

    
$result_limit getvalescaped("result_limit"0true);

    
# Update limit count for saved search
    
if ($result_limit 0)
        {
        
sql_query("update collection_savedsearch set result_limit='" $result_limit "' where collection='$ref'");
        }

    
refresh_collection_frame();
    }

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