You are here

function privatemsg_message_change_recipient in Privatemsg 7

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

Add or remove a recipient to an existing message.

Parameters

$mid: Message id for which the recipient should be added.

$recipient: Recipient id that should be added, for example uid.

$type: Type of the recipient, defaults to hidden.

$add: If TRUE, adds the recipient, if FALSE, removes it.

3 calls to privatemsg_message_change_recipient()
privatemsg_cron in ./privatemsg.module
Implements hook_cron().
privatemsg_load_recipients in ./privatemsg.pages.inc
Batch processing function for rebuilding the index.
_privatemsg_handle_recipients in ./privatemsg.module
Handle the non-user recipients of a new message.

File

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

Code

function privatemsg_message_change_recipient($mid, $uid, $type = 'user', $add = TRUE) {

  // The message is statically cached, so only a single load is necessary.
  $message = privatemsg_message_load($mid);
  $thread_id = $message->thread_id;
  if ($add) {

    // Only add the recipient if he does not block the author.
    $recipient = user_load($uid);
    $context = $thread_id == $mid ? array() : array(
      'thread_id' => $thread_id,
    );
    $user_blocked = module_invoke_all('privatemsg_block_message', $message->author, array(
      privatemsg_recipient_key($recipient) => $recipient,
    ), $context);
    if (count($user_blocked) != 0) {
      return;
    }

    // Make sure to only add a recipient once. The types user and hidden are
    // considered equal here.
    $query = db_select('pm_index', 'pmi');
    $query
      ->addExpression('1');
    $exists = $query
      ->condition('mid', $mid)
      ->condition('recipient', $uid)
      ->condition('type', $type == 'user' || $type == 'hidden' ? array(
      'user',
      'hidden',
    ) : $type)
      ->execute()
      ->fetchField();
    if (!$exists) {
      db_insert('pm_index')
        ->fields(array(
        'mid' => $mid,
        'thread_id' => $thread_id,
        'recipient' => $uid,
        'type' => $type,
        'is_new' => 1,
        'deleted' => 0,
      ))
        ->execute();
    }
  }
  else {
    db_delete('pm_index')
      ->condition('mid', $mid)
      ->condition('recipient', $uid)
      ->condition('type', $type)
      ->execute();
  }
  module_invoke_all('privatemsg_message_recipient_changed', $mid, $thread_id, $uid, $type, $add);
}