You are here

public static function drupalchatController::drupalchat_get_thread_history in DrupalChat 8

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

File

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

Class

drupalchatController

Namespace

Drupal\drupalchat\Controller

Code

public static function drupalchat_get_thread_history() {
  $user = \Drupal::currentUser();
  $json = array();
  if (isset($_POST['drupalchat_open_chat_uids'])) {
    $chat_ids = explode(',', Html::escape($_POST['drupalchat_open_chat_uids']));
    $json['messages'] = array();
    foreach ($chat_ids as $chat_id) {
      $messages = '';
      if ($user
        ->id() > 0) {
        $current_uid = $user
          ->id();
      }
      else {
        $current_uid = '0-' . drupalchatController::_drupalchat_get_sid();
      }
      if ($chat_id == 'c-0') {
        $messages = db_query('SELECT m.message_id, m.uid1, m.uid2, m.message, m.timestamp FROM {drupalchat_msg} m WHERE m.uid2 = \'c-0\' ORDER BY m.timestamp DESC LIMIT 30', array(
          ':uid1' => $current_uid,
          ':uid2' => $chat_id,
        ))
          ->fetchAll();
      }
      else {
        $messages = db_query('SELECT m.message_id, m.uid1, m.uid2, m.message, m.timestamp FROM {drupalchat_msg} m WHERE (m.uid2 = :uid2 AND m.uid1 = :uid1) OR (m.uid2 = :uid1 AND m.uid1 = :uid2) ORDER BY m.timestamp DESC LIMIT 30', array(
          ':uid1' => $current_uid,
          ':uid2' => $chat_id,
        ))
          ->fetchAll();
      }
      foreach ($messages as $message) {

        //print_r($message);
        if (!strpos($message->uid1, '-') && $message->uid1 != $user
          ->id() || strpos($message->uid1, '-') && $message->uid1 != '0-' . drupalchatController::_drupalchat_get_sid() || $message->uid2 == 'c-0') {
          if (!strpos($message->uid1, '-')) {
            $account = 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),
            );
            $drupalchat_user_picture = \Drupal::config('drupalchat.settings')
              ->get('drupalchat_user_picture') ?: 1;
            if ($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),
            );
            $drupalchat_user_picture = \Drupal::config('drupalchat.settings')
              ->get('drupalchat_user_picture') ?: 1;
            if ($drupalchat_user_picture == 1) {
              $temp_msg['p'] = drupalchatController::drupalchat_return_pic_url_any_user(user_load('0'));
            }
            $json['messages'][] = $temp_msg;
          }
        }
        else {
          if (!strpos($message->uid2, '-')) {
            $account = user_load($message->uid2);
            $temp_msg = array(
              'message' => Html::escape($message->message),
              'timestamp' => date("Hi:", $message->timestamp),
              'uid1' => $message->uid1,
              'name' => Html::escape(user_format_name($account)),
              'uid2' => $message->uid2,
              'message_id' => Html::escape($message->message_id),
            );
            $drupalchat_user_picture = \Drupal::config('drupalchat.settings')
              ->get('drupalchat_user_picture') ?: 1;
            if ($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),
            );
            $drupalchat_user_picture = \Drupal::config('drupalchat.settings')
              ->get('drupalchat_user_picture') ?: 1;
            if ($drupalchat_user_picture == 1) {
              $temp_msg['p'] = drupalchatController::drupalchat_return_pic_url_any_user(user_load('0'));
            }
            $json['messages'][] = $temp_msg;
          }
        }
      }
    }
    $json['messages'] = array_reverse($json['messages']);
  }
  return new JsonResponse($json);
}