Collections functions
Encryption functions
General functions
Theme permission functions
Resource functions

save_collection()

Description

Process the save action when saving a colleciton

Parameters

ColumnTypeDefaultDescription
$ref integer
$coldata array array

Return

void

Location

include/collections_functions.php lines 887 to 1095

Definition

 
function save_collection($ref$coldata=array())
    {
    global 
$theme_category_levels,$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","");
        for(
$n=1;$n<=$theme_category_levels;$n++)
            {
            if (
$n==1)
                {
                
$themeindex "";
                }
            else
                {
                
$themeindex $n;
                }
            
$themename getvalescaped("theme$themeindex","");
            if(
$themename != "")
                {
                
$coldata["theme" $themeindex] = $themename;
                }
            
            if (
getval("newtheme$themeindex","")!="")
                {
                
$coldata["theme"$n] = trim(getval("newtheme$themeindex",""));
                }    
            }
            
        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);    

    
// Create sql column update text
    
if (!hook('modifysavecollection'))
        {
        
$sqlset = array();
        foreach(
$coldata as $colopt=>$colset)
            {
            if(!isset(
$oldcoldata[$colopt]) || $colset != $oldcoldata[$colopt])
                {
                
$sqlset[$colopt] = $colset;    
                }                
            }
        
        if(
count($sqlset) > 0)
            {
            
$sqlupdate "";
            foreach(
$sqlset as $colopt => $colset)
                {
                if(
$sqlupdate != "")
                    {
                    
$sqlupdate .= ",";    
                    }
                
$sqlupdate .= $colopt "='" escape_check($colset) . "' ";   
                }
                
            
$sql "UPDATE collection SET " $sqlupdate " WHERE ref='" $ref "'";
            
sql_query($sql);
            
            
// 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 10th July 2020 01:05 Europe/London time based on the source file dated 9th July 2020 17:22 Europe/London time.