Collections functions
General functions
Node functions
Render functions
Theme permission functions
User functions
Resource functions

save_original_file_as_alternative()

Description

Save the original file being replaced, as an alternative file

Parameters

ColumnTypeDefaultDescription
$ref integer (required) ID of original resource

Return

boolean | int int = id of new alternative file; false = file not saved

Location

include/resource_functions.php lines 7316 to 7393

Definition

 
function save_original_file_as_alternative($ref)
    {
    
debug("save_original_file function called for resource ref: " . (int)$ref);
    if (!
$ref)
        {
        
debug("ERROR: Unable to save original file as alternative - no resource id passed");
        return 
false;
        }

    
/*
    global vars
    * @param boolean $alternative_file_previews                  Generate thumbs/previews for alternative files?
    * @param boolean $alternative_file_previews_batch            Generate thumbs/previews for alternative files?
    * @param array   $lang
    */

    
global $lang$alternative_file_previews$alternative_file_previews_batch$filename_field;

    
// Values may be passed in POST or GET data from upload_batch.php
    
$replace_resource_original_alt_filename getval('replace_resource_original_alt_filename'''); // alternative filename
    
$filename_field_use                     getval('filename_field'$filename_field); // GET variable - field to use for filename

    // Make the original into an alternative, need resource data so we can get filepath/extension
    
$origdata     get_resource_data($ref);
    
$origpath=get_resource_path($reftrue""true$origdata["file_extension"]);
    
# It's possible that there is no original in the filestore; quit if this is the case
    
if (!file_exists($origpath)) 
        {
        
debug("ERROR: Unable to find original file to save as alternative: " $origpath);
        return 
false;
        }

    
$origfilename get_data_by_field($ref$filename_field_use);

    
$newaltname        str_replace('%EXTENSION'strtoupper($origdata['file_extension']), $lang['replace_resource_original_description']);
    
$newaltdescription nicedate(date('Y-m-d H:i'), true);

    if(
'' != $replace_resource_original_alt_filename)
        {
        
$newaltname $replace_resource_original_alt_filename;
        }

    
$newaref add_alternative_file($ref$newaltname$newaltdescription$origfilename$origdata['file_extension'], $origdata['file_size']);

    
$newaltpath=get_resource_path($reftrue""true$origdata["file_extension"], -11false""$newaref);
    
# Move the old file to the alternative file location
    
if(!hook('save_original_alternative_extra''', array('origpath' => $origpath'newaltpath' => $newaltpath)))
        {
        
rename($origpath$newaltpath);
        }

    if (
$alternative_file_previews)
        {
        
// Move the old previews to new paths
        
$ps=ps_query("SELECT " columns_in("preview_size")  . " FROM preview_size",[],"schema");
        for (
$n=0;$n<count($ps);$n++)
            {
            
# Find the original
            
$orig_preview_path=get_resource_path($reftrue$ps[$n]["id"],false"");
            if (
file_exists($orig_preview_path))
                {
                
# Copy the old preview file to the alternative preview file location, not moved as original may still be required
                
$alt_preview_path=get_resource_path($reftrue$ps[$n]["id"], true"", -11false""$newaref);
                
copy($orig_preview_path$alt_preview_path);
                }
            
# Also for the watermarked versions.
            
$wmpath=get_resource_path($ref,true,$ps[$n]["id"],false,"jpg",-1,1,true );
            if (
file_exists($wmpath))
                {
                
# Move the old preview file to the alternative preview file location
                
$alt_preview_wmpath=get_resource_path($reftrue$ps[$n]["id"], true"", -11true""$newaref);
                
copy($wmpath$alt_preview_wmpath);
                }
            }
        }
    
debug("save_original_file_as_alternative() completed");
    return 
$newaref;
    }

This article was last updated 29th May 2024 12:05 Europe/London time based on the source file dated 24th May 2024 10:10 Europe/London time.