function _privatemsg_send

1 privatemsg.module _privatemsg_send($message)
7-1 privatemsg.module _privatemsg_send($message)
7-2 privatemsg.module _privatemsg_send($message)
6-2 privatemsg.module _privatemsg_send($message)

Internal function to save a message.

Parameters

$message: A $message array with the data that should be saved. If a thread_id exists it will be created as a reply to an existing thread. If not, a new thread will be created.

Return value

The updated $message array.

3 calls to _privatemsg_send()
privatemsg_new_submit in ./privatemsg.pages.inc
Submit callback for the privatemsg_new form.
privatemsg_new_thread in ./privatemsg.module
Send a new message.
privatemsg_reply in ./privatemsg.module
Send a reply message

File

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

Code

function _privatemsg_send($message) {
  $transaction = db_transaction();
  try {
    drupal_alter('privatemsg_message_presave', $message);
    field_attach_presave('privatemsg_message', $message);

    $query = db_insert('pm_index')->fields(array('mid', 'thread_id', 'recipient', 'type', 'is_new', 'deleted'));
    if (isset($message->read_all) && $message->read_all && isset($message->thread_id)) {
      // The message was sent in read all mode, add the author as recipient to all
      // existing messages.
      $query_messages = _privatemsg_assemble_query('messages', array($message->thread_id), NULL);
      foreach ($query_messages->execute()->fetchCol() as $mid) {
        $query->values(array('mid' => $mid, 'thread_id' => $message->thread_id, 'recipient' => $message->author->uid, 'type' => 'user', 'is_new' => 0, 'deleted' => 0));
      }
    }

    // 1) Save the message body first.
    $args = array();
    $args['subject'] = $message->subject;
    $args['author'] = $message->author->uid;
    $args['body'] = $message->body;
    $args['format'] = $message->format;
    $args['timestamp'] = $message->timestamp;
    $args['has_tokens'] = (int) $message->has_tokens;
    if (isset($message->reply_to_mid)) {
      $args['reply_to_mid'] = $message->reply_to_mid;
    }

    $mid = db_insert('pm_message')->fields($args)->execute();
    $message->mid = $mid;

    // Thread ID is the same as the mid if it's the first message in the thread.
    if (!isset($message->thread_id)) {
      $message->thread_id = $mid;
    }

    // 2) Save message to recipients.
    // Each recipient gets a record in the pm_index table.
    foreach ($message->recipients as $recipient) {
      $query->values(array('mid' => $mid, 'thread_id' => $message->thread_id, 'recipient' => $recipient->recipient, 'type' => $recipient->type, 'is_new' => 1, 'deleted' => 0));
    }

    // We only want to add the author to the pm_index table, if the message has
    // not been sent directly to him.
    if (!isset($message->recipients['user_' . $message->author->uid])) {
      $query->values(array('mid' => $mid, 'thread_id' => $message->thread_id, 'recipient' => $message->author->uid, 'type' => 'user', 'is_new' => 0, 'deleted' => 0));
    }
    $query->execute();

    module_invoke_all('privatemsg_message_insert', $message);
    field_attach_insert('privatemsg_message', $message);

  }
  catch (Exception $exception) {
    $transaction->rollback();
    watchdog_exception('privatemsg', $exception);
    throw $exception;
  }

  // If we reached here that means we were successful at writing all messages to db.
  return $message;
}