Collections functions
General functions
Render functions
Theme permission functions
Resource functions

get_resource_field_data_batch()

Description

get_resource_field_data_batch - Get all resource data for the given resources

Returns a multidimensional array with resource IDs as top level keys, then fields (order determined by $ord_by setting)
IMPORTANT: This differs from get_resource_field_data() in that only fields containing data will be returned.

e.g.
Array
(
[119912] => array
(
[0] => Array
(
[resource] => 119912
[value] => This is the title of resource 119912
[resource_type_field] => 8
[ref] => 8
[name] => title
[title] => Title
[field_constraint] => 0
[type] => 1))
....


"csvexport" (bool) - get data for CSV export (uses \ separator for category tree nodes)
"personal" (bool) - include data in fields marked as personal
"alldata" (bool) - include data in all fields, including technical metadata

Parameters

ColumnTypeDefaultDescription
$resources array (either an array of resource ids or an array returned from search results)
$use_permissions bool true Honour user permissions e.g. field access. TRUE 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)
$exportoptions mixed array Array of options as below

Return

array Array of resource data organised by resource then metadata field ID

Location

include/resource_functions.php lines 3142 to 3355

Definition

 
function get_resource_field_data_batch($resources,$use_permissions=true,$external_access=false,$ord_by=false$exportoptions = array())
    {
    
# Returns field data and field properties (resource_type_field and resource_data tables)
    # for all the resource references in the array $refs.
    # This will use a single SQL query and is therefore a much more efficient way of gathering
    # resource data for a list of resources (e.g. search result display for a page of resources).
    
if (count($resources)==0) {return array();} # return an empty array if no resources specified (for empty result sets)

    
global $view_title_field$NODE_FIELDS;

    
$csvexport = isset($exportoptions["csvexport"]) ? $exportoptions["csvexport"] : false;
    
$personal = isset($exportoptions["personal"]) ? $exportoptions["personal"] : false;
    
$alldata = isset($exportoptions["alldata"]) ? $exportoptions["alldata"] : false;

    
$restype = array();
    if(isset(
$resources[0]["resource_type"]))
        {
        
// This is an array of search results so we already have the resource types
        
$restype array_column($resources,"resource_type","ref");
        
$resourceids array_filter(array_column($resources,"ref"),function($v){return (string)(int)$v == (string)$v;});
        
$getresources $resources;
        }
    else
        {
        
$resources array_filter($resources,function($v){return (string)(int)$v == $v;});
        
$resourceids $resources;
        
$allresourcedata ps_query("SELECT ref, resource_type FROM resource WHERE ref IN (" ps_param_insert(count($resourceids)) . ")"ps_param_fill($resourceids,"i"));
        foreach(
$allresourcedata as $resourcedata)
            {
            
$restype[$resourcedata["ref"]] = $resourcedata["resource_type"];
            }
        
$getresources = array();
        foreach(
$resources as $resource)
            {
            
$getresources[]["ref"] = $resource;
            }
        }

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

    
$field_data_sql "
             SELECT rn.resource,
                    group_concat(n.name) AS `value`,
                    f.ref,
                    f.ref resource_type_field,
                    f.ref AS fref,
                    f.name,
                    f.title,
                    f.type,
                    f.order_by,
                    f.keywords_index,
                    f.partial_index,
                    f.resource_type,
                    f.display_field,
                    f.use_for_similar,
                    f.display_template,
                    f.tab,
                    f.smart_theme_name,
                    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.value_filter,
                    f.exiftool_filter,
                    f.hide_when_restricted,
                    f.omit_when_copying,
                    f.tooltip_text,
                    f.regexp_filter,
                    f.sync_field,
                    f.display_condition,
                    f.onchange_macro,
                    f.field_constraint,
                    f.linked_data_field,
                    f.fits_field,
                    f.browse_bar,
                    f.read_only,
                    f.active,
                    f.required AS frequired,
                    f.automatic_nodes_ordering,
                    f.personal_data,
                    f.include_in_csv_export,
                    f.full_width
               FROM resource_node rn
          LEFT JOIN node n ON n.ref=rn.node
          LEFT JOIN resource_type_field f ON f.ref=n.resource_type_field
              WHERE rn.resource IN (" 
ps_param_insert(count($resourceids)) . ")
                    AND (f.active=1 AND f.type IN (" 
ps_param_insert(count($node_fields)) . "))
              GROUP BY resource, f.ref"
;

    
$field_data_params array_merge(ps_param_fill($resourceids,"i"), ps_param_fill($node_fields,"i"));

    
$fields ps_query($field_data_sql,$field_data_params);

    
// Get category tree fields
    
$tree_fields get_resource_type_fields("","ref","asc",'',array(FIELD_TYPE_CATEGORY_TREE));
    
    foreach(
$tree_fields as $tree_field)
        {
        
// Establish the tree strings for all nodes belonging to the tree field
        
$addfield $tree_field;
        
// Now for each resource, build an array consisting of all of the paths for the selected nodes
        
foreach($getresources as $getresource)
            {
            
$treenodes get_cattree_nodes_ordered($tree_field["ref"], $getresource["ref"], false); # 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

            
$valstring $csvexport ? ("\"" implode("\",\"",$treenodenames) . "\"") : implode(",",$treenodenames);

            
$addfield["resource"] = $getresource["ref"];
            
$addfield["value"] = count($treenodenames) > $valstring "";
            
$addfield["resource_type_field"] = $tree_field["ref"];
            
$addfield["fref"] = $tree_field["ref"];
            
$fields[] = $addfield;
            }
        }

    if (empty(
$fields))
        {
        return array();
        }
    
// Convert to array with resource ID as index
    
$res=0;
    
$allresdata=array();
    
$validtypes = array();
    
# Support archive and global.
    
$inherit_global_fields = array();
    for (
$n=0;$n<count($fields);$n++)
        {
        
$rowadded false;
        if (!isset(
$allresdata[$fields[$n]["resource"]]))
            {
            
$allresdata[$fields[$n]["resource"]]=array();
            }

        
// Get valid field values for resource type
        
if(!isset($validtypes[$fields[$n]["ref"]]))
            {
            
$rtype $restype[$fields[$n]["resource"]];
            
$validtypes[$fields[$n]["ref"]] = array();
            
$validtypes[$fields[$n]["ref"]][] = $rtype;
            
$validtypes[$fields[$n]["ref"]][] = 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
            
if(!isset($inherit_global_fields[$fields[$n]["ref"]]))
                {
                
$inherit_global_fields[$fields[$n]["ref"]] = (bool) ps_value("SELECT inherit_global_fields AS `value` FROM resource_type WHERE ref = ?", array("i",$rtype), true"schema");
                }
            if(
$inherit_global_fields[$fields[$n]["ref"]])
                {
                
$validtypes[$fields[$n]["ref"]][] = 0;
                }
            }

            
// Add data to array
            
if  (
                    (!
$use_permissions
                    
||
                    (
$fields[$n]["resource"]<&& 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[$fields[$n]["ref"]])
                &&
                    (!(
$external_access && !$fields[$n]["external_user_access"]))
                &&
                   (!
$personal || $fields[$n]["personal_data"])
                &&
                   (
$alldata || $fields[$n]["include_in_csv_export"])
                )
                {
                
$fields[$n]["title"] = lang_or_i18n_get_translated($fields[$n]["title"], "fieldtitle-");
                
$allresdata[$fields[$n]["resource"]][$fields[$n]["ref"]] = $fields[$n];
                
$rowadded true;
                }

        
# Add title field even if $inherit_global_fields = false
        
if  (!$rowadded &&
                
$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
                
)
            {
            
$allresdata[$fields[$n]["resource"]][$fields[$n]["ref"]] = $fields[$n];
            }
        }
    
$fields = array();
    foreach(
$allresdata as $resourceid => $resdata)
        {
        
$fieldorder_by = array();
        
$fieldrestype = array();
        
$fieldref = array();
        foreach(
$resdata 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$allresdata[$resourceid]);
            }
        else
            {
            
array_multisort($fieldrestypeSORT_ASC$fieldorder_bySORT_ASC$fieldrefSORT_ASC$allresdata[$resourceid]);
            }
        }
    return 
$allresdata;
    }

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