You are here

function _privatemsg_send in Privatemsg 6

Same name and namespace in other branches
  1. 5.3 privatemsg.module \_privatemsg_send()
  2. 5 privatemsg.module \_privatemsg_send()
  3. 6.2 privatemsg.module \_privatemsg_send()
  4. 7.2 privatemsg.module \_privatemsg_send()
  5. 7 privatemsg.module \_privatemsg_send()

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()
pm_send in ./privatemsg.module
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 1854
Allows users to send private messages to other users.

Code

function _privatemsg_send($message) {
  drupal_alter('privatemsg_message_presave', $message);
  $index_sql = "INSERT INTO {pm_index} (mid, thread_id, uid, is_new, deleted) VALUES (%d, %d, %d, %d, 0)";
  if (isset($message['read_all']) && $message['read_all']) {

    // 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);
    $conversation = db_query($query_messages['query']);
    while ($result = db_fetch_array($conversation)) {
      if (!db_query($index_sql, $result['mid'], $message['thread_id'], $message['author']->uid, 0)) {
        return FALSE;
      }
    }
  }

  // 1) Save the message body first.
  $args = array();
  $args[] = $message['subject'];
  $args[] = $message['author']->uid;
  $args[] = $message['body'];
  $args[] = $message['format'];
  $args[] = $message['timestamp'];
  $message_sql = "INSERT INTO {pm_message} (subject, author, body, format, timestamp) VALUES ('%s', %d, '%s', %d, %d)";
  db_query($message_sql, $args);
  $mid = db_last_insert_id('pm_message', 'mid');
  $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) {
    if (!db_query($index_sql, $mid, $message['thread_id'], $recipient->uid, 1)) {

      // We assume if one insert failed then the rest may fail too against the
      // same table.
      return FALSE;
    }
  }

  // When author is also the recipient, we want to set message to UNREAD.
  // All other times the message is set to READ.
  $is_new = isset($message['recipients'][$message['author']->uid]) ? 1 : 0;

  // Also add a record for the author to the pm_index table.
  if (!db_query($index_sql, $mid, $message['thread_id'], $message['author']->uid, $is_new)) {
    return FALSE;
  }
  module_invoke_all('privatemsg_message_insert', $message);

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