Collections functions
Encryption functions
General functions
Render functions
Theme permission functions
Video functions
Resource functions

generateResourcesMetadataCSV()

Description

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

Parameters

ColumnTypeDefaultDescription
$resources array (array of resource ids)
$personal false
$alldata false
$outputfile ""

Return

string

Location

include/csv_export_functions.php lines 12 to 201

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;
    
    
// 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_query_cache_location();
    
$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;
        }

    
// 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["created_by"] = $lang["contributedby"];
    
$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'];
        }

    
// Array to store fields that have data, if no data we won't include it
    
$include_fields = array();

    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 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 && $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"])
                    && 
                        !(
checkperm("T" $restypefield["resource_type"]))
                    &&
                        (
                        
$restypefield["resource_type"] == $resdata["resource_type"]
                        ||
                        (
$restypefield["resource_type"] == && (bool)$resource_types[$resdata["resource_type"]]["inherit_global_fields"])
                        ||
                        (
$restypefield["resource_type"] == 999 && $resdata["archive"] == 2)
                        )
                    )
                    {
                    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
                    
$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 || checkperm("T{$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();
            }

        
$resources_fields_data json_decode(file_get_contents($cache_data[$n]),true);
        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";
            
$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 24th November 2020 20:05 Europe/London time based on the source file dated 17th November 2020 11:00 Europe/London time.