Collections functions
General functions
Render functions
Theme permission functions
Resource functions

get_resource_field_data()

Description

get_resource_field_data

Parameters

ColumnTypeDefaultDescription
$ref int Resource ID
$multi bool false Get all fields? False by default (only fields that apply to the given resource type)
$use_permissions bool true Honour user permissions e.g. field access. TRUE by default
$originalref int NULL Original resource ID to get data for. NULL by default
$external_access bool false Only get data permitted to view externally. FALSE by default
$ord_by bool false Use field order_by setting. FALSE by default (order is by resource type first)
$forcsv bool false Get data for CSV export (uses \ separator for category tree nodes). FALSE by default

Return

array|boolean

Location

include/resource_functions.php lines 2895 to 3105

Definition

 
function get_resource_field_data($ref,$multi=false,$use_permissions=true,$originalref=NULL,$external_access=false,$ord_by=false$forcsv false)
    {
    
# Returns field data and field properties (resource_type_field and resource_data tables)
    # for this resource, for display in an edit / view form.
    # Standard field titles are translated using $lang.  Custom field titles are i18n translated.

    
global $view_title_field$metadata_template_resource_type$NODE_FIELDS$FIXED_LIST_FIELD_TYPES;

    
# Find the resource type.
    
if (is_null($originalref)) {$originalref $ref;} # When a template has been selected, only show fields for the type of the original resource ref, not the template (which shows fields for all types)
    
$rtype ps_value("select resource_type value FROM resource WHERE ref=?",array("i",$originalref),0);
    
$rtype = ($rtype == "") ? $rtype;

    
# If using metadata templates,
    
$templatesql "";
    if (isset(
$metadata_template_resource_type) && $metadata_template_resource_type==$rtype) {
        
# Show all resource fields, just as with editing multiple resources.
        
$multi true;
    }

    
$return           = array();
    
$order_by_sql     = ($ord_by 'f.order_by, f.resource_type, f.ref' 'f.resource_type, f.order_by, f.ref');

    
// Remove Category tree fields as these need special handling
    
$node_fields    array_diff($NODE_FIELDS,array(FIELD_TYPE_CATEGORY_TREE));

    
$restypesql "";
    
$restype_params = [];
    if(!
$multi)
        {
        
$restypesql "AND (f.resource_type = 0 OR f.resource_type = 999 OR f.resource_type = ?)";
        
$restype_params[] = "i";$restype_params[] = $rtype;
        }

    
$field_data_sql "
             SELECT group_concat(n.name SEPARATOR ', ') AS `value`,
                    group_concat(n.ref) AS `nodes`,
                    f.ref,
                    f.ref resource_type_field,
                    f.ref AS fref,
                    f.name,
                    f.title,
                    f.field_constraint,
                    f.type,
                    f.order_by,
                    f.keywords_index,
                    f.partial_index,
                    f.resource_type,
                    f.resource_column,
                    f.display_field,
                    f.use_for_similar,
                    f.iptc_equiv,
                    f.display_template,
                    f.tab,
                    f.required,
                    f.required AS frequired,
                    f.smart_theme_name,
                    f.exiftool_field,
                    f.advanced_search,
                    f.simple_search,
                    f.help_text,
                    f.display_as_dropdown,
                    f.external_user_access,
                    f.autocomplete_macro,
                    f.hide_when_uploading,
                    f.hide_when_restricted,
                    f.value_filter,
                    f.exiftool_filter,
                    f.omit_when_copying,
                    f.tooltip_text,
                    f.regexp_filter,
                    f.sync_field,
                    f.display_condition,
                    f.onchange_macro,
                    f.linked_data_field,
                    f.automatic_nodes_ordering,
                    f.fits_field,
                    f.personal_data,
                    f.include_in_csv_export,
                    f.browse_bar,
                    f.read_only,
                    f.active,
                    f.full_width
               FROM resource_type_field f
          LEFT JOIN (SELECT ref, name, resource_type_field FROM node WHERE ref IN (SELECT node FROM resource_node WHERE resource = ?) ORDER BY order_by) AS n
                    ON n.resource_type_field = f.ref
              WHERE (f.active=1 AND f.type IN (" 
ps_param_insert(count($node_fields)) . ") " $restypesql ")
              GROUP BY f.ref
              ORDER BY 
{$order_by_sql}";

    
$field_data_params array_merge(["i"$ref], ps_param_fill($node_fields,"i"),$restype_params);
    if(!
$ord_by)
        {
        
debug('GENERAL/GET_RESOURCE_FIELD_DATA: use perms: ' . !$use_permissions);
        }

    
$fields ps_query($field_data_sql,$field_data_params);
    
# Build an array of valid types and only return fields of this type. Translate field titles.
    
$validtypes ps_array('SELECT ref AS `value` FROM resource_type',[],'schema');

    
# Support archive and global.
    
$validtypes[] = 0;
    
$validtypes[] = 999;

    
// Add category tree values, reflecting tree structure
    
$tree_resource_types = array('0',$rtype);
    if (
$multi)
        {
        
// All resource types checked as this is a metadata template.
        
$tree_resource_types $validtypes;
        }

    
$tree_fields get_resource_type_fields($tree_resource_types,"ref","asc",'',array(FIELD_TYPE_CATEGORY_TREE));
    foreach(
$tree_fields as $tree_field)
        {
        
$addfield$tree_field;

        
$treenodes get_cattree_nodes_ordered($tree_field["ref"], $reffalse); # True means get all nodes; False means get selected nodes
        
$treenodenames get_cattree_node_strings($treenodestrue); # True means names are paths to nodes; False means names are node names

        // Quoting each element is required for csv export
        
$valstring $forcsv ? ("\"" implode("\",\"",$treenodenames) . "\"") : implode(",",$treenodenames);
        
$addfield["value"] = count($treenodenames) > $valstring "";
        
$addfield["resource_type_field"] = $tree_field["ref"];
        
$addfield["fref"] = $tree_field["ref"];
        
$fields[] = $addfield;
        }

    if (empty(
$fields))
        {
        return 
false;
        }

    foreach(
$fields as $fkey => $field)
        {
        
$fieldorder_by[$fkey]   = $field["order_by"];
        
$fieldrestype[$fkey]    = $field["resource_type"];
        
$fieldref[$fkey]        = $field["ref"];
        }
    if(
$ord_by)
        {
        
array_multisort($fieldorder_bySORT_ASC$fieldrestypeSORT_ASC$fieldrefSORT_ASC$fields);
        }
    else
        {
        
array_multisort($fieldrestypeSORT_ASC$fieldorder_bySORT_ASC$fieldrefSORT_ASC$fields);
        }

    
// Resource types can be configured to not have global fields in which case we only present the user fields valid for
    // this resource type
    
$inherit_global_fields = (bool) ps_value("SELECT inherit_global_fields AS `value` FROM resource_type WHERE ref = ?", array("i",$rtype), true"schema");
    if(!
$inherit_global_fields && !$multi)
        {
        
$validtypes = array($rtype);

        
# Add title field even if $inherit_global_fields = false
        
for ($n 0$n count($fields); $n++)
            {
            if  (
                
$fields[$n]['ref'] == $view_title_field  #Check field against $title_field for default title reference
                
&&
                
metadata_field_view_access($fields[$n]["fref"]) #Check permissions to access title field
            
)
                {
                
$return[] = $fields[$n];
                break;
                }
            }
        }

    for (
$n 0$n count($fields); $n++)
        {
        if  (
                (!
$use_permissions
                
||
                (
$ref<&& checkperm("P" $fields[$n]["fref"])) // Upload only edit access to this field
                
||
                (
metadata_field_view_access($fields[$n]["fref"]) &&  !checkperm("T" $fields[$n]["resource_type"]))
                )
            &&
                
in_array($fields[$n]["resource_type"],$validtypes)
            &&
                (!(
$external_access && !$fields[$n]["external_user_access"]))
        )
            {
            
debug("field".$fields[$n]["title"]."=".$fields[$n]["value"]);
            
$fields[$n]["title"] = lang_or_i18n_get_translated($fields[$n]["title"], "fieldtitle-");

            
// Sort nodes
            
if(in_array($fields[$n]['type'],$FIXED_LIST_FIELD_TYPES)
                && 
$fields[$n]['type'] != FIELD_TYPE_CATEGORY_TREE
                
&& trim($fields[$n]['nodes']??"") != ""
                
&& (bool)$fields[$n]['automatic_nodes_ordering'])
                {
                
$fieldnoderefs explode(",",$fields[$n]['nodes']);
                
$fieldnodes get_nodes_by_refs($fieldnoderefs);
                
$ordered_nodes array_column(reorder_nodes($fieldnodes),"name");
                
$fields[$n]['value'] = implode(", ",array_map("i18n_get_translated",$ordered_nodes));
                }

            
$return[] = $fields[$n];
            }
        }
    
# Remove duplicate fields e.g. $view_title_field included when $inherit_global_fields is false and also as a valid field.
    
$return array_unique($returnSORT_REGULAR);

    
# Return reindexed array
    
$return array_values($return);

    return 
$return;
    }

This article was last updated 3rd October 2022 12:35 Europe/London time based on the source file dated 30th September 2022 14:20 Europe/London time.