comment_notify.module 24.9 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 12 13 14
define('COMMENT_NOTIFY_DISABLED', 0);
define('COMMENT_NOTIFY_NODE', 1);
define('COMMENT_NOTIFY_COMMENT', 2);


15
define('AUTHOR_MAILTEXT',
16
'Hi [user:name],
17

18
You have received a comment on: "[node:title]"
19 20

You can view the comment at the following url
21
[comment:url]
22 23 24

You will receive emails like this for all replies to your posts. You can disable this by logging in and going to your account settings.

25 26 27
Webmaster of [site:name]
[site:mission]
[site:url]');
28

29
define('DEFAULT_MAILTEXT',
30
'Hi [user:name],
31

32
[comment:name] has commented on: "[node:title]"
33 34 35

The post is about
----
36
[node:summary]
37 38 39
----

You can view the comment at the following url
40
[comment:url]
41

42
You can stop receiving emails when someone replies to this post,
43
by going to [comment:unsubscribe_url]
44

45
If you have auto-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.
46

47
You can set up auto-following feature for all future posts
48
by creating your own user with a few clicks here [site:url]/user/register
49 50 51

Thanks for your feedback,

52 53 54 55
Webmaster of [site:name]
[site:mission]
[site:url]');

56

57

58
/**
59
 * Provide an array of available options for notification on a comment.
60
 */
61 62 63 64 65 66 67
function _comment_notify_options() {
  $total_options = array(
    COMMENT_NOTIFY_NODE     => t('All comments'),
    COMMENT_NOTIFY_COMMENT  => t('Replies to my comment')
  );

  $available_options = array();
68
  $options = variable_get('comment_notify_available_alerts', drupal_map_assoc(array(COMMENT_NOTIFY_NODE, COMMENT_NOTIFY_COMMENT)));
69 70 71 72
  foreach ($options as $key => $available) {
    if ($key == $available) {
      $available_options[$available] = $total_options[$available];
    }
73
  }
74 75

  return $available_options;
76 77
}

78 79

function comment_notify_form_comment_form_alter(&$form, &$form_state, $form_id) {
80
  
81
  global $user;
82 83 84
  if (!(user_access('subscribe to comments') || user_access('administer comments'))) {
    return;
  }
85

86 87 88 89 90 91
  // Only add the checkbox if this is an enabled content type
  $node = node_load($form['nid']['#value'] ? $form['nid']['#value'] : $form['nid']['#default_value']);
  $enabled_types = variable_get('comment_notify_node_types', drupal_map_assoc(array($node->type)));
  if (empty($enabled_types[$node->type])) {
    return;
  }
92

93
  $available_options = _comment_notify_options();
94

95 96
  drupal_add_css(drupal_get_path('module', 'comment_notify') . '/comment_notify.css');
  drupal_add_js(drupal_get_path('module', 'comment_notify') . '/comment_notify.js');
97

98 99 100
  // Add the checkbox for anonymous users and set the default based on admin settings.
  if ($user->uid == 0) {
    // If anonymous user's can't enter their e-mail don't tempt them with the checkbox.
101
    if (empty($form['author']['mail'])) {
102
      return;
103
    }
104 105 106
    
    $form['#validate'][] = 'comment_notify_comment_validate';
    
107 108 109
    $preference = variable_get('comment_notify_default_anon_mailalert', COMMENT_NOTIFY_DISABLED);
  }
  else {
110
    module_load_include('inc', 'comment_notify', 'comment_notify');
111 112
    $preference = comment_notify_get_user_comment_notify_preference($user->uid);
  }
113

114 115
  // If you want to hide this on your site see http://drupal.org/node/322482
  $form['notify_settings'] = array(
116 117
      '#prefix' => '<div class="clear-block">',
      '#suffix' => '</div>',
118
      '#weight' => 0,
119
  );
120

121
  $form['notify_settings']['notify'] = array(
122 123
      '#type' => 'checkbox',
      '#title' => t('Notify me when new comments are posted'),
124
      '#default_value' => (bool) $preference,
125
  );
126

127 128
  if (count($available_options) > 1) {
    $form['notify_settings']['notify_type'] = array(
129 130 131
        '#type' => 'radios',
        '#options' => $available_options,
        '#default_value' => $preference,
132 133 134 135
    );
  }
  else {
    $form['notify_settings']['notify_type'] = array(
136 137
        '#type' => 'hidden',
        '#value' => key($available_options),
138 139 140 141 142 143
    );
  }
  $form['notify_settings']['notify_type']['#default_value'] = $preference;

  // If this is an existing comment we set the default value based on their selection last time.
  if ($form['cid']['#value'] != '') {
144
    module_load_include('inc', 'comment_notify', 'comment_notify');
145 146 147 148
    $notify = comment_notify_get_notification_type($form['cid']['#value']);
    $form['notify_settings']['notify']['#default_value'] = $notify;
    if (count($available_options) > 1) {
      $form['notify_settings']['notify_type']['#default_value'] = $notify;
149
    }
150 151
    else {
      $form['notify_settings']['notify_type']['#default_value'] = key($available_options);
152
    }
153 154
  }
}
155

156
/**
157
 * Implements hook_permission().
158
 */
159 160 161 162 163 164 165 166 167 168 169
function comment_notify_permission() {
  return array(
    'administer comment notify' => array(
      'title' => 'Administer Comment Notify',
      'description' => 'Change global comment notification settings.',
  ),
    'subscribe to comments' => array(
      'title' => 'Subscribe to comment notifications',
      'description' => 'Subscribe to recieve notifications when new comments are posted.',
  ),
  );
170
}
171 172

/**
173
 * Implements hook_menu().
174
 */
175 176
function comment_notify_menu() {

177
  $items['admin/config/people/comment_notify'] = array(
178
    'title' => 'Comment notify',
179 180 181
    'description' => 'Configure settings for e-mails about new comments.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('comment_notify_settings'),
182
    'access arguments' => array('administer comment notify'),
183
    'type' => MENU_NORMAL_ITEM,
184
  );
185
  $items['admin/config/people/comment_notify/settings'] = array(
186 187 188 189
    'title' => 'Settings',
    'description' => 'Configure settings for e-mails about new comments.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('comment_notify_settings'),
190
    'access arguments' => array('administer comment notify'),
191 192
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
193

194
  $items['admin/config/people/comment_notify/unsubscribe'] = array(
195 196 197 198 199
    'title' => 'Unsubscribe',
    'description' => 'Unsubscribe an email from all notifications.',
    'weight' => 2,
    'page callback' => 'drupal_get_form',
    'page arguments' => array('comment_notify_unsubscribe'),
200
    'access arguments' => array('administer comment notify'),
201 202
    'type' => MENU_LOCAL_TASK,
  );
203 204 205 206
  $items['comment_notify/disable/%'] = array(
    'title' => 'Disable comment notification',
    'page callback' => 'comment_notify_disable_page',
    'page arguments' => array(2),
207 208 209
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK
  );
210 211 212 213

  return $items;
}

214 215 216
/**
 * Page callback to allow users to unsubscribe simply by visiting the page.
 */
217
function comment_notify_disable_page($hash) {
218
  module_load_include('inc', 'comment_notify', 'comment_notify');
219 220
  if (comment_notify_unsubscribe_by_hash($hash)) {
    drupal_set_message(t('Your comment follow-up notification for this post was disabled. Thanks.'));
221 222
  }
  else {
223 224
    drupal_set_message(t('Sorry, there was a problem unsubscribing from notifications.'));
  }
225
  return ' ';
226 227
}

228 229

function comment_notify_comment_validate($comment) {
230
  global $user;
231 232
  // We assume that if they are non-anonymous then they have a valid mail.
  // For anonymous users, though, we verify that they entered a mail and let comment.module validate it is real.
233
  if (!$user->uid && $comment['notify_settings']['notify']['#value'] && empty($comment['author']['mail']['#value'])) {
234 235 236
    form_set_error('mail', t('If you want to subscribe to comments you must supply a valid e-mail address.'));
  }
}
237

238 239 240 241
function comment_notify_comment_publish($comment) {
  // And send notifications - the real purpose of the module.
  _comment_notify_mailalert($comment);
}
242

243
function comment_notify_comment_update($comment) {
244 245
  module_load_include('inc', 'comment_notify', 'comment_notify');
  
246
  // In case they have changed their status, save it in the database.
247 248 249
  if (isset($comment->notify_type)) {
    comment_notify_update_notification($comment->cid, $comment->notify_type);
  }
250 251 252 253 254 255 256 257
  // And send notifications - the real purpose of the module.
  if ($comment->status == COMMENT_PUBLISHED) {
    _comment_notify_mailalert($comment);
  }

}

function comment_notify_comment_insert($comment) {
258 259
  module_load_include('inc', 'comment_notify', 'comment_notify');
  
260 261 262
  global $user;
  // For new comments, we first build up a string to be used as the identifier for the alert
  // JS: This is terribly broken.  What if the user changes their email??!?
263
  // GK: The values are stored in the table for all eternity so it doesn't matter!11!!!!! (p.s. issues are better than snarkomments)
264
  // TODO however, if this were a comment created by a service or xmlrpc or something the global $user is a bad user to use.
265 266 267
  $mail = empty($comment->mail) ? $user->mail : $comment->mail;
  $notify_hash = drupal_get_token($mail . $comment->cid);

268
  if (isset($comment->notify)) {
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
    $notify = $comment->notify_type;
    // If they don't have a preference, save one.
    $current = comment_notify_get_user_comment_notify_preference($user->uid);
    if ($current == 0 && $user->uid) {
      comment_notify_set_user_notification_setting($user->uid, NULL, $comment->notify_type);
    }
  }
  else {
    $notify = 0;
  }
  // And then save the data.
  comment_notify_add_notification($comment->cid, $notify, $notify_hash);

  // And send notifications - the real purpose of the module.
  if ($comment->status == COMMENT_PUBLISHED) {
    _comment_notify_mailalert($comment);
285 286 287
  }
}

288
function comment_notify_comment_delete($comment) {
289
  module_load_include('inc', 'comment_notify', 'comment_notify');
290 291 292 293
  comment_notify_remove_all_notifications($comment->cid);
}


294
/**
295
 * Implement hook_form_alter().
296
 */
297
function comment_notify_form_alter(&$form, &$form_state, $form_id) {
298 299
  module_load_include('inc', 'comment_notify', 'comment_notify');
    
300 301 302
  if (!($form_id == 'user_register_form' || $form_id == 'user_profile_form')) {
    return;
  }
303
  elseif ($form['#user_category'] != 'account') {
304 305 306 307 308 309 310 311 312 313
    return;
  }

  $user = $form['#user'];
  if ($user->comment_notify_settings) {
    $node_notify = $user->comment_notify_settings->node_notify;
    $comment_notify = $user->comment_notify_settings->comment_notify;
  }

  $form['comment_notify_settings'] = array(
314 315
          '#type' => 'fieldset',
          '#title' => t('Comment follow-up notification settings'),
316 317 318 319 320 321
          '#weight' => 4,          '#collapsible' => TRUE
  );

  // Only show the node followup UI if the user has permission to create nodes.
  $nodes = FALSE;
  foreach (node_type_get_names() as $type) {
322
    if (user_access('create ' . $type . ' content')) {
323 324 325 326 327 328 329
      $nodes = TRUE;
      break;
    }
  }

  if (user_access('administer nodes') || $nodes) {
    $form['comment_notify_settings']['node_notify'] = array(
330 331
            '#type' => 'checkbox',
            '#title' => t('Receive node follow-up notification e-mails'),
332
            '#default_value' => isset($node_notify) ? $node_notify : comment_notify_variable_registry_get('node_notify_default_mailalert'),
333
            '#description' => t('Check this box to receive an e-mail notification for follow-ups on your nodes (pages, forum topics, etc). You can not disable notifications for individual threads.')
334 335 336 337
    );
  }
  else {
    $form['comment_notify_settings']['node_notify'] = array(
338 339
            '#type' => 'hidden',
            '#value' => COMMENT_NOTIFY_DISABLED,
340 341
    );
  }
342

343 344 345
  $available_options[COMMENT_NOTIFY_DISABLED] = t('No notifications');
  $available_options += _comment_notify_options();
  $form['comment_notify_settings']['comment_notify'] = array(
346
          '#type' => 'select',
347
          '#title' => t('Receive comment follow-up notification e-mails'),
348
          '#default_value' => isset($comment_notify) ? array($comment_notify) : array(comment_notify_variable_registry_get('comment_notify_default_registered_mailalert')),
349
          '#options' => $available_options,
350
          '#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.")
351 352 353 354
  );
  return $form;
  // Construct the user form
}
355

356 357 358 359 360
function comment_notify_user_update(&$edit, $account, $category) {
  if ($category != 'account') {
    return;
  }
  if (isset($edit['node_notify']) && isset($edit['comment_notify'])) {
361 362
    module_load_include('inc', 'comment_notify', 'comment_notify');
    
363 364 365 366 367 368 369
    // Save the values of node_notify_mailalert and comment_notify_mailalert
    // to {comment_notify_user_settings}.
    comment_notify_set_user_notification_setting($account->uid, $edit['node_notify'], $edit['comment_notify']);
  }
  // Unset them from $user so they don't also get saved into {users}.data.
  unset($edit['node_notify']);
  unset($edit['comment_notify']);
370

371
}
372

373
function comment_notify_user_load(&$users) {
374 375
  module_load_include('inc', 'comment_notify', 'comment_notify');
  
376
  // @todo: Why would we want to load this on every user load?
377 378 379
  foreach ($users as &$user) {
    $user->comment_notify_settings = comment_notify_get_user_notification_setting($user->uid);
  }
380

381
  return;
382 383
}

384
function comment_notify_user_cancel(&$edit, $account, $method) {
385
  module_load_include('inc', 'comment_notify', 'comment_notify');
386 387 388
  comment_notify_delete_user_notification_setting($account->uid);
}

389 390
/**
 * Private function to send the notifications.
greggles's avatar
greggles committed
391
 *
392 393 394
 * @param $comment
 *   The comment array as found in hook_comment $op = publish.
 */
395
function _comment_notify_mailalert($comment) {
396 397
  module_load_include('inc', 'comment_notify', 'comment_notify');
  
398
  $comment = (object) $comment;
399
  global $language;
400
  global $base_url;
401
  global $user;
402

403 404 405 406 407 408
  $nid = $comment->nid;
  $cid = $comment->cid;

  // Check to see if a notification has already been sent for this
  // comment so that edits to a comment don't trigger an additional
  // notification.
409
  if (comment_notify_is_notification_already_sent($cid)) {
410 411 412
    return;
  }

413
  $initial_language = $language;
414

415
  if (function_exists('locale')) {
416
    $languages = locale_language_list();
417 418 419
    $languages = $languages['name'];
  }

420
  $node = node_load($nid);
421 422 423 424 425 426 427

  // No mails if this is not an enabled content type.
  $enabled_types = variable_get('comment_notify_node_types', array($node->type => TRUE));
  if (empty($enabled_types[$node->type])) {
    return;
  }

428
  if (!isset($comment->mail)) {
429
    $comment_account = user_load_by_name($comment->name);
430 431 432 433 434
    $comment_mail = $comment_account->mail;
  }
  else {
    $comment_mail = $comment->mail;
  }
435 436 437
  $sent_to = array();

  // Send to a subscribed author if they are not the current commenter
438
  $author = user_load($node->uid);
439

440
  if (!empty($author->comment_notify_settings->node_notify) && $author->node_notify_mailalert == 1 && $user->uid != $author->uid && node_access('view', $node, $author)) {
441 442
    // Get the author's language.
    $language = user_preferred_language($author);
443
    $raw_values = array(
444 445
      'subject' => comment_notify_variable_registry_get('author_subject'),
      'body'  => comment_notify_variable_registry_get('node_notify_default_mailtext'), //JS @todo:change this.
446
    );
447 448 449 450
    foreach ($raw_values as $k => $v) {
      $message[$k] = comment_notify_tokenize(t($v), $comment, $node, $user);
    }

451
    drupal_mail('comment_notify', 'comment_notify_mail', $author->mail, $language, $message);
452 453
    $sent_to[] = $author->mail;
  }
454

455
  // For "reply to my comments" notifications, figure out what thread this is.
456
  $thread = comment_notify_get_thread($cid);
457

458
  // 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
459 460
  // Get the list of commenters to notify.
  $watchers = comment_notify_get_watchers($nid);
461

462
  foreach ($watchers as $alert) {
463 464 465
    $umail = empty($alert->umail) ? $alert->uinit : $alert->umail;
    $mail = empty($alert->cmail) ? $umail : $alert->cmail;

466
    $relevant_thread = drupal_substr($thread, 0, drupal_strlen($alert->thread) -1);
467
    if ($alert->notify == COMMENT_NOTIFY_COMMENT && strcmp($relevant_thread . '/', $alert->thread) != 0) {
468
      continue;
469
    }
470

471
    if ($mail != $comment_mail && !in_array($mail, $sent_to) && ($alert->uid != $comment->uid || $alert->uid == 0)) {
472

473 474
      $message = array();
      if (!empty($alert->uid)) {
475
        $recipient_user = user_load($alert->uid);
476 477 478 479
        $language = user_preferred_language($recipient_user);
      }
      else {
        $language = language_default();
480 481 482 483 484
        $recipient_user = drupal_anonymous_user();
      }
      // Make sure they have access to this node before showing a bunch of node information.
      if (!node_access('view', $node, $recipient_user)) {
        continue;
485
      }
486

487
      $raw_values = array(
488 489
        'subject' => comment_notify_variable_registry_get('watcher_subject'),
        'body'  => comment_notify_variable_registry_get('comment_notify_default_mailtext'), //JS @todo:change this var name.
490
      );
491 492 493 494 495 496 497 498 499

      foreach ($raw_values as $k => $v) {
        $message[$k] = comment_notify_tokenize(t($v), $comment, $node, $user);
      }

      //      array(
      //          '!link1' => url('comment_notify/disable/'. $alert->notify_hash, array('absolute' => TRUE))
      //      )

500
      drupal_mail('comment_notify', 'comment_notify_mail', $mail, $language, $message);
501
      $sent_to[] = $mail;
502

503
      // Make the mail link to user's /edit, unless it's an anonymous user.
504
      if ($alert->uid != 0) {
505
        $user_mail = l($mail, 'user/' . $alert->uid . '/edit');
506 507
      }
      else {
508
        $user_mail = check_plain($mail);
509
      }
510

511
      // Add an entry to the watchdog log.
512 513 514
      watchdog(
        'comment_notify',
        'Notified: !user_mail',
515 516
      array('!user_mail' => $user_mail),
      WATCHDOG_NOTICE,
517 518
      l(t('source comment'), 'node/' . $nid, array(
          'fragment' => 'comment-' . $alert->cid,
519
      ))
520
      );
521

522
      // revert to previous (site default) locale
523
      $language = $initial_language;
524 525
    }
  }
526 527
  // Record that a notification was sent for this comment so that
  // notifications aren't sent again if the comment is later edited.
528 529

  comment_notify_mark_comment_as_notified($cid);
530 531
}

532
/**
533
 * Implements hook_mail().
534
 */
535 536
function comment_notify_mail($key, &$message, $params) {
  $message['subject'] = $params['subject'];
537
  $message['body'][] = $params['body'];
538 539
}

540
/**
541
 * Callback for an administrative form to unsubscribe users by e-mail address.
542
 */
543
function comment_notify_unsubscribe($form, &$form_state) {
544 545 546 547 548 549 550 551 552 553 554
  $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;
}
555

556
/**
557
 * Based on admin submit, do the actual unsubscribe from notifications.
558
 */
559
function comment_notify_unsubscribe_submit($form, &$form_state) {
560
  module_load_include('inc', 'comment_notify', 'comment_notify');
561
  $email = trim($form_state['values']['email_to_unsubscribe']);
562
  $comments = comment_notify_unsubscribe_by_email($email);
563 564 565 566 567 568 569 570 571
  // 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."));
  }
}
572

573 574 575
/*
 * Page callback for administrative settings form.
 */
576
function comment_notify_settings() {
577 578
  module_load_include('inc', 'comment_notify', 'comment_notify');
  
579
  $form['comment_notify_settings'] = array();
580

581 582
  // Only perform comment_notify for certain node types.
  $enabled_types = comment_notify_variable_registry_get('node_types');
583
  $anonymous_problems = '';
584
  foreach (node_type_get_names() as $type => $name) {
585 586
    $checkboxes[$type] = check_plain($name);
    $default[] = $type;
587 588

    // If they don't have the ability to leave contact info, then we make a report
589
    if (isset($enabled_types[$type]) && $enabled_types[$type] && variable_get('comment_anonymous_' . $type, COMMENT_ANONYMOUS_MAYNOT_CONTACT) == COMMENT_ANONYMOUS_MAYNOT_CONTACT) {
590
      $account = drupal_anonymous_user();
591
      if (user_access('subscribe to comments', $account)) {
592
        $anonymous_problems[] = l(t('@content-type', array('@content-type' => $name)), 'admin/structure/types/manage/'. $type);
593 594 595
      }
    }
  }
greggles's avatar
greggles committed
596

597
  if (!empty($anonymous_problems)) {
598
    drupal_set_message(t('Anonymous commenters have the permission to subscribe to comments but cannot leave their contact information on the following content types: !types.  You should either disable subscriptions on those types here, revoke the permission for anonymous users, or enable anonymous users to leave their contact information in the comment settings.', array('!types' => implode(', ', $anonymous_problems))), 'status', FALSE);
599 600 601 602
  }

  $form['comment_notify_settings']['comment_notify_node_types'] = array(
    '#type' => 'checkboxes',
603
    '#title' => t('Content types to enable for comment notification'),
604
    '#default_value' => $enabled_types,
605 606 607 608
    '#options' => $checkboxes,
    '#description' => t('Comments on content types enabled here will have the option of comment notification.'),
  );

609 610
  $form['comment_notify_settings']['comment_notify_available_alerts'] = array(
    '#type' => 'checkboxes',
611
    '#title' => t('Available subscription modes'),
612
    '#return_value' => 1,
613
    '#default_value' => comment_notify_variable_registry_get('available_alerts'),
614
    '#description' => t('Choose which notification subscription styles are available for users'),
615
    '#options' => array(
616 617 618
  COMMENT_NOTIFY_NODE     => t('All comments'),
  COMMENT_NOTIFY_COMMENT  => t('Replies to my comment')
  )
619 620
  );

621 622
  $available_options[COMMENT_NOTIFY_DISABLED] = t('No notifications');
  $available_options += _comment_notify_options();
623
  $form['comment_notify_settings']['comment_notify_default_anon_mailalert'] = array(
624
    '#type' => 'select',
625
    '#title' => t('Default state for the notification selection box for anonymous users'),
626
    '#return_value' => 1,
627
    '#default_value' => comment_notify_variable_registry_get('default_anon_mailalert'),
628 629 630 631 632
    '#options' => $available_options,
  );

  $form['comment_notify_settings']['comment_notify_default_registered_mailalert'] = array(
    '#type' => 'select',
greggles's avatar
greggles committed
633
    '#title' => t('Default state for the notification selection box for registered users'),
634
    '#return_value' => 1,
635
    '#default_value' => comment_notify_variable_registry_get('default_registered_mailalert'),
636 637
    '#description' => t('This flag presets the flag for the follow-up notification on the form that anon users will see when posting a comment'),
    '#options' => $available_options,
638 639
  );

640
  $form['comment_notify_settings']['comment_notify_node_notify_default_mailalert'] = array(
641 642
    '#type' => 'checkbox',
    '#title' => t('Subscribe users to their node follow-up notification emails by default'),
643
    '#default_value' => comment_notify_variable_registry_get('node_notify_default_mailalert'),
644 645 646
    '#description' => t('If this is checked, new users will receive e-mail notifications for follow-ups on their nodes by default until they individually disable the feature.'),
  );

647
  $token_help = "Need a theme_token_info :(";
648

649
  $form['comment_notify_settings']['comment_notify_comment_notify_default_mailtext'] = array(
650
    '#type' => 'textarea',
651
    '#title' => t('Default mail text for sending out notifications to commenters'),
652 653
    '#suffix' => $token_help,
    '#default_value' => comment_notify_variable_registry_get('comment_notify_default_mailtext'),
654 655 656 657 658
    '#return_value' => 1,
    '#cols' => 80,
    '#rows' => 15
  );

659
  $form['comment_notify_settings']['comment_notify_node_notify_default_mailtext'] = array(
660
    '#type' => 'textarea',
661
    '#title' => t('Default mail text for sending out the notifications to node authors'),
662 663
    '#suffix' => $token_help,
    '#default_value' => comment_notify_variable_registry_get('node_notify_default_mailtext'),
664 665 666 667 668
     '#return_value' => 1,
     '#cols' => 80,
     '#rows' => 15
  );

669 670
  $form['#validate'] = array('comment_notify_settings_validate');

671
  return system_settings_form($form);
672
}
673 674 675 676 677 678 679 680 681 682

function comment_notify_settings_validate($form, &$form_state) {
  $sum_enabled = 0;
  foreach ($form_state['values']['comment_notify_available_alerts'] as $enabled) {
    $sum_enabled += $enabled;
  }
  if (!$sum_enabled) {
    form_set_error('comment_notify_available_alerts', 'You must enable at least one subscription mode.');
  }
}
683 684


685
function comment_notify_tokens($type, $tokens, array $data = array(), array $options = array()) {
686 687
  module_load_include('inc', 'comment_notify', 'comment_notify');
  
688 689 690 691 692 693 694 695 696 697
  // Add the tokens for unsubscribe links
  if ($type != "comment" ||!isset($data['comment'])) {
    return;
  }
  $cid = $data['comment']->cid;
  $hash = comment_notify_get_notify_hash($cid);
  if (!$hash) {
    return;
  }
  $replacements = array();
698
  $replacements['comment:unsubscribe_url'] = url('comment_notify/disable/' . $hash, array('absolute' => TRUE));
699 700 701 702
  return $replacements;

}