You are here

function privatemsg_message_load_multiple in Privatemsg 6.2

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

Load multiple messages.

Parameters

$pmids: Array of Message ids, pm.mid field

$account: For which account the message should be loaded. Defaults to the current user.

$reset: Reset the static cache.

Related topics

2 calls to privatemsg_message_load_multiple()
privatemsg_message_load in ./privatemsg.module
Load a single message.
privatemsg_thread_load in ./privatemsg.module
Load a thread with all the messages and participants.

File

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

Code

function privatemsg_message_load_multiple($pmids, $account = NULL, $reset = FALSE) {
  static $cache = array();
  if ($reset) {
    $cache = array();
  }

  // Avoid SQL error that would happen with an empty pm.mid IN () clause.
  if (empty($pmids)) {
    return array();
  }

  // If account is null, we need the current user's uid for caching.
  if (isset($account->uid)) {
    $uid = $account->uid;
  }
  else {
    $uid = $GLOBALS['user']->uid;
  }

  // Collect cached messages and remove any pmids that were found.
  $messages = array();
  foreach ($pmids as $key => $pmid) {
    if (isset($cache[$uid][$pmid])) {
      $messages[$pmid] = $cache[$uid][$pmid];
      unset($pmids[$key]);
    }
  }

  // Load any remaining uncached messages.
  if (!empty($pmids)) {
    $query = _privatemsg_assemble_query('load', $pmids, $account);
    $result = db_query($query['query']);
    while ($message = db_fetch_array($result)) {

      // Load author of message.
      if (!($message['author'] = privatemsg_user_load($message['author']))) {

        // If user does not exist, load anonymous user.
        $message['author'] = privatemsg_user_load(0);
      }
      $returned = module_invoke_all('privatemsg_message_load', $message);
      if (!empty($returned)) {
        $message = array_merge_recursive($returned, $message);
      }

      // Add to cache and to current result set.
      $cache[$uid][$message['mid']] = $message;
      $messages[$message['mid']] = $message;
    }
  }
  return $messages;
}