comment_notify.module 26.7 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']['notify'] = array(
122 123 124
    '#type' => 'checkbox',
    '#title' => t('Notify me when new comments are posted'),
    '#default_value' => (bool) $preference,
125
  );
126

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

  // Otherwise, the submit buttons will jump below the node preview.
  $form['actions']['#weight'] = 19;
139 140 141 142

  // 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']);
143
    $form['notify_settings']['notify']['#default_value'] = (bool) $notify;
144
    if (count($available_options) > 1) {
145
      $form['notify_settings']['notify_type']['#default_value'] = empty($notify) ? COMMENT_NOTIFY_NODE : $notify;
146
    }
147 148
    else {
      $form['notify_settings']['notify_type']['#default_value'] = key($available_options);
149
    }
150 151
  }
}
152

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

/**
170
 * Implements hook_menu().
171
 */
172 173
function comment_notify_menu() {

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

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

  return $items;
}

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

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

234 235 236 237
function comment_notify_comment_publish($comment) {
  // And send notifications - the real purpose of the module.
  _comment_notify_mailalert($comment);
}
238

239 240 241
/**
 * Implements hook_comment_update().
 */
242
function comment_notify_comment_update($comment) {
243
  module_load_include('inc', 'comment_notify', 'comment_notify');
244

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

}

function comment_notify_comment_insert($comment) {
264
  module_load_include('inc', 'comment_notify', 'comment_notify');
265

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

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

295
function comment_notify_comment_delete($comment) {
296
  module_load_include('inc', 'comment_notify', 'comment_notify');
297 298 299 300
  comment_notify_remove_all_notifications($comment->cid);
}


301
/**
302
 * Implement hook_form_alter().
303
 */
304
function comment_notify_form_alter(&$form, &$form_state, $form_id) {
305
  module_load_include('inc', 'comment_notify', 'comment_notify');
306

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

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

  if (user_access('administer nodes') || $nodes) {
    $form['comment_notify_settings']['node_notify'] = array(
338
      '#type' => 'checkbox',
339
      '#title' => t('Receive content follow-up notification e-mails'),
340
      '#default_value' => isset($node_notify) ? $node_notify : comment_notify_variable_registry_get('node_notify_default_mailalert'),
341
      '#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.')
342 343 344 345
    );
  }
  else {
    $form['comment_notify_settings']['node_notify'] = array(
346 347
      '#type' => 'hidden',
      '#value' => COMMENT_NOTIFY_DISABLED,
348 349
    );
  }
350

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

364 365 366 367 368
function comment_notify_user_update(&$edit, $account, $category) {
  if ($category != 'account') {
    return;
  }
  if (isset($edit['node_notify']) && isset($edit['comment_notify'])) {
369
    module_load_include('inc', 'comment_notify', 'comment_notify');
370

371 372 373 374 375 376 377
    // 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']);
378

379
}
380

381
function comment_notify_user_load($users) {
382
  module_load_include('inc', 'comment_notify', 'comment_notify');
383

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

389
  return;
390 391
}

392
function comment_notify_user_cancel($edit, $account, $method) {
393
  module_load_include('inc', 'comment_notify', 'comment_notify');
394 395 396
  comment_notify_delete_user_notification_setting($account->uid);
}

397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
/**
 * 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;
  }
}

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

431
  $comment = (object) $comment;
432
  global $language;
433
  global $base_url;
434
  global $user;
435
  $initial_language = $language;
436

437 438 439 440 441 442
  $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.
443
  if (!empty($comment->notified)) {
444 445 446
    return;
  }

447
  $node = node_load($nid);
448 449 450 451 452 453 454

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

455
  if (empty($comment->mail)) {
456
    $comment_account = user_load_by_name($comment->name);
457
    $comment_mail = isset($comment_account->mail) ? $comment_account->mail : '';
458 459 460 461
  }
  else {
    $comment_mail = $comment->mail;
  }
462 463 464
  $sent_to = array();

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

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

478
    drupal_mail('comment_notify', 'comment_notify_mail', $author->mail, $language, $message);
479
    $sent_to[] = strtolower($author->mail);
480
  }
481

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

485 486
  // Get the list of commenters to notify.
  $watchers = comment_notify_get_watchers($nid);
487

488
  foreach ($watchers as $alert) {
489 490 491 492
    // 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;
493

494
    $relevant_thread = drupal_substr($thread, 0, drupal_strlen($alert->thread) -1);
495
    if ($alert->notify == COMMENT_NOTIFY_COMMENT && strcmp($relevant_thread . '/', $alert->thread) != 0) {
496
      continue;
497
    }
498

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

501
      $message = array();
502 503
      $language = !empty($alert->uid) ? user_preferred_language($recipient_user) : language_default();

504 505 506
      // Make sure they have access to this node before showing a bunch of node information.
      if (!node_access('view', $node, $recipient_user)) {
        continue;
507
      }
508

509
      $raw_values = array(
510 511
        'subject' => comment_notify_variable_registry_get('watcher_subject'),
        'body'  => comment_notify_variable_registry_get('comment_notify_default_mailtext'), //JS @todo:change this var name.
512
      );
513 514

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

518
      drupal_mail('comment_notify', 'comment_notify_mail', $mail, $language, $message);
519
      $sent_to[] = strtolower($mail);
520

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

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

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

549
/**
550
 * Implements hook_mail().
551
 */
552 553
function comment_notify_mail($key, &$message, $params) {
  $message['subject'] = $params['subject'];
554
  $message['body'][] = $params['body'];
555 556
}

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

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

590 591 592
/*
 * Page callback for administrative settings form.
 */
593
function comment_notify_settings() {
594
  module_load_include('inc', 'comment_notify', 'comment_notify');
595

596
  $form['comment_notify_settings'] = array();
597

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

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

614
  if (!empty($anonymous_problems)) {
615
    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);
616 617 618 619
  }

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

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

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

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

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

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

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

686 687 688 689 690
  $form['comment_notify_settings']['token_help'] = array(
    '#theme' => 'token_tree',
    '#token_types' => array('comment'),
  );

691 692
  $form['#validate'] = array('comment_notify_settings_validate');

693
  return system_settings_form($form);
694
}
695 696 697 698 699 700 701 702 703 704

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.');
  }
}
705 706 707 708 709 710 711 712 713 714 715 716 717

/**
 * 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');
718 719
  if (!empty($comment->notify_hash)) {
    return 'comment_notify/disable/' . $comment->notify_hash;
720 721
  }
}
722 723 724 725
/**
 * Implements hook_field_extra_fields().
 */
function comment_notify_field_extra_fields() {
726 727 728 729 730 731 732 733 734 735 736 737 738
  module_load_include('inc', 'comment_notify', 'comment_notify');
  $extras = array();

  foreach (comment_notify_variable_registry_get('node_types') as $node_type) {
    if (isset($node_type)) {
      $extras['comment']['comment_node_' . $node_type]['form']['comment_notify_settings'] = array(
        'label' => t('Comment notify settings'),
        'description' => t('@node_type settings for Comment notify', array('@node_type' => ucwords($node_type))),
        'weight' => 1,
      );
    }
  }

739 740 741 742 743 744
  $extras['user']['user']['form']['comment_notify_settings'] = array(
    'label' => t('Comment notify settings'),
    'description' => t('User settings for Comment notify'),
    'weight' => 4,
  );
  return $extras;
745
}
746