You are here

function privatemsg_rules_new in Privatemsg 6.2

Same name and namespace in other branches
  1. 7.2 privatemsg_rules/privatemsg_rules.rules.inc \privatemsg_rules_new()
  2. 7 privatemsg_rules/privatemsg_rules.rules.inc \privatemsg_rules_new()

Helper function for sending a new message.

1 string reference to 'privatemsg_rules_new'
privatemsg_rules_rules_defaults in privatemsg_rules/privatemsg_rules.rules.inc
Implements hook_rules_defaults().

File

privatemsg_rules/privatemsg_rules.rules.inc, line 383
Hooks and callback functions for rules.module integration.

Code

function privatemsg_rules_new($subject, $body, $recipient, $author) {
  rules_log(t('Writing new message with subject %subject to %user from %author', array(
    '%subject' => $subject,
    '%user' => $recipient->name,
    '%author' => $author->name,
  )));

  // Enforce type user on recipient, see http://drupal.org/node/1079644.
  $recipient->type = 'user';
  $recipient->recipient = $recipient->uid;
  $result = privatemsg_new_thread(array(
    $recipient,
  ), $subject, $body, array(
    'author' => $author,
  ));
  if ($result['success']) {
    rules_log(t('New message sucessfully sent, !link', array(
      '!link' => l($subject, 'messages/view/' . $result['message']['thread_id']),
    )));
  }
  else {
    foreach ($result['messages']['error'] as $message) {
      rules_log(t('Message not sent, reported error: !error', array(
        '!error' => $message,
      )));
    }
  }
  foreach ($result['messages']['warning'] as $message) {
    rules_log(t('Warning message reported when trying to send message: !warning', array(
      '!warning' => $message,
    )), TRUE);
  }
}