You are here

function message_subscribe_get_subscribers in Message Subscribe 7

Get a list of user IDs that need to recieve the message.

array(
  1 => array(
    'flags' => array(
      'subscribe_node',
      'subscribe_user',
    ),
    'notifiers' => array(
      'email',
      'sms',
    ),
  ),
);

Parameters

$entity_type: The entity type.

$entity: The entity object.

$message: The Message object.

$subscribe_options: Optional; The options array to pass to message_subscribe_get_basic_context().

$context: Optional; The context array, passed by reference.

Return value

Array keyed with the user IDs to send email, and array with the flags the used it for the subscription, and the notifier names.

6 calls to message_subscribe_get_subscribers()
MessageSubscribeEmailNotificationsTest::testEmailNotifications in message_subscribe_email/message_subscribe_email.test
Test opting in/out of default email notifications.
MessageSubscribeEmailSubscribersTest::testGetSubscribers in message_subscribe_email/message_subscribe_email.test
Test getting the subscribers list.
MessageSubscribeSubscribersTest::testEntityAccess in ./message_subscribe.test
Assert subscribers list is entity-access aware.
MessageSubscribeSubscribersTest::testGetSubscribers in ./message_subscribe.test
Test getting the subscribers list.
MessageSubscribeSubscribersTest::testGetSubscribersExcludeSelf in ./message_subscribe.test
Testing the exclusion of the entity author from the subscribers lists.

... See full list

File

./message_subscribe.module, line 228
Subscribe API for the Message and Message notify modules.

Code

function message_subscribe_get_subscribers($entity_type, $entity, Message $message, $subscribe_options = array(), &$context = array()) {
  $context = $context ? $context : message_subscribe_get_basic_context($entity_type, $entity, $subscribe_options, $context);
  $notify_message_owner = isset($subscribe_options['notify message owner']) ? $subscribe_options['notify message owner'] : variable_get('message_subscribe_notify_own_actions', FALSE);
  $uids = array();

  // We don't use module_invoke_all() is we want to retain the array keys,
  // which are the user IDs.
  foreach (module_implements('message_subscribe_get_subscribers') as $module) {
    $function = $module . '_message_subscribe_get_subscribers';
    $result = $function($message, $subscribe_options, $context);
    $uids += $result;
  }

  // If we're not notifying blocked users, exclude those users from the result
  // set now so that we avoid unnecessarily loading those users later.
  if (empty($subscribe_options['notify blocked users']) && !empty($uids)) {
    $query = new EntityFieldQuery();
    $results = $query
      ->entityCondition('entity_type', 'user')
      ->propertyCondition('status', 1)
      ->propertyCondition('uid', array_keys($uids), 'IN')
      ->execute();
    if (!empty($results['user'])) {
      $uids = array_intersect_key($uids, $results['user']);
    }
    else {

      // There are no not blocked users to notify.
      $uids = array();
    }
  }

  // See if the author of the entity gets notified.
  if (isset($entity->uid) && !$notify_message_owner && array_key_exists($entity->uid, $uids)) {
    unset($uids[$entity->uid]);
  }
  $accounts = user_load_multiple(array_keys($uids));
  foreach ($uids as $uid => $values) {
    if (!empty($subscribe_options['entity access'])) {
      if (!entity_access('view', $entity_type, $entity, $accounts[$uid])) {

        // User doesn't have access to view the entity.
        unset($uids[$uid]);
      }
    }
  }
  $values = array(
    'context' => $context,
    'entity_type' => $entity_type,
    'entity' => $entity,
    'message' => $message,
    'subscribe_options' => $subscribe_options,
  );
  _message_subscribe_add_default_notifiers($uids);
  ksort($uids);
  drupal_alter('message_subscribe_get_subscribers', $uids, $values);
  return $uids;
}