You are here

function commons_notify_comment_insert in Drupal Commons 7.3

Implements hook_comment_insert().

File

modules/commons/commons_notify/commons_notify.module, line 74

Code

function commons_notify_comment_insert($comment) {
  $account = user_load($comment->uid);
  $node = node_load($comment->nid);
  $message_type = 'commons_notify_comment_created';
  $hook = 'comment_insert';
  if (!isset($node->og_group_ref[LANGUAGE_NONE][0]['target_id'])) {
    $message_type = 'commons_notify_comment_created_no_groups';
  }
  drupal_alter('commons_notify_message_selection', $message_type, $hook, $comment);
  $message = message_create($message_type, array(
    'uid' => $account->uid,
    'timestamp' => $comment->created,
  ));

  // Save reference to the node in the node reference field, and the
  // "publish" state (i.e. if the node is published or unpublished).
  $wrapper = entity_metadata_wrapper('message', $message);
  $wrapper->field_target_nodes[] = $node;
  $wrapper->field_target_comments[] = $comment;

  // The message should be published only if the node and the comment are
  // both published.
  // @todo: Deal with message publishing/unpublishing.

  /*
  $published = $node->status && $comment->status;
  $wrapper->field_published->set($published);
  */
  $wrapper
    ->save();
  $options = array(
    'rendered fields' => array(
      'message_notify_email_subject' => 'field_message_rendered_subject',
      'message_notify_email_body' => 'field_message_rendered_body',
    ),
  );
  message_subscribe_send_message('comment', $comment, $message, array(
    'email' => $options,
  ));
}