You are here

function antispam_notify_moderators in AntiSpam 7

Same name and namespace in other branches
  1. 6 antispam.module \antispam_notify_moderators()

Notify moderators of new/updated content, only content needing approval or nothing at all.

Parameters

string $content_type: Content type; can be either 'node' or 'comment' .

object $content: The content object to work on.

boolean $is_published: TRUE if content is in published status.

boolean $is_spam: TRUE if content has been marked as spam.

2 calls to antispam_notify_moderators()
_antispam_comment_save in ./antispam.module
Called from hook_comment_insert() and hook_comment_update().
_antispam_node_save in ./antispam.module
Called from hook_node_insert() and hook_node_update()

File

./antispam.module, line 1332
Primary hook implementations for the Antispam module.

Code

function antispam_notify_moderators($content_type, $content, $is_published, $is_spam) {
  global $user, $base_url;

  // Proceed only if e-mail notifications are enabled.
  if (!variable_get('antispam_email_enabled', 0)) {
    return;
  }

  // Make sure we have an object.
  $content = (object) $content;

  // Compute the related moderator permission.
  if ($content_type == 'comment') {
    $moderator_permission = 'moderate spam in comments';
    $administer_permission = 'administer comments';
  }
  else {
    $moderator_types = antispam_get_moderator_types();
    $moderator_permission = 'moderate spam in nodes of type ' . $moderator_types[$content->type];
    $administer_permission = 'administer nodes';
  }

  // Obtain list of moderators of the specified content type.
  $sql = 'SELECT u.uid, u.name, u.mail, m.email_for' . ' FROM {role_permission} p' . '   INNER JOIN {users_roles} r ON r.rid = p.rid' . '   INNER JOIN {users} u ON u.uid = r.uid OR u.uid = 1' . '   LEFT JOIN {antispam_moderator} m ON m.uid = u.uid' . ' WHERE p.permission LIKE :perm1' . ' OR p.permission LIKE :perm2';
  $result = db_query($sql, array(
    ':perm1' => '%' . $moderator_permission . '%',
    ':perm2' => '%' . $administer_permission . '%',
  ));
  $moderators = array();
  foreach ($result as $u) {

    // If a moderator submit a new node/comment, then exclude them from the
    // list.
    if ($u->uid != $user->uid) {
      $moderators[$u->uid] = array(
        'name' => $u->name,
        'email_to' => $u->mail,
        'email_for' => !is_null($u->email_for) ? $u->email_for : 'approval',
      );
    }
  }

  // Check to see if we need to add the admin to the notification moderator
  // list.
  $sql = 'SELECT u.uid, u.name, u.mail, m.email_for' . ' FROM {users} u' . ' LEFT JOIN {antispam_moderator} m ON m.uid = u.uid' . ' WHERE u.uid=1';
  $u = db_query($sql)
    ->fetchObject();
  if (!empty($u)) {

    // If the admin submit a new node/comment, then exclude from the list.
    if ($u->uid != $user->uid) {
      $moderators[$u->uid] = array(
        'name' => $u->name,
        'email_to' => $u->mail,
        'email_for' => !is_null($u->email_for) ? $u->email_for : 'approval',
      );
    }
  }

  // Extract unique email addresses and ignore those who have requested to not
  // get e-mail notifications.
  $unique_emails = array();
  foreach ($moderators as $uid => $moderator) {
    if ($moderator['email_for'] == 'all' || $moderator['email_for'] == 'approval' && !$is_published) {
      if (!isset($unique_emails[$moderator['email_to']])) {
        $unique_emails[$moderator['email_to']] = $uid;
      }
    }
  }
  if (count($unique_emails) <= 0) {
    return;
  }

  // If this is about a comment, try to load the node. Also, prepare arguments
  // for notification message.
  $site_name = variable_get('site_name', t('Drupal'));
  if ($content_type == 'comment') {
    if (!($node = antispam_content_load('node', $content->nid))) {
      watchdog('antispam', 'An error has ocurred while trying to notify moderators about a comment. The associated node could not be loaded. ', array(), WATCHDOG_NOTICE, l(t('view'), 'node/' . $content->nid, array(
        'fragment' => 'comment-' . $content->cid,
      )));
      return;
    }
    $message_args = array(
      '@title-label' => t('Subject'),
      '@content-title' => $content->subject,
      '@content-type' => t('comment'),
      '!content-link' => url('node/' . $content->nid, array(
        'fragment' => 'comment-' . $content->cid,
        'absolute' => TRUE,
      )),
    );
  }
  else {
    $message_args = array(
      '@title-label' => t('Title'),
      '@content-title' => $content->title,
      '@content-type' => $moderator_types[$content->type],
      '!content-link' => url('node/' . $content->nid, array(
        'absolute' => TRUE,
      )),
    );
  }
  $message_args['@content-status'] = ($is_published ? t('published') : t('not published')) . ($is_spam ? ' (' . t('marked as spam') . ')' : '');
  $message_args['@site-name'] = $site_name;
  $message_args['!site-link'] = $base_url . base_path();
  $message_title = t('[@site-name] moderator notification - Posted @content-type \'@content-title\'', $message_args);
  $message_body = t(<<<EOT
Hello @user-name,

You can use the following information to review this @content-type:

@title-label: @content-title
URL: !content-link
Status: @content-status

Please, do not reply to this e-mail. It is an automated notification you are receiving because you are a moderator at @site-name. If you no longer wish to receive such notifications, you can change your moderator settings in your user profile.

Thank you

!site-link
EOT
, $message_args);

  // Log the notification to watchdog.
  watchdog('antispam', 'Trying to notify the following recipients: %emails', array(
    '%emails' => implode(', ', array_keys($unique_emails)),
  ));

  // Send e-mails.
  foreach ($unique_emails as $email_to => $uid) {
    $message_body = str_replace('@user-name', check_plain($moderators[$uid]['name']), $message_body);
    $params = array(
      'body' => $message_body,
      'subject' => $message_title,
    );
    drupal_mail('antispam', date("Y-m-d"), $email_to, language_default(), $params);
  }
}