You are here

function answers_notifications_create_message in Answers 7.4

Creates new message with given parameters.

Parameters

string $type: Type of the message.

int $uid: User ID, to which to assign message.

object $node: Node which is used to get the context and list of subscribers.

array $fields: Values to place in corresponding fields.

array $uids: User IDs array. It's used in case of a single recipient for the message.

1 call to answers_notifications_create_message()
answers_notifications_new_answer_notify in answers_notifications/answers_notifications.module
Notifies all question subscribers if new answer is posted.

File

answers_notifications/answers_notifications.module, line 54

Code

function answers_notifications_create_message($type, $uid, $node, array $fields = array(), array $uids = array()) {

  // Create message.
  $message = message_create($type, array(
    'uid' => $uid,
  ));
  $wrapper = entity_metadata_wrapper('message', $message);

  // Set necessary fields to use with tokens.
  foreach ($fields as $name => $value) {
    $wrapper->{$name}
      ->set($value);
  }
  $message
    ->save();

  // Add parameters in order to avoid creating message for every subscriber.
  $notify_options = array(
    'internal' => array(
      'save on fail' => FALSE,
      'save on success' => FALSE,
      'mid' => $message->mid,
    ),
  );
  $subscribe_options = array(
    'save message' => FALSE,
    'author' => $uid,
  );
  if (!empty($uids)) {
    $subscribe_options['uids'] = $uids;
  }
  return message_subscribe_send_message('node', $node, $message, $notify_options, $subscribe_options);
}