Collections functions
General functions
Node functions
Render functions
Theme permission functions
User 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 int -1 ID of resource type
$origin string '' Origin of resource when uploading, leave blank if not an upload

Return

boolean|integer

Location

include/resource_functions.php lines 3843 to 3959

Definition

 
function copy_resource($from,$resource_type=-1,$origin='')
    {
    
debug("copy_resource: copy_resource(\$from = {$from}, \$resource_type = {$resource_type})");
    global 
$userref;
    global 
$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();

    
# Filtering data join columns to only copy those relevant to the new resource.
    
if ($resource_type === -1)
        {
        
# New resource will be of the same resource type so get fields valid for the source resource type
        
$query 'SELECT rtf.ref AS value FROM resource_type_field AS rtf
            INNER JOIN resource AS r ON (rtf.global = 1 OR rtf.ref IN (SELECT resource_type_field FROM resource_type_field_resource_type rtjoin WHERE rtjoin.resource_type = r.resource_type))
            WHERE r.ref = ?;'
;
        
$relevant_rtype_fields ps_array($query, ["i"$from]);
        }
    else
        {
        
# New resource has a different resource type so only copy fields relevant to the new resource type
        
$query 'SELECT ref AS value FROM resource_type_field WHERE global = 1 OR ref IN (SELECT resource_type_field FROM resource_type_field_resource_type WHERE resource_type = ?);';
        
$relevant_rtype_fields ps_array($query, ["i"$resource_type]);
        }
    
$filtered_joins array_values(array_intersect($joins$relevant_rtype_fields));
    
$joins_sql = empty($filtered_joins) ? '' ',' implode(','array_map(prefix_value('field'), $filtered_joins));

    
$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<0)
        {
        
# Update the user record
        
ps_query("update resource set created_by=? where ref=?",array("i",$userref,"i",$to));
        }

    
# Copy Metadata
    
copy_resource_nodes($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,$origin);

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

    return 
$to;
    }

This article was last updated 29th March 2024 11:35 Europe/London time based on the source file dated 28th March 2024 11:35 Europe/London time.