You are here

function privatemsg_sql_messages in Privatemsg 6

Same name and namespace in other branches
  1. 6.2 privatemsg.module \privatemsg_sql_messages()
  2. 7.2 privatemsg.module \privatemsg_sql_messages()
  3. 7 privatemsg.module \privatemsg_sql_messages()

Query definition to load messages of one or multiple threads.

Parameters

$fragments: Query fragments array.

$threads: Array with one or multiple thread id's.

$account: User object for which the messages are being loaded.

$load_all: Deleted messages are only loaded if this is set to TRUE.

Related topics

File

./privatemsg.module, line 1248
Allows users to send private messages to other users.

Code

function privatemsg_sql_messages(&$fragments, $threads, $account = NULL, $load_all = FALSE) {
  $fragments['primary_table'] = '{pm_index} pmi';
  $fragments['select'][] = 'pmi.mid';
  $fragments['where'][] = 'pmi.thread_id IN (' . db_placeholders($threads) . ')';
  $fragments['query_args']['where'] += $threads;
  $fragments['inner_join'][] = 'INNER JOIN {pm_message} pm ON (pm.mid = pmi.mid)';
  if ($account) {

    // Only load the user's messages.
    $fragments['where'][] = 'pmi.uid = %d';
    $fragments['query_args']['where'][] = $account->uid;
  }
  if (!$load_all) {

    // Also load deleted messages when requested.
    $fragments['where'][] = 'pmi.deleted = 0';
  }

  // Only load each mid once.
  $fragments['group_by'][] = 'pmi.mid';
  $fragments['group_by'][] = 'pm.timestamp';

  // Order by timestamp first.
  $fragments['order_by'][] = 'pm.timestamp ASC';

  // If there are multiple inserts during the same second (tests, for example)
  // sort by mid second to have them in the same order as they were saved.
  $fragments['order_by'][] = 'pmi.mid ASC';
}