You are here

public function PrivateMessageMapper::getThreadIdsForUser in Private Message 8.2

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

Retrieve a list of thread IDs for threads the user belongs to.

Parameters

\Drupal\user\UserInterface $user: The user whose most recently thread IDs should be retrieved.

mixed $count: The number of thread IDs to retrieve or FALSE to retrieve them all.

int $timestamp: A timestamp relative to which only thread IDs with an earlier timestamp should be returned.

Return value

array An array of thread IDs if any threads exist.

Overrides PrivateMessageMapperInterface::getThreadIdsForUser

File

src/Mapper/PrivateMessageMapper.php, line 106

Class

PrivateMessageMapper
Interface for the Private Message Mapper class.

Namespace

Drupal\private_message\Mapper

Code

public function getThreadIdsForUser(UserInterface $user, $count = FALSE, $timestamp = FALSE) {
  $query = 'SELECT DISTINCT(thread.id), MAX(thread.updated) ' . 'FROM {private_message_threads} AS thread ' . 'JOIN {pm_thread_history} pm_thread_history ' . 'ON pm_thread_history.thread_id = thread.id AND pm_thread_history.uid = :history_uid ' . 'JOIN {private_message_thread__members} AS thread_member ' . 'ON thread_member.entity_id = thread.id AND thread_member.members_target_id = :uid ' . 'JOIN {private_message_thread__private_messages} AS thread_messages ' . 'ON thread_messages.entity_id = thread.id ' . 'JOIN {private_messages} AS messages ' . 'ON messages.id = thread_messages.private_messages_target_id ' . 'WHERE pm_thread_history.delete_timestamp <= messages.created ';
  $vars = [
    ':uid' => $user
      ->id(),
    ':history_uid' => $user
      ->id(),
  ];
  if ($timestamp) {
    $query .= 'AND updated < :timestamp ';
    $vars[':timestamp'] = $timestamp;
  }
  $query .= 'GROUP BY thread.id ORDER BY MAX(thread.updated) DESC, thread.id';
  if ($count > 0) {
    $thread_ids = $this->database
      ->queryRange($query, 0, $count, $vars)
      ->fetchCol();
  }
  else {
    $thread_ids = $this->database
      ->query($query, $vars)
      ->fetchCol();
  }
  return is_array($thread_ids) ? $thread_ids : [];
}