Collections functions
General functions
Render functions
Theme permission functions
Resource functions

set_resource_defaults()

Description

Set resource defaults. Optional, a list of field IDs can be passed on to only update certain fields.
IMPORTANT: this function will always set the resource defaults if any are found. The "client code"
is where developers decide whether this should happen

@global string $userresourcedefaults Resource defaults rules value based on user group a user belongs to

Parameters

ColumnTypeDefaultDescription
$ref integer Resource ID
$specific_fields array array Specific field ID(s) to update

Return

boolean

Location

include/resource_functions.php lines 1237 to 1273

Definition

 
function set_resource_defaults($ref, array $specific_fields = array())
    {
    global 
$userresourcedefaults;

    if(
'' == $userresourcedefaults)
        {
        return 
false;
        }

    foreach(
explode(';'$userresourcedefaults) as $rule)
        {
        
$rule_detail         explode('='trim($rule));
        
$field_shortname     $rule_detail[0];
        
$field_default_value $rule_detail[1];

        
// Find field(s) - multiple fields can be returned to support several fields with the same name
        
$fields ps_array("SELECT ref AS `value` FROM resource_type_field WHERE name = ?",["s",$field_shortname],"schema");

        if(
=== count($fields))
            {
            continue;
            }

        
// Sometimes we may want to set resource defaults only to specific fields so we ignore anything else
        
if(count($specific_fields))
            {
            
$fields array_intersect($fields$specific_fields);
            }

        foreach(
$fields as $field_ref)
            {
            
update_field($ref$field_ref$field_default_value);
            }
        }

    return 
true;
    }

This article was last updated 30th September 2022 16:05 Europe/London time based on the source file dated 30th September 2022 14:20 Europe/London time.