You are here

public static function drupalchatController::drupalchat_get_messages in DrupalChat 8

drupal_get_messages()

File

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

Class

drupalchatController

Namespace

Drupal\drupalchat\Controller

Code

public static function drupalchat_get_messages() {
  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) {
      $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(
          'uid' => $_SESSION['drupalchat_switch_user'],
          'api_key' => \Drupal::config('drupalchat.settings')
            ->get('drupalchat_external_api_key'),
        );

        // $data = json_encode(array(
        //   'uid' => $_SESSION['drupalchat_switch_user'],
        //   'api_key' => \Drupal::config('drupalchat.settings')->get('drupalchat_external_api_key'),));
        // $options = array(
        //   'method' => 'POST',
        //   'data' => $data,
        //   'timeout' => 15,
        //   'headers' => array('Content-Type' => 'application/json'),
        // );
        if (\Drupal::currentUser()
          ->hasPermission('access drupalchat all logs')) {
          $f_o = drupal_get_form('drupalchat_user_entry_form');
          $output .= drupal_render($f_o)
            ->__toString();
        }

        /*****************/
        $url = DRUPALCHAT_EXTERNAL_A_HOST . ':' . DRUPALCHAT_EXTERNAL_A_PORT . '/r/';
        $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) {
          $query = json_decode($request
            ->getBody());
        }

        //$result = drupal_http_request(DRUPALCHAT_EXTERNAL_A_HOST . ':' . DRUPALCHAT_EXTERNAL_A_PORT . '/r/', $options);

        //$query = json_decode($result->data);
      }
      else {
        $guid = $user
          ->id() ? $user
          ->id() : '0-' . _drupalchat_get_sid();
        $query = db_query('SELECT u.name as name, g.uid as uid, g.message as message, g.TIMESTAMP as timestamp
                       FROM (
               SELECT uid, message, TIMESTAMP
                 FROM (
               (
                 SELECT m1.uid1 AS uid, m1.timestamp AS TIMESTAMP, m1.message AS message
                 FROM {drupalchat_msg} m1
                 INNER JOIN (
                   SELECT MAX( t1.timestamp ) AS TIMESTAMP, t1.uid1
                 FROM {drupalchat_msg} t1
                 WHERE t1.uid2 =  :uid
                 GROUP BY t1.uid1
                 ) recent ON recent.timestamp = m1.timestamp
                 AND recent.uid1 = m1.uid1
                 ORDER BY TIMESTAMP DESC
               )
               UNION (
                 SELECT m1.uid2 AS uid, m1.timestamp AS TIMESTAMP, m1.message AS message
                 FROM {drupalchat_msg} m1
                 INNER JOIN (
                   SELECT MAX( t1.timestamp ) AS TIMESTAMP, t1.uid2
                 FROM {drupalchat_msg} t1
                 WHERE t1.uid1 =  :uid
                 GROUP BY t1.uid2
                 )recent ON recent.timestamp = m1.timestamp
                 AND recent.uid2 = m1.uid2
                 ORDER BY TIMESTAMP DESC
               )
                ) AS f
              ORDER BY 3 DESC
              ) AS g INNER JOIN {drupalchat_users} u ON
              (g.uid = u.uid AND u.uid!= 0) OR (u.uid = 0 AND g.uid = CONCAT(\'0-\', u.session))
            GROUP BY uid', array(
          ':uid' => $guid,
        ));
      }
      foreach ($query as $record) {

        // @FIXME
        // l() expects a Url object, created from a route name or external URI.
        // $output .= '<div style="display:block;border-bottom: 1px solid #ccc; padding: 10px;"><div style="font-size:130%; display: inline;">' . l($record->name,'drupalchat/messages/message/' . $record->uid) . '</div><div style="float:right;color:#AAA; font-size: 70%;">' . format_date($record->timestamp,'long') . '</div><div style="display: block; padding: 10px;">' . Html::escape($record->message) . '</div></div>';
      }

      //$output .= '</tbody></table>';

      //$user_item = user_load($user->id());

      //$output .= '<pre>' . print_r($user_item,true) . '</pre>';

      //$output .= theme('user_picture', array('account' =>$user_item));
    }
    return $output;
  }
}