You are here

function mass_contact_role_recipients in Mass Contact 7

Retrieves a list of users by roles.

Gets the user IDs for all users in all roles included in the category.

Parameters

array $recipients: The list of items for this category. For this plugin implementation, it is an array of role IDs.

Return value

null|array The user IDs that are part of specified roles.

1 string reference to 'mass_contact_role_recipients'
mass_contact_role.inc in plugins/mass_contact_role.inc

File

plugins/mass_contact_role.inc, line 42

Code

function mass_contact_role_recipients(array $recipients) {

  // Check to see if a role has been selected.
  if (!isset($recipients['mass_contact_role']) || empty($recipients['mass_contact_role'])) {
    return;
  }
  $result = array();

  // Select the users.
  if (!empty($recipients['mass_contact_role'][0]) && $recipients['mass_contact_role'][0] == 2) {

    // If the selected role is the authenticated users role, get everyone.
    $query = "SELECT u.uid FROM {users} u LEFT JOIN {users_roles} ur ON u.uid = ur.uid WHERE status = 1";
    $result = db_query($query)
      ->fetchAllAssoc('uid', PDO::FETCH_ASSOC);
  }
  elseif (!empty($recipients['mass_contact_role'])) {

    // Get the list of users who belong to the current role.
    $query = "SELECT u.uid FROM {users} u LEFT JOIN {users_roles} ur ON u.uid = ur.uid WHERE ur.rid IN (:ur_rids) AND status = 1";
    $result = db_query($query, array(
      ':ur_rids' => $recipients['mass_contact_role'],
    ))
      ->fetchAllAssoc('uid', PDO::FETCH_ASSOC);
  }
  if (empty($result)) {
    return;
  }

  // Collect the uids.
  $uids = array();
  foreach ($result as $record) {

    // For each record in the result set, add the user's ID to the array.
    $uids[] = (int) $record['uid'];
  }
  return $uids;
}