Collections functions
Encryption functions
General functions
Render functions
Theme permission functions
Video functions
Resource 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 182 to 371

Definition

 
function message_send_unread_emails()
    {
    global 
$lang$applicationname$actions_enable$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($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"];
                }
            }
        }
        
    
# Get all unread notifications created since last run, or all mesages sent to inactive users. 
    
$unreadmessages=sql_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.ref IN ('" implode("','",$digestusers) . "') AND u.email<>'' AND (m.created>'" $lastrun "'" . (count($sendall) > " OR u.ref IN ('" implode("','",$sendall) . "')" "") . ") ORDER BY m.created DESC");
    
    
$inactive_message_auto_digest_period_saved $inactive_message_auto_digest_period;
    foreach(
$digestusers as $digestuser)
        {
        
// Reset config before setting up user so that any user groups processed later are not affected by the override
        
$inactive_message_auto_digest_period $inactive_message_auto_digest_period_saved;
        
$messageuser=get_user($digestuser);
        if(!
$messageuser)
            {
            
// Invalid user
            
continue;
            }
        
setup_user($messageuser);
        
get_config_option($digestuser,'user_pref_inactive_digest'$user_pref_inactive_digest);
        
get_config_option($digestuser,'user_pref_daily_digest'$user_pref_daily_digest);
        
        if(
$inactive_message_auto_digest_period == || (!$user_pref_inactive_digest && !$user_pref_daily_digest)) // This may be set differently as a group configuration overerride or disabled by user
            
{
            
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"];
                
$message .= "<tr><td>" nicedate($unreadmessage["created"], truetruetrue) . "</td><td>" $unreadmessage["message"] . "</td><td><a href='" $unreadmessage["url"] . "'>" $lang["link"] . "</a></td></tr>";
                
$messagerefs[]=$unreadmessage["messageref"];
                }
            }
        if(
$actions_on)
            {
            
//debug("Checking actions for user " . $unreadmessage["userref"]);
            
if(!$actions_on){break;}
            
$user_actions get_user_actions(false);
            if (
count($user_actions) > 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 />";
                
$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 .= "<a href=\"" $editlink "\" >&nbsp;&nbsp;" $lang["action-edit"] . "</a>";}
                    if(
$viewlink!=""){$message .= "<a href=\"" $viewlink "\" >&nbsp;&nbsp;" $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)
            {
            
sql_query("UPDATE user_message SET seen='" MESSAGE_ENUM_NOTIFICATION_TYPE_EMAIL "' WHERE message IN ('" implode("','",$messagerefs) . "') and user = '" $digestuser "'");
            }
        }

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

This article was last updated 30th November 2020 19:35 Europe/London time based on the source file dated 4th August 2020 08:55 Europe/London time.