You are here

comment_notify.module in Comment Notify 5

This module provides comment follow-up e-mail notification for anonymous and registered users.

File

comment_notify.module
View source
<?php

/**
 * @file
 *
 * This module provides comment follow-up e-mail notification for anonymous and registered users.
 */
define('DEFAULT_MAILTEXT', 'Hi !name,

!commname has commented on: "!node_title"

The post is about
----
!node_teaser
----

You can view the comment at the following url
!comment_url

You can stop receiving emails when someone replies to this blog post,
by going to !link1

If you have auto-blog-following enabled in your account, you will receive emails like this for all replies to a blog post you commented on. You can disable this by logging in and going to your account settings or unchecking the flag at the time you post the comment.

You can set up blog-following feature for all future posts
by creating your own user with a few clicks here !uri/user/register

Thanks for your feedback,

Webmaster of !site
!mission
!uri');

/**
 * Implementation of hook_help().
 */
function comment_notify_help($section) {
  switch ($section) {
    case 'admin/modules#description':
      return t('Comment follow-up e-mail notification for anonymous and registered users.');
      break;
  }
}

/**
 * Insert our checkbox, and populate fields.
 * set validation hook.
 */
function comment_notify_form_alter($form_id, &$form) {
  global $user;
  if ($form_id != 'comment_form') {
    return;
  }
  $op = isset($_POST['op']) ? $_POST['op'] : '';
  if ($op == t('Preview comment')) {
    drupal_set_message(t('ATTENTION: Your comment is NOT YET posted - please click the post button to confirm your post'));

    //extra submit button on top
    if (!form_get_errors() && (variable_get('comment_preview', COMMENT_PREVIEW_REQUIRED) == COMMENT_PREVIEW_OPTIONAL || $op == t('Preview comment') || $op == t('Post comment'))) {
      $form['submitextra'] = array(
        '#type' => 'fieldset',
        '#title' => t('Comment is not posted yet - please click post button to confirm your post'),
        '#weight' => -99,
        '#collapsible' => FALSE,
      );
      $form['submitextra']['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Post comment'),
        '#weight' => -20,
      );
    }
  }
  if ($user->uid == 0 || variable_get('comment_notify_regged_checkbox', TRUE)) {
    $form['notify'] = array(
      '#type' => 'checkbox',
      '#title' => t('Notify me of follow-up comments posted here.'),
      '#default_value' => $user->uid != 0 ? $user->comment_notify_mailalert : variable_get('comment_notify_default_anon_mailalert', TRUE),
    );
  }
  else {
    $form['notify'] = array(
      '#type' => 'hidden',
      '#title' => t('Mail me updates to this comment.'),
      '#default_value' => $user->comment_notify_mailalert,
    );
  }
  if ($form['cid']['#value'] != '') {
    $form['notify']['#default_value'] = $form['#parameters'][1]['notify'];
  }
}

/**
 * Implementation of hook_perm
 */
function comment_notify_perm() {
  return array(
    'Administer comment notify',
  );
}

/**
 * Implementation of hook_menu().
 */
function comment_notify_menu($may_cache) {
  $items = array();
  global $user;
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/settings/comment_notify',
      'title' => t('Comment Notify'),
      'description' => t('Configure settings for e-mails about new comments.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'comment_notify_settings',
      ),
      'access' => user_access('Administer comment notify'),
    );
    $items[] = array(
      'path' => 'admin/settings/comment_notify/settings',
      'title' => t('Settings'),
      'callback' => 'drupal_get_form',
      'description' => t('Configure settings for e-mails about new comments.'),
      'callback arguments' => array(
        'comment_notify_settings',
      ),
      'access' => user_access('Administer comment notify'),
      'type' => MENU_DEFAULT_LOCAL_TASK,
    );
    $items[] = array(
      'path' => 'admin/settings/comment_notify/unsubscribe',
      'title' => t('Unsubscribe'),
      'weight' => 2,
      'callback' => 'drupal_get_form',
      'description' => t('Unsubscribe an email from all notifications.'),
      'callback arguments' => array(
        'comment_notify_unsubscribe',
      ),
      'access' => user_access('Administer comment notify'),
      'type' => MENU_LOCAL_TASK,
    );
  }
  else {
    $items[] = array(
      'path' => 'comment_notify',
      'title' => t('comment notify'),
      'callback' => 'comment_notify_page',
      'access' => 1,
      'type' => MENU_CALLBACK,
    );
  }
  return $items;
}
function comment_notify_page() {
  global $user;
  $breadcrumb = NULL;
  $op = $_POST['op'];
  $edit = $_POST['edit'];
  $page_content = ' ';
  if (empty($op)) {
    $op = arg(1);
  }
  $arg = arg(2);
  switch ($op) {
    case 'disable':
      $key = $arg;
      db_query("UPDATE {comments} c, {users} u\n          SET c.notify = 0\n        WHERE u.uid = c.uid\n          AND md5(concat(c.mail, ifnull(u.mail, u.init), c.uid, c.name, c.nid)) = '%s'", $arg);
      drupal_set_message(t('Your comment follow-up notification for this post was disabled. Thanks.'));
      $title = t('Disabled comment follow-up notification feature for this post.');
      break;
    default:
      $title = t('Comment Notify');
      break;
  }
  drupal_set_title($title);
  drupal_set_breadcrumb($breadcrumb);
  print theme('page', $page_content);
}

/**
 * save our data.
 */
function comment_notify_validate($form_id, $form_values) {
  if ($form_values['optin']) {
    foreach (array(
      'optin',
    ) as $field) {
      $_SESSION['comment_notify'][$field] = $form_values[$field];
    }
  }
  else {
    foreach (array(
      'optin',
    ) as $field) {
      unset($_SESSION['comment_notify'][$field]);
    }
  }
}

/**
 * implement hook_comment and check the publish status
 */
function comment_notify_comment($comment, $op) {
  global $user;
  switch ($op) {
    case 'publish':
      _comment_notify_mailalert($comment);
      break;
    case 'update':
    case 'insert':
      $sql = 'UPDATE {comments} set notify = %d where cid = %d';
      db_query($sql, $comment['notify'], $comment['cid']);
      break;
  }
}

/**
 * Implementation of hook_user().
 */
function comment_notify_user($type, &$edit, &$user, $category = NULL) {
  switch ($type) {
    case 'form':
      if ($category == 'account') {
        $form = array();
        $form['comment_notify_settings'] = array(
          '#type' => 'fieldset',
          '#title' => t('Comment follow-up notification settings'),
          '#weight' => 4,
          '#collapsible' => TRUE,
        );
        $form['comment_notify_settings']['comment_notify_mailalert'] = array(
          '#type' => 'checkbox',
          '#title' => t('Receive comment follow-up notification e-mails'),
          '#default_value' => isset($edit['comment_notify_mailalert']) ? $edit['comment_notify_mailalert'] : 1,
          '#description' => t('Check this box to receive e-mail notification for follow-up comments to comments you posted. You can later disable this on a post-by-post basis... so if you leave this to YES, you can still disable follow-up notifications for comments you don\'t want follow-up mails anymore - i.e. for very popular posts.'),
        );
        return $form;
      }
      break;
  }
}
function _comment_notify_mailalert($comment) {
  $comment = (object) $comment;
  global $locale;
  global $base_url;
  $initial_locale = $locale;
  if (function_exists('locale')) {
    $languages = locale_supported_languages();
    $languages = $languages['name'];
  }
  $nid = $comment->nid;
  $cid = $comment->cid;
  $commname = $comment->name;
  $commtext = $comment->comment;
  $commsubj = $comment->subject;
  $node = node_load($nid);
  if (!isset($comment->mail)) {
    $comment_account = user_load(array(
      'name' => $commname,
    ));
    $comment_mail = $comment_account->mail;
  }
  else {
    $comment_mail = $comment->mail;
  }
  $from = variable_get('site_mail', ini_get('sendmail_from'));
  $result = db_query('SELECT DISTINCT c.cid, u.init, c.uid, c.name, c.nid, IF(length(c.mail) < 1, ifnull(u.mail, u.init), c.mail) mail, c.uid, c.name, max(cid) cid, md5(concat(c.mail, ifnull(u.mail, u.init), c.uid, c.name, c.nid)) mymd5
    FROM {comments} c LEFT OUTER JOIN {users} u ON u.uid = c.uid
    WHERE nid = %d  AND notify = 1 AND c.status = 0
      AND LENGTH(IF(LENGTH(c.mail) < 1, ifnull(u.mail, u.init), c.mail)) > 1
      AND IF(LENGTH(c.mail) < 1, ifnull(u.mail, u.init), c.mail) like \'%@%.%\'
    GROUP BY IF(LENGTH(c.mail) < 1, ifnull(u.mail, u.init), c.mail), c.name', $nid);
  $count = 0;
  $sent_to = array();
  while ($alert = db_fetch_object($result)) {
    if ($alert->mail != $comment_mail && !in_array($alert->mail, $sent_to) && $alert->uid != $comment->uid) {
      if (function_exists('locale') && $languages[$user->language]) {
        $locale = $user->language;
      }
      $subject = t('!site :: new comment for your post.', array(
        '!site' => variable_get('site_name', 'drupal'),
      ));
      $message = t(variable_get('comment_notify_default_mailtext', DEFAULT_MAILTEXT), array(
        '!commname' => $commname,
        '!commtext' => $commtext,
        '!commsubj' => $commsubj,
        '!comment_url' => url('node/' . $nid, NULL, NULL, 1) . '#comment-' . $cid,
        '!node_title' => $node->title,
        '!node_teaser' => $node->teaser,
        '!mission' => variable_get('site_mission', ''),
        '!node_body' => $node->body,
        '!name' => $alert->name,
        '!site' => variable_get('site_name', 'drupal'),
        '!uri' => $base_url,
        '!uri_brief' => substr($base_url, strlen('http://')),
        '!date' => format_date(time()),
        '!login_uri' => url('user', NULL, NULL, 1),
        '!edit_uri' => url('user/' . $alert->uid . '/edit', NULL, NULL, 1),
        '!link1' => url('comment_notify/disable/' . $alert->mymd5, NULL, NULL, 1),
      ));
      drupal_mail('comment_notify_mail', $alert->mail, $subject, $message, $from, array());
      $count++;
      $sent_to[] = $alert->mail;
      if ($alert->uid != 0) {
        $watchdog_message = t('Notified: <a href="!url">@user_mail</a>', array(
          '!url' => url('user/' . $alert->uid . '/edit'),
          '@user_mail' => $alert->mail,
        ));
      }
      else {
        $watchdog_message = t('Notified @user_mail', array(
          '@user_mail' => $alert->mail,
        ));
      }

      // Add an entry to the watchdog log.
      watchdog('comment_notify', $watchdog_message, WATCHDOG_NOTICE, l(t('source comment'), 'node/' . $nid, NULL, NULL, 'comment-' . $alert->cid));

      // revert to previous (site default) locale
      $locale = $initial_locale;
    }
  }
}

/**
 * Callback for a form to unsubscribe users.
 */
function comment_notify_unsubscribe() {
  $form['comment_notify_unsubscribe'] = array();
  $form['comment_notify_unsubscribe']['email_to_unsubscribe'] = array(
    '#type' => 'textfield',
    '#title' => t('Email to unsubscribe'),
  );
  $form['comment_notify_unsubscribe']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Unsubscribe this e-mail'),
  );
  return $form;
}

/** 
 * Actual unsubscribe of users. 
 */
function comment_notify_unsubscribe_submit($form_id, $form_values) {
  $email = trim($form_values['email_to_unsubscribe']);

  // If they have a uid, use that, otherwise update comments directly
  $result = db_result(db_query_range("SELECT uid FROM {users} WHERE mail = '%s'", $email, 0, 1));
  if ($result > 0) {
    $comments = db_result(db_query("SELECT COUNT(1) FROM {comments} WHERE uid = %d AND notify = 1", $result));
    db_query("UPDATE {comments} SET notify = 0 WHERE uid = %d", $result);
  }
  else {
    $comments = db_result(db_query("SELECT COUNT(1) FROM {comments} WHERE mail = '%s' AND notify = 1", $email));
    db_query("UPDATE {comments} SET notify = 0 WHERE mail = '%s'", $email);
  }

  // Update the admin about the state of this comment notification subscription.
  if ($comments == 0) {
    drupal_set_message(t("There were no active comment notifications for that email."));
  }
  else {
    drupal_set_message(format_plural($comments, "Email unsubscribed from 1 comment notification.", "Email unsubscribed from @count comment notifications."));
  }
}
function comment_notify_settings() {
  $form['comment_notify_settings'] = array();
  $form['comment_notify_settings']['comment_notify_regged_checkbox'] = array(
    '#type' => 'checkbox',
    '#title' => t('Let registered users select notification on a node basis'),
    '#return_value' => 1,
    '#default_value' => variable_get('comment_notify_regged_checkbox', TRUE),
    '#description' => t('letting registered users select/unselect if they want to be notified. If this is disabled, this setting is propagated from their user profile, hence reducing and clutter and confusion'),
  );
  $form['comment_notify_settings']['comment_notify_default_anon_mailalert'] = array(
    '#type' => 'checkbox',
    '#title' => t('Fill checkbox for follow-up with YES by default for anonymous users, so they will get an email alert for follow-up comments if they do not uncheck it then'),
    '#return_value' => 1,
    '#default_value' => variable_get('comment_notify_default_anon_mailalert', TRUE),
    '#description' => t('This flag presets the flag for the follow-up notification on the form that anon users will see when posting a comment'),
  );
  $form['comment_notify_settings']['comment_notify_default_mailtext'] = array(
    '#type' => 'textarea',
    '#title' => t('Default mail text for sending out the notifications.'),
    '#description' => t('You can use the following variables to be replaced:
      <ul>
      <li>!commname = the username who posted the comment
      <li>!commtext = the text of the posted comment
      <li>!commsubj = the subject of the posted comment
      <li>!comment_url = the full url of the post and comment - note: if you have paging enabled, this does not work correct - set your max comments per page so that all fit on one page or reverse order them
      <li>!node_title = the title of the node that was commented on
      <li>!node_teaser = the teaser of the node that was commented on
      <li>!node_body = the body of the node that was commented on
      <li>!mission = site_mission text
      <li>!name = username receiving the alert
      <li>!site = your site
      <li>!uri = base_url of site
      <li>!uri_brief = base_url of site w/o http
      <li>!date = the current time
      <li>!login_uri  uri to login the user
      <li>!edit_uri = uri to edit user profile
      <li>!link1 the QUICKLINK to disable future follow-up otifications for the user
      </ul>'),
    '#default_value' => variable_get('comment_notify_default_mailtext', t(DEFAULT_MAILTEXT)),
    '#return_value' => 1,
    '#cols' => 80,
    '#rows' => 15,
  );
  return system_settings_form($form);
}

Functions

Namesort descending Description
comment_notify_comment implement hook_comment and check the publish status
comment_notify_form_alter Insert our checkbox, and populate fields. set validation hook.
comment_notify_help Implementation of hook_help().
comment_notify_menu Implementation of hook_menu().
comment_notify_page
comment_notify_perm Implementation of hook_perm
comment_notify_settings
comment_notify_unsubscribe Callback for a form to unsubscribe users.
comment_notify_unsubscribe_submit Actual unsubscribe of users.
comment_notify_user Implementation of hook_user().
comment_notify_validate save our data.
_comment_notify_mailalert

Constants

Namesort descending Description
DEFAULT_MAILTEXT @file