You are here

public static function drupalchatController::_drupalchat_buddylist_online in DrupalChat 8

2 calls to drupalchatController::_drupalchat_buddylist_online()
drupalchatController::drupalchat_poll in src/Controller/drupalchatController.php
Process and get messages
drupalchatController::_drupalchat_chat in src/Controller/drupalchatController.php

File

src/Controller/drupalchatController.php, line 367
Contains Drupal\drupalchat\Controller\drupalchatController

Class

drupalchatController

Namespace

Drupal\drupalchat\Controller

Code

public static function _drupalchat_buddylist_online($buddylist) {
  global $base_url;
  $user = \Drupal::currentUser();
  $database = \Drupal::database();
  $users = array();
  if (\Drupal::config('drupalchat.settings')
    ->get('drupalchat_enable_chatroom') == 1) {
    $users['c-0'] = array(
      'name' => t('Public Chatroom')
        ->__toString(),
      'status' => '1',
    );
    if (\Drupal::config('drupalchat.settings')
      ->get('drupalchat_user_picture') == 1) {
      $users['c-0']['p'] = $base_url . '/' . drupal_get_path('module', 'drupalchat') . '/css/themes/' . \Drupal::config('drupalchat.settings')
        ->get('drupalchat_theme') . '/images/default_room.png';
    }
  }
  if (\Drupal::config('drupalchat.settings')
    ->get('drupalchat_rel') > DRUPALCHAT_REL_AUTH) {

    // Return empty on an empty buddylist
    if (empty($buddylist)) {
      $users['total'] = 0;
      return $users;
    }
    $result = $database
      ->select('drupalchat_users', 'n')
      ->fields('n', [
      'uid',
      'name',
      'status',
    ])
      ->condition('timestamp', time() - \Drupal::config('drupalchat.settings')
      ->get('drupalchat_user_latency'), '>=')
      ->condition('uid', $buddylist, 'IN')
      ->execute()
      ->fetchAll();
  }
  else {
    if ($user
      ->id() > 0) {
      $result = $database
        ->select('drupalchat_users', 'n')
        ->fields('n', [
        'uid',
        'name',
        'status',
        'session',
      ])
        ->condition('uid', $user
        ->id(), '<>')
        ->condition('timestamp', time() - \Drupal::config('drupalchat.settings')
        ->get('drupalchat_user_latency'), '>=')
        ->execute()
        ->fetchAll();
    }
    else {
      $result = $database
        ->select('drupalchat_users', 'n')
        ->fields('n', [
        'uid',
        'name',
        'status',
        'session',
      ])
        ->condition('timestamp', time() - \Drupal::config('drupalchat.settings')
        ->get('drupalchat_user_latency'), '>=')
        ->condition('session', drupalchatController::_drupalchat_get_sid(), '<>')
        ->execute()
        ->fetchAll();
    }
  }
  foreach ($result as $buddy) {
    if ($buddy->uid > 0) {
      $account = \Drupal::entityManager()
        ->getStorage('user')
        ->load($buddy->uid);
      $users[$buddy->uid] = array(
        'name' => Html::escape(user_format_name($account)),
        'status' => $buddy->status,
      );
      if (\Drupal::config('drupalchat.settings')
        ->get('drupalchat_user_picture') == 1) {
        $users[$buddy->uid]['p'] = drupalchatController::drupalchat_return_pic_url_any_user(\Drupal::entityManager()
          ->getStorage('user')
          ->load($buddy->uid));
      }
    }
    else {
      $users[$buddy->uid . '-' . $buddy->session] = array(
        'name' => Html::escape($buddy->name),
        'status' => $buddy->status,
      );
      if (\Drupal::config('drupalchat.settings')
        ->get('drupalchat_user_picture') == 1) {
        $users[$buddy->uid . '-' . $buddy->session]['p'] = drupalchatController::drupalchat_return_pic_url_any_user(\Drupal::entityManager()
          ->getStorage('user')
          ->load('0'));
      }
    }
  }
  $users['total'] = count($users);
  if (\Drupal::config('drupalchat.settings')
    ->get('drupalchat_enable_chatroom') == 1) {
    $users['total']--;
  }
  return $users;
}