You are here

function privatemsg_rules_new_role in Privatemsg 7.2

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

Helper function for sending a new message to a role.

File

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

Code

function privatemsg_rules_new_role($args, $element) {
  $args['subject'] = privatemsg_rules_replace_tokens($args['subject']);
  $args['body'] = privatemsg_rules_replace_tokens($args['body']);
  $recipient_array = array();
  $recipient_names = array();
  foreach ($args['roles'] as $rid) {
    $recipient_array[$rid] = user_role_load($rid);
    $recipient_array[$rid]->type = 'role';
    $recipient_array[$rid]->recipient = $recipient_array[$rid]->rid;
    $recipient_names[] = $recipient_array[$rid]->name;
  }
  $recipient_names = implode(', ', $recipient_names);
  rules_log('Writing new message with subject %subject to roles %roles from %author.', array(
    '%subject' => $args['subject'],
    '%author' => $args['author']->name,
    '%roles' => $recipient_names,
  ));
  $result = privatemsg_new_thread($recipient_array, $args['subject'], $args['body'], $args);
  if ($result['success']) {
    rules_log('New message successfully sent, !link.', array(
      '!link' => l($args['subject'], 'messages/view/' . $result['message']->thread_id),
    ));
  }
  else {
    foreach ($result['messages']['error'] as $message) {
      rules_log('Message not sent, reported error: !error.', array(
        '!error' => $message,
      ), RulesLog::ERROR);
    }
  }
  foreach ($result['messages']['warning'] as $message) {
    rules_log('Warning message reported when trying to send message: !warning.', array(
      '!warning' => $message,
    ), RulesLog::WARN);
  }

  // Return thread ID of new thread (or NULL if failed).
  if ($result['success']) {
    return array(
      'thread_id' => $result['message']->thread_id,
    );
  }
  else {
    return array(
      'thread_id' => NULL,
    );
  }
}