Collections functions
General functions
Render functions
Theme permission 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 1292 to 1793

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',0,true);
        
$coldata["keywords"]            = getval("keywords","");
        
$coldata["result_limit"]        = getval("result_limit",0,true);
        
$coldata["relateall"]           = getval("relateall","") != "";
        
$coldata["removeall"]           = getval("removeall","") != "";
        
$coldata["deleteall"]           = getval("deleteall","") != "";
        
$coldata["users"]               = getval("users","");

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

    if (!
hook('modifysavecollection'))
        {
        
$sqlset = array();
        foreach(
$coldata as $colopt => $colset)
            {
            
// skip data that is not a collection property (e.g result_limit or deleteall) otherwise the $sqlset will have an
            // incorrect SQL query for the update statement.
            
if(in_array($colopt, ['result_limit''relateall''removeall''deleteall''users']))
                {
                continue;
                }

            
// Set type to public unless explicitly passed
            
if($colopt == "public" && $colset == && !isset($coldata["type"]))
                {
                
$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"];
                    }
                
                if(isset(
$colset["thumbnail_selection_method"]) || isset($colset["name"]))
                    {
                    
// Prevent the parent from being changed if user only modified the thumbnail_selection_method or name
                    
$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;
            }

        
/*
        Order by is applicable only to featured collections.
        Determine if we have to reset and, if required, re-order featured collections at the tree level

        ----------------------------------------------------------------------------------------------------------------
                                                        |     Old       |        Set        | 
                                                        |---------------|-------------------|
        Use cases                                       | Type | Parent | Type    | Parent  | Reset order_by? | Re-order?
        ------------------------------------------------|------|--------|-----------------------------------------------
        Move FC to private                              | 3    | null   | 0       | null    | yes             | no
        Move FC to public                               | 3    | any    | 4       | null    | yes             | no
        Move FC to new parent                           | 3    | null   | not set | X       | yes             | yes
        Save FC but don’t change type or parent         | 3    | null   | not set | null    | no              | no
        Save a child FC but don’t change type or parent | 3    | X      | not set | not set | no              | no
        Move public to private                          | 4    | null   | 0       | null    | no              | no
        Move public to FC (root)                        | 4    | null   | 3       | not set | yes             | yes
        Move public to FC (others)                      | 4    | null   | 3       | X       | yes             | yes
        Save public but don’t change type or parent     | 4    | null   | 4       | not set | no              | no
        Create FC at root                               | 0    | null   | 3       | not set | yes             | yes
        Create FC at other level                        | 0    | null   | 3       | X       | yes             | yes
        ----------------------------------------------------------------------------------------------------------------
        */
        // Saving a featured collection without changing its type or parent
        
$rob_cond_fc_no_change = (
            isset(
$oldcoldata['type']) && $oldcoldata['type'] === COLLECTION_TYPE_FEATURED
            
&& !isset($sqlset['type'])
            && (!isset(
$sqlset['parent']) || is_null($sqlset['parent']))
        );
        
// Saving a public collection without changing it into a featured collection
        
$rob_cond_public_col_no_change = (
            isset(
$oldcoldata['type'], $sqlset['type'])
            && 
$oldcoldata['type'] === COLLECTION_TYPE_PUBLIC
            
&& $sqlset["type"] !== COLLECTION_TYPE_FEATURED
        
);
        if( !(
$rob_cond_fc_no_change || $rob_cond_public_col_no_change) )
            {
            
$sqlset['order_by'] = 0;

            if(
                
// Type changed to featured collection
                
(isset($sqlset['type']) && $sqlset['type'] === COLLECTION_TYPE_FEATURED)

                
// Featured collection moved in the tree (ie parent changed)
                
|| ($oldcoldata['type'] === COLLECTION_TYPE_FEATURED && !isset($sqlset['type']) && isset($sqlset['parent']))
            )
                {
                
$reorder_fcs true;
                }
            }


        
// Update collection record
        
if(count($sqlset) > 0)
            {
            
$sqlupdate "";
            
$clear_fc_query_cache false;
            
$collection_columns = [
                
'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',
            ];
            
$params = [];
            foreach(
$sqlset as $colopt => $colset)
                {
                
// Only valid collection columns should be processed
                
if(!in_array($colopt$collection_columns))
                    {
                    continue;
                    }

                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 " = ";
                    if(
$colset == 0){$sqlupdate .= 'NULL';}
                    else
                        {
                        
$sqlupdate .= '?';
                        
$params array_merge($params,['i'$colset]);
                        }
                    
                    continue;
                    }

                if(
$colopt == 'allow_changes')
                    {
                    
$colset = (int) $colset;
                    }

                
$sqlupdate .= $colopt " = ? ";
                
$params array_merge($params, ['s'$colset]);
                }
            if(
$sqlupdate !== '')
                {
                
$sql "UPDATE collection SET {$sqlupdate} WHERE ref = ?";
                
ps_query($sqlarray_merge($params, ['i'$ref]));

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

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

    
index_collection($ref);

    
# If 'users' is specified (i.e. access is private) then rebuild users list
    
if (isset($coldata["users"]))
        {
&nb