Collections functions
Encryption functions
General functions
Theme permission functions
Resource functions

autocomplete_blank_fields()

Description

Fill in any blank fields for the resource


For example:
- when creating a resource, autocomplete_blank_fields() should always be triggered regardless if user has data in its user template.
- when copying resource/ extracting embedded metadata, autocomplete_blank_fields() should not overwrite if there is data
for that field as at this point you probably have the expected data for your field.

Parameters

ColumnTypeDefaultDescription
$resource integer Resource ID
$force_run boolean Allow code to force running this function and update the fields even if there is data.
$return_changes boolean false When true an array of fields changed by autocomplete is returned.

Return

boolean|array Success/fail or array of changes made

Location

include/resource_functions.php lines 5037 to 5110

Definition

 
function autocomplete_blank_fields($resource$force_run$return_changes false)
    {
    global 
$FIXED_LIST_FIELD_TYPES$lang;

    if((string)(int)
$resource != (string)$resource)
        {
        return 
false;
        }

    
$resource_escaped escape_check($resource);
    
$resource_type sql_value("SELECT resource_type AS `value` FROM resource WHERE ref = '{$resource_escaped}'"0);

    
$fields sql_query("
        SELECT ref,
               type,
               autocomplete_macro
          FROM resource_type_field
         WHERE (resource_type = 0 || resource_type = '
{$resource_type}')
           AND length(autocomplete_macro) > 0
    "
"schema");

    
$fields_updated = array();

    foreach(
$fields as $field)
        {
        if(
in_array($field['type'], $FIXED_LIST_FIELD_TYPES))
            {
            if(
count(get_resource_nodes($resource$field['ref'], true)) > 0)
                {
                continue;
                }
            
$value "";
            }
        else
            {
            
$value sql_value("SELECT `value` FROM resource_data WHERE resource = '{$resource_escaped}' AND resource_type_field = '{$field['ref']}'"'');
            }

        
$run_autocomplete_macro $force_run || hook('run_autocomplete_macro');
        if(
strlen(trim($value)) == || $run_autocomplete_macro)
            {
            
# Empty value. Autocomplete and set.
            
$value = eval($field['autocomplete_macro']);
            if(
in_array($field['type'], $FIXED_LIST_FIELD_TYPES))
                {
                
$autovals str_getcsv($value);
                
$autonodes = array();
                foreach(
$autovals as $autoval)
                    {
                    
$nodeid get_node_id($autoval,$field['ref']);
                    if(
$nodeid !== false)
                        {
                        
$autonodes[] = $nodeid;
                        }
                    }
                
natsort($autonodes);
                
add_resource_nodes($resource,$autonodes,false,false);
                
log_node_changes($resource,$autonodes,array(),$lang["autocomplete_log_note"]);
                
$fields_updated[$field['ref']] = implode(",",$autonodes);
                }
            else
                {
                
update_field($resource$field['ref'], $value);
                
$fields_updated[$field['ref']] = $value;
                }
            }
        }

    if (
$return_changes)
        {
        return 
$fields_updated;
        }
    return 
true;
    }

This article was last updated 12th July 2020 00:35 Europe/London time based on the source file dated 10th July 2020 16:06 Europe/London time.