You are here

function privatemsg_sql_load in Privatemsg 6.2

Same name and namespace in other branches
  1. 6 privatemsg.module \privatemsg_sql_load()

Query function for loading a single or multiple messages.

Parameters

$fragments: Query fragments array.

$pmids: Array of pmids.

$account: Account for which the messages should be loaded.

Related topics

File

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

Code

function privatemsg_sql_load(&$fragments, $pmids, $account = NULL) {
  $fragments['primary_table'] = '{pm_message} pm';
  $fragments['select'][] = "pm.mid";
  $fragments['select'][] = "pm.author";
  $fragments['select'][] = "pm.subject";
  $fragments['select'][] = "pm.body";
  $fragments['select'][] = "pm.timestamp";
  $fragments['select'][] = "pm.format";
  if ($account) {

    // The is_new only has a meaning if querying for a specific account.
    $fragments['select'][] = "pmi.is_new";
  }
  $fragments['select'][] = "pmi.thread_id";
  $fragments['inner_join'][] = 'INNER JOIN {pm_index} pmi ON pm.mid = pmi.mid';

  // Use IN() to load multiple messages at the same time.
  $fragments['where'][] = 'pmi.mid IN (' . db_placeholders($pmids) . ')';
  $fragments['query_args']['where'] += $pmids;
  if ($account) {
    $fragments['where'][] = "pmi.recipient = %d AND pmi.type IN ('user', 'hidden')";
    $fragments['query_args']['where'][] = $account->uid;
  }
  else {

    // Avoid loading the message multiple times due to the join.
    // @todo: Find a better way to do this, move thread_id to pm_message?
    $fragments['group_by'][] = "pm.mid";
    $fragments['group_by'][] = "pm.author";
    $fragments['group_by'][] = "pm.subject";
    $fragments['group_by'][] = "pm.body";
    $fragments['group_by'][] = "pm.timestamp";
    $fragments['group_by'][] = "pm.format";
    $fragments['group_by'][] = "pmi.thread_id";
  }
  $fragments['order_by'][] = 'pm.timestamp ASC';
  $fragments['order_by'][] = 'pm.mid ASC';
}