You are here

function push_notifications_privatemsg_message_insert in Push Notifications 7

Implements hook_privatemsg_message_insert.

File

./push_notifications.module, line 257
Push Notifications functionality.

Code

function push_notifications_privatemsg_message_insert($message) {
  if (variable_get('push_notifications_privatemsg_integration', 0)) {

    // Compose the payload. If the body is empty, just use the subject line.
    // Otherwise, combine subject and body.
    $payload = empty($message->body) ? $message->subject : $message->subject . ' ' . $message->body;
    $payload = 'From ' . $message->author->name . ': ' . $payload;

    // Compose an array of recipients.
    $recipients = array();
    foreach ($message->recipients as $recipient) {
      if ($recipient->type == "role" && $recipient->name != 'authenticated user') {
        $results = db_select('users_roles', 'ur')
          ->fields('ur', array(
          'uid',
        ))
          ->condition('ur.rid', $recipient->rid, '=')
          ->execute()
          ->fetchCol();
      }
      elseif ($recipient->type == "role" && $recipient->name == 'authenticated user') {
        $results = db_select('users', 'u')
          ->fields('u')
          ->execute()
          ->fetchCol();
      }
      if ($recipient->type == "role") {
        foreach ($results as $result) {
          $recipients[] = $result;
        }
      }
      else {
        $recipients[] = $recipient->uid;
      }
    }
    push_notifications_send_message($recipients, $payload);
  }
}