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

copyResourceDataValues()

Description

Copy resource data from one resource to another one.

Parameters

ColumnTypeDefaultDescription
$from integer Resource we are copying data from
$to
$resource_type ""
$ref integer Resource we are copying data to

Return

void

Location

include/resource_functions.php lines 6423 to 6484

Definition

 
function copyResourceDataValues($from$to$resource_type "")
    {
    
$from            escape_check($from);    
    
$to              escape_check($to);
    
$omit_fields_sql '';

    
// When copying normal resources from one to another, check for fields that should be excluded
    // NOTE: this does not apply to user template resources (negative ID resource)
    
if($from 0)
        {
        
$omitfields      ps_array("SELECT ref AS `value` FROM resource_type_field WHERE omit_when_copying = 1", array(), "schema");
        if (
count($omitfields) > 0)
            {
            
$omit_fields_sql "AND rd.resource_type_field NOT IN ('" implode("','"$omitfields) . "')";
            }
        else
            {
            
$omit_fields_sql "";
            }
        }
    
    
$resource_type_sql "AND (rtf.resource_type = r.resource_type OR rtf.resource_type = 999 OR rtf.resource_type = 0)";
    
// Don't consider resource types if saving metadata template as fields from all types should be copied.
    
global $metadata_template_resource_type;
    if (isset(
$metadata_template_resource_type) && $resource_type==$metadata_template_resource_type)
        {
        
$resource_type_sql "";
        }

    
// This is for logging after the insert statement
    
$data_to_add sql_query("
        SELECT
            rd.resource_type_field AS field,
            rd.value AS value
        FROM resource_data AS rd
        JOIN resource AS r ON rd.resource = r.ref
        JOIN resource_type_field AS rtf ON rd.resource_type_field = rtf.ref
        
{$resource_type_sql}
        WHERE rd.resource = '
{$from}'
        
{$omit_fields_sql}
    "
);

    
sql_query("
        INSERT INTO resource_data(resource, resource_type_field, value)
             SELECT '
{$to}',
                    rd.resource_type_field,
                    rd.value
               FROM resource_data AS rd
               JOIN resource AS r ON rd.resource = r.ref
               JOIN resource_type_field AS rtf ON rd.resource_type_field = rtf.ref
               
{$resource_type_sql}
              WHERE rd.resource = '
{$from}'
                
{$omit_fields_sql}
    "
);
    
    foreach(
$data_to_add as $data)
        {
        
resource_log($to,LOG_CODE_EDITED,$data["field"],'','',$data["value"]);
        }

    return;
    }

This article was last updated 28th June 2022 15:05 Europe/London time based on the source file dated 27th June 2022 12:50 Europe/London time.