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

copy_locked_fields()

Description

Update resource metadata for 'locked' fields from last edited resource.
NB: $fields and $all_selected_nodes are passed by reference

Parameters

ColumnTypeDefaultDescription
$ref integer - resource id being updated
&$fields
&$all_selected_nodes
$locked_fields array - array of locked data columns (may also include resource table columns - handled by copy_locked_data)
$lastedited integer - last edited resource to copy data from
$save boolean false - save data to database (as opposed to just updating the $fields array e.g. for edit page)
$fields array - resource $fields array
$all_selected_nodes array - array of existing resource nodes

Return

void

Location

include/resource_functions.php lines 6450 to 6542

Definition

 
function copy_locked_fields($ref, &$fields,&$all_selected_nodes,$locked_fields,$lastedited$save=false)
    {
    
debug("copy_locked_fields resource " $ref " lastedited: " $lastedited);
    global 
$FIXED_LIST_FIELD_TYPES$tabs_on_edit;
    foreach(
$locked_fields as $locked_field)
            {
            if(!
is_numeric($locked_field))
                {
                
// These are handled by copy_locked_data
                
continue;
                }
            
            
// Check if this field is listed in the $fields array - if resource type has changed it may not be present
            
$key array_search($locked_fieldarray_column($fields'ref'));
            if(
$key!==false)
                {
                
$fieldtype $fields[$key]["type"];
                }    
            else
                {
                
$lockfieldinfo get_resource_type_field($locked_field);
                
$fieldtype $lockfieldinfo["type"];
                }                
            
            if(
in_array($fieldtype$FIXED_LIST_FIELD_TYPES))
                {
                
// Replace nodes for this field
                
$field_nodes get_nodes($locked_fieldNULL$fieldtype == FIELD_TYPE_CATEGORY_TREE);
                
$field_node_refs array_column($field_nodes,"ref");
                
$stripped_nodes array_diff ($all_selected_nodes$field_node_refs);
                
$locked_nodes get_resource_nodes($lastedited$locked_field);
                
$all_selected_nodes array_merge($stripped_nodes$locked_nodes);

                if(
$save)
                    {
                    
debug("- adding locked field nodes for resource " $ref ", field id: " $locked_field);
                    
delete_resource_nodes($ref,$field_node_refs);
                    if(
count($locked_nodes) > 0)
                        {
                        
add_resource_nodes($ref$locked_nodesfalse);
                        }

                    
# If this is a 'joined' field it still needs to add it to the resource column
                    
$joins=get_resource_table_joins();
                    if (
in_array($locked_field,$joins))
                        {
                        
$node_vals = array();
                        
// Build new value:
                        
foreach($locked_nodes as $locked_node)
                            {
                            foreach (
$field_nodes as $key => $val
                                {
                                if (
$val['ref'] === $locked_node
                                    {
                                    
array_push($node_vals$field_nodes[$key]["name"]);
                                    }
                                }
                            
$resource_type_field=$field_nodes[$key]["resource_type_field"];
                            
$values_string implode(",",$node_vals);
                            
sql_query("update resource set field".$resource_type_field."='".escape_check(truncate_join_field_value(strip_leading_comma($values_string)))."' where ref='".escape_check($ref)."'");
                            }
                        } 
                    }
                }
            else
                {
                
debug(" - checking field values for last resource " $lastedited " field id: " $locked_field);
                if(!isset(
$last_fields))
                    {
                    
$last_fields get_resource_field_data($lastedited,!hook("customgetresourceperms"),NULL,"",$tabs_on_edit);
                    }
                
                
$addkey array_search($locked_fieldarray_column($last_fields'ref'));
                if(
$key!==false)
                    {
                    
// Field is already present - just update the value
                
debug(" - updating field value for resource " $lastedited " field id: " $locked_field);
                    
$fields[$key]["value"] = $last_fields[$addkey]["value"];
                    }
                else
                    {
                    
// Add the field to the $fields array   
                
debug(" - adding field value for resource " $lastedited " field id:" $locked_field);
                    
$fields[] = $last_fields[$addkey];
                    }
                if(
$save)
                    {
                    
debug("- adding locked field value for resource " $ref ", field id: " $locked_field);
                    
update_field($ref,$locked_field,$last_fields[$addkey]["value"]);
                    }
                }
            }
    }

This article was last updated 1st December 2020 08:35 Europe/London time based on the source file dated 30th November 2020 11:55 Europe/London time.