Collections functions
Encryption functions
General functions
Theme permission functions
Resource functions

generateResourcesMetadataCSV()

Description

Generates the CSV content of the metadata for resources passed in the array

Parameters

ColumnTypeDefaultDescription
$resources array
$personal false
$alldata false

Return

string

Location

include/csv_export_functions.php lines 12 to 117

Definition

 
function generateResourcesMetadataCSV(array $resources,$personal=false,$alldata=false)
    {
    global 
$lang$csv_export_add_original_size_url_column$file_checksums;
    
$return                 '';
    
$csv_field_headers      = array();
    
$resources_fields_data  = array();

    foreach(
$resources as $resource)
        {
        
$resdata get_resource_data($resource['ref']);

        
// Add resource type
        
$restype get_resource_type_name($resdata["resource_type"]);
        
$csv_field_headers["resource_type"] = $lang["resourcetype"];
        
$resources_fields_data[$resource['ref']]["resource_type"] = $restype;
        
        
// Add contributor
        
$udata=get_user($resdata["created_by"]);
        if (
$udata!==false)
            {
            
$csv_field_headers["created_by"] = $lang["contributedby"];
            
$resources_fields_data[$resource['ref']]["created_by"] = (trim($udata["fullname"]) != "" $udata["fullname"] :  $udata["username"]);
            }

        if (
$alldata && $file_checksums)
            {
            
$csv_field_headers["file_checksum"] = $lang["filechecksum"];
            
$resources_fields_data[$resource['ref']]["file_checksum"] = $resdata["file_checksum"];
            }

        foreach(
get_resource_field_data($resource['ref'], falsetrueNULL'' != getval('k'''), true,true) as $field_data)
            {
            
// If $personal=true, return personal_data fields only.
            // If $alldata=false, return only fields marked as 'Include in CSV export'
            
if ((!$personal || $field_data["personal_data"]) && ($alldata || $field_data["include_in_csv_export"]))
                {
                
$csv_field_headers[$field_data['ref']] = $field_data['title'];
                
$resources_fields_data[$resource['ref']][$field_data['resource_type_field']] = $field_data['value'];
                }
            }

        
// Add original size URL column values
        
if(!$csv_export_add_original_size_url_column)
            {
            continue;
            }

        
/*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($resource);
        if(
!= $access || checkperm("T{$resource['resource_type']}_"))
            {
            continue;
            }

        
$filepath      get_resource_path($resource['ref'], true''false$resource['file_extension'], -11false'', -1false);
        
$original_link get_resource_path($resource['ref'], false''false$resource['file_extension'], -11false'', -1false);
        if(
file_exists($filepath))
            {
            
$resources_fields_data[$resource['ref']]['original_link'] = $original_link;
            }
        }

    
// Add original size URL column
    
if($csv_export_add_original_size_url_column)
        {
        
$csv_field_headers['original_link'] = $lang['collection_download_original'];
        }

    
$csv_field_headers array_unique($csv_field_headers);

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

    
// Results
    
$csv_row '';
    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("\n","\r","\""),array("","","\"\""),i18n_get_translated($field_value)) . '",';
                    }
                }
            }
        
        
$csv_row rtrim($csv_row',');
        
$csv_row .= "\n";
        
$return  .= $csv_row;
        }

    return 
$return;
    }

This article was last updated 14th July 2020 00:35 Europe/London time based on the source file dated 24th June 2020 14:15 Europe/London time.