You are here

function drupalchat_get_messages in DrupalChat 7.2

Same name and namespace in other branches
  1. 6.2 drupalchat.module \drupalchat_get_messages()
  2. 7 drupalchat.module \drupalchat_get_messages()
1 string reference to 'drupalchat_get_messages'
drupalchat_menu in ./drupalchat.module
Implements hook_menu().

File

./drupalchat.module, line 1053
Module code for DrupalChat.

Code

function drupalchat_get_messages() {
  if (!(user_access('access drupalchat own logs') || user_access('access drupalchat all logs'))) {
    drupal_access_denied();
  }
  else {
    global $user;
    if ($user->uid > 0 || _drupalchat_get_sid() != -1) {
      $output = '';
      if (check_plain(variable_get('drupalchat_polling_method', DRUPALCHAT_AJAX)) == DRUPALCHAT_COMMERCIAL) {
        global $user;
        if (!isset($_SESSION['drupalchat_switch_user'])) {
          $_SESSION['drupalchat_switch_user'] = $user->uid ? $user->uid : '0-' . _drupalchat_get_sid();
        }
        $data = json_encode(array(
          'uid' => $_SESSION['drupalchat_switch_user'],
          'api_key' => check_plain(variable_get('drupalchat_external_api_key', NULL)),
        ));
        $options = array(
          'method' => 'POST',
          'data' => $data,
          'timeout' => 15,
          'headers' => array(
            'Content-Type' => 'application/json',
          ),
        );
        if (user_access('access drupalchat all logs')) {
          $f_o = drupal_get_form('drupalchat_user_entry_form');
          $output .= drupal_render($f_o);
        }
        $result = drupal_http_request(DRUPALCHAT_EXTERNAL_A_HOST . ':' . DRUPALCHAT_EXTERNAL_A_PORT . '/r/', $options);
        $query = json_decode($result->data);
      }
      else {
        $guid = $user->uid ? $user->uid : '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) {
        $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;">' . check_plain($record->message) . '</div></div>';
      }

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

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

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

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