comment_notify.module 26.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 15 16 17 18 19 20 21 22 23
define('AUTHOR_MAILTEXT',
'Hi !name,

You have received a comment on: "!node_title"

You can view the comment at the following url
!comment_url

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.

Webmaster of !site
!mission
!uri');

24 25
define('DEFAULT_MAILTEXT',
'Hi !name,
26

27
!commname has commented on: "!node_title"
28 29 30

The post is about
----
31
!node_teaser
32 33 34
----

You can view the comment at the following url
35
!comment_url
36

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

40
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.
41

42
You can set up auto-following feature for all future posts
43
by creating your own user with a few clicks here !uri/user/register
44 45 46

Thanks for your feedback,

47 48 49
Webmaster of !site
!mission
!uri');
50

51 52 53 54
define('COMMENT_NOTIFY_DISABLED', 0);
define('COMMENT_NOTIFY_NODE', 1);
define('COMMENT_NOTIFY_COMMENT', 2);

55 56 57
/**
 * Implementation of hook_help().
 */
58 59
function comment_notify_help($path, $arg) {
  switch ($path) {
60
    case 'admin/modules#description':
61
      return t('Comment follow-up e-mail notification for anonymous and registered users.');
62 63 64 65
      break;
  }
}

66
/**
67
 * Insert our checkbox, add a submit button, and populate fields.
68
 */
69
function comment_notify_form_alter(&$form, &$form_state, $form_id) {
70 71
  global $user;

72
  // Only do alter the form if it's a comment form and the user has the permission to subscribe
73
  if ($form_id != 'comment_form' || !user_access('subscribe to comments')) {
74 75
    return;
  }
76

77 78
  // Only add the checkbox if this is an enabled content type
  $node = node_load($form['nid']['#value']);
79 80
  $enabled_types = variable_get('comment_notify_node_types', array($node->type => TRUE));
  if (empty($enabled_types[$node->type])) {
81
    return;
82
  }
83

84 85 86
  drupal_add_css(drupal_get_path('module', 'comment_notify') .'/comment_notify.css');
  drupal_add_js(drupal_get_path('module', 'comment_notify') .'/comment_notify.js');

87
  $total_options = array(
88 89
    COMMENT_NOTIFY_NODE     => t('All comments'),
    COMMENT_NOTIFY_COMMENT  => t('Replies to my comment')
90
  );
91 92
  $options = variable_get('comment_notify_available_alerts', array(COMMENT_NOTIFY_NODE, COMMENT_NOTIFY_COMMENT));
  foreach ($options as $key => $available) {
93
    if ($key == $available) {
94 95 96
      $available_options[$available] = $total_options[$available];
    }
  }
97

98
  // Add the checkbox for anonymous users and set the default based on admin settings.
99
  if ($user->uid == 0) {
100
    // If anonymous user's can't enter their e-mail don't tempt them with the checkbox.
101 102 103
    if (empty($form['mail'])) {
      return;
    }
104
    $preference = variable_get('comment_notify_default_anon_mailalert', COMMENT_NOTIFY_DISABLED);
105
  }
106 107
  else {
    $user_preference = db_result(db_query("SELECT comment_notify FROM {comment_notify_user_settings} WHERE uid = %d", $user->uid));
108
    $preference = !empty($user_preference) ? $user_preference : variable_get('comment_notify_default_registered_mailalert', COMMENT_NOTIFY_DISABLED);
109 110
  }

111
  // If you want to hide this on your site see http://drupal.org/node/322482
112
  $form['notify_open'] = array('#value' => '<div id="comment-notify">');
113
  $form['notify'] = array(
114 115 116 117 118
    '#type' => 'checkbox',
    '#title' => t('Notify me when new comments are posted'),
    '#default_value' => $preference,
  );

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

133
  // If this is an existing comment we set the default value based on their selection last time.
134
  if ($form['cid']['#value'] != '') {
135 136
    $notify = db_result(db_query("SELECT notify FROM {comment_notify} WHERE cid = %d", $form['cid']['#value']));
    $form['notify']['#default_value'] = $notify;
137
    $form['notify_type']['#default_value'] = $notify;
138
  }
139
  $form['notify_close'] = array('#value' => '</div>', '#weight' => 9);
140
}
141

142
/**
143
 * Implementation of hook_perm().
144 145
 */
function comment_notify_perm() {
146
  return array('administer comment notify', 'subscribe to comments');
147
}
148 149 150 151

/**
 * Implementation of hook_menu().
 */
152 153 154
function comment_notify_menu() {

  $items['admin/settings/comment_notify'] = array(
155
    'title' => 'Comment notify',
156 157 158
    'description' => 'Configure settings for e-mails about new comments.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('comment_notify_settings'),
159
    'access arguments' => array('administer comment notify'),
160
    'type' => MENU_NORMAL_ITEM,
161
  );
162
  $items['admin/settings/comment_notify/settings'] = array(
163 164 165 166
    'title' => 'Settings',
    'description' => 'Configure settings for e-mails about new comments.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('comment_notify_settings'),
167
    'access arguments' => array('administer comment notify'),
168 169
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
170

171 172 173 174 175 176
  $items['admin/settings/comment_notify/unsubscribe'] = array(
    'title' => 'Unsubscribe',
    'description' => 'Unsubscribe an email from all notifications.',
    'weight' => 2,
    'page callback' => 'drupal_get_form',
    'page arguments' => array('comment_notify_unsubscribe'),
177
    'access arguments' => array('administer comment notify'),
178 179 180 181 182 183 184 185
    'type' => MENU_LOCAL_TASK,
  );
  $items['comment_notify'] = array(
    'title' => 'comment notify',
    'page callback' => 'comment_notify_page',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK
  );
186 187 188 189

  return $items;
}

190 191 192
/**
 * Page callback to allow users to unsubscribe simply by visiting the page.
 */
193 194 195
function comment_notify_page() {
  global $user;

196 197
  $op = $_POST['op'];
  $edit = $_POST['edit'];
198

199
  $page_content = ' ';
200 201 202 203 204 205 206
  if (empty($op)) {
    $op = arg(1);
  }
  $arg = arg(2);

  switch ($op) {
    case 'disable':
207
      $key = $arg;
208
      db_query("UPDATE {comment_notify} SET notify = 0 WHERE notify_hash = '%s'", $arg);
209 210 211

      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.');
212 213
      break;
    default;
214
      $title = t('Comment notify');
215 216
      break;
  }
217

218
  drupal_set_title($title);
219
  return $page_content;
220 221
}

222
/**
223
 * Implementation of hook_comment().
224 225 226 227
 */
function comment_notify_comment($comment, $op) {
  global $user;

228 229
  // In theory, the update or insert operations are duplicates with publish which
  // would lead to duplicate messages.  _comment_notify_mailalert() protects against that.
230
  switch ($op) {
231
    case 'validate':
232 233
       // 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.
234 235 236 237
      if (!$user->uid && $comment['notify'] && empty($comment['mail'])) {
        form_set_error('mail', t('If you want to subscribe to comments you must supply a valid e-mail address.'));
      }
      break;
238 239 240 241
    case 'publish':
      // And send notifications - the real purpose of the module.
      _comment_notify_mailalert($comment);
      break;
242
    case 'update':
243
      // In case they have changed their status, save it in the database.
244
      $sql = 'UPDATE {comment_notify} SET notify = %d WHERE cid = %d';
245 246 247 248 249 250
      if ($comment['notify']) {
        db_query($sql, $comment['notify_type'], $comment['cid']);
      }
      else {
        db_query($sql, 0, $comment['cid']);
      }
251 252 253 254
      // And send notifications - the real purpose of the module.
      if ($comment['status'] == COMMENT_PUBLISHED) {
        _comment_notify_mailalert($comment);
      }
255
      break;
256
    case 'insert':
257
      // For new comments, we first build up a string to be used as the identifier for the alert
258
      $mail = empty($comment['mail']) ? $user->mail : $comment['mail'];
259
      $notify_hash = drupal_get_token($mail . $comment['cid']);
260 261 262 263

      if ($comment['notify']) {
        $notify = $comment['notify_type'];
        $current = db_result(db_query("SELECT count(1) from {comment_notify_user_settings} WHERE uid = %d", $user->uid));
264
        if ($current == 0 && $user->uid) {
265 266 267 268 269 270
          db_query("INSERT INTO {comment_notify_user_settings} (uid, comment_notify) VALUES (%d, %d)", $user->uid, $comment['notify_type']);
        }
      }
      else {
        $notify = $comment['notify'];
      }
271
      // And then save the data.
272 273
      db_query("INSERT INTO {comment_notify} (cid, notify, notify_hash) values (%d, %d, '%s')", $comment['cid'], $notify, $notify_hash);

274 275 276 277
      // And send notifications - the real purpose of the module.
      if ($comment['status'] == COMMENT_PUBLISHED) {
        _comment_notify_mailalert($comment);
      }
278
      break;
279 280 281
    case 'delete':
      db_query("DELETE FROM {comment_notify} WHERE cid = %d", $comment->cid);
      break;
282 283 284 285 286 287 288 289 290
  }
}

/**
 * Implementation of hook_user().
 */
function comment_notify_user($type, &$edit, &$user, $category = NULL) {
  switch ($type) {
    case 'form':
291
      if ($category == 'account' && user_access('subscribe to comments', $user)) {
292 293
        $form = array();
        $form['comment_notify_settings'] = array(
294 295 296 297 298 299
          '#type' => 'fieldset',
          '#title' => t('Comment follow-up notification settings'),
          '#weight' => 4,
          '#collapsible' => TRUE
        );

300 301 302 303 304 305 306
        $form['comment_notify_settings']['node_notify_mailalert'] = array(
          '#type' => 'checkbox',
          '#title' => t('Receive node follow-up notification e-mails'),
          '#default_value' => isset($edit['node_notify_mailalert']) ? $edit['node_notify_mailalert'] : FALSE,
          '#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.')
        );

307
        $form['comment_notify_settings']['comment_notify_mailalert'] = array(
308
          '#type' => 'select',
309
          '#title' => t('Receive comment follow-up notification e-mails'),
310
          '#default_value' => isset($edit['comment_notify_mailalert']) ? $edit['comment_notify_mailalert'] : variable_get('comment_notify_default_registered_mailalert', COMMENT_NOTIFY_DISABLED),
311 312
          '#options' => array(
            COMMENT_NOTIFY_DISABLED => t('No notifications'),
313 314
            COMMENT_NOTIFY_NODE     => t('All comments'),
            COMMENT_NOTIFY_COMMENT  => t('Replies to my comment')
315
          ),
316
          '#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.")
317
        );
318 319
        return $form;
      }
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
      break;

    case 'submit':
      // Save the values of node_notify_mailalert and comment_notify_mailalert
      // to {comment_notify_user_settings}.
      if (db_result(db_query('SELECT uid FROM {comment_notify_user_settings} WHERE uid = %d', $user->uid))) {
        db_query('UPDATE {comment_notify_user_settings} SET node_notify = %d, comment_notify = %d WHERE uid = %d', $edit['node_notify_mailalert'], $edit['comment_notify_mailalert'], $user->uid);
      }
      else {
        db_query('INSERT INTO {comment_notify_user_settings} (uid, node_notify, comment_notify) VALUES (%d, %d, %d)', $user->uid, $edit['node_notify_mailalert'], $edit['comment_notify_mailalert']);
      }

      // Unset them from $user so they don't also get saved into {users}.data.
      unset($edit['node_notify_mailalert']);
      unset($edit['comment_notify_mailalert']);
      break;

    case 'load':
      $user_settings = db_fetch_array(db_query('SELECT node_notify AS node_notify_mailalert, comment_notify AS comment_notify_mailalert FROM {comment_notify_user_settings} WHERE uid = %d', $user->uid));
339 340 341 342
      if (!empty($user_settings)) {
        foreach ($user_settings as $property => $value) {
          $user->$property = $value;
        }
343 344 345 346 347 348
      }
      break;

    case 'delete':
      db_query('DELETE FROM {comment_notify_user_settings} WHERE uid = %d', $user->uid);
      break;
349

350 351 352 353
      break;
  }
}

354 355
/**
 * Private function to send the notifications.
greggles's avatar
greggles committed
356
 *
357 358 359
 * @param $comment
 *   The comment array as found in hook_comment $op = publish.
 */
360 361
function _comment_notify_mailalert($comment) {
  $comment = (object) $comment;
362
  global $language;
363
  global $base_url;
364
  global $user;
365

366 367 368 369 370 371 372 373 374 375
  $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.
  if (db_result(db_query('SELECT cid from {comment_notify} WHERE cid = %d AND notified = %d', $cid, 1))) {
    return;
  }

376
  $initial_language = $language;
377

378
  if (function_exists('locale')) {
379
    $languages = locale_language_list();
380 381 382
    $languages = $languages['name'];
  }

383 384
  $node = node_load($nid);
  if (!isset($comment->mail)) {
385
    $comment_account = user_load(array('name' => $comment->name));
386 387 388 389 390
    $comment_mail = $comment_account->mail;
  }
  else {
    $comment_mail = $comment->mail;
  }
391 392 393 394
  $sent_to = array();

  // Send to a subscribed author if they are not the current commenter
  $author = user_load(array('uid' => $node->uid));
395
  if (!empty($author->node_notify_mailalert) && $author->node_notify_mailalert == 1 && $user->mail != $author->mail) {
396 397 398 399
    // Get the author's language.
    $language = user_preferred_language($author);
    $message['subject'] = t('!site :: new comment for your post.', array('!site' => variable_get('site_name', 'drupal')));
    $message['body'] = t(
400 401
      variable_get('node_notify_default_mailtext', AUTHOR_MAILTEXT),
      array(
402 403 404
        '!commname' => $comment->name,
        '!commtext' => $comment->comment,
        '!commsubj' => $comment->subject,
405
        '!comment_url' => url('node/'. $nid, array('absolute' => TRUE, 'fragment' => 'comment-'. $cid)),
406 407 408 409 410 411 412
        '!node_title' =>  $node->title,
        '!node_teaser' => $node->teaser,
        '!mission' => variable_get('site_mission', ''),
        '!node_body' =>  $node->body,
        '!name' => $author->name,
        '!site' => variable_get('site_name', 'drupal'),
        '!uri' => $base_url,
413
        '!uri_brief' => preg_replace('!^https?://!', '', $base_url),
414
        '!date' => format_date(time()),
415
        '!login_uri' => url('user', array('absolute' => TRUE)),
416
        '!edit_uri' => url('user/'. $alert->uid .'/edit', array('absolute' => TRUE))
417 418
      )
    );
419
    drupal_mail('comment_notify', 'comment_notify_mail', $author->mail, $language, $message);
420 421
    $sent_to[] = $author->mail;
  }
422

423 424 425
  // For "reply to my comments" notifications, figure out what thread this is.
  $thread = db_result(db_query("SELECT thread FROM {comments} WHERE cid = %d", $cid));

426
  //Get the list of commenters to notify
427
  $result = db_query("SELECT DISTINCT c.cid, c.uid, c.name, c.nid, c.mail AS cmail, u.mail AS umail, u.init AS uinit, c.uid, c.name, cn.notify, cn.notify_hash, c.thread
428
    FROM {comments} c INNER JOIN {comment_notify} cn on c.cid = cn.cid LEFT OUTER JOIN {users} u ON c.uid = u.uid
429
    WHERE nid = %d AND cn.notify > 0 AND c.status = 0 AND (u.status = 1 OR u.uid = 0)", $nid
430
  );
431
  // 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
432

433
  while ($alert = db_fetch_object($result)) {
434 435 436
    $umail = empty($alert->umail) ? $alert->uinit : $alert->umail;
    $mail = empty($alert->cmail) ? $umail : $alert->cmail;

437 438
    $relevant_thread = substr($thread, 0, drupal_strlen($alert->thread) -1);
    if ($alert->notify == COMMENT_NOTIFY_COMMENT && strcmp($relevant_thread . '/', $alert->thread) != 0) {
439
      continue;
440
    }
441
    if ($mail != $comment_mail && !in_array($mail, $sent_to) && $alert->uid != $comment->uid) {
442 443
      $message = array();
      if (!empty($alert->uid)) {
greggles's avatar
greggles committed
444
        $recipient_user = user_load(array('uid' => $alert->uid));
445 446 447 448
        $language = user_preferred_language($recipient_user);
      }
      else {
        $language = language_default();
449
      }
450

451
      $message['subject'] = t('!site :: new comment for your post.', array('!site' => variable_get('site_name', 'drupal')));
452
      $message['body'] = t(
453 454
        variable_get('comment_notify_default_mailtext', DEFAULT_MAILTEXT),
        array(
455 456 457
          '!commname' => $comment->name,
          '!commtext' => $comment->comment,
          '!commsubj' => $comment->subject,
458
          '!comment_url' => url('node/'. $nid, array('absolute' => TRUE, 'fragment' => 'comment-'. $cid)),
459 460 461 462 463 464 465
          '!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,
466
          '!uri_brief' => preg_replace('!^https?://!', '', $base_url),
467
          '!date' => format_date(time()),
468 469
          '!login_uri' => url('user', array('absolute' => TRUE)),
          '!edit_uri' => url('user/'. $alert->uid .'/edit', array('absolute' => TRUE)),
470
          '!link1' => url('comment_notify/disable/'. $alert->notify_hash, array('absolute' => TRUE))
471
        )
472
      );
473
      drupal_mail('comment_notify', 'comment_notify_mail', $mail, $language, $message);
474
      $sent_to[] = $mail;
475

476
      // Make the mail link to user's /edit, unless it's an anonymous user.
477
      if ($alert->uid != 0) {
478
        $user_mail = l($mail, 'user/'. $alert->uid .'/edit');
479 480
      }
      else {
481
        $user_mail = check_plain($mail);
482
      }
483

484
      // Add an entry to the watchdog log.
485 486 487 488 489 490 491 492 493
      watchdog(
        'comment_notify',
        'Notified: !user_mail',
        array('!user_mail' => $user_mail),
        WATCHDOG_NOTICE,
        l(t('source comment'), 'node/'. $nid, array(
          'fragment' => 'comment-'. $alert->cid,
        ))
      );
494

495
      // revert to previous (site default) locale
496
      $language = $initial_language;
497 498
    }
  }
499 500 501
  // Record that a notification was sent for this comment so that
  // notifications aren't sent again if the comment is later edited.
  db_query('UPDATE {comment_notify} SET notified = 1 WHERE cid = %d', $cid);
502 503
}

504 505
function comment_notify_mail($key, &$message, $params) {
  $message['subject'] = $params['subject'];
506
  $message['body'][] = $params['body'];
507 508 509
}


510
/**
511
 * Callback for an administrative form to unsubscribe users by e-mail address.
512 513 514 515 516 517 518 519 520 521 522 523 524
 */
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;
}
525

526
/**
527
 * Based on admin submit, do the actual unsubscribe from notifications.
528
 */
529 530
function comment_notify_unsubscribe_submit($form, &$form_state) {
  $email = trim($form_state['values']['email_to_unsubscribe']);
531
  // If they have a uid, use that, otherwise update comments directly
532
  $result = db_result(db_query_range("SELECT uid FROM {users} WHERE mail = '%s'", $email, 0, 1));
533
  if ($result > 0) {
534
    $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 > 0", $result));
535
    db_query("UPDATE {comment_notify} SET notify = 0 WHERE cid IN (SELECT cid FROM  {comments} WHERE uid = %d)", $result);
536 537
  }
  else {
538
    $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 > 0", $email));
539
    db_query("UPDATE {comment_notify} SET notify = 0 WHERE cid IN (SELECT cid FROM {comments} WHERE mail = '%s')", $email);
540 541 542 543 544 545 546 547 548 549
  }
  // 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."));
  }
}
550

551 552 553
/*
 * Page callback for administrative settings form.
 */
554 555
function comment_notify_settings() {
  $form['comment_notify_settings'] = array();
556 557

  // Only perform comment_notify for certain node types (default, all)
558
  $enabled_types = variable_get('comment_notify_node_types', FALSE);
559
  $anonymous_problems = '';
560 561 562
  foreach (node_get_types('names') as $type => $name) {
    $checkboxes[$type] = check_plain($name);
    $default[] = $type;
563 564

    // If they don't have the ability to leave contact info, then we make a report
greggles's avatar
greggles committed
565
    if (isset($enabled_types[$type]) && $enabled_types[$type] && variable_get('comment_anonymous_'. $type, COMMENT_ANONYMOUS_MAYNOT_CONTACT) == COMMENT_ANONYMOUS_MAYNOT_CONTACT) {
566
      $account = user_load(array('uid' => 0));
567
      if (user_access('subscribe to comments', $account)) {
568 569 570 571
        $anonymous_problems[] = l(t('@content-type', array('@content-type' => $name)), 'admin/content/node-type/'. $type);
      }
    }
  }
greggles's avatar
greggles committed
572

573 574
  if (!empty($anonymous_problems)) {
    drupal_set_message(t('Anonymous commenters have the permission to subscribe to comments but cannot leave their contact infromation 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))));
575 576 577 578
  }

  $form['comment_notify_settings']['comment_notify_node_types'] = array(
    '#type' => 'checkboxes',
579
    '#title' => t('Content types to enable for comment notification'),
580 581 582 583 584
    '#default_value' => variable_get('comment_notify_node_types', $default),
    '#options' => $checkboxes,
    '#description' => t('Comments on content types enabled here will have the option of comment notification.'),
  );

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

597 598
  $available_options = array(
      COMMENT_NOTIFY_DISABLED => t('No notifications'),
599 600
      COMMENT_NOTIFY_NODE     => t('All comments'),
      COMMENT_NOTIFY_COMMENT  => t('Replies to my comment')
601 602
    );

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 608 609 610 611 612 613 614 615 616 617
    '#default_value' => variable_get('comment_notify_default_anon_mailalert', COMMENT_NOTIFY_NODE),
    '#options' => $available_options,
  );

  $form['comment_notify_settings']['comment_notify_default_registered_mailalert'] = array(
    '#type' => 'select',
    '#title' => t('Default state for the notification selection box for registered users.'),
    '#return_value' => 1,
    '#default_value' => variable_get('comment_notify_default_registered_mailalert', COMMENT_NOTIFY_NODE),
    '#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

621 622
  $form['comment_notify_settings']['comment_notify_default_mailtext'] = array(
    '#type' => 'textarea',
623
    '#title' => t('Default mail text for sending out notifications to commenters'),
624 625
    '#description' => t(
      'You can use the following variables to be replaced:
626
      <ul>
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
      <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
  );

651 652
  $form['comment_notify_settings']['node_notify_default_mailtext'] = array(
    '#type' => 'textarea',
653
    '#title' => t('Default mail text for sending out the notifications to node authors'),
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
    '#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
      </ul>'
    ),
    '#default_value' => variable_get('node_notify_default_mailtext', t(AUTHOR_MAILTEXT)),
     '#return_value' => 1,
     '#cols' => 80,
     '#rows' => 15
  );

680 681
  $form['#validate'] = array('comment_notify_settings_validate');

682
  return system_settings_form($form);
683
}
684 685 686 687 688 689 690 691 692 693

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.');
  }
}