Collections functions
General functions
Render functions
Theme permission functions
Resource functions

save_request()

Description

Handle the posted request form, when saving a request in the admin area.

Parameters

ColumnTypeDefaultDescription
$request integer The request record ID

Return

boolean Was this successful?

Location

include/request_functions.php lines 58 to 215

Definition

 
function save_request($request)
    {
    
# Use the posted form to update the request
    
global $applicationname,$baseurl,$lang,$request_senduserupdates,$admin_resource_access_notifications,$userref;

    
$status=getval("status","",true);
    
$expires=getval("expires","");
    
$currentrequest=get_request($request);
    
$oldstatus=$currentrequest["status"];
    
$assigned_to=getval("assigned_to","");
    
$reason=getval("reason","");
    
$reasonapproved=getval("reasonapproved","");
    
$approved_declined=false;

    
# --------------------- User Assignment ------------------------
    # Process an assignment change if this user can assign requests to other users
    
if ($currentrequest["assigned_to"]!=$assigned_to && checkperm("Ra"))
        {
        if (
$assigned_to==0)
            {
            
# Cancel assignment
            
ps_query("UPDATE request SET assigned_to=NULL WHERE ref = ?", array("i",$request));
            }
        else
            {
            
# Update and notify user
            
ps_query("UPDATE request SET assigned_to = ? WHERE ref = ?",array("i",$assigned_to"i",$request));

            
$assigned_to_user=get_user($assigned_to);

            
$assignedmessage = new ResourceSpaceUserNotification();
            
$assignedmessage->set_subject($applicationname ": " );
            
$assignedmessage->append_subject("lang_requestassignedtoyou");
            
$assignedmessage->set_text("lang_requestassignedtoyoumail");
            
$assignedmessage->user_preference = ["user_pref_resource_access_notifications"=>["requiredvalue"=>true,"default"=>$admin_resource_access_notifications],"actions_resource_requests" =>["requiredvalue"=>false,"default"=>true]];
            
$assignedmessage->url $baseurl "/?q=" $request;
            
$assignedmessage->eventdata = ["type"  => MANAGED_REQUEST,"ref"   => $request];
            
send_user_notification([$assigned_to],$assignedmessage);
            
// Change text for requesting user
            
$assignedmessage->set_text("lang_requestassignedtouser",["%"],[$assigned_to_user["fullname"] . " (" $assigned_to_user["email"] . ")" ]);
            
send_user_notification([$currentrequest["user"]],$assignedmessage);
            }
        }
    
    
# Has either the status or the expiry date changed?
    
if (($oldstatus!=$status || $expires!=$currentrequest["expires"]) && $status==1)
        {
        
# --------------- APPROVED -------------
        # Send approval e-mail
        // $reasonapproved=str_replace(array("\\r","\\n"),"\n",$reasonapproved);$reasonapproved=str_replace("<br/><br/>","\n",$reasonapproved); # Fix line breaks.
        
$approved_declined true;
        
$reasonapproved unescape($reasonapproved);
        
$colurl $baseurl "/?c=" $currentrequest["collection"];

        
// Don't add event data for the requesting user's notification or message will be deleted immediately
        
$approvemessage= new ResourceSpaceUserNotification;
        
$approvemessage->set_subject($applicationname ": ");
        
$approvemessage->append_subject("lang_requestcollection");
        
$approvemessage->append_subject(" - ");;
        
$approvemessage->append_subject("lang_resourcerequeststatus1");
        
$approvemessage->set_text("lang_requestapprovedmail");
        
$approvemessage->append_text("<br/><br/>");
        
$approvemessage->append_text("lang_approvalreason");
        
$approvemessage->append_text(": " $reasonapproved);
        
$approvemessage->url $colurl;
        
$templatevars["message"] = $approvemessage->get_text();
        
$templatevars["url"] = $colurl;
        if (
$expires!="")
            {
            
# Add expiry time to message.
            
$approvemessage->append_text("<br/><br/>");
            
$approvemessage->append_text("lang_requestapprovedexpires");
            
$approvemessage->append_text(" " nicedate($expires) . "\n");
            
$templatevars["expires"] = "<br/><br/>" $lang["requestapprovedexpires"] . " " nicedate($expires) . "\n";;
            }
        else
            {
            
$templatevars["expires"] = '';  
            }
        
$approvemessage->template "requestapprovedmail_email";
        
$approvemessage->templatevars $templatevars;
        
send_user_notification([$currentrequest["user"]],$approvemessage);
       
        
# Mark resources as full access for this user
        
foreach (get_collection_resources($currentrequest["collection"]) as $resource)
            {
            
open_access_to_user($currentrequest["user"],$resource,$expires);
            }
            
        
# Clear any outstanding notifications about this request that may have been sent to other admins
        
message_remove_related(MANAGED_REQUEST,$request);
        }

    elseif (
$oldstatus!=$status && $status==2)  
        {
        
# --------------- DECLINED -------------
        # Send declined e-mail
        
$approved_declined true;
        
$reason unescape($reason);
        
$colurl $baseurl "/?c=" $currentrequest["collection"];
        
$declinemessage= new ResourceSpaceUserNotification;
        
$declinemessage->set_subject($applicationname ": ");
        
$declinemessage->append_subject("lang_requestcollection");
        
$declinemessage->append_subject(" - ");;
        
$declinemessage->append_subject("lang_resourcerequeststatus2");
        
$declinemessage->set_text("lang_requestdeclinedmail");
        
$declinemessage->append_text("<br/><br/>");
        
$declinemessage->append_text("lang_declinereason");
        
$declinemessage->append_text(": " $reason);
        
$declinemessage->url $colurl;
        
$templatevars["message"] = $declinemessage->get_text();
        
$templatevars["url"] = $colurl;
        
$declinemessage->template "requestdeclined_email";
        
$declinemessage->templatevars $templatevars;
        
send_user_notification([$currentrequest["user"]],$declinemessage);

        
# Remove access that my have been granted by an inadvertant 'approved' command.
        
foreach (get_collection_resources($currentrequest["collection"]) as $resource)
            {
            
remove_access_to_user($currentrequest["user"],$resource);
            }
            
        
# Clear any outstanding notifications about this request that may have been sent to other admins
        
message_remove_related(MANAGED_REQUEST,$request);
        }

    if (
$oldstatus!=$status && $status==0)
        {
        
# --------------- PENDING -------------
        # Moved back to pending. Delete any permissions set by a previous 'approve'.
        
foreach (get_collection_resources($currentrequest["collection"]) as $resource)
            {
            
remove_access_to_user($currentrequest["user"],$resource);
            }
        }

    
# Save status
    
$expires_parm=($expires=="" null $expires);
    
$parameters=array("i",$status"s",$expires_parm"s",$reason"s",$reasonapproved"i",$request);
    
ps_query("UPDATE request SET status=?, expires=?, reason=?, reasonapproved=? WHERE ref=?"$parameters);

    
# Set user that approved or declined the request
    
if ($approved_declined)
        {
        
$parameters=array("i",$userref"i",$request);
        
ps_query("UPDATE request SET approved_declined_by=? WHERE ref=?"$parameters);
        }

    if (
getval("delete","")!="")
        {
        
# Delete the request - this is done AFTER any e-mails have been sent out so this can be used on approval.
        
ps_query("DELETE FROM request WHERE ref=?",array("i",$request));

        
# Clear any outstanding notifications about this request that may have been sent to other admins
        
message_remove_related(MANAGED_REQUEST,$request);
        return 
true;
        }
    }

This article was last updated 4th October 2022 16:35 Europe/London time based on the source file dated 29th September 2022 16:25 Europe/London time.