You are here

function privatemsg_reply in Privatemsg 6

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

Send a reply message

This functions replies on an existing thread.

Parameters

$thread_id: Thread id

$body: The body text of the new message

$options: Additional options, possible keys: author => User object of the author timestamp => Time when the message was sent

Return value

An array with a key success and messages. This key contains an array where the key is the error type (error, warning, notice) and an array with messages of that type.. If success is TRUE, it also contains a key $message with the created $message array, the same that is passed to hook_privatemsg_message_insert().

It is theoretically possible for success to be TRUE and message to be FALSE. For example if one of the privatemsg database tables become corrupted. When testing for success of message being sent it is always best to see if ['message'] is not FALSE as well as ['success'] is TRUE.

Example messages values:

array(
  'error' => array(
    'A error message',
  ),
);

Related topics

4 calls to privatemsg_reply()
PrivatemsgAPITestCase::testPrivatemsgApiReply in tests/privatemsgapi.test
PrivatemsgBlockUserCase::testBlockAndUnblock in pm_block_user/pm_block_user.test
PrivatemsgTestCase::testDelete in ./privatemsg.test
PrivatemsgTestCase::testPaging in ./privatemsg.test

File

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

Code

function privatemsg_reply($thread_id, $body, $options = array()) {
  global $user;
  $author = drupal_clone($user);
  $message = array();
  $message['body'] = $body;

  // set custom options, if any
  if (!empty($options)) {
    $message += $options;
  }

  // apply defaults
  $message += array(
    'author' => $author,
    'timestamp' => time(),
    'format' => filter_resolve_format(FILTER_FORMAT_DEFAULT),
  );

  // We don't know the subject and the recipients, so we need to load them..
  // thread_id == mid on the first message of the thread
  $first_message = privatemsg_message_load($thread_id, $message['author']);
  if (!$first_message) {
    return array(
      t('Thread %thread_id not found, unable to answer', array(
        '%thread_id' => $thread_id,
      )),
    );
  }
  $message['thread_id'] = $thread_id;

  // Load participants.
  $message['recipients'] = _privatemsg_load_thread_participants($thread_id);

  // Remove author.
  if (isset($message['recipients'][$message['author']->uid]) && count($message['recipients']) > 1) {
    unset($message['recipients'][$message['author']->uid]);
  }
  $message['subject'] = $first_message['subject'];
  $validated = _privatemsg_validate_message($message);
  if ($validated['success']) {
    $validated['message'] = _privatemsg_send($message);
  }
  return $validated;
}