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

email_collection_request()

Description

Email a collection request to the team responsible for dealing with requests. Request mode 0 only (non managed).

Parameters

ColumnTypeDefaultDescription
$ref integer
$details mixed
$external_email mixed

Return

void

Location

include/request_functions.php lines 259 to 423

Definition

 
function email_collection_request($ref,$details,$external_email)
    {
    global 
$applicationname,$email_from,$baseurl,$email_notify,$username,$useremail,$lang,$request_senduserupdates,$userref,$resource_type_request_emails,$resource_request_reason_required,$admin_resource_access_notifications$always_email_from_user,$collection_empty_on_submit;
    
    if (
trim($details)=="" && $resource_request_reason_required) {return false;}
    
    
$message="";
    
#if (isset($username) && trim($username)!="") {$message.=$lang["username"] . ": " . $username . "\n";}
    
    
$templatevars['url']=$baseurl."/?c=".$ref;
    
$collectiondata=get_collection($ref);
    if (isset(
$collectiondata["name"])){
    
$templatevars["title"]=$collectiondata["name"];}
    
    
# Create a copy of the collection which is the one sent to the team. This is so that the admin
    # user can e-mail back an external URL to the collection if necessary, to 'unlock' full (open) access.
    # The user cannot then gain access to further resources by adding them to their original collection as the
    # shared collection is a copy.
    # A complicated scenario that is best avoided using 'managed requests'.
    
$newcopy=create_collection(-1,$lang["requestcollection"]);
    
copy_collection($ref,$newcopy);
    
    
// Make sure a collection does not include resources that may have been hidden from the user due
    // to archive state, resource type or access changes and that they are not aware they are requesting.
    // Without this a full copy can confuse the request administrator
    
$col_visible do_search("!collection" $ref,'','','',-1,'desc',false,0,false,false,'',false,false,true);
    
$colresources get_collection_resources($ref);    
    foreach(
$colresources as $colresource)
        {
        if(!
in_array($colresource,array_column($col_visible,"ref")))
            {
            
remove_resource_from_collection($colresource,$newcopy,false);
            }
        }
    
    if(
$collection_empty_on_submit)
        {
        
remove_all_resources_from_collection($ref);    
        }
        
    
$ref=$newcopy;
    
    
$templatevars["requesturl"]=$baseurl."/?c=".$ref;
    
    
$templatevars['username']=$username " (" $useremail ")";
    
$userdata=get_user($userref);
    
$templatevars["fullname"]=$userdata["fullname"];
    
    
reset ($_POST);
    foreach (
$_POST as $key=>$value)
        {
        if (
strpos($key,"_label")!==false)
            {
            
# Add custom field
            
$setting=trim($_POST[str_replace("_label","",$key)]);
            if (
$setting!="")
                {
                
$message.=$value ": " $_POST[str_replace("_label","",$key)] . "\n\n";
                }
            }
        }
    if (
trim($details)!="") {$message.=$lang["requestreason"] . ": " newlines($details) . "\n\n";}
    
    
# Add custom fields
    
$c="";
    global 
$custom_request_fields,$custom_request_required;
    if (isset(
$custom_request_fields))
        {
        
$custom=explode(",",$custom_request_fields);
    
        
# Required fields?
        
if (isset($custom_request_required)) {$required=explode(",",$custom_request_required);}
    
        for (
$n=0;$n<count($custom);$n++)
            {
            if (isset(
$required) && in_array($custom[$n],$required) && getval("custom" $n,"")=="")
                {
                return 
false# Required field was not set.
                
}
            
            
$message.=i18n_get_translated($custom[$n]) . ": " getval("custom" $n,"") . "\n\n";
            }
        }
        
    
$amendedmessage=hook('amend_request_message','', array($userref$ref, isset($collectiondata) ? $collectiondata : array(), $message, isset($collectiondata)));
    if(
$amendedmessage)
        {
        
$message=$amendedmessage;
        }
    
    
$templatevars["requestreason"]=$message;
    
    
$userconfirmmessage $lang["requestsenttext"] . "\n\n$message";
    
$message=$lang["user_made_request"] . "\n\n" $lang["username"] . ": " $username "\n$message";
    
$notification_message=$message;
    
$message.=$lang["viewcollection"] . ":\n" .  $templatevars['url'];
    
    
$admin_notify_emails=array();    
    
$admin_notify_users=array();
    
# Check if alternative request email notification address is set, only valid if collection contains resources of the same type 
    
if(isset($resource_type_request_emails))
        {
        
$requestrestypes=array_unique(sql_array("select r.resource_type as value from collection_resource cr left join resource r on cr.resource=r.ref where cr.collection='$ref'"));
        if(
count($requestrestypes)==&& isset($resource_type_request_emails[$requestrestypes[0]]))
            {
            
$admin_notify_emails[]=$resource_type_request_emails[$requestrestypes[0]];
            }
        }
    else
        {
        
$notify_users=get_notification_users("RESOURCE_ACCESS");
        foreach(
$notify_users as $notify_user)
            {
            
get_config_option($notify_user['ref'],'user_pref_resource_access_notifications'$send_message$admin_resource_access_notifications);        
            if(
$send_message==false){continue;}     
            
            
get_config_option($notify_user['ref'],'email_user_notifications'$send_email);    
            if(
$send_email && $notify_user["email"]!="")
                {
                
$admin_notify_emails[] = $notify_user['email'];             
                }        
            else
                {
                
$admin_notify_users[]=$notify_user["ref"];
                }
            }
        }
    foreach(
$admin_notify_emails as $admin_notify_email)
        {
        
send_mail($admin_notify_email,$applicationname ": " $lang["requestcollection"] . " - $ref",$message,($always_email_from_user)?$useremail:$email_from,($always_email_from_user)?$useremail:$email_from,"emailcollectionrequest",$templatevars);
        }
    
    if (
count($admin_notify_users)>0)
        {
        
message_add($admin_notify_users,$notification_message,$templatevars["requesturl"],$userrefMESSAGE_ENUM_NOTIFICATION_TYPE_SCREEN,MESSAGE_DEFAULT_TTL_SECONDS,COLLECTION_REQUEST$ref);
        }
    
    
# $userref and $useremail will be that of the internal requestor    
    # - We need to send the $userconfirmmessage to the internal requestor saying that their request has been submitted    
    
if (isset($userref) && $request_senduserupdates)
        {
        
get_config_option($userref,'email_user_notifications'$send_email);    
        if(
$send_email && filter_var($useremailFILTER_VALIDATE_EMAIL))
            {
            
send_mail($useremail,$applicationname ": " $lang["requestsent"] . " - $ref",$userconfirmmessage,$email_from,$email_notify,"emailusercollectionrequest",$templatevars);
            }        
        else
            {
            
message_add($userref,$userconfirmmessage$templatevars['url']);
            }
        }

    
# $userref and $useremail will be null for external requestor
    # - We can only send an email to the email address provided on the external request 
    
if (!isset($userref) && filter_var($external_emailFILTER_VALIDATE_EMAIL))
        {
        
send_mail($external_email,$applicationname ": " $lang["requestsent"] . " - $ref",$userconfirmmessage,$email_from,NULL,"emailusercollectionrequest",$templatevars);
        }    
    
    
# Increment the request counter for each resource in the requested collection
    
sql_query("update resource set request_count=request_count+1 " 
             
."where ref in(select cr.resource from collection_resource cr where cr.collection='$ref' and cr.resource = ref)");
    
    return 
true;
    }

This article was last updated 3rd December 2020 13:05 Europe/London time based on the source file dated 28th September 2020 10:05 Europe/London time.