Collections functions
General functions
Node functions
Render functions
Theme permission functions
User functions
Resource functions

save_resource_type_field()

Description

Save resource type field - used on pages/admin/admin_resource_type_field_edit.php

Parameters

ColumnTypeDefaultDescription
$ref int Field ID
$columns array Array of column data
bool $postdata:
$migrate_data
$onload_message
$lang
true; []; foreach$resource_types as $resource_type
true
false
$postdata mixed POST'd data

Return

bool *

Location

include/config_functions.php lines 1644 to 1782

Definition

 
function save_resource_type_field(int $ref, array $columns$postdata): bool
    
{
    global 
$regexp_slash_replace$migrate_data$onload_message$lang$baseurl;

    
$existingfield get_resource_type_field($ref);
    
$params = [];

    
$resource_types=get_resource_types("",true,false,true);

    
// Array of resource types to remove data from if no longer associated with field
    
$remove_data_restypes = [];
    foreach(
$resource_types as $resource_type)
        {
        
$resource_type_array[$resource_type["ref"]]=$resource_type["name"];
        }
    
$valid_columns columns_in("resource_type_field",null,null,true);
    foreach (
$columns as $column=>$column_detail)       
        {
        if(!
in_array($column,$valid_columns))
            {
            continue;
            }
        if (
$column_detail[2]==1)
            {
            
$val= (int)(bool)($postdata[$column] ?? 0);
            }       
        else
            {
            
$val=trim($postdata[$column] ?? "");
            if(
$column == 'regexp_filter')
                {
                
$val str_replace('\\'$regexp_slash_replace$val);   
                }
        
            if(
$column == "type" && $val != $existingfield["type"] && (bool)($postdata["migrate_data"] ?? false))
                {
                
// Need to migrate field data
                
$migrate_data true;               
                }
            
            
// Set shortname if not already set or invalid
            
if($column=="name" && ($val=="" || in_array($val,array("basicday","basicmonth","basicyear"))))
                {
                
$val="field" $ref;
                }

            if(
$column === 'tab' && $val == 0)
                {
                
$val ''# set to blank so the code will convert to SQL NULL later
                
}
            }
        if (isset(
$sql))
            {
            
$sql.=",";
            }
        else
            {
            
$sql="UPDATE resource_type_field SET ";
            }       
        
        
$sql.="{$column}=";
        if (
$val=="")
            {
            
$sql.="NULL";
            }
        else    
            {
            
$sql.="?";
            
$params[]=($column_detail[2]==1?"i":"s"); // Set the type, boolean="i", other two are strings
            
$params[]=$val;
            }
        if(
$column == "global")
            {
            
// Also need to update all resource_type_field -> resource_type associations
            
$setrestypes = [];
            
$currentrestypes get_resource_type_field_resource_types([$existingfield]);
            if(
$val == 0) {
                
// Only need to check them if field is not global
                
foreach($resource_type_array as $resource_type=>$resource_type_name)
                    {
                    if(
trim($postdata["field_restype_select_" $resource_type] ?? "") != "")
                        {
                        
$setrestypes[] = $resource_type;
                        }
                    }
                
// Set to remove existing data from the resource types that had data stored
                
if ($existingfield["type"] == 1) {
                    
$remove_data_restypes array_column($resource_type_array,"ref");
                } else {
                    
$remove_data_restypes array_diff($currentrestypes[$ref],$setrestypes);
                }
            }
            
update_resource_type_field_resource_types($ref,$setrestypes);
            if (empty(
$setrestypes)) {
                
$setrestypes array_column(get_resource_types("",false,true,true),"ref");
            }
            
log_activity(
                
null,
                
LOG_CODE_EDITED,
                
implode(", "$setrestypes),
                
'resource_type_field',
                
'Resource Types',
                
$ref,
                
null,
                
implode(", ",$currentrestypes[$ref])
            );
            }

        
log_activity(null,LOG_CODE_EDITED,$val,'resource_type_field',$column,$ref);
        }
    
// add field_constraint sql
    
if (isset($postdata["field_constraint"]) && trim($postdata["field_constraint"]) != "")
        {
        
$sql.=",field_constraint=?";
        
$params[]="i";$params[]= (int)$postdata["field_constraint"];
        }

    
// Add automatic nodes ordering if set (available only for fixed list fields - except category trees)
    
$sql .= ", automatic_nodes_ordering = ?";
    
$params[]="i";$params[]= (== ($postdata['automatic_nodes_ordering'] ?? 0) ? 0);

    
$sql .= " WHERE ref = ?";
    
$params[]="i";$params[]=$ref;

    
ps_query($sql,$params);
    
clear_query_cache("schema");
    
clear_query_cache("featured_collections");

    if(
count($remove_data_restypes)>0)
        {
        
// Don't delete invalid nodes immediately in case of accidental/inadvertent change - just show a link to the cleanup page
        
$cleanup_url generateURL($baseurl "/pages/tools/cleanup_invalid_nodes.php",["cleanupfield"=>$ref"cleanuprestype"=>implode(",",$remove_data_restypes)]);
        
$onload_message= ["title" => $lang["cleanup_invalid_nodes"],"text" => str_replace("%%CLEANUP_LINK%%","<br/><a href='" $cleanup_url "' target='_blank'>" $lang["cleanup_invalid_nodes"] . "</a>",$lang["information_field_restype_deselect_cleanup"])];
        }
    
    
hook('afterresourcetypefieldeditsave');
    
    return 
true;
    }

This article was last updated 27th April 2024 08:35 Europe/London time based on the source file dated 18th April 2024 17:15 Europe/London time.