comment_notify.module 15.6 KB
Newer Older
1 2 3 4 5
<?php
// $Id$

/**
 * @file
6 7
 *
 * This module provides comment follow-up e-mail notification for anonymous and registered users.
8 9
 */

10 11
define('DEFAULT_MAILTEXT',
'Hi !name,
12

13
!commname has commented on: "!node_title"
14 15 16

The post is about
----
17
!node_teaser
18 19 20
----

You can view the comment at the following url
21
!comment_url
22

23 24
You can stop receiving emails when someone replies to this blog post,
by going to !link1
25 26 27

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.

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

Thanks for your feedback,

33 34 35
Webmaster of !site
!mission
!uri');
36 37 38 39 40 41 42

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

48
/**
49 50 51
 * Insert our checkbox, and populate fields.
 * set validation hook.
 */
52
function comment_notify_form_alter($form_id, &$form) {
53 54
  global $user;

55 56 57
  if ($form_id != 'comment_form') {
    return;
  }
58 59 60 61

  $op = isset($_POST['op']) ? $_POST['op'] : '';

  if ($op == t('Preview comment')) {
62
    drupal_set_message(t('ATTENTION: Your comment is NOT YET posted - please click the post button to confirm your post'));
63 64 65 66

    //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(
67 68 69 70 71
        '#type' => 'fieldset',
        '#title' => t('Comment is not posted yet - please click post button to confirm your post'),
        '#weight' => -99,
        '#collapsible' => FALSE
      );
72

73 74 75 76 77
      $form['submitextra']['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Post comment'),
        '#weight' => -20
      );
78 79 80
    }
  }

81
  if ($user->uid == 0) {
82 83
    $form['notify'] = array(
      '#type' => 'checkbox',
84
      '#title' => t('Notify me of follow-up comments posted here.'),
85 86 87 88 89 90 91 92 93
      '#default_value' => ($user->uid != 0) ? $user->comment_notify_mailalert : variable_get('comment_notify_default_anon_mailalert', TRUE),
    );
  }
  elseif (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),
      '#description' => t('You can change the default for this field in "Comment follow-up notification settings" on <a href="!uri">your account edit page</a>', array('!uri' => url('user/'. $user->uid .'/edit'))),
94 95 96
    );
  }
  else {
97
    $form['notify'] = array(
98 99
      '#type' => 'hidden',
      '#title' => t('Mail me updates to this comment.'),
100
      '#default_value' => $user->comment_notify_mailalert,
101 102 103
    );
  }

104
  if ($form['cid']['#value'] != '') {
105 106
    $notify = db_result(db_query("SELECT notify FROM {comment_notify} WHERE cid = %d", $form['cid']['#value']));
    $form['notify']['#default_value'] = $notify;
107 108
  }
}
109

110 111 112 113 114 115
/**
 * Implementation of hook_perm
 */
function comment_notify_perm() {
  return array('Administer comment notify');
}
116 117 118 119 120 121 122 123 124

/**
 * Implementation of hook_menu().
 */
function comment_notify_menu($may_cache) {
  $items = array();
  global $user;

  if ($may_cache) {
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
    $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,
    );
152 153
  }
  else {
154 155 156 157 158 159 160
    $items[] = array(
      'path' => 'comment_notify',
      'title' => t('comment notify') ,
      'callback' => 'comment_notify_page',
      'access' => 1,
      'type' => MENU_CALLBACK
    );
161 162 163 164 165 166 167 168
  }
  return $items;
}

function comment_notify_page() {
  global $user;

  $breadcrumb = NULL;
169 170
  $op = $_POST['op'];
  $edit = $_POST['edit'];
171

172
  $page_content = ' ';
173 174 175 176 177 178 179
  if (empty($op)) {
    $op = arg(1);
  }
  $arg = arg(2);

  switch ($op) {
    case 'disable':
180
      $key = $arg;
181
      db_query("UPDATE {comment_notify} c SET c.notify = 0 WHERE notify_hash = '%s'", $arg);
182 183 184

      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.');
185 186 187 188 189
      break;
    default;
      $title = t('Comment Notify');
      break;
  }
190

191 192
  drupal_set_title($title);
  drupal_set_breadcrumb($breadcrumb);
193
  print theme('page', $page_content);
194 195
}

196
/**
197 198
 * save our data.
 */
199
function comment_notify_validate($form_id, $form_values) {
200 201 202 203 204

  if ($form_values['optin']) {
    foreach (array('optin') as $field) {
      $_SESSION['comment_notify'][$field] = $form_values[$field];
    }
205 206
  }
  else {
207 208 209 210 211 212
    foreach (array('optin') as $field) {
      unset($_SESSION['comment_notify'][$field]);
    }
  }
}

213 214
/**
 * implement hook_comment and check the publish status
215 216 217 218
 */
function comment_notify_comment($comment, $op) {
  global $user;

219
  switch ($op) {
220
    case 'publish':
221 222 223
      _comment_notify_mailalert($comment);
      break;
    case 'update':
224
      $sql = 'UPDATE {comment_notify} SET notify = %d WHERE cid = %d';
225
      db_query($sql, $comment['notify'], $comment['cid']);
226
      break;
227 228 229 230 231 232 233

    case 'insert':
      $mail = empty($comment['mail']) ? $user->mail : $comment['mail'];
      $md5_string = $mail . $user->uid . $comment['name'] . $comment['nid'];
      $sql = "INSERT INTO {comment_notify} (cid, notify, notify_hash) values (%d, %d, '%s')";
      db_query($sql, $comment['cid'], $comment['notify'], md5($md5_string));
      break;
234 235 236 237 238 239 240 241 242
  }
}

/**
 * Implementation of hook_user().
 */
function comment_notify_user($type, &$edit, &$user, $category = NULL) {
  switch ($type) {
    case 'form':
243
      if ($category == 'account') {
244 245
        $form = array();
        $form['comment_notify_settings'] = array(
246 247 248 249 250 251
          '#type' => 'fieldset',
          '#title' => t('Comment follow-up notification settings'),
          '#weight' => 4,
          '#collapsible' => TRUE
        );

252
        $form['comment_notify_settings']['comment_notify_mailalert'] = array(
253 254
          '#type' => 'checkbox',
          '#title' => t('Receive comment follow-up notification e-mails'),
255
          '#default_value' => isset($edit['comment_notify_mailalert']) ? $edit['comment_notify_mailalert'] : 0,
256 257
          '#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.')
        );
258 259 260

        return $form;
      }
261

262 263 264 265
      break;
  }
}

266 267
function _comment_notify_mailalert($comment) {
  $comment = (object) $comment;
268 269 270 271
  global $locale;
  global $base_url;

  $initial_locale = $locale;
272

273 274 275 276 277
  if (function_exists('locale')) {
    $languages = locale_supported_languages();
    $languages = $languages['name'];
  }

278 279 280 281 282 283 284 285 286 287 288 289 290
  $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;
  }
291

292
  $from = variable_get('site_mail', ini_get('sendmail_from'));
293 294 295
  $result = db_query('SELECT DISTINCT c.cid, u.init, c.uid, c.name, c.nid, c.mail cmail, u.mail umail, u.init uinit, c.uid, c.name, cn.notify_hash mymd5
    FROM {comments} c INNER JOIN {comment_notify} cn on c.cid = cn.cid LEFT OUTER JOIN {users} u ON c.uid = u.uid
    WHERE nid = %d  AND cn.notify = 1 AND c.status = 0', $nid
296
  );
297
  // TODO? the original big query had stuff making sure the mail was populated and contained .+@.+ Perhaps check for that here and set notify = 0 if that is the case for this cid
298

299 300
  $count = 0;
  $sent_to = array();
301

302
  while ($alert = db_fetch_object($result)) {
303 304 305 306
    $umail = empty($alert->umail) ? $alert->uinit : $alert->umail;
    $mail = empty($alert->cmail) ? $umail : $alert->cmail;

    if ($mail != $comment_mail && !in_array($mail, $sent_to) && $alert->uid != $comment->uid) {
307 308 309 310

      if (function_exists('locale') && $languages[$user->language]) {
        $locale = $user->language;
      }
311

312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
      $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)
        )
      );

335
      drupal_mail('comment_notify_mail', $mail, $subject, $message, $from, array());
336
      $count++;
337
      $sent_to[] = $mail;
338

339 340
      if ($alert->uid != 0) {
        $watchdog_message = t('Notified: <a href="!url">@user_mail</a>', 
341
          array('!url' => url('user/'. $alert->uid .'/edit'), '@user_mail' => $mail)) ;
342

343 344
      }
      else {
345
        $watchdog_message = t('Notified @user_mail', array('@user_mail' => $mail));
346
      }
347

348 349 350
      // 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));
351

352 353
      // revert to previous (site default) locale
      $locale = $initial_locale;
354 355 356 357
    }
  }
}

358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
/**
 * 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;
}
373

374 375 376 377 378 379 380 381
/** 
 * 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) {
382 383
    $comments = db_result(db_query("SELECT COUNT(1) FROM {comments} c INNER JOIN {comment_notify} cn ON c.cid = cn.cid WHERE c.uid = %d AND cn.notify = 1", $result));
    db_query("UPDATE {comment_notify} cn INNER JOIN {comments} c ON cn.cid = c.cid SET cn.notify = 0 WHERE c.uid = %d", $result);
384 385
  }
  else {
386 387
    $comments = db_result(db_query("SELECT COUNT(1) FROM {comments} c INNER JOIN {comment_notify} cn ON c.cid = cn.cid WHERE c.mail = '%s' AND cn.notify = 1", $email));
    db_query("UPDATE {comment_notify} cn INNER JOIN {comments} c ON cn.cid = c.cid SET cn.notify = 0 WHERE c.mail = '%s'", $email);
388 389 390 391 392 393 394 395 396 397
  }
  // 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."));
  }
}
398

399 400 401
/*
 * Page callback for administrative settings form.
 */
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
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:
425
      <ul>
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
      <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);
451
}