You are here

function privatemsg_reply in Privatemsg 7.2

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

7 calls to privatemsg_reply()
PrivatemsgAPITestCase::testPrivatemsgApiReply in ./privatemsg.test
PrivatemsgBlockUserCase::testBlockAndUnblock in pm_block_user/pm_block_user.test
PrivatemsgTestCase::testDelete in ./privatemsg.test
PrivatemsgTestCase::testDisablePrivatemsg in ./privatemsg.test
Test functionality around disabling private messaging.
PrivatemsgTestCase::testPaging in ./privatemsg.test

... See full list

File

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

Code

function privatemsg_reply($thread_id, $body, $options = array()) {
  global $user;
  $author = clone $user;
  $message = (object) $options;
  $message->body = $body;

  // Apply defaults - this will not overwrite existing keys.
  if (!isset($message->author)) {
    $message->author = $author;
  }
  if (!isset($message->timestamp)) {
    $message->timestamp = REQUEST_TIME;
  }
  if (!isset($message->format)) {
    $message->format = filter_default_format($message->author);
  }

  // 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(
      'success' => FALSE,
      'messages' => array(
        'error' => array(
          t('Thread %thread_id not found, unable to answer.', array(
            '%thread_id' => $thread_id,
          )),
        ),
      ),
    );
  }

  // Add the reply_to_mid, so we can mark that message as replied
  $thread = privatemsg_thread_load($thread_id);
  if (empty($thread)) {
    return array(
      'success' => FALSE,
      'messages' => array(
        'error' => array(
          t('A thread was not returned for ID %thread_id. The user might not be allowed to participate in it.', array(
            '%thread_id' => $thread_id,
          )),
        ),
      ),
    );
  }
  else {
    $message->reply_to_mid = end($thread['messages'])->mid;
  }
  $message->thread_id = $thread_id;

  // Load participants
  $message->recipients = _privatemsg_load_thread_participants($thread_id, $message->author);
  $message->subject = $first_message->subject;
  $validated = _privatemsg_validate_message($message);
  if ($validated['success']) {
    $validated['message'] = _privatemsg_send($message);
    if ($validated['message'] !== FALSE) {
      _privatemsg_handle_recipients($validated['message']->mid, $validated['message']->recipients, FALSE);
    }
  }
  return $validated;
}