You are here

public static function drupalchatController::drupalchat_get_messages_specific in DrupalChat 8

File

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

Class

drupalchatController

Namespace

Drupal\drupalchat\Controller

Code

public static function drupalchat_get_messages_specific($id = "1") {
  if (!(\Drupal::currentUser()
    ->hasPermission('access drupalchat own logs') || \Drupal::currentUser()
    ->hasPermission('access drupalchat all logs'))) {
    drupal_access_denied();
  }
  else {
    $user = \Drupal::currentUser();
    if ($user
      ->id() > 0 || drupalchatController::_drupalchat_get_sid() != -1) {
      $guid = $user
        ->id() ? $user
        ->id() : '0-' . drupalchatController::_drupalchat_get_sid();
      $output = '';
      if (\Drupal::config('drupalchat.settings')
        ->get('drupalchat_polling_method') == DRUPALCHAT_COMMERCIAL) {
        global $user;
        if (!isset($_SESSION['drupalchat_switch_user'])) {
          $_SESSION['drupalchat_switch_user'] = $user
            ->id() ? $user
            ->id() : '0-' . drupalchatController::_drupalchat_get_sid();
        }
        $data = array(
          'uid1' => $_SESSION['drupalchat_switch_user'],
          'uid2' => $id,
          'api_key' => \Drupal::config('drupalchat.settings')
            ->get('drupalchat_external_api_key'),
        );
        $url = DRUPALCHAT_EXTERNAL_A_HOST . ':' . DRUPALCHAT_EXTERNAL_A_PORT . '/q/';
        $client = \Drupal::httpClient();
        try {
          $request = $client
            ->post($url, [
            'verify' => false,
            'form_params' => $data,
          ]);
        } catch (BadResponseException $exception) {
          $code = $exception
            ->getResponse()
            ->getStatusCode();
          $error = $exception
            ->getResponse()
            ->getReasonPhrase();
          $e = array(
            'code' => $code,
            'error' => $error,
          );
          return $e;
        } catch (RequestException $exception) {
          $e = array(
            'code' => $exception
              ->getResponse()
              ->getStatusCode(),
            'error' => $exception
              ->getResponse()
              ->getReasonPhrase(),
          );
          return $e;
        }
        if (json_decode($request
          ->getStatusCode()) == 200) {
          $q = json_decode($request
            ->getBody());
        }

        // $result = drupal_http_request(DRUPALCHAT_EXTERNAL_A_HOST . ':' . DRUPALCHAT_EXTERNAL_A_PORT .  '/q/', $options);
        // $q = json_decode($result->data);
      }
      else {
        $result = db_select('drupalchat_msg', 'm');
        $result
          ->innerJoin('drupalchat_users', 'u', '(m.uid1 = u.uid AND u.uid!= 0) OR (u.uid = 0 AND m.uid1 = CONCAT(\'0-\', u.session))');
        $result
          ->innerJoin('drupalchat_users', 'v', '(m.uid2 = v.uid AND v.uid!= 0) OR (v.uid = 0 AND m.uid2 = CONCAT(\'0-\', v.session))');
        $result
          ->addField('u', 'name', 'from_name');
        $result
          ->addField('v', 'name', 'to_name');
        $result
          ->fields('m', array(
          'uid1',
          'uid2',
          'message',
          'timestamp',
        ));
        $result
          ->condition(db_or()
          ->condition(db_and()
          ->condition('uid1', $guid)
          ->condition('uid2', $id))
          ->condition(db_and()
          ->condition('uid1', $id)
          ->condition('uid2', $guid)));
        $q = $result
          ->execute();
      }
      $oldname = NULL;
      foreach ($q as $record) {
        if ($oldname == $record->from_name) {
          $output .= '<div style="display: block; padding-top: 0%; padding-bottom: 0%;">' . $record->message . '</div>';
        }
        else {
          $output .= '<div style="display:block;border-bottom: 1px solid #ccc; padding: 1% 0% 1% 0%;"></div><div style="display:block; padding-top: 1%; padding-bottom: 0%"><div style="font-size:100%; display: inline;"><a href="#">' . $record->from_name . '</a></div><div style="float:right;font-size: 70%;">' . format_date($record->timestamp, 'long') . '</div><div style="display: block; padding-top: 1%; padding-bottom: 0%">' . Html::escape($record->message) . '</div></div>';
        }
        $oldname = $record->from_name;
      }
      $output .= '';
    }
    return $output;
  }
}