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

message_send_unread_emails()

Description

Send a summary of all unread notifications as an email
from the standard cron_copy_hitcount

Parameters

This function accepts no parameters.

Return

boolean Returns false if not due to run

Location

include/message_functions.php lines 479 to 793

Definition

 
function message_send_unread_emails()
    {
    global 
$lang$applicationname$baseurl$list_search_results_title_trim$user_pref_daily_digest$applicationname$actions_on$inactive_message_auto_digest_period$user_pref_inactive_digest;

    
$lastrun get_sysvar('daily_digest''1970-01-01');

    
# Don't run if already run in last 24 hours.
    
if (time()-strtotime($lastrun) < 24*60*60)
        {
        echo 
" - Skipping message_send_unread_emails (daily_digest) - last run: " $lastrun "<br />\n";
        return 
false;
        }

    
$sendall = array();

    
// Get all the users who have chosen to receive the digest email (or the ones that have opted out if set globally)
    
if($user_pref_daily_digest)
        {
        
$allusers=get_users("","","u.username","",-1,1);
        
$nodigestusers get_config_option_users('user_pref_daily_digest',0);
        
$digestusers=array_diff(array_column($allusers,"ref"),$nodigestusers);
        }
    else
        {
        
$digestusers=get_config_option_users('user_pref_daily_digest',1);
        }

    if(
$inactive_message_auto_digest_period && is_numeric($inactive_message_auto_digest_period))
        {
        
// Add any users who have not logged on to the array
        
$allusers get_users(0,"","u.ref",false,-1,1,false,"u.ref, u.username, u.last_active");
        foreach(
$allusers as $user)
            {
            if(!
in_array($user["ref"],$digestusers) && strtotime((string)$user["last_active"]) < date(time() - $inactive_message_auto_digest_period *  60 60 *24))
                {
                
debug("message_send_unread_emails: Processing unread messages for inactive user: " $user["username"]);
                
$digestusers[] = $user["ref"];
                
$sendall[] = $user["ref"];
                }
            }
        }

    if (!empty(
$digestusers))
        {
        
$digestuserschunks array_chunk($digestusers,SYSTEM_DATABASE_IDS_CHUNK_SIZE);
        
$unreadmessages = [];
        foreach(
$digestuserschunks as $chunk)
            {
            
# Get all unread notifications created since last run, or all messages sent to inactive users.
            # Build array of sql query parameters

            
$parameters = array();

            
$parameters array_merge($parametersps_param_fill($chunk,"i"));
            
$parameters array_merge($parameters, array("s",$lastrun));
            
$digestusers_sql " AND u.ref IN (" ps_param_insert(count($chunk)) . ")";

            
$sendall_chunk array_intersect($sendall,$chunk);
            if (
count($sendall_chunk) > 0)
                {
                
$parameters  array_merge($parametersps_param_fill($sendall_chunk,"i"));
                
$sendall_sql " OR u.ref IN (" ps_param_insert(count($sendall_chunk)) . ")";
                }
            else
                {
                
$sendall_sql "";
                }

            
$unreadmessages array_merge(
                
$unreadmessages,
                
ps_query(
                
"SELECT u.ref AS userref, u.email, m.ref AS messageref, m.message, m.created, m.url
                    FROM user_message um
                        JOIN user u ON u.ref = um.user
                        JOIN message m ON m.ref = um.message
                        WHERE um.seen = 0
                        
$digestusers_sql
                        AND u.email <> ''
                        AND (m.created > ?
                            
$sendall_sql)
                        ORDER BY m.created DESC"
,
                    
$parameters
                
));
            }
        }
    else
        {
        
$parameters = array("s",$lastrun);
        
$unreadmessages ps_query(
            
"SELECT u.ref AS userref, u.email, m.ref AS messageref, m.message, m.created, m.url
                FROM user_message um
                    JOIN user u ON u.ref = um.user
                    JOIN message m ON m.ref = um.message
                    WHERE um.seen = 0
                    AND u.email <> ''
                    AND (m.created > ?)
                    ORDER BY m.created DESC"
,
                
$parameters
            
);

        if (!empty(
$sendall))
            {
            
$sendall_chunks array_chunk($sendall,SYSTEM_DATABASE_IDS_CHUNK_SIZE);

            foreach (
$sendall_chunks as $sendall_chunk)
                {
                if (
count($sendall_chunk) > 0)
                    {
                    
$parameters_chunk  array_merge($parametersps_param_fill($sendall_chunk,"i"));
                    
$sendall_sql " OR u.ref IN (" ps_param_insert(count($sendall_chunk)) . ")";
                    }

                
$unreadmessages array_merge(
                    
$unreadmessages,
                    
ps_query("SELECT u.ref AS userref, u.email, m.ref AS messageref, m.message, m.created, m.url
                        FROM user_message um
                            JOIN user u ON u.ref = um.user
                            JOIN message m ON m.ref = um.message
                            WHERE um.seen = 0
                            AND u.email <> ''
                            AND (m.created > ?
                                
$sendall_sql)
                            ORDER BY m.created DESC"
,
                        
$parameters_chunk
                    
)
                );
                }
            }
        }


    
// Keep record of the current value for these config options. setup_user() may override them with the user group specific ones.
    
$current_inactive_message_auto_digest_period $inactive_message_auto_digest_period;
    
$current_user_pref_inactive_digest $user_pref_inactive_digest;
    
$current_user_pref_daily_digest $user_pref_daily_digest;

    foreach(
$digestusers as $digestuser)
        {
        
// Reset config variables before setting up the user to not have logic influenced by the previous iteration.
        
$inactive_message_auto_digest_period $current_inactive_message_auto_digest_period;
        
$user_pref_inactive_digest $current_user_pref_inactive_digest;
        
$user_pref_daily_digest $current_user_pref_daily_digest;

        
$messageuser=get_user($digestuser);
        if(!
$messageuser)
            {
            
// Invalid user
            
continue;
            }

        
setup_user($messageuser);

        
$pref_msg_user_for_inactive_digest $pref_msg_user_pref_daily_digest null;
        
get_config_option($digestuser'user_pref_inactive_digest'$pref_msg_user_for_inactive_digest);
        
get_config_option($digestuser'user_pref_daily_digest'$pref_msg_user_pref_daily_digest);

        if(
$inactive_message_auto_digest_period == || (!$pref_msg_user_for_inactive_digest && !$pref_msg_user_pref_daily_digest))
            {
            
debug("Skipping email digest for user ref " $digestuser " as user or group preference disabled");
            continue;
            }

        
$usermail $messageuser["email"];
        if(!
filter_var($usermailFILTER_VALIDATE_EMAIL))
            {
            
debug("Skipping email digest for user ref " $digestuser " due to invalid email:  " $usermail);
            continue;
            }

        
$messageflag=false;
        
$actionflag=false;
        
// Set up an array of message to delete for this user if they have chosen to purge the messages
        
$messagerefs=array();

        
// Start the new email
        
if(in_array($digestuser,$sendall))
            {
            
$message $lang['email_auto_digest_inactive'] . "<br /><br />";
            }
        else
            {
            
$message $lang['email_daily_digest_text'] . "<br /><br />";
            }
        
$message .= "<style>.InfoTable td {padding:5px; margin: 0px;border: 1px solid #000;}</style><table class='InfoTable'>";
        
$message .= "<tr><th>" $lang["columnheader-date_and_time"] . "</th><th>" $lang["message"] . "</th><th></th></tr>";

        foreach(
$unreadmessages as $unreadmessage)
            {
            if(
$unreadmessage["userref"] == $digestuser)
                {
                
// Message applies to this user
                
$messageflag=true;
                
$usermail $unreadmessage["email"];
                
$msgurl $unreadmessage["url"];
                if(
substr($msgurl,0,1) == "/")
                    {
                    
// If a relative link is provided make sure we add the full URL when emailing
                    
$msgurl $baseurl $msgurl;
                    }
                
$message .= "<tr><td>" nicedate($unreadmessage["created"], truetruetrue) . "</td><td>" $unreadmessage["message"] . "</td><td><a href='" $msgurl "'>" $lang["link"] . "</a></td></tr>";
                
$messagerefs[]=$unreadmessage["messageref"];
                }
            }

        if(
count($messagerefs) == 0)
            {
            
$message .= "<tr><td colspan='3'>" $lang["nomessages"] . "</td></tr>";
            }

        if(
$actions_on)
            {
            if(!
$actions_on){break;}

            
$user_actions get_user_actions(false'''date''DESC');
            
$action_count count($user_actions);
            
$actions_truncated false;
            if (
$action_count 1000)
                {
                
$user_actions array_slice($user_actions01000);
                
$actions_truncated true;
                }

            if (
$action_count 0)
                {
                
$actionflag=true;
                
debug("Adding actions to message for user " $usermail);
                if(
$messageflag)
                    {
                    
$message .= "</table><br /><br />";
                    }
                
$message .= $lang['email_daily_digest_actions'] . "<br /><br />"$lang["actions_introtext"] . "<br />";

                if (
$actions_truncated)
                    {
                    
$message .= escape(str_replace(array('[total]''[application_name]'), array($action_count$applicationname), $lang['email_actions_truncated'])) . "<br />";
                    }

                
$message .= "<style>.InfoTable td {padding:5px; margin: 0px;border: 1px solid #000;}</style><table class='InfoTable'>";
                
$message .= "<tr><th>" $lang["date"] . "</th>";
                
$message .= "<th>" $lang["property-reference"] . "</th>";
                
$message .= "<th>" $lang["description"] . "</th>";
                
$message .= "<th>" $lang["type"] . "</th></tr>";


                foreach(
$user_actions as $user_action)
                    {
                    
$actionlinks=hook("actioneditlink",'',array($user_action));
                    if(
$actionlinks)
                      {
                      
$actioneditlink=$actionlinks["editlink"];
                      
$actionviewlink=$actionlinks["viewlink"];
                      }
                    else
                      {
                      
$actioneditlink '';
                      
$actionviewlink '';
                      }

                    if(
$user_action["type"]=="resourcereview")
                      {
                      
$actioneditlink $baseurl "/pages/edit.php";
                      
$actionviewlink $baseurl "/pages/view.php";
                      }
                    elseif(
$user_action["type"]=="resourcerequest")
                      {
                      
$actioneditlink $baseurl "/pages/team/team_request_edit.php";
                      }
                    elseif(
$user_action["type"]=="userrequest")
                      {
                      
$actioneditlink $baseurl "/pages/team/team_user_edit.php";
                      }

                    
$linkparams["ref"] = $user_action["ref"];
                    
$editlink=($actioneditlink=='')?'':generateURL($actioneditlink,$linkparams);
                    
$viewlink=($actionviewlink=='')?'':generateURL($actionviewlink,$linkparams);
                    
$message .= "<tr>";
                    
$message .= "<td>" nicedate($user_action["date"], truetruetrue) . "</td>";
                    
$message .= "<td><a href=\"" $editlink "\" >" $user_action["ref"] . "</a></td>";
                    
$message .= "<td>" tidy_trim(TidyList($user_action["description"]),$list_search_results_title_trim) . "</td>";
                    
$message .= "<td>" $lang["actions_type_" $user_action["type"]] . "</td>";
                    
$message .= "<td><div class=\"ListTools\">";
                    if(
$editlink!=""){$message .= "&nbsp;&nbsp;<a href=\"" $editlink "\" >" $lang["action-edit"] . "</a>";}
                    if(
$viewlink!=""){$message .= "&nbsp;&nbsp;<a href=\"" $viewlink "\" >" $lang["view"] . "</a>";}
                    
$message .= "</div>";
                    
$message .= "</td></tr>";
                    } 
// End of each $user_actions loop
                
}
            }

        
// Send the email
        
debug("Sending summary to user ref " $digestuser ", email " $usermail);
        
$message .= "</table>";

        
$userprefurl $baseurl "/pages/user/user_preferences.php#UserPreferenceEmailSection";
        
$message .= "<br /><br />" $lang["email_digest_disable"] . "<br /><a href='" $userprefurl  "'>" $userprefurl "</a>";

        if(
$messageflag || $actionflag)
            {
            
// Send mail
            
send_mail($usermail,$applicationname ": " $lang["email_daily_digest_subject"],$message);
            }

        
get_config_option($digestuser,'user_pref_daily_digest_mark_read'$mark_read);
        if(
$mark_read && count($messagerefs) > 0)
            {
            
$parameters = array("i",MESSAGE_ENUM_NOTIFICATION_TYPE_EMAIL);
            
$parameters array_merge($parameters,ps_param_fill($messagerefs,"i"));
            
$parameters array_merge($parameters, array("i",$digestuser));
            
ps_query("UPDATE user_message SET seen = ? WHERE message IN (" ps_param_insert(count($messagerefs)) . ") and user = ?"$parameters);
            }
        }

    
set_sysvar("daily_digest",date("Y-m-d H:i:s"));
    return 
true;
    }

This article was last updated 4th December 2024 08:35 Europe/London time based on the source file dated 27th November 2024 15:30 Europe/London time.