You are here

public function PrivateMessageMapper::getThreadIdForMembers in Private Message 8.2

Same name and namespace in other branches
  1. 8 src/Mapper/PrivateMessageMapper.php \Drupal\private_message\Mapper\PrivateMessageMapper::getThreadIdForMembers()

Retrieve the ID of a thread from the database.

The thread returned will contain all of the given UIDs, and only the given UIDs.

Parameters

array $uids: An array of User IDs of users whose thread should be retrieved.

Return value

int|bool If a thread is found, the thread ID will be returned. Otherwise FALSE will be returned.

Overrides PrivateMessageMapperInterface::getThreadIdForMembers

File

src/Mapper/PrivateMessageMapper.php, line 45

Class

PrivateMessageMapper
Interface for the Private Message Mapper class.

Namespace

Drupal\private_message\Mapper

Code

public function getThreadIdForMembers(array $members) {
  $uids = [];
  foreach ($members as $member) {
    $uids[$member
      ->id()] = $member
      ->id();
  }

  // Select threads common for the given members.
  $query = $this->database
    ->select('private_message_thread__members', 'pmt')
    ->fields('pmt', [
    'entity_id',
  ])
    ->groupBy('entity_id');

  // Add conditions where the threads are in the set of threads for each of
  // the users.
  foreach ($uids as $uid) {
    $subQuery = $this->database
      ->select('private_message_thread__members', 'pmt')
      ->fields('pmt', [
      'entity_id',
    ])
      ->condition('members_target_id', $uid);
    $query
      ->condition('entity_id', $subQuery, 'IN');
  }
  $thread_ids = $query
    ->execute()
    ->fetchCol();

  // Exclude threads with other participants.
  foreach ($thread_ids as $thread_id) {
    $query = $this->database
      ->select('private_message_thread__members', 'pmt')
      ->condition('members_target_id', $uids, 'NOT IN')
      ->condition('entity_id', $thread_id);
    if ($query
      ->countQuery()
      ->execute()
      ->fetchField() == 0) {
      return $thread_id;
    }
  }
  return FALSE;
}