Collections functions
General functions
Node functions
Render functions
Theme permission functions
User 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
bool $external_email:
$email_from
$baseurl
$username
$useremail
$lang
$request_senduserupdates
$userref
$resource_type_request_emails
$resource_request_reason_required
$resource_type_request_emails_and_email_notify
$admin_resource_access_notifications;
$external_email mixed

Location

include/request_functions.php lines 288 to 471

Definition

 
function email_collection_request($ref,$details,$external_email): bool
    
{
    global 
$applicationname,$email_from,$baseurl,$username,$useremail,$lang,$request_senduserupdates,$userref,$resource_type_request_emails,
    
$resource_request_reason_required,$resource_type_request_emails_and_email_notify,$admin_resource_access_notifications;

    if (
trim($details)=="" && $resource_request_reason_required) {return false;}

    
$message= new ResourceSpaceUserNotification;

    
$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);
            }
        }

    
$ref=$newcopy;

    
$templatevars["requesturl"]=$baseurl."/?c=".$ref;

    if (isset(
$userref))
        {
        
$templatevars['username']=$username " (" $useremail ")";
        
$userdata=get_user($userref);
        if(
$userdata===false){return false;} # Unable to get user credentials
        
$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->append_text($value ": " $_POST[str_replace("_label","",$key)] . "<br /><br />");
                }
            }
        }
    if (
trim($details)!="")
        {
        
$message->append_text("lang_requestreason");
        
$message->append_text(": " newlines($details) . "<br /><br />");
        }

    
# Add custom fields
    
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->append_text("i18n_" $custom[$n]);
            
$message->append_text(": " getval("custom" $n,"") . "<br /><br />");
            }
        }
        
    
$amendedmessage=hook('amend_request_message','', array($userref$ref, isset($collectiondata) ? $collectiondata : array(), $message, isset($collectiondata)));

    if(
$amendedmessage)
        {
        
$message=$amendedmessage;
        }

    
$templatevars["requestreason"]=$message->get_text();

    
// Create notification message
    
$notification_message = clone $message;
    
$notification_message->set_subject($applicationname ": ");
    
$notification_message->append_subject("lang_requestcollection");
    
$notification_message->append_subject(" - "  $ref);
    
$introtext[] = ["lang_user_made_request"];
    
$introtext[] = ["<br /><br />"];
    if (isset(
$username))
        {
        
$introtext[] = ["lang_username"];
        
$introtext[] = [": "];
        
$introtext[] = [$username];
        
$introtext[] = ["<br /><br />"];
        }
    
$notification_message->prepend_text_multi($introtext);
    
$notification_message->append_text("lang_viewcollection");
    
$notification_message->append_text(":");
    
$notification_message->url $templatevars['requesturl'];
    
$notification_message->user_preference = ["user_pref_resource_access_notifications"=>["requiredvalue"=>true,"default"=>$admin_resource_access_notifications]];
    
$notification_message->template "emailcollectionrequest";
    
$notification_message->templatevars $templatevars;

    
$notify_users = [];
    
$notify_emails = [];
    
# Legacy: 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) && !can_use_owner_field())
        {
        
$requestrestypes=ps_array("SELECT r.resource_type AS value FROM collection_resource cr 
                                    LEFT JOIN resource r ON cr.resource=r.ref WHERE cr.collection = ?"
,array("i",$ref));
        
$requestrestypes=array_unique($requestrestypes);
        if(
count($requestrestypes)==&& isset($resource_type_request_emails[$requestrestypes[0]]))
            {
            
// Is this a system user? If so we can send a notification instead of an email
            
$emailusers get_user_by_email($resource_type_request_emails[$requestrestypes[0]]);
            if(
is_array($emailusers) && count($emailusers) > 0)
                {
                
$notify_users array_merge($notify_users,$emailusers);
                }
            else
                {
                
$notify_emails[]=$resource_type_request_emails[$requestrestypes[0]];
                }
            }
        }

    if((
count($notify_users)==&& count($notify_emails)==0) || $resource_type_request_emails_and_email_notify)
        {
        
$admin_notify_users=get_notification_users("RESOURCE_ACCESS");
        
$notify_users array_merge($notify_users,$admin_notify_users);
        }
    
$notify_users array_keys(get_notification_users_by_owner_field($notify_users$colresources));
    
send_user_notification($notify_users,$notification_message);
    foreach(
$notify_emails as $notify_email)
        {
        
send_mail($notify_email,$applicationname ": " $lang["requestcollection"] . " - $ref",$message->get_text(),$email_from,$email_from,"emailcollectionrequest",$templatevars);
        }

    
$userconfirmmessage = clone $message;
    
$userconfirmmessage->set_subject($applicationname ": ");
    
$userconfirmmessage->append_subject(" - "  $ref);
    
$userconfirmmessage->prepend_text("<br /><br />");
    
$userconfirmmessage->prepend_text("lang_requestsenttext");
    
$userconfirmmessage->url $templatevars['url'];
    
$userconfirmmessage->template "emailusercollectionrequest";
    
$userconfirmmessage->templatevars $templatevars;

    
# $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)
        {
        
send_user_notification([$userref],$userconfirmmessage);
        }

    
# $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->get_text(),$email_from,null,"emailusercollectionrequest",$templatevars);
        }

    
# Increment the request counter for each resource in the requested collection
    
ps_query("UPDATE resource set request_count=request_count+1
               where ref in(select cr.resource from collection_resource cr where cr.collection=? and cr.resource = ref)"
,array("i",$ref));

    return 
true;
    }

This article was last updated 19th March 2024 07:05 Europe/London time based on the source file dated 28th February 2024 10:20 Europe/London time.