Coding standards
Security in ResourceSpace
Developer reference
Database
Action functions
Admin functions
Ajax functions
Annotation functions
API functions
Collections functions
Comment functions
Config functions
CSV export functions
Dash functions
Debug functions
Encryption functions
Facial recognition functions
File functions
General functions
Language functions
Log functions
Login functions
Message functions
Migration functions
Node functions
PDF functions
Plugin functions
Render functions
Reporting functions
Request functions
Research functions
Slideshow functions
Theme permission functions
User functions
Video functions
Database functions
Metadata functions
Resource functions
Search functions
Map functions
Job functions
Tab functions
Test 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
$translate_value bool true Field value will be translated with i18n_get_translated() unless FALSE supplied.

Return

array|boolean

Location

include/resource_functions.php lines 3345 to 3532

Definition

 
function get_resource_field_data($ref$multi false$use_permissions true$originalref null$external_access false$ord_by false$forcsv false$translate_value true)
    {
    
# 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$pagename;

    
# 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(
$use_permissions && checkperm("T" $rtype))
        {
        return 
false;
        }

    
# If using metadata templates,
    
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.ref' 'f.global desc, f.order_by, f.ref');

    
// Category tree fields need special handling
    
$nontree_field_types    array_diff($NODE_FIELDS,array(FIELD_TYPE_CATEGORY_TREE));

    
$field_restypes get_resource_type_field_resource_types();
    
$restypesql "";
    
$restype_params = [];
    if(!
$multi)
        {
        
$restypesql "AND (f.global=1 OR f.ref=? OR f.ref IN (SELECT resource_type_field FROM resource_type_field_resource_type rtjoin WHERE rtjoin.resource_type=?))";
        
$restype_params[] = "i";$restype_params[] = $view_title_field;
        
$restype_params[] = "i";$restype_params[] = $rtype;
        }

    
$field_info_sql 
    
"SELECT f.ref resource_type_field,
            f.ref AS fref,
            f.required AS frequired, " 
.
            
columns_in("resource_type_field""f") . 
    
" FROM resource_type_field f
    WHERE (f.active=1 AND f.type IN (" 
ps_param_insert(count($nontree_field_types)) . ") " $restypesql ")
    ORDER BY 
{$order_by_sql}";

    
$field_info_params array_merge(ps_param_fill($nontree_field_types,"i"),$restype_params);

    
# Fetch field information first
    
$fields ps_query($field_info_sql,$field_info_params);

    
# Now fetch field values ensuring that multiple node values are ordered correctly
    
$field_value_sql 
    
"SELECT rn.resource, n.resource_type_field `field`, rn.node, n.name 
        FROM resource_node rn
    INNER JOIN node n on n.ref = rn.node and rn.resource=?
    ORDER BY n.resource_type_field, n.order_by"
;

    
$field_values ps_query($field_value_sql,array("i"$ref));

    
$field_node_list=array();
    
$field_ref_list=array();
    
$last_field=null;
    
    
# Assemble comma separated lists of node names and refs to attach to the fields array
    
foreach($field_values as $field_value) {
        
$this_field=$field_value['field'];
        if(
$this_field===$last_field) {
            
$field_node_list[$this_field]['values'] .= (", " $field_value['name']);
            
$field_ref_list[$this_field]['refs'] .= ("," $field_value['node']);
        }
        else {
            
$field_node_list[$this_field]['values'] = $field_value['name'];
            
$field_ref_list[$this_field]['refs'] = $field_value['node'];
            
$last_field=$this_field;
        }
    }

    
# Attach the lists of node names and refs to the corresponding fields array entry
    
foreach (array_keys($fields) as $fikey) {
        
$this_field $fields[$fikey]['ref'];
        if(isset(
$field_node_list[$this_field])) {
            
$fields[$fikey]['value'] = $field_node_list[$this_field]['values'];
            
$fields[$fikey]['nodes'] = $field_ref_list[$this_field]['refs'];
        }
        else{
            
$fields[$fikey]['value'] = null;
            
$fields[$fikey]['nodes'] = null;
        }
    }

    
# Build an array of valid resource types and only return fields for these types. Translate field titles.
    
if ($multi)
        {
        
// Get all fields
        
$valid_resource_types ps_array('SELECT ref AS `value` FROM resource_type',[],'schema');
        }
    else
        {
        
$valid_resource_types = [$rtype] ;
        }

    
# Add category tree values, reflecting tree structure
    
$tree_fields get_resource_type_fields($valid_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"];
        
$fieldglobal[$fkey]     = $field["global"];
        
$fieldref[$fkey]        = $field["ref"];
        }
    if(
$ord_by)
        {
        
array_multisort($fieldorder_bySORT_ASC$fieldglobalSORT_ASC$fieldrefSORT_ASC$fields);
        }
    else
        {
        
array_multisort($fieldglobalSORT_DESC$fieldorder_bySORT_ASC$fieldrefSORT_ASC$fields);
        }

    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"]))
                )
            &&
                (!(
$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-");
            
// Add in associated resource types
            
$fields[$n]["resource_types"] = implode(",",$field_restypes[$fields[$n]["ref"]]);
            
// 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");
                if (
$translate_value)
                    {
                    
$fields[$n]['value'] = implode(", "array_map("i18n_get_translated"$ordered_nodes));
                    }
                else
                    {
                    
$fields[$n]['value'] = implode(", "$ordered_nodes);
                    }
                }

            
$return[] = $fields[$n];
            }
        }
    
# Remove duplicate fields
    
$return array_unique($returnSORT_REGULAR);

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

    return 
$return;
    }

This article was last updated 11th December 2024 20:05 Europe/London time based on the source file dated 11th December 2024 15:55 Europe/London time.