You are here

function privatemsg_message_change_recipient in Privatemsg 6.2

Same name and namespace in other branches
  1. 7.2 privatemsg.module \privatemsg_message_change_recipient()
  2. 7 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.

4 calls to privatemsg_message_change_recipient()
privatemsg_cron in ./privatemsg.module
Implements hook_cron().
privatemsg_forward_form_submit in privatemsg_forward/privatemsg_forward.module
Submit function for forward form.
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 2643
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 = privatemsg_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.
    if ($type == 'user' || $type == 'hidden') {
      $exists = db_result(db_query("SELECT 1 FROM {pm_index} WHERE type IN ('user', 'hidden') AND recipient = %d AND mid = %d", $uid, $mid));
    }
    else {
      $exists = db_result(db_query("SELECT 1 FROM {pm_index} WHERE type = '%s' AND recipient = %d AND mid = %d", $type, $uid, $mid));
    }
    if (!$exists) {
      $add_sql = "INSERT INTO {pm_index} (mid, thread_id, recipient, type, is_new, deleted) VALUES (%d, %d, %d, '%s', 1, 0)";
      db_query($add_sql, $mid, $thread_id, $uid, $type);
    }
  }
  else {
    if ($type == 'hidden' || $type == 'user') {

      // If type is hidden OR user, delete both.
      $delete_sql = "DELETE FROM {pm_index} WHERE mid = %d AND thread_id = %d AND recipient = %d AND type IN ('user', 'hidden')";
    }
    else {
      $delete_sql = "DELETE FROM {pm_index} WHERE mid = %d AND thread_id = %d AND recipient = %d AND type = '%s'";
    }
    db_query($delete_sql, $mid, $thread_id, $uid, $type);
  }
  module_invoke_all('privatemsg_message_recipient_changed', $mid, $thread_id, $uid, $type, $add);
}