Collections functions
Encryption functions
General functions
Render functions
Theme permission functions
Video 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 defautl (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

void

Location

include/resource_functions.php lines 2729 to 2900

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;

    
# 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 sql_value("select resource_type value FROM resource WHERE ref='" escape_check($originalref) . "'",0);
    
$rtype = ($rtype == "") ? $rtype;

    
# If using metadata templates, 
    
$templatesql "";
    global 
$metadata_template_resource_type$NODE_FIELDS;
    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 'order_by, resource_type, ref' 'resource_type, order_by, ref');
    
    
    
// Remove Category tree fields as these need special handling

    
$node_fields_exclude implode(','$NODE_FIELDS);
    
$node_fields    array_diff($NODE_FIELDS,array(FIELD_TYPE_CATEGORY_TREE));
    
$node_fields_list implode(','$node_fields);

    
$fieldsSQL "
             SELECT d.value,
                    f1.ref resource_type_field,
                    f1.*,
                    f1.required AS frequired,
                    f1.ref AS fref,
                    f1.field_constraint,
                    f1.automatic_nodes_ordering,
                    f1.personal_data,
                    f1.include_in_csv_export,
                    f1.full_width
               FROM resource_type_field AS f1
          LEFT JOIN resource_data d
                 ON d.resource_type_field = f1.ref AND d.resource = '" 
escape_check($ref) . "'
              WHERE (
                            f1.active=1 and
                            f1.type NOT IN (
{$node_fields_exclude})
                        AND (" 
. ($multi "1 = 1" "f1.resource_type = 0 OR f1.resource_type = 999 OR f1.resource_type = '{$rtype}'") . ")
                    )

              UNION

             SELECT group_concat(if(rn.resource = '" 
escape_check($ref) . "', n.name, NULL)) AS `value`,
                    f2.ref resource_type_field,
                    f2.*,
                    f2.required AS frequired,
                    f2.ref AS fref,
                    f2.field_constraint,
                    f2.automatic_nodes_ordering,
                    f2.personal_data,
                    f2.include_in_csv_export,
                    f2.full_width
               FROM resource_type_field AS f2
          LEFT JOIN node AS n ON n.resource_type_field = f2.ref
          LEFT JOIN resource_node AS rn ON rn.node = n.ref AND rn.resource = '" 
escape_check($ref) . "'
              WHERE (
                            f2.active=1 and
                            f2.type IN (
{$node_fields_list})
                        AND (" 
. ($multi "1 = 1" "f2.resource_type = 0 OR f2.resource_type = 999 OR f2.resource_type = '{$rtype}'") . ")
                    )
           GROUP BY ref
           ORDER BY 
{$order_by_sql}
    "
;

    if(!
$ord_by)
        {
        
debug('GENERAL/GET_RESOURCE_FIELD_DATA: use perms: ' . !$use_permissions);
        }

    
$fields sql_query($fieldsSQL);

    
// Add category tree values, reflecting tree structure
    
$tree_fields get_resource_type_fields(array('0',$rtype),"ref","asc",'',array(FIELD_TYPE_CATEGORY_TREE));
    foreach(
$tree_fields as $tree_field)
        {
        
$addfield$tree_field;

        
$treenodes get_resource_nodes($ref$tree_field["ref"], trueSORT_ASC);
        
$treetext_arr get_tree_strings($treenodes);
        
// Quoting each element is required for csv export
        
$valstring $forcsv ? ("\"" implode("\",\"",$treetext_arr) . "\"") : implode(",",$treetext_arr);
        
$addfield["value"] = count($treetext_arr) > $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);
        }

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

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

    
// 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) sql_value("SELECT inherit_global_fields AS `value` FROM resource_type WHERE ref = '{$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-"); 
            
$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 
$return;
    }

This article was last updated 30th November 2020 14:05 Europe/London time based on the source file dated 30th November 2020 11:55 Europe/London time.