Collections functions
General functions
Render 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.
$field_ref integer 0 Optional parameter to specify which metadata field should be processed. Left blank, all fields will be processed (default behaviour).

Return

boolean|array Success/fail or array of changes made

Location

include/resource_functions.php lines 5703 to 5784

Definition

 
function autocomplete_blank_fields($resource$force_run$return_changes falseint $field_ref 0)
    {
    global 
$FIXED_LIST_FIELD_TYPES$lang;

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

    
$resource_type ps_value("SELECT resource_type AS `value` FROM resource WHERE ref = ?", ["i",$resource], 0);

    
$sql_set_field '';
    
$sql_set_field_params = array();
    if (
$field_ref 0)
        {
        
$sql_set_field ' AND rtf.ref = ?';
        
$sql_set_field_params = array('i'$field_ref);
        }

    
$fields ps_query(
        
"SELECT rtf.ref, rtf.type, rtf.autocomplete_macro
          FROM resource_type_field rtf
          LEFT JOIN resource_type rt ON rt.ref = ?
          WHERE length(rtf.autocomplete_macro) > 0
          AND (   (rtf.resource_type<>0 AND rtf.resource_type = rt.ref)
               OR (rtf.resource_type=0  AND rt.inherit_global_fields=1)
              ) 
$sql_set_field"array_merge(array("i"$resource_type), $sql_set_field_params), "schema");

    
$fields_updated = array();

    foreach(
$fields as $field)
        {
        
$run_autocomplete_macro $force_run || hook('run_autocomplete_macro');
        
# The autocomplete macro will run if the existing value is blank, or if forced to always run
        
if(count(get_resource_nodes($resource$field['ref'], true)) == || $run_autocomplete_macro)
            {
            
# Autocomplete and update using the returned value
            
$value = eval(eval_check_signed($field['autocomplete_macro']));
            if(
in_array($field['type'], $FIXED_LIST_FIELD_TYPES))
                {
                
# Multiple values are comma separated
                
$autovals str_getcsv((string) $value);
                
$autonodes = array();
                
# Establish an array of nodes from the values
                
foreach($autovals as $autoval)
                    {
                    
$nodeid get_node_id($autoval,$field['ref']);
                    if(
$nodeid !== false)
                        {
                        
$autonodes[] = $nodeid;
                        }
                    }
                
# Add nodes if any were established
                
if (count($autonodes) > 0)
                    {
                    
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);

                    
# If this is a 'joined' field we need to add it to the resource column
                    
$joins get_resource_table_joins();
                    if (
in_array($field['ref'], $joins))
                        {
                        
update_resource_field_column($resource$field['ref'], $value);
                        }
                    }
                }
            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 4th February 2023 12:05 Europe/London time based on the source file dated 3rd February 2023 13:55 Europe/London time.