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

generateResourcesMetadataCSV()

Description

Generates the CSV content of the metadata for resources passed in the array
The CSV is echoed to output for direct download or saved to a file

Parameters

ColumnTypeDefaultDescription
$resources array array of resource ids to create a CSV for
$personal bool false flag to include only fields expected to include personal data
$alldata bool false flag to include extra data from the resource table
$outputfile string "" optional file path to output CSV to

Return

bool|void TRUE if the file has been created, void if the data has been sent as a direct download

Location

include/csv_export_functions.php lines 17 to 229

Definition

 
function generateResourcesMetadataCSV(array $resources,$personal=false,$alldata=false,$outputfile="")
    {
    global 
$lang$csv_export_add_original_size_url_column$file_checksums$k$scramble_key,
        
$get_resource_data_cache,$csv_export_add_data_fields;
    
    
// Write the CSV to a disk to avoid memory issues with large result sets
    
$tempcsv trim($outputfile) != "" $outputfile get_temp_dir() . "/csv_export_" uniqid() . ".csv";

    
$csv_field_headers      = array();
    
$csvoptions = array("csvexport"=>true,"personal"=>$personal,"alldata"=>$alldata);
    
$allfields get_resource_type_fields("","order_by","asc");
    
$cache_location get_temp_dir();
    
$cache_data = array();
    
$restypearr get_resource_types();
    
$resource_types = array();
    
// Sort into array with ids as keys
    
foreach($restypearr as $restype)
        {
        
$resource_types[$restype["ref"]] = $restype;
        }

    
$field_restypes get_resource_type_field_resource_types();

    
// Break resources up into smaller arrays to avoid hitting memory limits
    
$resourcebatches array_chunk($resources2000);

    
$csv_field_headers["resource_type"] = $lang["resourcetype"];
    
$csv_field_headers["status"] = $lang['status'];
    
$csv_field_headers["created_by"] = $lang["contributedby"];
    if (
$alldata) {
        
$csv_field_headers["access"] = $lang['access'];
    }
    if(
$file_checksums && $alldata)
        {
        
$csv_field_headers["file_checksum"] = $lang["filechecksum"];
        }
    
// Add original size URL column
    
if($csv_export_add_original_size_url_column)
        {
        
$csv_field_headers['original_link'] = $lang['collection_download_original'];
        }

    for(
$n=0;$n<count($resourcebatches);$n++)
        {
        
$resources_fields_data = array();
        
$fullresdata get_resource_field_data_batch($resourcebatches[$n],true,$k != '',true,$csvoptions);

        
// Get data for all resources
        
$resource_data_array get_resource_data_batch($resourcebatches[$n]);
        foreach(
$resourcebatches[$n] as $resource)
            {
            
$resdata = isset($resource_data_array[$resource]) ? $resource_data_array[$resource] : false;
            if(!
$resdata || checkperm("T" $resdata["resource_type"]))
                {
                continue;
                }

            
// Add resource type
            
$restype get_resource_type_name($resdata["resource_type"]);
            
$resources_fields_data[$resource]["resource_type"] = $restype;

            
// Add resource status
            
$resources_fields_data[$resource]['status'] = $lang["status{$resource_data_array[$resource]['archive']}"] ?? $lang['unknown'];
            
            if (
$alldata) {
            
// Add resource access
            
$resources_fields_data[$resource]['access'] = $lang["access{$resource_data_array[$resource]['access']}"] ?? $lang['unknown'];
            }

            
// Add contributor
            
$udata=get_user($resdata["created_by"]);
            if (
$udata!==false)
                {
                
$resources_fields_data[$resource]["created_by"] = (trim($udata["fullname"]??"") != "" $udata["fullname"] :  $udata["username"]);
                }

            if (
$alldata)
                {
                if (isset(
$csv_export_add_data_fields))
                    {
                    foreach(
$csv_export_add_data_fields as $addfield)
                        {
                        
$resources_fields_data[$resource][$addfield["column"]] = $resdata[$addfield["column"]];
                        
$csv_field_headers[$addfield["column"]]=$addfield["title"];
                        }
                    }
                if (
$file_checksums)
                    {
                    
$resources_fields_data[$resource]["file_checksum"] = $resdata["file_checksum"];
                    }
                }
            
            foreach(
$allfields as $restypefield)
                {
                if  (
                    
metadata_field_view_access($restypefield["ref"])  
                    && 
                        (!
$personal || $restypefield["personal_data"])
                    && 
                        (
$alldata || $restypefield["include_in_csv_export"])
                    &&
                        (
                        isset(
$field_restypes[$restypefield["ref"]]) && in_array($resdata["resource_type"],$field_restypes[$restypefield["ref"]])
                        )
                    )
                    {
                    if(!isset(
$csv_field_headers[$restypefield["ref"]]))
                        {
                        
$csv_field_headers[$restypefield["ref"]] = $restypefield['title'];
                        }
                    
// Check if the resource has a value for this field in the data retrieved
                    
if(isset($fullresdata[$resource]))
                        {
                        
$resdataidx =array_search($restypefield["ref"], array_column($fullresdata[$resource], 'ref'));
                        
$fieldvalue = ($resdataidx !== false) ? $fullresdata[$resource][$resdataidx]["value"] : "";
                        
$resources_fields_data[$resource][$restypefield['ref']] = $fieldvalue;
                        }
                    }
                }

            
/*Provide the original URL only if we have access to the resource or the user group
            doesn't have restricted access to the original size*/
            
$access get_resource_access($resdata);
            if(
!= $access || resource_has_access_denied_by_RT_size($resdata['resource_type'], ''))
                {
                continue;
                }
            if(
$csv_export_add_original_size_url_column)
                {
                
$filepath      get_resource_path($resourcetrue''false$resdata['file_extension'], -11false'', -1false);
                
$original_link get_resource_path($resourcefalse''false$resdata['file_extension'], -11false'', -1false);
                if(
file_exists($filepath))
                    {
                    
$resources_fields_data[$resource]['original_link'] = $original_link;
                    }
                }
            }

        if(
count($resources_fields_data) > 0)
            {
            
// Save data to temporay files in order to prevent memory limits being reached
            
$tempjson json_encode($resources_fields_data);
            
$cache_data[$n] = $cache_location "/csv_export_" md5($scramble_key $tempjson) . ".json"// Scrambled path to cache
            
file_put_contents($cache_data[$n], $tempjson);
            
$tempjson null;
            }
        }
   
    
$csv_field_headers array_unique($csv_field_headers);

    
// Header
    
$header "\"" $lang['resourceids'] . "\",\"" implode('","'$csv_field_headers) . "\"\n";
    
file_put_contents($tempcsv,$header);

    
// Results
    
for($n=0;$n<count($resourcebatches);$n++)
        {
        
$filedata "";
        
$resources_fields_data = array();
        if(
file_exists($cache_data[$n]))
            {
            
$resources_fields_data json_decode(file_get_contents($cache_data[$n]),true);
            }
        if(
is_null($resources_fields_data))
            {
            
$resources_fields_data = array();
            }

        foreach(
$resources_fields_data as $resource_id => $resource_fields)
            {
            
// First column will always be Resource ID
            
$csv_row $resource_id ',';

            
// Field values
            
foreach($csv_field_headers as $column_header => $column_header_title)
                {
                if(!
array_key_exists($column_header$resource_fields))
                    {
                    
$csv_row .= '"",';
                    continue;
                    }

                foreach(
$resource_fields as $field_name => $field_value)
                    {
                    if(
$column_header == $field_name)
                        {
                        
$csv_row .= '"' str_replace(array("\""),array("\"\""),i18n_get_translated($field_value)) . '",';
                        }
                    }
                }
            
            
$csv_row rtrim($csv_row',');
            
$csv_row .= "\n";
            
$filedata .= $csv_row;
            }
        
        
// Add this data to the file and delete disk copy of array
        
file_put_contents($tempcsv,$filedataFILE_APPEND);
        if(
file_exists($cache_data[$n]))
            {
            
unlink($cache_data[$n]);
            }
        }        
    
    if(
$outputfile != "")
        {
        
// Data has been saved to file, just return
        
return true;
        }

    
// Echo the data for immediate download
    
echo file_get_contents($tempcsv);
    }

This article was last updated 17th November 2024 15:35 Europe/London time based on the source file dated 3rd July 2024 10:00 Europe/London time.