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) {
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'),
);
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());
}
}
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) {
}
}
return $output;
}
}