You are here

function message_subscribe_message_subscribe_get_subscribers in Message Subscribe 7

Same name and namespace in other branches
  1. 8 message_subscribe.module \message_subscribe_message_subscribe_get_subscribers()

Implements hook_message_subscribe_get_subscribers().

Parameters

$context: Array keyed with the entity type and array of entity IDs as the value. According to this context this function will retrieve the related subscribers.

$message: The message object.

Return value

Array keyed with the user ID and the value:

  • "flags": Array with the flag names that resulted with including

the user.

  • "notifiers": Array with the Message notifier name plugins.

See also

flag_get_content_flags()

File

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

Code

function message_subscribe_message_subscribe_get_subscribers(Message $message, $subscribe_options = array(), $context = array()) {
  $uids = array();
  $subscribe_options += array(
    'last uid' => 0,
    'range' => FALSE,
  );

  // Determine if a range is needed for the query.
  $range = $subscribe_options['range'];

  // Find the users that subscribed to each context.
  foreach ($context as $entity_type => $entity_ids) {
    if (!$entity_ids) {
      continue;
    }

    // Get all flags on given entity type.
    if (!($flags = message_subscribe_flag_get_flags($entity_type))) {
      continue;
    }
    $fids = array();
    foreach ($flags as $flag) {
      $fids[$flag->fid] = $flag->name;
    }

    // Query all the entity IDs inside the given flags. We don't use
    // flag_get_content_flags() as we want to get all the flaggings of an
    // entity-type in a single query.
    if (FLAG_API_VERSION == 2) {
      $query = db_select('flag_content', 'fc')
        ->condition('content_type', $entity_type)
        ->condition('content_id', $entity_ids, 'IN');
    }
    else {
      $query = db_select('flagging', 'fc')
        ->condition('entity_type', $entity_type)
        ->condition('entity_id', $entity_ids, 'IN');
    }
    $query
      ->fields('fc')
      ->condition('fid', array_keys($fids), 'IN')
      ->condition('fc.uid', $subscribe_options['last uid'], '>')
      ->orderBy('fc.uid', 'ASC');
    if ($range) {
      $query
        ->range(0, $range);

      // If we're mpt notifying blocked users, we need to take this into account
      // in this query so that the range is accurate.
      if (empty($subscribe_options['notify blocked users'])) {
        $query
          ->join('users', 'users', 'users.uid = fc.uid');
        $query
          ->condition('users.status', '1', '=');
      }
    }
    $result = $query
      ->execute();
    foreach ($result as $row) {
      $uids[$row->uid] = !empty($uids[$row->uid]) ? $uids[$row->uid] : array(
        'notifiers' => array(),
      );

      // Register the flag name.
      $flag_name = $fids[$row->fid];
      $uids[$row->uid]['flags'][] = $flag_name;
      if ($range) {
        --$range;
        if ($range == 0) {

          // We've reach the requested item.
          return $uids;
        }
      }
    }
  }
  return $uids;
}