Collections functions
Encryption functions
General functions
Theme permission functions
Resource 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

void

Location

include/request_functions.php lines 433 to 894

Definition

 
function managed_collection_request($ref,$details,$ref_is_resource=false)
    {   
    global 
$applicationname,$email_from,$baseurl,$email_notify,$username,$useremail,$userref,$lang,$request_senduserupdates,$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$always_email_from_user$collection_empty_on_submit;

    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.
    
if ($ref_is_resource)
        {
        
$admin_mail_template="emailresourcerequest";
        
$user_mail_template="emailuserresourcerequest";
        
        
$resourcedata=get_resource_data($ref);
        
$templatevars['thumbnail']=get_resource_path($ref,true,"thm",false,"jpg",$scramble=-1,$page=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/".get_nopreview_icon($resourcedata["resource_type"],$resourcedata["file_extension"],false);
        }
        
$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"));
        
add_resource_to_collection($ref,$c,true);
        
$ref=$c# Proceed as normal
        
}
    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"));
        
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,$c,false);
                }
            }
        
        if(
$collection_empty_on_submit)
            {
            
remove_all_resources_from_collection($ref);    
            }
        
        
$ref=$c# Proceed as normal        
        
        
$admin_mail_template="emailcollectionrequest";
        
$user_mail_template="emailusercollectionrequest";
    
        
$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"];
    
    
$message="";
    
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 ": " $setting "\n\n";
                }
            }
        }
    if (
trim($details)!="") {$message.=$lang["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.="\n\n" 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;
        }
        
    
# Setup the create request SQL
    
global $request_query;
    
$request_query "insert into request(user,collection,created,request_mode,status,comments) values ('$userref','$ref',now(),1,0,'" escape_check($message) . "')";
    
    
// Set flag to send default notifications unless we override e.g. by $manage_request_admin 
    
$send_default_notifications true;
            
    global 
$notify_manage_request_admin$assigned_to_user$admin_resource_access_notifications;
    
    
$notify_manage_request_admin false;
    
$notification_sent false;

    
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 sprintf("
                    INSERT INTO request(
                                            user,
                                            collection,
                                            created,
                                            request_mode,
                                            `status`,
                                            comments,
                                            assigned_to
                                       )
                         VALUES (
                                     '%s',
                                     '%s',
                                     NOW(),
                                     1,
                                     0,
                                     '%s',
                                     '%s'
                                );
                "
,
                
$userref,
                
$ref,
                
escape_check($message),
                
$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)));

    
// 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']);

        
// 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"));
                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"));
            foreach (
$collection_resources as $collection_resource_id)
                {
                
add_resource_to_collection($collection_resource_id$collections[$collection_type]);
                }
            }
        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 $manage_request_admin[$request_resource_type];
                    
get_config_option($assigned_to,'email_user_notifications'$send_email);
                    if(
$send_email)
                        {
                        
// We need the user's email address
                        
$assigned_to_user get_user($manage_request_admin[$request_resource_type]);
                        
$assigned_to_user_emails[] = $assigned_to_user['email'];
                        }
                    else
                        {
                        
// We will be sending a notification message 
                        
$assigned_to_users[] = get_user($assigned_to);
                        }
                    }
                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;