You are here

public function drupalchatController::drupalchat_poll in DrupalChat 8

Process and get messages

1 string reference to 'drupalchatController::drupalchat_poll'
drupalchat.routing.yml in ./drupalchat.routing.yml
drupalchat.routing.yml

File

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

Class

drupalchatController

Namespace

Drupal\drupalchat\Controller

Code

public function drupalchat_poll() {

  //global $user;
  $user = \Drupal::currentUser();
  $initial_time = time();
  $message_count = 0;

  /*if (isset($_GET['drupalchat_last_timestamp'])) {
      $last_timestamp = Html::escape($_GET['drupalchat_last_timestamp']);
    }*/
  if ($_GET['drupalchat_last_timestamp'] > 0) {
    $last_timestamp = Html::escape($_GET['drupalchat_last_timestamp']);
  }
  else {
    $last_timestamp = $initial_time;

    //$last_timestamp = 1;
  }
  $buddylist = drupalchatController::_drupalchat_buddylist($user
    ->id());
  $buddylist_online_old = drupalchatController::_drupalchat_buddylist_online($buddylist);
  $polling_method = \Drupal::config('drupalchat.settings')
    ->get('drupalchat_polling_method') ?: DRUPALCHAT_AJAX;

  //echo $polling_method;

  //die;
  $json['messages'] = array();
  if ($polling_method == DRUPALCHAT_AJAX) {
    drupalchatController::_drupalchat_touch_user($user
      ->id());
    \Drupal::moduleHandler()
      ->invokeAll('drupalchat_ajaxpoll');

    // AJAX poll hook
  }
  elseif ($polling_method == DRUPALCHAT_LONGPOLL) {
    do {
      sleep(3);
      $buddylist_online = drupalchatController::_drupalchat_buddylist_online($buddylist);
      if ($user
        ->id() > 0) {
        $message_count = db_query(' SELECT COUNT(*)
                                    FROM {drupalchat_msg} m
                                    WHERE (m.uid2 IN (:uid2,\'c-0\') OR m.uid1 = :uid2)
                                    AND m.timestamp > :timestamp', array(
          ':uid2' => $user
            ->id(),
          ':timestamp' => $last_timestamp,
        ))
          ->fetchField();
      }
      else {
        $message_count = db_query(' SELECT COUNT(*)
                                    FROM {drupalchat_msg} m
                                    WHERE (m.uid2 IN (:uid2,\'c-0\') OR m.uid1 = :uid2)
                                    AND m.timestamp > :timestamp', array(
          ':uid2' => '0-' . _drupalchat_get_sid(),
          ':timestamp' => $last_timestamp,
        ))
          ->fetchField();
      }
      drupalchatController::_drupalchat_touch_user($user
        ->id());
      \Drupal::moduleHandler()
        ->invokeAll('drupalchat_longpoll');

      // Long poll hook
    } while (time() - $initial_time < ini_get('max_execution_time') - 5 && $message_count == 0 && drupalchatController::_drupalchat_buddylist_diff($buddylist_online_old, $buddylist_online));
  }
  if ($message_count > 0 || $polling_method == DRUPALCHAT_AJAX) {
    if ($user
      ->id() > 0) {
      $messages = db_query('SELECT m.message_id, m.uid1, m.uid2, m.message, m.timestamp FROM {drupalchat_msg} m WHERE (m.uid2 IN (:uid2,\'c-0\') OR m.uid1 = :uid2) AND m.timestamp > :timestamp ORDER BY m.timestamp ASC', array(
        ':uid2' => $user
          ->id(),
        ':timestamp' => $last_timestamp,
      ));
    }
    else {
      $messages = db_query('SELECT m.message_id, m.uid1, m.uid2, m.message, m.timestamp FROM {drupalchat_msg} m WHERE (m.uid2 IN (:uid2,\'c-0\') OR m.uid1 = :uid2) AND m.timestamp > :timestamp ORDER BY m.timestamp ASC', array(
        ':uid2' => '0-' . drupalchatController::_drupalchat_get_sid(),
        ':timestamp' => $last_timestamp,
      ));
    }

    //while ($message = db_fetch_object($messages)) {

    // Drupal 7
    foreach ($messages as $message) {

      //$arr = explode("-", $message->uid1, 2);
      if (!strpos($message->uid1, '-') && $message->uid1 != $user
        ->id() || strpos($message->uid1, '-') && $message->uid1 != '0-' . drupalchatController::_drupalchat_get_sid()) {
        if (!strpos($message->uid1, '-')) {
          $account = \Drupal::entityManager()
            ->getStorage('user')
            ->load($message->uid1);
          $temp_msg = array(
            'message' => html::escape($message->message),
            'timestamp' => date("H:i", $message->timestamp),
            'uid1' => $message->uid1,
            'name' => Html::escape(user_format_name($account)),
            'uid2' => $message->uid2,
            'message_id' => Html::escape($message->message_id),
          );
          if (\Drupal::config('drupalchat.settings')
            ->get('drupalchat_user_picture') == 1) {
            $temp_msg['p'] = drupalchatController::drupalchat_return_pic_url_any_user($account);
          }
          $json['messages'][] = $temp_msg;
        }
        else {
          $arr = explode("-", $message->uid1, 2);
          $sid = $arr[1];
          $name = db_query('SELECT name FROM {drupalchat_users} WHERE uid = :uid AND session = :sid', array(
            ':uid' => '0',
            ':sid' => $sid,
          ))
            ->fetchField();
          $temp_msg = array(
            'message' => Html::escape($message->message),
            'timestamp' => date("H:i", $message->timestamp),
            'uid1' => $message->uid1,
            'name' => $name,
            'uid2' => $message->uid2,
            'message_id' => Html::escape($message->message_id),
          );
          if (\Drupal::config('drupalchat.settings')
            ->get('drupalchat_user_picture') == 1) {
            $temp_msg['p'] = drupalchatController::drupalchat_return_pic_url_any_user(\Drupal::entityManager()
              ->getStorage('user')
              ->load('0'));
          }
          $json['messages'][] = $temp_msg;
        }
      }
      else {
        if (!strpos($message->uid2, '-')) {
          $account = \Drupal::entityManager()
            ->getStorage('user')
            ->load($message->uid2);
          $temp_msg = array(
            'message' => Html::escape($message->message),
            'timestamp' => date("H:i", $message->timestamp),
            'uid1' => $message->uid1,
            'name' => Html::escape(user_format_name($account)),
            'uid2' => $message->uid2,
            'message_id' => Html::escape($message->message_id),
          );
          if (\Drupal::config('drupalchat.settings')
            ->get('drupalchat_user_picture') == 1) {
            $temp_msg['p'] = drupalchatController::drupalchat_return_pic_url_any_user($account);
          }
          $json['messages'][] = $temp_msg;
        }
        else {
          $arr = explode("-", $message->uid2, 2);
          $sid = $arr[1];
          $name = db_query('SELECT name FROM {drupalchat_users} WHERE uid = :uid AND session = :sid', array(
            ':uid' => '0',
            ':sid' => $sid,
          ))
            ->fetchField();
          $temp_msg = array(
            'message' => Html::escape($message->message),
            'timestamp' => date("H:i", $message->timestamp),
            'uid1' => $message->uid1,
            'name' => $name,
            'uid2' => $message->uid2,
            'message_id' => Html::escape($message->message_id),
          );
          if (\Drupal::config('drupalchat.settings')
            ->get('drupalchat_user_picture') == 1) {
            $temp_msg['p'] = drupalchatController::drupalchat_return_pic_url_any_user(\Drupal::entityManager()
              ->getStorage('user')
              ->load('0'));
          }
          $json['messages'][] = $temp_msg;
        }
      }

      //if($message->
      if ($message->timestamp > $last_timestamp) {
        $last_timestamp = $message->timestamp;
      }
    }
  }
  $json['status'] = 0;
  $json['total_messages'] = $message_count;
  $json['last_timestamp'] = $last_timestamp;
  $json['buddylist'] = isset($buddylist_online) ? $buddylist_online : $buddylist_online_old;

  /*echo '<pre>';
    print_r($json);
    echo '</pre>';*/
  return new JsonResponse($json);
}