Collections functions
General functions
Render functions
Theme permission functions
Resource functions

message_add()

Description

Add a new resourcespace system message

Parameters

ColumnTypeDefaultDescription
$users mixed User ID, or array of user IDs
$text string Message text
$url string "" URL to include as link in message
$owner int null ID of message creator/owner
$notification_type int MESSAGE_ENUM_NOTIFICATION_TYPE_SCREEN Message type e.g. MESSAGE_ENUM_NOTIFICATION_TYPE_SCREEN, MESSAGE_ENUM_NOTIFICATION_TYPE_EMAIL. See definitions.php
$ttl_seconds int MESSAGE_DEFAULT_TTL_SECONDS Lifetime of message in seconds before expiry
$related_activity int 0 ID of related activity type - see SYSTEM NOTIFICATION TYPES section in definitions.php
$related_ref int 0 Related activity ID - used with type above to delete redundant messages e.g. once a user or resource request has been approved

Return

void

Location

include/message_functions.php lines 276 to 345

Definition

 
function message_add($users,$text,$url="",$owner=null,$notification_type=MESSAGE_ENUM_NOTIFICATION_TYPE_SCREEN,$ttl_seconds=MESSAGE_DEFAULT_TTL_SECONDS$related_activity=0$related_ref=0)
    {
    global 
$userref,$applicationname,$lang$baseurl$baseurl_short,$header_colour_style_override;
    
    if(!
is_int_loose($notification_type))
        {
        
$notification_type=intval($notification_type); // make sure this in an integer
        
}
    
    
$orig_text=$text;

    if (!
is_array($users))
        {
        
$users=array($users);
        }

    if(
checkperm('E'))
        {
        
$validusers get_users(0,"","u.username",true,1);
        
$validuserrefs array_column($validusers,"ref");
        
$users array_filter($users,function($user) use ($validuserrefs) {return in_array($user,$validuserrefs);});
        }

    if(
is_null($owner) || (isset($userref) && $userref != $owner))
        {
        
// Can't send messages from another user
        
$owner=$userref;
        }

    
ps_query("INSERT INTO `message` (`owner`, `created`, `expires`, `message`, `url`, `related_activity`, `related_ref`, `type`) VALUES (? , NOW(), DATE_ADD(NOW(), INTERVAL ? SECOND), ?, ?, ?, ?, ?)", array("i",$owner,"i",$ttl_seconds,"s",$text,"s",str_replace($baseurl.'/'$baseurl_short$url),"i",$related_activity,"i",$related_ref,"i",$notification_type));
    
$message_ref sql_insert_id();

    foreach(
$users as $user)
        {
        
ps_query("INSERT INTO `user_message` (`user`, `message`) VALUES (?, ?)", array("i",(int)$user,"i",$message_ref));
        
        
// send an email if the user has notifications and emails setting and the message hasn't already been sent via email
        
if(~$notification_type MESSAGE_ENUM_NOTIFICATION_TYPE_EMAIL)
            {
            
get_config_option($user,'email_and_user_notifications'$notifications_always_email);
            if(
$notifications_always_email)
                {
                
$email_to=ps_value("SELECT email value FROM user WHERE ref = ?", array("i",$user), "");
                if(
$email_to!=='')
                    {
                    if(
substr($url,0,1) == "/")
                        {
                        
// If a relative link is provided make sure we add the full URL when emailing
                        
$url $baseurl $url;
                        }
                    
$message_text=nl2br($orig_text);

                    
// Add system header image to email
                    
$headerimghtml "";
                    
$img_url get_header_image(true);
                    
$img_div_style "max-height:50px;padding: 5px;";
                    
$img_div_style .= "background: " . ((isset($header_colour_style_override) && $header_colour_style_override != '') ? $header_colour_style_override "rgba(0, 0, 0, 0.6)") . ";";        
                    
$headerimghtml '<div style="' $img_div_style '"><img src="' $img_url '" style="max-height:50px;"  /></div><br /><br />';
                              
                    if(
strpos($message_text,$url) === false)
                        {
                        
// Add the URL to the message if not already present
                        
$message_text $message_text "<br /><br /><a href='" $url "'>" $url "</a>";
                        }
                    
send_mail($email_to,$applicationname ": " $lang['notification_email_subject'],$headerimghtml $message_text);
                    }
                }
            }
        }
    }

This article was last updated 5th October 2022 03:05 Europe/London time based on the source file dated 4th October 2022 23:15 Europe/London time.