You are here

function commentaccess_comment_presave in Comment Access 7

Implementation of hook_comment_presave()

Enforces the approval queue, shows the approval message, and optionally sends the e-mail notification to nodeauthor.

File

./commentaccess.module, line 85
Provides users with permissions for comments on nodes they own.

Code

function commentaccess_comment_presave($comment) {

  // Check if the comment needs approval.
  $requires_approval = commentaccess_requires_approval($comment);
  if ($requires_approval === NULL) {

    // Use the default message if admin approval is needed, see comment_form_submit().
    if (!user_access('administer comments')) {
      drupal_set_message(t('Your comment has been queued for review by site administrators and will be published after approval.'));
    }
    return;
  }
  if ($requires_approval) {
    $node = node_load($comment->nid);

    // Set the comment to an unpublished state
    $comment->status = COMMENT_NOT_PUBLISHED;

    // Set the approval queue message for the comment author
    $status_msg_php = variable_get('commentaccess_approval_php');
    if ($status_msg_php != '' && module_exists('php')) {
      $status_msg = php_eval($status_msg_php);
    }
    else {
      $status_msg = variable_get('commentaccess_approval_msg', "Your comment will be posted once it's been approved.");
      $status_msg = t($status_msg, array(
        '@poster' => !empty($comment->name) ? $comment->name : variable_get('anonymous', 'anonymous'),
        '@node_owner' => $node->name,
        '@subject' => $comment->subject,
      ));
    }
    drupal_set_message($status_msg);

    // Send the mail if enabled
    $nodeauthor = user_load($node->uid);
    if (!$nodeauthor) {

      // Node author does not exist. Don't send notification e-mail.
      return;
    }
    $commentaccess_settings = _commentaccess_get_account_settings($nodeauthor);

    // Get the comment body text.
    $comment_body_items = field_get_items('comment', $comment, 'comment_body');
    if (empty($comment_body_items)) {
      $comment_body_text = '';
    }
    else {
      $comment_body_item = reset($comment_body_items);
      if (empty($comment_body_item['format']) || $comment_body_item['format'] == 'plain_text') {
        $comment_body_text = $comment_body_item['value'];
      }
      else {

        // The body is in html. For usage in the e-mail convert it to plain text.
        $comment_body_text = drupal_html_to_text($comment_body_item['value']);
      }
    }
    if ($commentaccess_settings['commentaccess_email']) {
      $replacements = array(
        '@approver' => $node->name,
        '@subject' => $comment->subject,
        '@comment' => $comment_body_text,
        '@commenter' => !empty($comment->name) ? $comment->name : variable_get('anonymous', 'anonymous'),
        '@nodelink' => url('node/' . $node->nid, array(
          'absolute' => TRUE,
        )),
        '@commentlink' => url('node/' . $node->nid, array(
          'absolute' => TRUE,
          'fragment' => 'comment-' . $comment->cid,
        )),
        '@site' => variable_get("site_name", "Drupal"),
        '@siteurl' => $GLOBALS["base_url"],
      );
      $params = array(
        'replacements' => $replacements,
      );
      drupal_mail('commentaccess', 'commentaccess_email', $nodeauthor->mail, user_preferred_language($nodeauthor), $params);
    }
  }
  else {

    // No approval necessary: post comment directly!
    $comment->status = COMMENT_PUBLISHED;
  }
}