You are here

public static function OpignoMessageThread::getUnreadThreadCount in Opigno messaging 8

Returns unread threads count.

Parameters

string $return_fields: Return unread threads fields.

Return value

int|array Unread threads count or ids array.

2 calls to OpignoMessageThread::getUnreadThreadCount()
OpignoMessageThread::markReadAll in src/OpignoMessageThread.php
Marks all unread treads as read.
opigno_messaging_preprocess_views_view_field in ./opigno_messaging.module
Implements hook_preprocess_views_view_field().

File

src/OpignoMessageThread.php, line 47

Class

OpignoMessageThread
Class OpignoMessageThread.

Namespace

Drupal\opigno_messaging

Code

public static function getUnreadThreadCount($return_fields = '') {
  $pm_service = \Drupal::service('private_message.service');
  $uid = \Drupal::currentUser()
    ->id();
  if ($uid > 0 && isset($pm_service)) {
    $unread_count = \Drupal::service('private_message.service')
      ->getUnreadThreadCount();
    if ($unread_count > 0) {
      if ($return_fields) {
        $db_connection = \Drupal::service('database');
        $query = $db_connection
          ->select('private_message_thread__last_access_time', 'pmtlat');
        $query
          ->join('pm_thread_access_time', 'pmtat', 'pmtat.id = pmtlat.last_access_time_target_id AND pmtat.owner = :uid', [
          ':uid' => $uid,
        ]);
        $query
          ->join('private_message_threads', 'pmt', 'pmt.id = pmtlat.entity_id AND pmt.updated >= pmtat.access_time');
        $query
          ->join('private_message_thread__last_delete_time', 'pmtldt', 'pmtldt.entity_id = pmtlat.entity_id');
        $query
          ->join('pm_thread_delete_time', 'pmtdt', 'pmtdt.id = pmtldt.last_delete_time_target_id AND pmtdt.delete_time < pmt.updated');
        $query
          ->join('private_message_thread__members', 'pmtm', 'pmtm.entity_id = pmt.id AND pmtm.members_target_id = :uid', [
          ':uid' => $uid,
        ]);
        $query
          ->join('private_message_thread__private_messages', 'pmtpm', 'pmtpm.entity_id = pmt.id');
        $query
          ->join('private_messages', 'pm', 'pm.id = pmtpm.private_messages_target_id AND NOT ((pm.owner = :uid) AND (pm.created = pmt.updated))', [
          ':uid' => $uid,
        ]);
        $query
          ->fields('pmtlat', [
          'entity_id',
        ]);
        $query
          ->fields('pmtat', [
          'access_time',
          'id',
        ]);
        $query
          ->fields('pmt', [
          'updated',
        ]);
        $unread_thread = $query
          ->execute()
          ->fetchAllAssoc('entity_id');
        foreach ($unread_thread as $unread) {
          $ids[] = $unread->{$return_fields};
        }
        if (!empty($ids)) {
          return $ids;
        }
      }
      else {
        return $unread_count;
      }
    }
  }
  return $return_fields ? [] : 0;
}