Collections functions
General functions
Node functions
Render functions
Theme permission functions
User functions
Resource functions

send_user_notification()

Description

Send system notifications to specified users, checking the user preferences first if specified


This will contain two arrays:-
"emails" array of emails sent, with the following elements:-
"email" => Email address
"subject" => Email subject
"body" => Body text

"messages" Array of system messages sent with the following elements :-
"user" => User ID
"message" => message text
"url" => url

Parameters

ColumnTypeDefaultDescription
$users array Array of user IDs or array of user details from get_users()
$notifymessage ResourceSpaceUserNotification An instance of a ResourceSpaceUserNotification object holding message properties
$forcemail bool false Force system to send email instead of notification?

Return

array Array containing resulting messages - can be used for testing when emails are not being sent

Location

include/message_functions.php lines 987 to 1147

Definition

 
function send_user_notification(array $users$notifymessage$forcemail=false)
    {
    global 
$userref$lang$plugins$header_colour_style_override;

    
// Need to global $applicationname as it is used inside the lang files
    
global $applicationname;
    
$userlanguages = []; // This stores the users in their relevant language key element

    // Set up $results array
    
$results = [];
    
$results["messages"] = [];
    
$results["emails"] = [];
    foreach(
$users as $notify_user)
        {
        
$userdetails $notify_user;
        if(!
is_array($userdetails))
            {
            
$userdetails get_user((int)$userdetails);
            }
        elseif(!isset(
$userdetails["lang"]))
            {
            
// Need full user info
            
$userdetails get_user($userdetails["ref"]);
            }        
        if(
$userdetails == false)
            {
            continue;
            }
        
        
$send_message=true;
        
// Check if preferences should prevent the notification from being sent
        
if(isset($notifymessage->user_preference) && is_array($notifymessage->user_preference))
            {
            foreach(
$notifymessage->user_preference as $preference=>$vals)
                {
                if(
$preference != "")
                    {
                    
$requiredvalue  = (bool)$vals["requiredvalue"];
                    
$default        = (bool)$vals["default"];
                    
get_config_option($userdetails['ref'],$preference$check_pref,$default);
                    
debug(" - Required preference: " $preference " = " . ($requiredvalue "TRUE" "FALSE"));
                    
debug(" - User preference value: " $preference " = " . ($check_pref "TRUE" "FALSE"));
                    if(
$check_pref != $requiredvalue)
                        {
                        
debug("Skipping notification to user #" $userdetails['ref']);
                        
$send_message=false;                        
                        }
                    }
                }
            }
        if(
$send_message==false)
            {
            continue;
            }
        
debug("Sending notification to user #" $userdetails["ref"]);
        
get_config_option($userdetails['ref'],'email_user_notifications'$send_email);
        if(!isset(
$userlanguages[$userdetails['lang']]))
            {
            
$userlanguages[$userdetails['lang']] = [];
            
$userlanguages[$userdetails['lang']]["emails"] = [];
            
$userlanguages[$userdetails['lang']]["message_users"] = [];
            }
        if((
$send_email && filter_var($userdetails["email"], FILTER_VALIDATE_EMAIL)) || $forcemail)
            {
            
debug("Sending email to user #" $userdetails["ref"]);
            
$userlanguages[$userdetails['lang']]["emails"][] = $userdetails["email"];
            }
        else
            {
            
debug("Sending system message to user #" $userdetails["ref"]);
            
$userlanguages[$userdetails['lang']]["message_users"][]=$userdetails["ref"];
            }
        }
    
$url $notifymessage->url ?? null;
    
$headerimghtml "";
    if(!isset(
$notifymessage->template))
        {
        
// Add header image to email if not using template
        
$img_url get_header_image(true);
        
$img_div_style 'float: left;width: 100%;max-height:50px;padding: 5px;';
        
$img_div_style .= "background: " . ((isset($header_colour_style_override) && $header_colour_style_override != '') ? $header_colour_style_override "#fff") . ";";

        
$headerimghtml .= '<div style="' $img_div_style '">';
        
$headerimghtml .= '<div style="float: left;">';
        
$headerimghtml .= '<div>';        
        
        
$headerimghtml .= '<img src="' $img_url '" style="max-height:50px;"  />';
        
$headerimghtml .= '</div></div></div><br /><br />';
        }

    foreach(
$userlanguages as $userlanguage=>$notifications)
        {
        
debug("Processing notifications for language: '" $userlanguage "'");
        
// Save the current lang array
        
$saved_lang $lang;
        if (
$userlanguage!="en")
            {
            if (
substr($userlanguage21)=='-' && substr($userlanguage02)!='en')
                {
                
$langpath dirname(__FILE__)."/../languages/" safe_file_name(substr($userlanguage02)) . ".php";
                if(
file_exists($langpath))
                    {
                    include 
$langpath;
                    }
                }
            
$langpath dirname(__FILE__)."/../languages/" safe_file_name($userlanguage) . ".php";
            if(
file_exists($langpath))
                {
                include 
$langpath;
                }
            }

        
# Register plugin languages in reverse order
        
for ($n=count($plugins)-1;$n>=0;$n--)
            {
            if (!isset(
$plugins[$n]))
                {
                continue;
                }
            
register_plugin_language($plugins[$n]);
            }

        
// Load in the correct language strings
        
lang_load_site_text($lang,"",$userlanguage);

        
$subject $notifymessage->get_subject();
        
$messagetext $notifymessage->get_text();
        if (
count($notifications["message_users"])>0)
            {
            
$activitytype $notifymessage->eventdata["type"] ?? null;
            
$relatedactivity $notifymessage->eventdata["ref"] ?? null;
            foreach(
$notifications["message_users"] as $notifyuser)
                {
                
$results["messages"][] = ["user"=>$notifyuser,"message"=>$messagetext,"url"=>$url];
                }
            
message_add($notifications["message_users"],$messagetext, (string) $url,$userref,MESSAGE_ENUM_NOTIFICATION_TYPE_SCREEN,MESSAGE_DEFAULT_TTL_SECONDS,$activitytype,$relatedactivity);
            }
        if (
count($notifications["emails"])>0)
            {
            if(!empty(
$url) && !is_null($url) && strpos($messagetext,$url) === false)
                {
                
// Add the URL to the message if not already present
                
$messagetext $messagetext "<br /><br /><a href='" $url "'>" $url "</a>";
                }

            foreach(
$notifications["emails"] as $emailrecipient)
                {
                
send_mail($emailrecipient,$subject,$headerimghtml $messagetext,"","",$notifymessage->template,$notifymessage->templatevars);
                
$results["emails"][] = ["email"=>$emailrecipient,"subject"=>$subject,"body"=>$headerimghtml $messagetext];
                }

            foreach(
$notifications["message_users"] as $notifyuser)
                {
                
$results[$notifyuser] = ["type"=>"messsage","body"=>$messagetext];
                }
            }
        
// Restore the saved $lang array
        
$lang $saved_lang;
        }
    return 
$results;
    }

This article was last updated 19th March 2024 08:05 Europe/London time based on the source file dated 15th March 2024 09:00 Europe/London time.