Coding standards
Security in ResourceSpace
Developer reference
Database
Action functions
Admin functions
Ajax functions
Annotation functions
API functions
Collections functions
Comment functions
Config functions
CSV export functions
Dash functions
Debug functions
Encryption functions
Facial recognition functions
File functions
General functions
Language functions
Log functions
Login functions
Message functions
Migration functions
Node functions
PDF functions
Plugin functions
Render functions
Reporting functions
Request functions
Research functions
Slideshow functions
Theme permission functions
User functions
Video functions
Database functions
Metadata functions
Resource functions
Search functions
Map functions
Job functions
Tab functions
Test 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 1725 to 1889

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 = [];
    
$multiple_deduplicate false;               

    
$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;               
                }

            if(
$column == "type" && $val != $existingfield["type"] && $existingfield["type"] === FIELD_TYPE_CATEGORY_TREE)
                {
                
// Need to deduplicate nodes if going from category tree to multiple value fixed field type
                
if ($postdata['type'] == FIELD_TYPE_CHECK_BOX_LIST || $postdata['type'] == FIELD_TYPE_DYNAMIC_KEYWORDS_LIST
                    {
                    
$multiple_deduplicate 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);

    
// Deduplicate node names if necessary
    
if ($multiple_deduplicate
        {
        
$deduplicate_sql =   
        
"UPDATE node n1
            JOIN ( SELECT name, ref, ROW_NUMBER() OVER (PARTITION BY name ORDER BY ref) AS rn
                    FROM node where resource_type_field = ?
                ) n2 ON n1.ref = n2.ref
            SET n1.name = CONCAT(n2.name, '_', n2.rn - 1)
        WHERE n2.rn > 1"
;             
        
ps_query($deduplicate_sql, array("i",$ref));
        }


    
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 17th November 2024 15:35 Europe/London time based on the source file dated 13th November 2024 15:50 Europe/London time.