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 71 to 127

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;
    
    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);
                    
send_mail($email_to,$applicationname ": " $lang['notification_email_subject'],$message_text "<br/><br/><a href='" $url "' >" $url "</a>");
                    }
                }
            }
        }
    }

This article was last updated 24th May 2022 19:05 Europe/London time based on the source file dated 26th January 2022 11:10 Europe/London time.