You are here

function _privatemsg_send in Privatemsg 6.2

Same name and namespace in other branches
  1. 5.3 privatemsg.module \_privatemsg_send()
  2. 5 privatemsg.module \_privatemsg_send()
  3. 6 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()
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 1823
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, recipient, type, is_new, deleted) VALUES (%d, %d, %d, '%s', %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, 'user', 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->recipient, $recipient->type, 1)) {

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

  // 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])) {
    if (!db_query($index_sql, $mid, $message['thread_id'], $message['author']->uid, 'user', 0)) {
      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;
}