You are here

function _pm_email_notify_send_check in Privatemsg 7.2

Same name and namespace in other branches
  1. 6.2 pm_email_notify/pm_email_notify.module \_pm_email_notify_send_check()

Retrieve notification setting of a user and check if they should receive an e-mail notification for a message.

Note: This function tries to return as quickly as possible, to avoid extra processing in batch executions.

Parameters

$uid: User uid

$message: Message.

Return value

bool Whether or not to notify the user in question.

2 calls to _pm_email_notify_send_check()
pm_email_notify_privatemsg_message_recipient_changed in pm_email_notify/pm_email_notify.module
Implements hook_privatemsg_message_recipient_changed().
pm_email_notify_send_mail in pm_email_notify/pm_email_notify.module
Send a pm notification email to a recipient.

File

pm_email_notify/pm_email_notify.module, line 139
Notifies users about new Private Messages via Email.

Code

function _pm_email_notify_send_check($uid, $message) {
  static $notifications = array();
  $mid = $message->mid;
  $thread_id = $message->thread_id;
  $level = _pm_email_notify_user_level($uid);

  // If the user has notifications disabled, we can skip the rest.
  if ($level == PM_EMAIL_NOTIFY_LEVEL_DISABLED) {
    return FALSE;
  }

  // If the user has all notifications enabled, we can skip the rest.
  if ($level == PM_EMAIL_NOTIFY_LEVEL_ALL) {
    return TRUE;
  }

  // Cache the result set in case this method is executed in batched operation
  // which will perform many unnecessary repeated processing.
  if (!isset($notifications[$uid][$mid])) {

    // Prime the setting to false.
    $notifications[$uid][$mid] = FALSE;
    if ($level == PM_EMAIL_NOTIFY_LEVEL_THREAD) {

      // Is this the origin of a thread?
      $notifications[$uid][$mid] = $mid == $thread_id;
    }
    elseif ($level == PM_EMAIL_NOTIFY_LEVEL_UNREAD_ONCE) {

      // If this is the first message of a thread, always send a notification.
      if ($mid == $thread_id) {
        $notifications[$uid][$mid] = TRUE;
      }
      else {

        // Check if this user has more than a single unread message
        // in that thread. If yes, they already got a notification.
        // They always have at least one unread message because they just
        // received one.
        $unread_count = db_query("SELECT COUNT(*) FROM {pm_index} WHERE thread_id = :thread_id AND is_new = 1 AND recipient = :recipient AND type IN ('user', 'hidden')", array(
          ':thread_id' => $thread_id,
          ':recipient' => $uid,
        ))
          ->fetchField();
        $notifications[$uid][$mid] = $unread_count == 1;
      }
    }
  }
  return $notifications[$uid][$mid];
}