Coding standards
Security in ResourceSpace
Developer reference
Database
Action functions
Admin functions
Ajax functions
Annotation functions
API functions
Collections functions
Comment functions
Config functions
CSV export functions
Dash functions
Debug functions
Encryption functions
Facial recognition functions
File functions
General functions
Language functions
Log functions
Login functions
Message functions
Migration functions
Node functions
PDF functions
Plugin functions
Render functions
Reporting functions
Request functions
Research functions
Slideshow functions
Theme permission functions
User functions
Video functions
Database functions
Metadata functions
Resource functions
Search functions
Map functions
Job functions
Tab functions
Test functions

managed_collection_request()

Description

Request mode 1 - quests are managed via the administrative interface. Sends an e-mail but also logs the request in the request table.

Parameters

ColumnTypeDefaultDescription
$ref mixed
$details mixed
$ref_is_resource mixed false

Return

boolean

Location

include/request_functions.php lines 482 to 871

Definition

 
function managed_collection_request($ref,$details,$ref_is_resource=false)
    {   
    global 
$applicationname,$email_from,$baseurl,$email_notify,$username,$useremail,$userref,$lang,
        
$watermark,$filename_field,$view_title_field,$access,$resource_type_request_emails,
        
$resource_type_request_emails_and_email_notify$manage_request_admin,$resource_request_reason_required,
        
$admin_resource_access_notifications$notify_manage_request_admin,
        
$assigned_to_user$admin_resource_access_notifications;

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

    
# Has a resource reference (instead of a collection reference) been passed?
    # Manage requests only work with collections. Create a collection containing only this resource.

    
$admin_mail_template="emailcollectionrequest";
    
$user_mail_template="emailusercollectionrequest";

    if (
$ref_is_resource)
        {
        
$resourcedata=get_resource_data($ref);
        
$templatevars['thumbnail']=get_resource_path($ref,true,"thm",false,"jpg",-1,1,($watermark)?(($access==1)?true:false):false);

        
# Allow alternative configuration settings for this resource type
        
resource_type_config_override($resourcedata['resource_type']);

        if (!
file_exists($templatevars['thumbnail'])){
        
$templatevars['thumbnail']="../gfx/no_preview/default.png";
        }
        
$templatevars['url']=$baseurl."/?r=".$ref;
        if (isset(
$filename_field)) {
            
$templatevars["filename"] = $lang["fieldtitle-original_filename"] . ": " get_data_by_field($ref$filename_field);
        }
        if (isset(
$resourcedata["field" $view_title_field])) {
            
$templatevars["title"] = $resourcedata["field" $view_title_field];
        }

        
$c=create_collection($userref,$lang["request"] . " " date("ymdHis"),0,0,0,false,array("type" => COLLECTION_TYPE_REQUEST));
        
add_resource_to_collection($ref,$c,true);
        
$ref=$c# Proceed as normal
        
$colresources get_collection_resources($ref);
        }
    else
        {
        
# Create a copy of the collection to attach to the request so that subsequent collection changes do not affect the request
        
$c=create_collection($userref,$lang["request"] . " " date("ymdHis"),0,0,0,false,array("type" => COLLECTION_TYPE_REQUEST));
        
copy_collection($ref,$c);

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

        
$ref=$c# Proceed as normal

        
$collectiondata=get_collection($ref);
        
$templatevars['url']=$baseurl."/?c=".$ref;
        if (isset(
$collectiondata["name"])) {
            
$templatevars["title"] = $collectiondata["name"];
        }
        }

    
# Formulate e-mail text
    
$templatevars['username']=$username;
    
$templatevars["useremail"]=$useremail;
    
$userdata=get_user($userref);
    
$templatevars["fullname"]=$userdata["fullname"];

    
// set up notification object
    
$message = new ResourceSpaceUserNotification();

    
$templatevars["extra"] = "";
    
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 ": " $setting "\n");
                
$templatevars["extra"] .= $value ": " $setting "<br /><br />";
                }
            }
        }
    if (
trim($details)!="")
        {
        
$message->append_text("lang_requestreason");
        
$message->append_text(": " newlines($details));
        
$templatevars["requestreason"] = newlines($details);
        }

    
# 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->append_text("<br />\n");
            
$message->append_text("i18n_" $custom[$n]);
            
$message->append_text(": " getval("custom" $n,""));
            }
        }

    
$assignedmessage = new ResourceSpaceUserNotification();
    
$assignedmessage->set_text("lang_requestassignedtoyoumail");
    
$assignedmessage->append_text("<br /><br />");
    
$assignedmessage->set_subject($applicationname ": ");
    
$assignedmessage->append_subject("lang_requestassignedtoyou");
    
// Add core message text (reason, custom fields etc.)
    
$assignedmessage->append_text_multi($message->get_text(true));
    
$amendedmessage=hook('amend_request_message','', array($userref$ref, isset($collectiondata) ? $collectiondata : array(), $message, isset($collectiondata)));
    if(
$amendedmessage)
        {
        
$assignedmessage->set_text($amendedmessage);
        }

    
# Setup the principal create request SQL
    
global $request_query;
    
$request_query = new PreparedStatementQuery();
    
$request_query->sql "INSERT INTO request(user, collection, created, request_mode, status, comments) 
                            VALUES (?, ?, NOW(), 1, 0, ?)"
;
    
$request_query->parameters = array("i",$userref"i",$ref"s",$message->get_text());

    
// Set flag to send default notifications unless we override e.g. by $manage_request_admin 
    
$send_default_notifications true;

    
$notify_manage_request_admin false;
    
$notification_sent false;

    
// The following hook determines the assigned administrator
    // If there isn't one, then the principal request_query setup earlier remains as-is without an assigned_to
    // If there is one then the hook replaces the request_query with one which has an assigned_to 
    
hook('autoassign_individual_requests''', array($userref$ref$message, isset($collectiondata)));

    
// Regular Processing: autoassign using the resource type - one resource was requested and no plugin is preventing this from running
    
if($ref_is_resource && !is_null($manage_request_admin) && is_array($manage_request_admin) && !empty($manage_request_admin))
        {
        
$admin_notify_user 0;
        
$request_resource_type $resourcedata["resource_type"];
        if(
array_key_exists($request_resource_type$manage_request_admin)) 
            {
            
$admin_notify_user=$manage_request_admin[$request_resource_type];

            
$request_query->sql "INSERT INTO request(user, collection, created, request_mode, status, comments, assigned_to)
                                    VALUES (?, ?, NOW(), 1, 0, ?, ?)"
;
            
$request_query->parameters = array("i",$userref"i",$ref"s",$message->get_text(), "i",$admin_notify_user);

            
// Setup assigned to user for bypass hook later on    
            
if($admin_notify_user !== 0
                {
                
$assigned_to_user get_user($admin_notify_user);
                
$notify_manage_request_admin true;
                }
            }
        }   

    
hook('autoassign_collection_requests''', array($userref, isset($collectiondata) ? $collectiondata : array(), $message, isset($collectiondata)));

    
$can_use_owner_field can_use_owner_field();

    
// Regular Processing: autoassign using the resource type - collection request and no plugin is preventing this from running
    
if(isset($collectiondata) && !is_null($manage_request_admin) && is_array($manage_request_admin) && !empty($manage_request_admin))
        {
        
$all_r_types get_resource_types();

        
$resources get_collection_resources($collectiondata['ref']);
        
$resources = ($resources === false ? array() : $resources);
    
        
// Get distinct resource types found in this collection:
        
$collection_resources_by_type = array();
        foreach (
$resources as $resource_id
            {
            
$resource_data get_resource_data($resource_id);
            
// Create a list of resource IDs based on type to separate them into different collections:
            
$collection_resources_by_type[$resource_data['resource_type']][] = $resource_id;
            }

        
// Split into collections based on resource type:
        
foreach ($collection_resources_by_type as $collection_type => $collection_resources)
            {
            
// Store all resources of unmanaged type in one collection which will be sent to the system administrator:
            
if(!isset($manage_request_admin[$collection_type]))
                {
                
$collections['not_managed'] = create_collection($userref$collectiondata['name'] . " " date("ymdHis"),0,0,0,false,array("type" => COLLECTION_TYPE_REQUEST));
                foreach (
$collection_resources as $collection_resource_id
                    {
                    
add_resource_to_collection($collection_resource_id$collections['not_managed']);
                    }
                continue;
                }
                
            
$r_type_index array_search($collection_typearray_column($all_r_types'ref'));
            if (
$r_type_index===false)
                {
                continue;
                }

            
$collections[$collection_type] = create_collection($userref$collectiondata['name'] . ' : ' $all_r_types[$r_type_index]["name"] . " " date("ymdHis"),0,0,0,false,array("type" => COLLECTION_TYPE_REQUEST));
            foreach (
$collection_resources as $collection_resource_id)
                {
                
// Add collection resources of the given type to the resource type specific collection 
                // The col_access_control parameter is true meaning that adding to the collection is permitted for this call
                
add_resource_to_collection($collection_resource_id$collections[$collection_type], false""""true);
                }
            }
        if(isset(
$collections) && count($collections) > 0)
            {
            foreach (
$collections as $request_resource_type => $collection_id)
                {
                
$assigned_to '';
                
$assigned_to_users=array();
                
$assigned_to_user_emails=array();
                if(
array_key_exists($request_resource_type$manage_request_admin))
                    {
                    
$assigned_to_users[] = $manage_request_admin[$request_resource_type];
                    }
                else
                    {
                    
// No specific user allocated, get all users, adding $email_notify address if this does not belong to a system user
                    
$assigned_to_users=get_notification_users("RESOURCE_ACCESS");
                    
$email_notify_is_user=false;
                    foreach (
$assigned_to_users as $assigned_to_user)
                        {
                        if(
$assigned_to_user['email']==$email_notify)
                            {
                            
$email_notify_is_user=true;
                            }
                        }
                    if(!
$email_notify_is_user){$assigned_to_user_emails[] = $email_notify;}                        
                    }
                    
                if(
trim($assigned_to) != '')
                    {
                    
$request_query "INSERT INTO request(user, collection, created, request_mode, `status`, comments, assigned_to)
                                            VALUES (?, ?, NOW(), 1, 0, ?, ?);"
;
                    
$parameters=array("i"$userref"i",$collection_id"s",$message->get_text(), "i",$assigned_to);
                    }
                else
                    {
                    
$request_query "INSERT INTO request(user, collection, created, request_mode, `status`, comments)
                                           VALUES (?, ?, NOW(), 1, 0, ?);"
;
                    
$parameters=array("i"$userref"i",$collection_id"s",$message->get_text());
                    }

                
ps_query($request_query$parameters);
                
$request sql_insert_id();

                
$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_users,$assignedmessage);
                } 
# End for each collection

            
$notify_manage_request_admin false;
            
$notification_sent true;
            
$send_default_notifications false;
            }
        else
            {
            
# No collections
            
return false;
            }

        } 
// End of default manager (regular processing)
    
else
        {
        if(
hook('bypass_end_managed_collection_request''', array(!isset($collectiondata), $ref$request_query$message$templatevars$assigned_to_user$admin_mail_template$user_mail_template)))
            {
            return 
true;
            }
        else
            
// Back to regular processing
            
{
            
ps_query($request_query->sql$request_query->parameters);
            
$request=sql_insert_id();
            }
        }

    
hook("afterrequestcreate""", array($request));

    if(
$send_default_notifications)
        {
        
# Automatically notify the admin who was assigned the request if we set this earlier:
        
$templatevars["request_id"]=$request;
        
$templatevars["requesturl"]=$baseurl."/?q=".$request;

        
$admin_notify_message = new ResourceSpaceUserNotification();
        
$admin_notify_message->set_subject($applicationname ": " );
        
$admin_notify_message->append_subject("lang_requestassignedtoyou");
        
$admin_notify_message->set_text("lang_requestassignedtoyoumail");
        
$admin_notify_message->append_text("<br /><br />");
        
$admin_notify_message->append_text("lang_username");
        
$admin_notify_message->append_text(": " $username "<br />\n");
        
$admin_notify_message->append_text_multi($message->get_text(true));
        
$admin_notify_message->user_preference = ["user_pref_resource_access_notifications"=>["requiredvalue"=>true,"default"=>$admin_resource_access_notifications]];
        
$admin_notify_message->url $templatevars['requesturl'];
        
$admin_notify_message->eventdata = ["type" => MANAGED_REQUEST,"ref" => $request];
        if(
$notify_manage_request_admin)
            {
            
send_user_notification([$admin_notify_user],$admin_notify_message);
            
$notification_sent true;
            }

        
$admin_notify_users=array();

        
# 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)
            {
            
// Legacy support for $resource_type_request_emails
            
$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]]))
                {
                
$emailusers get_user_by_email($resource_type_request_emails[$requestrestypes[0]]);
                }  
            if(
is_array($emailusers) && count($emailusers) > 0)
                {
                
send_user_notification($emailusers,$admin_notify_message);
                }
            else
                {
                
send_mail($resource_type_request_emails[$requestrestypes[0]],$applicationname ": " $lang["requestcollection"] . " - $ref","<p>" $admin_notify_message->get_text() . "</p>" $admin_notify_message->url ,$email_from,$email_from,$admin_mail_template,$templatevars);
                }
            }

        if(!
$notification_sent && $can_use_owner_field)
            {
            
$admin_notify_users array_keys(get_notification_users_by_owner_field(get_notification_users("RESOURCE_ACCESS"), $colresources));
            
$admin_notify_message->set_subject($applicationname ": " );
            
$admin_notify_message->append_subject("lang_requestcollection");
            
$admin_notify_message->append_subject(" - " $ref);
            
$admin_notify_message->eventdata = ["type" => MANAGED_REQUEST,"ref" => $request];
            
send_user_notification($admin_notify_users,$admin_notify_message);
            
$notification_sent true;
            }

        if(!
$notification_sent)
            {
            
$default_notify_users get_notification_users("RESOURCE_ACCESS"); 
            
// Exclude any users who will already have an action appearing
            
$action_users get_config_option_users("actions_resource_requests",true);
            
$admin_notify_users array_diff(array_column($default_notify_users,"ref"),$action_users);
  
            
$admin_notify_message->set_subject($applicationname ": " );
            
$admin_notify_message->append_subject("lang_requestcollection");
            
$admin_notify_message->append_subject(" - " $ref);
            
$admin_notify_message->eventdata = ["type" => MANAGED_REQUEST,"ref" => $request];
            
$admin_notify_message->user_preference = ["user_pref_resource_access_notifications"=>["requiredvalue"=>true,"default"=>$admin_resource_access_notifications],"actions_resource_requests" =>["requiredvalue"=>false,"default"=>true]];
            
send_user_notification($admin_notify_users,$admin_notify_message);
            }
        }

    
$user_message = new ResourceSpaceUserNotification();
    
$user_message->set_subject($applicationname ": " );
    
$user_message->append_subject("lang_requestsent");
    
$user_message->append_subject(" - " $ref);
    
$user_message->set_text("lang_requestsenttext");
    
$user_message->append_text("<br /><br />");
    
$user_message->append_text_multi($message->get_text(true));
    
$user_message->append_text("<br /><br />");
    
$user_message->append_text("lang_clicktoviewresource");
    
$user_message->url $baseurl "/?c=" $ref;
    
// Note no user_preference set so that message will always send
    
send_user_notification([$userref],$user_message);

    
# 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 11th December 2024 20:35 Europe/London time based on the source file dated 18th November 2024 16:40 Europe/London time.