comment_notify.module 24.3 KB
Newer Older
1 2 3 4
<?php

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

9 10 11 12 13
define('COMMENT_NOTIFY_DISABLED', 0);
define('COMMENT_NOTIFY_NODE', 1);
define('COMMENT_NOTIFY_COMMENT', 2);


14
define('AUTHOR_MAILTEXT',
15
'Hi [comment:node:author],
16

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

19 20 21 22 23
----
[comment:title]
[comment:body]
----

24
You can view the comment at the following url
25
[comment:url]
26

27 28
You will receive emails like this for all replies to your posts. You can
disable this by logging in and changing the settings on your user account at
29
[comment:node:author:edit-url].
30

31
-- [site:name] team
32
[site:url]');
33

34
define('DEFAULT_MAILTEXT',
35
'Hi [comment-subscribed:author],
36

37
[comment:author] has commented on: "[comment:node:title]"
38 39

----
40 41
[comment:title]
[comment:body]
42 43 44
----

You can view the comment at the following url
45
[comment:url]
46

47
You can stop receiving emails when someone replies to this post,
48
by going to [comment-subscribed:unsubscribe-url]
49

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

53
-- [site:name] team
54 55
[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
  // Add the checkbox for anonymous users.
99 100
  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
    $form['#validate'][] = 'comment_notify_comment_validate';
105
  }
106 107
  module_load_include('inc', 'comment_notify', 'comment_notify');
  $preference = comment_notify_get_user_comment_notify_preference($user->uid);
108

109 110
  // If you want to hide this on your site see http://drupal.org/node/322482
  $form['notify_settings'] = array(
111
    '#weight' => 0,
112
  );
113

114
  $form['notify_settings']['notify'] = array(
115 116 117
    '#type' => 'checkbox',
    '#title' => t('Notify me when new comments are posted'),
    '#default_value' => (bool) $preference,
118
  );
119

120 121 122 123 124 125 126 127
  $form['notify_settings']['notify_type'] = array(
    '#type' => 'radios',
    '#options' => $available_options,
    '#default_value' => $preference,
  );
  if (count($available_options) == 1) {
    $form['notify_settings']['notify_type']['#type'] = 'hidden';
    $form['notify_settings']['notify_type']['#value'] = key($available_options);
128
  }
129 130 131

  // Otherwise, the submit buttons will jump below the node preview.
  $form['actions']['#weight'] = 19;
132 133 134

  // If this is an existing comment we set the default value based on their selection last time.
  if ($form['cid']['#value'] != '') {
135
    module_load_include('inc', 'comment_notify', 'comment_notify');
136 137 138 139
    $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;
140
    }
141 142
    else {
      $form['notify_settings']['notify_type']['#default_value'] = key($available_options);
143
    }
144 145
  }
}
146

147
/**
148
 * Implements hook_permission().
149
 */
150 151 152 153 154 155 156 157 158 159 160
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.',
  ),
  );
161
}
162 163

/**
164
 * Implements hook_menu().
165
 */
166 167
function comment_notify_menu() {

168
  $items['admin/config/people/comment_notify'] = array(
169
    'title' => 'Comment notify',
170 171 172
    'description' => 'Configure settings for e-mails about new comments.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('comment_notify_settings'),
173
    'access arguments' => array('administer comment notify'),
174
    'type' => MENU_NORMAL_ITEM,
175
  );
176
  $items['admin/config/people/comment_notify/settings'] = array(
177 178 179 180
    'title' => 'Settings',
    'description' => 'Configure settings for e-mails about new comments.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('comment_notify_settings'),
181
    'access arguments' => array('administer comment notify'),
182 183
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
184

185
  $items['admin/config/people/comment_notify/unsubscribe'] = array(
186 187 188 189 190
    'title' => 'Unsubscribe',
    'description' => 'Unsubscribe an email from all notifications.',
    'weight' => 2,
    'page callback' => 'drupal_get_form',
    'page arguments' => array('comment_notify_unsubscribe'),
191
    'access arguments' => array('administer comment notify'),
192 193
    'type' => MENU_LOCAL_TASK,
  );
194 195 196 197
  $items['comment_notify/disable/%'] = array(
    'title' => 'Disable comment notification',
    'page callback' => 'comment_notify_disable_page',
    'page arguments' => array(2),
198 199 200
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK
  );
201 202 203 204

  return $items;
}

205 206 207
/**
 * Page callback to allow users to unsubscribe simply by visiting the page.
 */
208
function comment_notify_disable_page($hash) {
209
  module_load_include('inc', 'comment_notify', 'comment_notify');
210 211
  if (comment_notify_unsubscribe_by_hash($hash)) {
    drupal_set_message(t('Your comment follow-up notification for this post was disabled. Thanks.'));
212 213
  }
  else {
214 215
    drupal_set_message(t('Sorry, there was a problem unsubscribing from notifications.'));
  }
216
  return ' ';
217 218
}

219
function comment_notify_comment_validate($comment) {
220
  global $user;
221 222
  // 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.
223
  if (!$user->uid && $comment['notify_settings']['notify']['#value'] && empty($comment['author']['mail']['#value'])) {
224 225 226
    form_set_error('mail', t('If you want to subscribe to comments you must supply a valid e-mail address.'));
  }
}
227

228 229 230 231
function comment_notify_comment_publish($comment) {
  // And send notifications - the real purpose of the module.
  _comment_notify_mailalert($comment);
}
232

233 234 235
/**
 * Implements hook_comment_update().
 */
236
function comment_notify_comment_update($comment) {
237
  module_load_include('inc', 'comment_notify', 'comment_notify');
238

239
  // In case they have changed their status, save it in the database.
240 241 242
  if (isset($comment->notify_type)) {
    comment_notify_update_notification($comment->cid, $comment->notify_type);
  }
243 244 245 246 247 248 249 250
  // And send notifications - the real purpose of the module.
  if ($comment->status == COMMENT_PUBLISHED) {
    _comment_notify_mailalert($comment);
  }

}

function comment_notify_comment_insert($comment) {
251
  module_load_include('inc', 'comment_notify', 'comment_notify');
252

253
  global $user;
254 255 256 257
  // For new comments, we first build up a string to be used as the identifier for the alert.
  // This identifier is used to later unsubscribe the user or allow them to
  // potentially edit their comment / preferences if they are anonymous.
  // The string is built from their mail and comment identifier.
258 259 260
  $mail = empty($comment->mail) ? $user->mail : $comment->mail;
  $notify_hash = drupal_get_token($mail . $comment->cid);

261
  if (!empty($comment->notify)) {
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
    $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);
278 279 280
  }
}

281
function comment_notify_comment_delete($comment) {
282
  module_load_include('inc', 'comment_notify', 'comment_notify');
283 284 285 286
  comment_notify_remove_all_notifications($comment->cid);
}


287
/**
288
 * Implement hook_form_alter().
289
 */
290
function comment_notify_form_alter(&$form, &$form_state, $form_id) {
291
  module_load_include('inc', 'comment_notify', 'comment_notify');
292

293 294 295
  if (!($form_id == 'user_register_form' || $form_id == 'user_profile_form')) {
    return;
  }
296
  elseif ($form['#user_category'] != 'account') {
297 298 299 300 301 302 303 304 305 306
    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(
307 308 309 310
    '#type' => 'fieldset',
    '#title' => t('Comment follow-up notification settings'),
    '#weight' => 4,
    '#collapsible' => TRUE
311 312 313 314
  );

  // Only show the node followup UI if the user has permission to create nodes.
  $nodes = FALSE;
315 316
  foreach (node_type_get_names() as $type => $name) {
    if (node_access('create', $type)) {
317 318 319 320 321 322 323
      $nodes = TRUE;
      break;
    }
  }

  if (user_access('administer nodes') || $nodes) {
    $form['comment_notify_settings']['node_notify'] = array(
324 325 326 327
      '#type' => 'checkbox',
      '#title' => t('Receive node follow-up notification e-mails'),
      '#default_value' => isset($node_notify) ? $node_notify : comment_notify_variable_registry_get('node_notify_default_mailalert'),
      '#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.')
328 329 330 331
    );
  }
  else {
    $form['comment_notify_settings']['node_notify'] = array(
332 333
      '#type' => 'hidden',
      '#value' => COMMENT_NOTIFY_DISABLED,
334 335
    );
  }
336

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

350 351 352 353 354
function comment_notify_user_update(&$edit, $account, $category) {
  if ($category != 'account') {
    return;
  }
  if (isset($edit['node_notify']) && isset($edit['comment_notify'])) {
355
    module_load_include('inc', 'comment_notify', 'comment_notify');
356

357 358 359 360 361 362 363
    // 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']);
364

365
}
366

367
function comment_notify_user_load($users) {
368
  module_load_include('inc', 'comment_notify', 'comment_notify');
369

370
  // @todo: Why would we want to load this on every user load?
371 372 373
  foreach ($users as &$user) {
    $user->comment_notify_settings = comment_notify_get_user_notification_setting($user->uid);
  }
374

375
  return;
376 377
}

378
function comment_notify_user_cancel(&$edit, $account, $method) {
379
  module_load_include('inc', 'comment_notify', 'comment_notify');
380 381 382
  comment_notify_delete_user_notification_setting($account->uid);
}

383 384
/**
 * Private function to send the notifications.
greggles's avatar
greggles committed
385
 *
386 387 388
 * @param $comment
 *   The comment array as found in hook_comment $op = publish.
 */
389
function _comment_notify_mailalert($comment) {
390
  module_load_include('inc', 'comment_notify', 'comment_notify');
391

392
  $comment = (object) $comment;
393
  global $language;
394
  global $base_url;
395
  global $user;
396
  $initial_language = $language;
397

398 399 400 401 402 403
  $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.
404
  if (comment_notify_is_notification_already_sent($cid)) {
405 406 407
    return;
  }

408
  $node = node_load($nid);
409 410 411 412 413 414 415

  // 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;
  }

416
  if (empty($comment->mail)) {
417
    $comment_account = user_load_by_name($comment->name);
418 419 420 421 422
    $comment_mail = $comment_account->mail;
  }
  else {
    $comment_mail = $comment->mail;
  }
423 424 425
  $sent_to = array();

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

428
  if (!empty($author->comment_notify_settings->node_notify) && $author->comment_notify_settings->node_notify == 1 && $user->uid != $author->uid && node_access('view', $node, $author)) {
429 430
    // Get the author's language.
    $language = user_preferred_language($author);
431
    $raw_values = array(
432 433
      'subject' => comment_notify_variable_registry_get('author_subject'),
      'body'  => comment_notify_variable_registry_get('node_notify_default_mailtext'), //JS @todo:change this.
434
    );
435
    foreach ($raw_values as $k => $v) {
436
      $message[$k] = token_replace(t($v), array('comment' => $comment));
437 438
    }

439
    drupal_mail('comment_notify', 'comment_notify_mail', $author->mail, $language, $message);
440 441
    $sent_to[] = $author->mail;
  }
442

443
  // For "reply to my comments" notifications, figure out what thread this is.
444
  $thread = comment_notify_get_thread($cid);
445

446
  // 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
447 448
  // Get the list of commenters to notify.
  $watchers = comment_notify_get_watchers($nid);
449

450
  foreach ($watchers as $alert) {
451 452 453 454
    // If the user is not anonymous, always load the current e-mail address
    // from his or her user account instead of trusting $comment->mail.
    $recipient_user = !empty($alert->uid) ? user_load($alert->uid) : drupal_anonymous_user();
    $mail = !empty($recipient_user->mail) ? $recipient_user->mail : $alert->cmail;
455

456
    $relevant_thread = drupal_substr($thread, 0, drupal_strlen($alert->thread) -1);
457
    if ($alert->notify == COMMENT_NOTIFY_COMMENT && strcmp($relevant_thread . '/', $alert->thread) != 0) {
458
      continue;
459
    }
460

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

463
      $message = array();
464 465
      $language = !empty($alert->uid) ? user_preferred_language($recipient_user) : language_default();

466 467 468
      // Make sure they have access to this node before showing a bunch of node information.
      if (!node_access('view', $node, $recipient_user)) {
        continue;
469
      }
470

471
      $raw_values = array(
472 473
        'subject' => comment_notify_variable_registry_get('watcher_subject'),
        'body'  => comment_notify_variable_registry_get('comment_notify_default_mailtext'), //JS @todo:change this var name.
474
      );
475 476

      foreach ($raw_values as $k => $v) {
477
        $message[$k] = token_replace(t($v), array('comment' => $comment, 'comment-subscribed' => $alert));
478 479
      }

480
      drupal_mail('comment_notify', 'comment_notify_mail', $mail, $language, $message);
481
      $sent_to[] = $mail;
482

483
      // Make the mail link to user's /edit, unless it's an anonymous user.
484
      if ($alert->uid != 0) {
485
        $user_mail = l($mail, 'user/' . $alert->uid . '/edit');
486 487
      }
      else {
488
        $user_mail = check_plain($mail);
489
      }
490

491
      // Add an entry to the watchdog log.
492 493
      watchdog(
        'comment_notify',
494 495 496 497 498 499
        'Notified: @user_mail',
        array('@user_mail' => $user_mail),
          WATCHDOG_NOTICE,
          l(t('source comment'), 'node/' . $nid, array(
            'fragment' => 'comment-' . $alert->cid,
          ))
500
      );
501

502
      // Revert to previous (site default) locale.
503
      $language = $initial_language;
504 505
    }
  }
506 507
  // Record that a notification was sent for this comment so that
  // notifications aren't sent again if the comment is later edited.
508 509

  comment_notify_mark_comment_as_notified($cid);
510 511
}

512
/**
513
 * Implements hook_mail().
514
 */
515 516
function comment_notify_mail($key, &$message, $params) {
  $message['subject'] = $params['subject'];
517
  $message['body'][] = $params['body'];
518 519
}

520
/**
521
 * Callback for an administrative form to unsubscribe users by e-mail address.
522
 */
523
function comment_notify_unsubscribe($form, &$form_state) {
524 525 526 527 528 529 530 531 532 533 534
  $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;
}
535

536
/**
537
 * Based on admin submit, do the actual unsubscribe from notifications.
538
 */
539
function comment_notify_unsubscribe_submit($form, &$form_state) {
540
  module_load_include('inc', 'comment_notify', 'comment_notify');
541
  $email = trim($form_state['values']['email_to_unsubscribe']);
542
  $comments = comment_notify_unsubscribe_by_email($email);
543 544 545 546 547 548 549 550 551
  // 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."));
  }
}
552

553 554 555
/*
 * Page callback for administrative settings form.
 */
556
function comment_notify_settings() {
557
  module_load_include('inc', 'comment_notify', 'comment_notify');
558

559
  $form['comment_notify_settings'] = array();
560

561 562
  // Only perform comment_notify for certain node types.
  $enabled_types = comment_notify_variable_registry_get('node_types');
563
  $anonymous_problems = '';
564
  foreach (node_type_get_names() as $type => $name) {
565 566
    $checkboxes[$type] = check_plain($name);
    $default[] = $type;
567 568

    // If they don't have the ability to leave contact info, then we make a report
569
    if (isset($enabled_types[$type]) && $enabled_types[$type] && variable_get('comment_anonymous_' . $type, COMMENT_ANONYMOUS_MAYNOT_CONTACT) == COMMENT_ANONYMOUS_MAYNOT_CONTACT) {
570
      $account = drupal_anonymous_user();
571
      if (user_access('subscribe to comments', $account)) {
572
        $anonymous_problems[] = l(t('@content-type', array('@content-type' => $name)), 'admin/structure/types/manage/'. $type);
573 574 575
      }
    }
  }
greggles's avatar
greggles committed
576

577
  if (!empty($anonymous_problems)) {
578
    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);
579 580 581 582
  }

  $form['comment_notify_settings']['comment_notify_node_types'] = array(
    '#type' => 'checkboxes',
583
    '#title' => t('Content types to enable for comment notification'),
584
    '#default_value' => $enabled_types,
585 586 587 588
    '#options' => $checkboxes,
    '#description' => t('Comments on content types enabled here will have the option of comment notification.'),
  );

589 590
  $form['comment_notify_settings']['comment_notify_available_alerts'] = array(
    '#type' => 'checkboxes',
591
    '#title' => t('Available subscription modes'),
592
    '#return_value' => 1,
593
    '#default_value' => comment_notify_variable_registry_get('available_alerts'),
594
    '#description' => t('Choose which notification subscription styles are available for users'),
595
    '#options' => array(
596 597 598
  COMMENT_NOTIFY_NODE     => t('All comments'),
  COMMENT_NOTIFY_COMMENT  => t('Replies to my comment')
  )
599 600
  );

601 602
  $available_options[COMMENT_NOTIFY_DISABLED] = t('No notifications');
  $available_options += _comment_notify_options();
603
  $form['comment_notify_settings']['comment_notify_default_anon_mailalert'] = array(
604
    '#type' => 'select',
605
    '#title' => t('Default state for the notification selection box for anonymous users'),
606
    '#return_value' => 1,
607
    '#default_value' => comment_notify_variable_registry_get('default_anon_mailalert'),
608 609 610 611 612
    '#options' => $available_options,
  );

  $form['comment_notify_settings']['comment_notify_default_registered_mailalert'] = array(
    '#type' => 'select',
greggles's avatar
greggles committed
613
    '#title' => t('Default state for the notification selection box for registered users'),
614
    '#return_value' => 1,
615
    '#default_value' => comment_notify_variable_registry_get('default_registered_mailalert'),
616 617
    '#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,
618 619
  );

620
  $form['comment_notify_settings']['comment_notify_node_notify_default_mailalert'] = array(
621 622
    '#type' => 'checkbox',
    '#title' => t('Subscribe users to their node follow-up notification emails by default'),
623
    '#default_value' => comment_notify_variable_registry_get('node_notify_default_mailalert'),
624 625 626
    '#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.'),
  );

627
  $form['comment_notify_settings']['comment_notify_comment_notify_default_mailtext'] = array(
628
    '#type' => 'textarea',
629
    '#title' => t('Default mail text for sending out notifications to commenters'),
630
    '#default_value' => comment_notify_variable_registry_get('comment_notify_default_mailtext'),
631 632
    '#return_value' => 1,
    '#cols' => 80,
633 634 635
    '#rows' => 15,
    '#token_types' => array('comment'),
    '#element_validate' => array('token_element_validate'),
636 637
  );

638
  $form['comment_notify_settings']['comment_notify_node_notify_default_mailtext'] = array(
639
    '#type' => 'textarea',
640
    '#title' => t('Default mail text for sending out the notifications to node authors'),
641
    '#default_value' => comment_notify_variable_registry_get('node_notify_default_mailtext'),
642 643
     '#return_value' => 1,
     '#cols' => 80,
644 645 646
     '#rows' => 15,
     '#token_types' => array('comment'),
     '#element_validate' => array('token_element_validate'),
647 648
  );

649 650 651 652 653
  $form['comment_notify_settings']['token_help'] = array(
    '#theme' => 'token_tree',
    '#token_types' => array('comment'),
  );

654 655
  $form['#validate'] = array('comment_notify_settings_validate');

656
  return system_settings_form($form);
657
}
658 659 660 661 662 663 664 665 666 667

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.');
  }
}
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686

/**
 * Get the unsubscribe link for a comment subscriber.
 *
 * @param $comment
 *   The subscribed comment object.
 *
 * @return
 *   A string with the internal path to the unsubscribe link, ready to be
 *   passed to the url() function.
 *
 * @see comment_notify_get_notify_hash().
 */
function comment_notify_get_unsubscribe_url($comment) {
  module_load_include('inc', 'comment_notify', 'comment_notify');
  if ($hash = comment_notify_get_notify_hash($comment->cid)) {
    return 'comment_notify/disable/' . $hash;
  }
}