comment_notify.module 26.1 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 60 61 62 63 64 65 66 67 68 69 70 71 72
/**
 * Implements hook_init().
 */
function comment_notify_init() {
  // Add on every page - they are both very small so it's better to add
  // everywhere than force a second file on some pages.
  $options = array('every_page' => TRUE);
  $path = drupal_get_path('module', 'comment_notify');
  drupal_add_css($path . '/comment_notify.css', $options);

  // We only add the JS if more than one subscription mode is enabled.
  $available_options = _comment_notify_options();
  if (count($available_options) > 1) {
    drupal_add_js($path . '/comment_notify.js', $options);
  }
}
73

74
/**
75
 * Provide an array of available options for notification on a comment.
76
 */
77 78 79 80 81 82 83
function _comment_notify_options() {
  $total_options = array(
    COMMENT_NOTIFY_NODE     => t('All comments'),
    COMMENT_NOTIFY_COMMENT  => t('Replies to my comment')
  );

  $available_options = array();
84
  $options = variable_get('comment_notify_available_alerts', drupal_map_assoc(array(COMMENT_NOTIFY_NODE, COMMENT_NOTIFY_COMMENT)));
85 86 87 88
  foreach ($options as $key => $available) {
    if ($key == $available) {
      $available_options[$available] = $total_options[$available];
    }
89
  }
90 91

  return $available_options;
92 93
}

94 95

function comment_notify_form_comment_form_alter(&$form, &$form_state, $form_id) {
96
  global $user;
97 98 99
  if (!(user_access('subscribe to comments') || user_access('administer comments'))) {
    return;
  }
100

101 102 103 104 105 106
  // 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;
  }
107

108
  $available_options = _comment_notify_options();
109
  // Add the checkbox for anonymous users.
110
  if ($user->uid == 0) {
111
    // If anonymous users can't enter their e-mail don't tempt them with the checkbox.
112
    if (empty($form['author']['mail'])) {
113
      return;
114
    }
115
    $form['#validate'][] = 'comment_notify_comment_validate';
116
  }
117 118
  module_load_include('inc', 'comment_notify', 'comment_notify');
  $preference = comment_notify_get_user_comment_notify_preference($user->uid);
119

120 121
  // If you want to hide this on your site see http://drupal.org/node/322482
  $form['notify_settings'] = array(
122
    '#weight' => 19,
123
  );
124

125
  $form['notify_settings']['notify'] = array(
126 127 128
    '#type' => 'checkbox',
    '#title' => t('Notify me when new comments are posted'),
    '#default_value' => (bool) $preference,
129
  );
130

131 132 133 134 135 136 137 138
  $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);
139
  }
140 141 142

  // Otherwise, the submit buttons will jump below the node preview.
  $form['actions']['#weight'] = 19;
143 144 145 146

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

157
/**
158
 * Implements hook_permission().
159
 */
160 161 162 163 164 165 166 167 168 169 170
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.',
  ),
  );
171
}
172 173

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

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

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

  return $items;
}

215 216 217
/**
 * Page callback to allow users to unsubscribe simply by visiting the page.
 */
218
function comment_notify_disable_page($hash) {
219
  module_load_include('inc', 'comment_notify', 'comment_notify');
220 221
  if (comment_notify_unsubscribe_by_hash($hash)) {
    drupal_set_message(t('Your comment follow-up notification for this post was disabled. Thanks.'));
222 223
  }
  else {
224 225
    drupal_set_message(t('Sorry, there was a problem unsubscribing from notifications.'));
  }
226
  return ' ';
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 244 245
/**
 * Implements hook_comment_update().
 */
246
function comment_notify_comment_update($comment) {
247
  module_load_include('inc', 'comment_notify', 'comment_notify');
248

249 250 251 252 253 254 255
  // Take the status of the "notify" checkbox if they unchecked it.
  if (empty($comment->notify)) {
    $status = $comment->notify;
  }
  else {
    $status = $comment->notify_type;
  }
256
  // In case they have changed their status, save it in the database.
257 258
  if (isset($status)) {
    comment_notify_update_notification($comment->cid, $status);
259
  }
260 261 262 263 264 265 266 267
  // And send notifications - the real purpose of the module.
  if ($comment->status == COMMENT_PUBLISHED) {
    _comment_notify_mailalert($comment);
  }

}

function comment_notify_comment_insert($comment) {
268
  module_load_include('inc', 'comment_notify', 'comment_notify');
269

270
  global $user;
271 272 273
  // 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.
274 275
  // The string is built with token and their host and comment identifier.
  // It is stored and referenced, we really just need something unique/unguessable.
276 277
  $hostname = isset($comment->hostname) ? $comment->hostname : (isset($user->hostname) ? $user->hostname : '');
  $notify_hash = drupal_get_token($hostname . $comment->cid);
278

279
  if (!empty($comment->notify)) {
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
    $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);
296 297 298
  }
}

299
function comment_notify_comment_delete($comment) {
300
  module_load_include('inc', 'comment_notify', 'comment_notify');
301 302 303 304
  comment_notify_remove_all_notifications($comment->cid);
}


305
/**
306
 * Implement hook_form_alter().
307
 */
308
function comment_notify_form_alter(&$form, &$form_state, $form_id) {
309
  module_load_include('inc', 'comment_notify', 'comment_notify');
310

311 312 313
  if (!($form_id == 'user_register_form' || $form_id == 'user_profile_form')) {
    return;
  }
314
  elseif ($form['#user_category'] != 'account') {
315 316 317 318 319 320 321 322 323 324
    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(
325 326 327 328
    '#type' => 'fieldset',
    '#title' => t('Comment follow-up notification settings'),
    '#weight' => 4,
    '#collapsible' => TRUE
329 330 331 332
  );

  // Only show the node followup UI if the user has permission to create nodes.
  $nodes = FALSE;
333 334
  foreach (node_type_get_names() as $type => $name) {
    if (node_access('create', $type)) {
335 336 337 338 339 340 341
      $nodes = TRUE;
      break;
    }
  }

  if (user_access('administer nodes') || $nodes) {
    $form['comment_notify_settings']['node_notify'] = array(
342
      '#type' => 'checkbox',
343
      '#title' => t('Receive content follow-up notification e-mails'),
344
      '#default_value' => isset($node_notify) ? $node_notify : comment_notify_variable_registry_get('node_notify_default_mailalert'),
345
      '#description' => t('Check this box to receive an e-mail notification for follow-ups on your content. You can not disable notifications for individual threads.')
346 347 348 349
    );
  }
  else {
    $form['comment_notify_settings']['node_notify'] = array(
350 351
      '#type' => 'hidden',
      '#value' => COMMENT_NOTIFY_DISABLED,
352 353
    );
  }
354

355 356 357
  $available_options[COMMENT_NOTIFY_DISABLED] = t('No notifications');
  $available_options += _comment_notify_options();
  $form['comment_notify_settings']['comment_notify'] = array(
358 359
    '#type' => 'select',
    '#title' => t('Receive comment follow-up notification e-mails'),
360
    '#default_value' => isset($comment_notify) ? array($comment_notify) : array(comment_notify_variable_registry_get('default_registered_mailalert')),
361 362
    '#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.")
363 364 365 366
  );
  return $form;
  // Construct the user form
}
367

368 369 370 371 372
function comment_notify_user_update(&$edit, $account, $category) {
  if ($category != 'account') {
    return;
  }
  if (isset($edit['node_notify']) && isset($edit['comment_notify'])) {
373
    module_load_include('inc', 'comment_notify', 'comment_notify');
374

375 376 377 378 379 380 381
    // 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']);
382

383
}
384

385
function comment_notify_user_load($users) {
386
  module_load_include('inc', 'comment_notify', 'comment_notify');
387

388
  // @todo: Why would we want to load this on every user load?
389 390 391
  foreach ($users as &$user) {
    $user->comment_notify_settings = comment_notify_get_user_notification_setting($user->uid);
  }
392

393
  return;
394 395
}

396
function comment_notify_user_cancel(&$edit, $account, $method) {
397
  module_load_include('inc', 'comment_notify', 'comment_notify');
398 399 400
  comment_notify_delete_user_notification_setting($account->uid);
}

401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
/**
 * Implements hook_comment_load().
 */
function comment_notify_comment_load($comments) {
  // Load some comment_notify specific information into the comment object.
  $query = db_select('comment_notify', 'cn');
  $query->join('comment', 'c', 'c.cid = cn.cid');
  $query->leftJoin('users', 'u', 'c.uid = u.uid');
  $query->condition('c.cid', array_keys($comments));
  $query->fields('cn', array('cid', 'notify', 'notify_hash', 'notified'));
  $query->addField('c', 'mail', 'cmail');
  $query->addField('u', 'init', 'uinit');
  $query->addField('u', 'mail', 'umail');

  $records = $query->execute()->fetchAllAssoc('cid');
  foreach ($records as $cid => $record) {
    $comments[$cid]->notify = $record->notify;
    $comments[$cid]->notify_hash = $record->notify_hash;
    $comments[$cid]->notified = $record->notified;
    $comments[$cid]->cmail = $record->cmail;
    $comments[$cid]->uinit = $record->uinit;
    $comments[$cid]->umail = $record->umail;
  }
}

426 427
/**
 * Private function to send the notifications.
greggles's avatar
greggles committed
428
 *
429 430 431
 * @param $comment
 *   The comment array as found in hook_comment $op = publish.
 */
432
function _comment_notify_mailalert($comment) {
433
  module_load_include('inc', 'comment_notify', 'comment_notify');
434

435
  $comment = (object) $comment;
436
  global $language;
437
  global $base_url;
438
  global $user;
439
  $initial_language = $language;
440

441 442 443 444 445 446
  $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.
447
  if (!empty($comment->notified)) {
448 449 450
    return;
  }

451
  $node = node_load($nid);
452 453 454 455 456 457 458

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

459
  if (empty($comment->mail)) {
460
    $comment_account = user_load_by_name($comment->name);
461
    $comment_mail = isset($comment_account->mail) ? $comment_account->mail : '';
462 463 464 465
  }
  else {
    $comment_mail = $comment->mail;
  }
466 467 468
  $sent_to = array();

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

471
  if (!empty($author->comment_notify_settings->node_notify) && $author->comment_notify_settings->node_notify == 1 && $user->uid != $author->uid && node_access('view', $node, $author)) {
472 473
    // Get the author's language.
    $language = user_preferred_language($author);
474
    $raw_values = array(
475 476
      'subject' => comment_notify_variable_registry_get('author_subject'),
      'body'  => comment_notify_variable_registry_get('node_notify_default_mailtext'), //JS @todo:change this.
477
    );
478
    foreach ($raw_values as $k => $v) {
479
      $message[$k] = token_replace(t($v), array('comment' => $comment), array('sanitize' => FALSE));
480 481
    }

482
    drupal_mail('comment_notify', 'comment_notify_mail', $author->mail, $language, $message);
483 484
    $sent_to[] = $author->mail;
  }
485

486
  // For "reply to my comments" notifications, figure out what thread this is.
487
  $thread = isset($comment->thread) ? $comment->thread : '';
488

489 490
  // Get the list of commenters to notify.
  $watchers = comment_notify_get_watchers($nid);
491

492
  foreach ($watchers as $alert) {
493 494 495 496
    // 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;
497

498
    $relevant_thread = drupal_substr($thread, 0, drupal_strlen($alert->thread) -1);
499
    if ($alert->notify == COMMENT_NOTIFY_COMMENT && strcmp($relevant_thread . '/', $alert->thread) != 0) {
500
      continue;
501
    }
502

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

505
      $message = array();
506 507
      $language = !empty($alert->uid) ? user_preferred_language($recipient_user) : language_default();

508 509 510
      // Make sure they have access to this node before showing a bunch of node information.
      if (!node_access('view', $node, $recipient_user)) {
        continue;
511
      }
512

513
      $raw_values = array(
514 515
        'subject' => comment_notify_variable_registry_get('watcher_subject'),
        'body'  => comment_notify_variable_registry_get('comment_notify_default_mailtext'), //JS @todo:change this var name.
516
      );
517 518

      foreach ($raw_values as $k => $v) {
519
        $message[$k] = token_replace(t($v), array('comment' => $comment, 'comment-subscribed' => $alert), array('sanitize' => FALSE));
520 521
      }

522
      drupal_mail('comment_notify', 'comment_notify_mail', $mail, $language, $message);
523
      $sent_to[] = $mail;
524

525
      // Make the mail link to user's /edit, unless it's an anonymous user.
526
      if ($alert->uid != 0) {
527
        $user_mail = l($mail, 'user/' . $alert->uid . '/edit');
528 529
      }
      else {
530
        $user_mail = check_plain($mail);
531
      }
532

533
      // Add an entry to the watchdog log.
534 535
      watchdog(
        'comment_notify',
536 537 538 539 540 541
        'Notified: @user_mail',
        array('@user_mail' => $user_mail),
          WATCHDOG_NOTICE,
          l(t('source comment'), 'node/' . $nid, array(
            'fragment' => 'comment-' . $alert->cid,
          ))
542
      );
543

544
      // Revert to previous (site default) locale.
545
      $language = $initial_language;
546 547
    }
  }
548 549
  // Record that a notification was sent for this comment so that
  // notifications aren't sent again if the comment is later edited.
550
  comment_notify_mark_comment_as_notified($comment);
551 552
}

553
/**
554
 * Implements hook_mail().
555
 */
556 557
function comment_notify_mail($key, &$message, $params) {
  $message['subject'] = $params['subject'];
558
  $message['body'][] = $params['body'];
559 560
}

561
/**
562
 * Callback for an administrative form to unsubscribe users by e-mail address.
563
 */
564
function comment_notify_unsubscribe($form, &$form_state) {
565 566 567 568 569 570 571 572 573 574 575
  $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;
}
576

577
/**
578
 * Based on admin submit, do the actual unsubscribe from notifications.
579
 */
580
function comment_notify_unsubscribe_submit($form, &$form_state) {
581
  module_load_include('inc', 'comment_notify', 'comment_notify');
582
  $email = trim($form_state['values']['email_to_unsubscribe']);
583
  $comments = comment_notify_unsubscribe_by_email($email);
584 585 586 587 588 589 590 591 592
  // 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."));
  }
}
593

594 595 596
/*
 * Page callback for administrative settings form.
 */
597
function comment_notify_settings() {
598
  module_load_include('inc', 'comment_notify', 'comment_notify');
599

600
  $form['comment_notify_settings'] = array();
601

602 603
  // Only perform comment_notify for certain node types.
  $enabled_types = comment_notify_variable_registry_get('node_types');
604
  $anonymous_problems = '';
605
  foreach (node_type_get_names() as $type => $name) {
606 607
    $checkboxes[$type] = check_plain($name);
    $default[] = $type;
608 609

    // If they don't have the ability to leave contact info, then we make a report
610
    if (isset($enabled_types[$type]) && $enabled_types[$type] && variable_get('comment_anonymous_' . $type, COMMENT_ANONYMOUS_MAYNOT_CONTACT) == COMMENT_ANONYMOUS_MAYNOT_CONTACT) {
611
      $account = drupal_anonymous_user();
612
      if (user_access('subscribe to comments', $account)) {
613
        $anonymous_problems[] = l(t('@content-type', array('@content-type' => $name)), 'admin/structure/types/manage/' . $type);
614 615 616
      }
    }
  }
greggles's avatar
greggles committed
617

618
  if (!empty($anonymous_problems)) {
619
    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);
620 621 622 623
  }

  $form['comment_notify_settings']['comment_notify_node_types'] = array(
    '#type' => 'checkboxes',
624
    '#title' => t('Content types to enable for comment notification'),
625
    '#default_value' => $enabled_types,
626 627 628 629
    '#options' => $checkboxes,
    '#description' => t('Comments on content types enabled here will have the option of comment notification.'),
  );

630 631
  $form['comment_notify_settings']['comment_notify_available_alerts'] = array(
    '#type' => 'checkboxes',
632
    '#title' => t('Available subscription modes'),
633
    '#return_value' => 1,
634
    '#default_value' => comment_notify_variable_registry_get('available_alerts'),
635
    '#description' => t('Choose which notification subscription styles are available for users'),
636
    '#options' => array(
637 638 639
  COMMENT_NOTIFY_NODE     => t('All comments'),
  COMMENT_NOTIFY_COMMENT  => t('Replies to my comment')
  )
640 641
  );

642 643
  $available_options[COMMENT_NOTIFY_DISABLED] = t('No notifications');
  $available_options += _comment_notify_options();
644
  $form['comment_notify_settings']['comment_notify_default_anon_mailalert'] = array(
645
    '#type' => 'select',
646
    '#title' => t('Default state for the notification selection box for anonymous users'),
647
    '#return_value' => 1,
648
    '#default_value' => comment_notify_variable_registry_get('default_anon_mailalert'),
649 650 651 652 653
    '#options' => $available_options,
  );

  $form['comment_notify_settings']['comment_notify_default_registered_mailalert'] = array(
    '#type' => 'select',
greggles's avatar
greggles committed
654
    '#title' => t('Default state for the notification selection box for registered users'),
655
    '#return_value' => 1,
656
    '#default_value' => comment_notify_variable_registry_get('default_registered_mailalert'),
657 658
    '#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,
659 660
  );

661
  $form['comment_notify_settings']['comment_notify_node_notify_default_mailalert'] = array(
662 663
    '#type' => 'checkbox',
    '#title' => t('Subscribe users to their node follow-up notification emails by default'),
664
    '#default_value' => comment_notify_variable_registry_get('node_notify_default_mailalert'),
665 666 667
    '#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.'),
  );

668
  $form['comment_notify_settings']['comment_notify_comment_notify_default_mailtext'] = array(
669
    '#type' => 'textarea',
670
    '#title' => t('Default mail text for sending out notifications to commenters'),
671
    '#default_value' => comment_notify_variable_registry_get('comment_notify_default_mailtext'),
672 673
    '#return_value' => 1,
    '#cols' => 80,
674 675 676
    '#rows' => 15,
    '#token_types' => array('comment'),
    '#element_validate' => array('token_element_validate'),
677 678
  );

679
  $form['comment_notify_settings']['comment_notify_node_notify_default_mailtext'] = array(
680
    '#type' => 'textarea',
681
    '#title' => t('Default mail text for sending out the notifications to node authors'),
682
    '#default_value' => comment_notify_variable_registry_get('node_notify_default_mailtext'),
683 684
     '#return_value' => 1,
     '#cols' => 80,
685 686 687
     '#rows' => 15,
     '#token_types' => array('comment'),
     '#element_validate' => array('token_element_validate'),
688 689
  );

690 691 692 693 694
  $form['comment_notify_settings']['token_help'] = array(
    '#theme' => 'token_tree',
    '#token_types' => array('comment'),
  );

695 696
  $form['#validate'] = array('comment_notify_settings_validate');

697
  return system_settings_form($form);
698
}
699 700 701 702 703 704 705 706 707 708

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.');
  }
}
709 710 711 712 713 714 715 716 717 718 719 720 721

/**
 * 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.
 */
function comment_notify_get_unsubscribe_url($comment) {
  module_load_include('inc', 'comment_notify', 'comment_notify');
722 723
  if (!empty($comment->notify_hash)) {
    return 'comment_notify/disable/' . $comment->notify_hash;
724 725
  }
}
726 727 728 729 730 731 732 733 734 735
/**
 * Implements hook_field_extra_fields().
 */
function comment_notify_field_extra_fields() {
  $extras['user']['user']['form']['comment_notify_settings'] = array(
    'label' => t('Comment notify settings'),
    'description' => t('User settings for Comment notify'),
    'weight' => 4,
  );
  return $extras;
736
}