You are here

function privatemsg_service_get in Privatemsg 6.2

Get all of the logged in user's private messages.

Parameters

$type: String. Which type of messages you would like to retrieve. Possibilities are inbox or sent.

$load_full: Boolean. Whether to load the full message for each message or not.

$offset: Integer (optional). An offset integer for paging.

$limit: Integer (optional). A limit integer for paging.

Return value

An array of messages.

1 string reference to 'privatemsg_service_get'
privatemsg_service_service in privatemsg_service/privatemsg_service.module
Implementation of hook_service().

File

privatemsg_service/privatemsg_service.inc, line 27
Link general private message module functionalities to services module.

Code

function privatemsg_service_get($type = 'inbox', $load_full = FALSE, $offset = 0, $limit = 0, $uid = 0) {
  $messages = array();
  $pms = array();

  // If no type passed set default type to inbox.
  if (!$type) {
    $type = 'inbox';
  }

  // User needs to be authenticated to proceed.
  global $user;
  if (!user_is_logged_in()) {
    return services_error(t('This user is not logged in.'), 403);
  }

  // If a user id other than the current user's ID is passed,
  // validate that the authenticated user has the correct
  // permissions to read another user's messages.
  if ($uid > 0 && $uid != $user->uid) {
    if (user_access("read all private messages")) {
      $account = user_load($uid);
    }
    else {
      return services_error(t('This user does not have permissions to read all messages.'), 403);
    }
  }
  else {
    $account = $user;
  }

  // Construct the query and retrieve the correct set of messages.
  $query = _privatemsg_assemble_query('list', $account, $type);

  // Handle any paging if an offset or a limit was passed in.
  if (empty($offset) && !empty($limit)) {
    $result = db_query_range($query['query'], 0, $limit);
  }
  elseif (!empty($offset) && !empty($limit)) {
    $result = db_query_range($query['query'], $offset, $limit);
  }
  elseif (!empty($offset) && empty($limit)) {
    $rows = (int) db_result(db_query($query['count']));
    $result = db_query_range($query['query'], $offset, $rows);
  }
  else {

    // No paging, we are retrieving everything.
    $result = db_query($query['query']);
  }

  // Loop the result object to get the messages.
  while ($row = db_fetch_object($result)) {

    // If the full thread should be loaded, created an array
    // using the thread_id as the index.
    if ($load_full) {
      $pms[$row->thread_id] = $row;
    }
    else {
      $pms[] = $row;
    }
  }

  // Load the full thread, if necessary.
  if (!empty($pms) && $load_full) {
    foreach ($pms as $thread_id => $msg) {
      $messages[] = privatemsg_thread_load($thread_id, $account, NULL, FALSE);
    }
  }
  else {

    // Replace array of participant user IDs (only)
    // with more information.
    $messages = _privatemsg_service_enhance_participants($pms);
  }

  // Return messages.
  return $messages;
}