Collections functions
General functions
Render functions
Theme permission functions
Resource functions

copy_resource()

Description

Create a new resource, copying all data from the resource with reference $from.
Note this copies only the data and not any attached file. It's very unlikely the
same file would be in the system twice, however users may want to clone an existing resource
to avoid reentering data if the resource is very similar.
If $resource_type if specified then the resource type for the new resource will be set to $resource_type
rather than simply copied from the $from resource.

Parameters

ColumnTypeDefaultDescription
$from int ID of resource
$resource_type mixed -1 ID of resource type

Return

boolean|integer

Location

include/resource_functions.php lines 3500 to 3615

Definition

 
function copy_resource($from,$resource_type=-1)
    {
    
debug("copy_resource: copy_resource(\$from = {$from}, \$resource_type = {$resource_type})");
    global 
$userref;
    global 
$always_record_resource_creator$upload_then_edit;



    
# Check that the resource exists
    
if (ps_value("SELECT COUNT(*) value FROM resource WHERE ref = ?",["i",$from],0)==0)
        {
        return 
false;
        }

    
# copy joined fields to the resource column
    
$joins=get_resource_table_joins();

    
// Filter the joined columns so we only have the ones relevant to this resource type
    
$query 'SELECT rtf.ref AS value
                    FROM resource_type_field AS rtf
            INNER JOIN resource AS r ON (rtf.resource_type != r.resource_type AND rtf.resource_type != 0)
                    WHERE r.ref = ?;'
;

    
$irrelevant_rtype_fields ps_array($query,["i",$from]);
    
$irrelevant_rtype_fields array_values(array_intersect($joins$irrelevant_rtype_fields));
    
$filtered_joins array_values(array_diff($joins$irrelevant_rtype_fields));

    
$joins_sql="";
    foreach (
$filtered_joins as $join){
        
$joins_sql.=",field$join ";
    }

    
$archive=ps_value("SELECT archive value FROM resource WHERE ref = ?",["i",$from],0);

    if (
$archive == ""// Needed if user does not have a user template
        
{
        
$archive =0;
        }

    
# Determine if the user has access to the source archive status
    
if (!checkperm("e" $archive))
        {
        
# Find the right permission mode to use
        
for ($n=-2;$n<3;$n++)
            {
            if (
checkperm("e" $n)) {$archive=$n;break;}
            }
        }

    
$sql ''$params = [];
    if(
$resource_type == -1){$sql 'resource_type';}
    else{
$sql '?'$params = ['i'$resource_type];}

    
# First copy the resources row
    
ps_query("insert into resource(resource_type,creation_date,rating,archive,access,created_by $joins_sql) select {$sql},now(),rating, ?,access,created_by $joins_sql from resource where ref= ?"array_merge($params, ['i'$archive'i'$from]));
    
$to=sql_insert_id();

    
# Set that this resource was created by this user.
    # This needs to be done if either:
    # 1) The user does not have direct 'resource create' permissions and is therefore contributing using My Contributions directly into the active state
    # 2) The user is contributiting via My Contributions to the standard User Contributed pre-active states.
    
if ((!checkperm("c")) || $archive<|| (isset($always_record_resource_creator) && $always_record_resource_creator))
        {
        
# Update the user record
        
ps_query("update resource set created_by=? where ref=?",array("i",$userref,"i",$to));
        }

    
# Copy Metadata
    
copyAllDataToResource($from,$to);

    
# Copy relationships
    
copyRelatedResources($from$to);

    
# Copy access
    
ps_query("insert into resource_custom_access(resource,usergroup,access) select ?,usergroup,access from resource_custom_access where resource=?",array("i",$to,"i",$from));

    
// Set any resource defaults
    // Expected behaviour: set resource defaults only on upload and when
    // there is no edit access OR no existing value
    
if($from || $upload_then_edit)
        {
        
$fields_to_set_resource_defaults = array();
        
$fields_data                     get_resource_field_data($fromfalsefalse);

        
// Set resource defaults only for fields user hasn't set
        // $from data may have not been copied to new resource by copy_resource_nodes() if user has no edit access to field
        
foreach($fields_data as $field_data)
            {
            if(
metadata_field_edit_access($field_data['ref'])
                && 
metadata_field_view_access($field_data['ref']) 
                && 
trim((string)$field_data['value']) != "" // Field has a value 
                
&& !($upload_then_edit && $from 0))
                {
                continue;
                }

            
$fields_to_set_resource_defaults[] = $field_data['ref'];
            }

        if(
count($fields_to_set_resource_defaults))
            {
            
set_resource_defaults($to$fields_to_set_resource_defaults);
            }
        }

    
// Autocomplete any blank fields without overwriting any existing metadata
    
autocomplete_blank_fields($tofalse);

    
# Log this
    
daily_stat("Create resource",$to);
    
resource_log($to,LOG_CODE_CREATED,0);

    
hook("afternewresource""", array($to));

    return 
$to;
    }

This article was last updated 5th October 2022 04:05 Europe/London time based on the source file dated 30th September 2022 14:20 Europe/London time.