Projet

Général

Profil

Paste
Télécharger (26,9 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / comment_notify / comment_notify.module @ 87dbc3bf

1
<?php
2

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

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

    
13

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

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

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

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

    
27
You will receive emails like this for all replies to your posts. You can
28
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
[comment:title]
41
[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
[site:url]');
55

    
56

    
57
/**
58
 * Implements hook_init().
59
 */
60
function comment_notify_init() {
61
  // Add on every page - they are both very small so it's better to add
62
  // everywhere than force a second file on some pages.
63
  $options = array('every_page' => TRUE);
64
  $path = drupal_get_path('module', 'comment_notify');
65
  drupal_add_css($path . '/comment_notify.css', $options);
66

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

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

    
83
  $available_options = array();
84
  $options = variable_get('comment_notify_available_alerts', drupal_map_assoc(array(COMMENT_NOTIFY_NODE, COMMENT_NOTIFY_COMMENT)));
85
  foreach ($options as $key => $available) {
86
    if ($key == $available) {
87
      $available_options[$available] = $total_options[$available];
88
    }
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
  if (!(user_access('subscribe to comments') || user_access('administer comments'))) {
98
    return;
99
  }
100

    
101
  // Only add the checkbox if this is an enabled content type
102
  $node = node_load($form['nid']['#value'] ? $form['nid']['#value'] : $form['nid']['#default_value']);
103
  $enabled_types = variable_get('comment_notify_node_types', drupal_map_assoc(array($node->type)));
104
  if (empty($enabled_types[$node->type])) {
105
    return;
106
  }
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
  module_load_include('inc', 'comment_notify', 'comment_notify');
118
  $preference = comment_notify_get_user_comment_notify_preference($user->uid);
119

    
120
  // If you want to hide this on your site see http://drupal.org/node/322482
121
  $form['notify_settings']['notify'] = array(
122
    '#type' => 'checkbox',
123
    '#title' => t('Notify me when new comments are posted'),
124
    '#default_value' => (bool) $preference,
125
  );
126

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

    
137
  // If this is an existing comment we set the default value based on their selection last time.
138
  if ($form['cid']['#value'] != '') {
139
    $notify = comment_notify_get_notification_type($form['cid']['#value']);
140
    $form['notify_settings']['notify']['#default_value'] = (bool) $notify;
141
    if (count($available_options) > 1) {
142
      $form['notify_settings']['notify_type']['#default_value'] = empty($notify) ? COMMENT_NOTIFY_NODE : $notify;
143
    }
144
    else {
145
      $form['notify_settings']['notify_type']['#default_value'] = key($available_options);
146
    }
147
  }
148
}
149

    
150
/**
151
 * Implements hook_permission().
152
 */
153
function comment_notify_permission() {
154
  return array(
155
    'administer comment notify' => array(
156
      'title' => 'Administer Comment Notify',
157
      'description' => 'Change global comment notification settings.',
158
  ),
159
    'subscribe to comments' => array(
160
      'title' => 'Subscribe to comment notifications',
161
      'description' => 'Subscribe to recieve notifications when new comments are posted.',
162
  ),
163
  );
164
}
165

    
166
/**
167
 * Implements hook_menu().
168
 */
169
function comment_notify_menu() {
170

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

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

    
205
  return $items;
206
}
207

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

    
221
function comment_notify_comment_validate($comment) {
222
  global $user;
223
  // We assume that if they are non-anonymous then they have a valid mail.
224
  // For anonymous users, though, we verify that they entered a mail and let comment.module validate it is real.
225
  if (!$user->uid && $comment['notify_settings']['notify']['#value'] && empty($comment['author']['mail']['#value'])) {
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
function comment_notify_comment_publish($comment) {
231
  // And send notifications - the real purpose of the module.
232
  _comment_notify_mailalert($comment);
233
}
234

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

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

    
257
}
258

    
259
/**
260
 * Implements hook_comment_insert().
261
 */
262
function comment_notify_comment_insert($comment) {
263
  module_load_include('inc', 'comment_notify', 'comment_notify');
264

    
265
  global $user;
266
  // For new comments, we first build up a string to be used as the identifier for the alert.
267
  // This identifier is used to later unsubscribe the user or allow them to
268
  // potentially edit their comment / preferences if they are anonymous.
269
  // The string is built with token and their host and comment identifier.
270
  // It is stored and referenced, we really just need something unique/unguessable.
271
  $hostname = isset($comment->hostname) ? $comment->hostname : (isset($user->hostname) ? $user->hostname : '');
272
  $notify_hash = drupal_get_token($hostname . $comment->cid);
273

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

    
288
  // And send notifications - the real purpose of the module.
289
  if ($comment->status == COMMENT_PUBLISHED) {
290
    _comment_notify_mailalert($comment);
291
  }
292
}
293

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

    
299

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

    
306
  if (!($form_id == 'user_register_form' || $form_id == 'user_profile_form')) {
307
    return;
308
  }
309
  elseif ($form['#user_category'] != 'account') {
310
    return;
311
  }
312

    
313
  $user = $form['#user'];
314
  if (!empty($user->comment_notify_settings)) {
315
    $node_notify = $user->comment_notify_settings->node_notify;
316
    $comment_notify = $user->comment_notify_settings->comment_notify;
317
  }
318

    
319
  $form['comment_notify_settings'] = array(
320
    '#type' => 'fieldset',
321
    '#title' => t('Comment follow-up notification settings'),
322
    '#weight' => 4,
323
    '#collapsible' => TRUE
324
  );
325

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

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

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

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

    
370
    // Save the values of node_notify_mailalert and comment_notify_mailalert
371
    // to {comment_notify_user_settings}.
372
    comment_notify_set_user_notification_setting($account->uid, $edit['node_notify'], $edit['comment_notify']);
373
  }
374
  // Unset them from $user so they don't also get saved into {users}.data.
375
  unset($edit['node_notify']);
376
  unset($edit['comment_notify']);
377

    
378
}
379

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

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

    
388
  return;
389
}
390

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

    
396
/**
397
 * Implements hook_comment_load().
398
 */
399
function comment_notify_comment_load($comments) {
400
  // Load some comment_notify specific information into the comment object.
401
  $query = db_select('comment_notify', 'cn');
402
  $query->join('comment', 'c', 'c.cid = cn.cid');
403
  $query->leftJoin('users', 'u', 'c.uid = u.uid');
404
  $query->condition('c.cid', array_keys($comments));
405
  $query->fields('cn', array('cid', 'notify', 'notify_hash', 'notified'));
406
  $query->addField('c', 'mail', 'cmail');
407
  $query->addField('u', 'init', 'uinit');
408
  $query->addField('u', 'mail', 'umail');
409

    
410
  $records = $query->execute()->fetchAllAssoc('cid');
411
  foreach ($records as $cid => $record) {
412
    $comments[$cid]->notify = $record->notify;
413
    $comments[$cid]->notify_type = $record->notify;
414
    $comments[$cid]->notify_hash = $record->notify_hash;
415
    $comments[$cid]->notified = $record->notified;
416
    $comments[$cid]->cmail = $record->cmail;
417
    $comments[$cid]->uinit = $record->uinit;
418
    $comments[$cid]->umail = $record->umail;
419
  }
420
}
421

    
422
/**
423
 * Private function to send the notifications.
424
 *
425
 * @param $comment
426
 *   The comment array as found in hook_comment $op = publish.
427
 */
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
  $nid = $comment->nid;
438
  $cid = $comment->cid;
439

    
440
  // Check to see if a notification has already been sent for this
441
  // comment so that edits to a comment don't trigger an additional
442
  // notification.
443
  if (!empty($comment->notified)) {
444
    return;
445
  }
446

    
447
  $node = node_load($nid);
448

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

    
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
  else {
460
    $comment_mail = $comment->mail;
461
  }
462
  $sent_to = array();
463

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

    
467
  // Do they explicitly want this? Or is it default to send to users?
468
  // Is the comment author not the node author? Do they have access? Do they have an email (e.g. anonymous)?
469
  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 && !isset($author->comment_notify_settings->node_notify))) && $user->uid != $author->uid && node_access('view', $node, $author) && !empty($author->mail)) {
470
    // Get the author's language.
471
    $language = user_preferred_language($author);
472
    $raw_values = array(
473
      'subject' => comment_notify_variable_registry_get('author_subject'),
474
      'body'  => comment_notify_variable_registry_get('node_notify_default_mailtext'), //JS @todo:change this.
475
    );
476
    foreach ($raw_values as $k => $v) {
477
      $message[$k] = token_replace(t($v), array('comment' => $comment), array('sanitize' => FALSE));
478
    }
479

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

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

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

    
490
  foreach ($watchers as $alert) {
491
    // If the user is not anonymous, always load the current e-mail address
492
    // from his or her user account instead of trusting $comment->mail.
493
    $recipient_user = !empty($alert->uid) ? user_load($alert->uid) : drupal_anonymous_user();
494
    $mail = !empty($recipient_user->mail) ? $recipient_user->mail : $alert->cmail;
495

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

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

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

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

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

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

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

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

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

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

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

    
559
/**
560
 * Callback for an administrative form to unsubscribe users by e-mail address.
561
 */
562
function comment_notify_unsubscribe($form, &$form_state) {
563
  $form['comment_notify_unsubscribe'] = array();
564
  $form['comment_notify_unsubscribe']['email_to_unsubscribe'] = array(
565
    '#type' => 'textfield',
566
    '#title' => t('Email to unsubscribe'),
567
  );
568
  $form['comment_notify_unsubscribe']['submit'] = array(
569
    '#type' => 'submit',
570
    '#value' => t('Unsubscribe this e-mail'),
571
  );
572
  return $form;
573
}
574

    
575
/**
576
 * Based on admin submit, do the actual unsubscribe from notifications.
577
 */
578
function comment_notify_unsubscribe_submit($form, &$form_state) {
579
  module_load_include('inc', 'comment_notify', 'comment_notify');
580
  $email = trim($form_state['values']['email_to_unsubscribe']);
581
  $comments = comment_notify_unsubscribe_by_email($email);
582
  // Update the admin about the state of this comment notification subscription.
583
  if ($comments == 0) {
584
    drupal_set_message(t("There were no active comment notifications for that email."));
585
  }
586
  else {
587
    drupal_set_message(format_plural($comments, "Email unsubscribed from 1 comment notification.",
588
      "Email unsubscribed from @count comment notifications."));
589
  }
590
}
591

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

    
598
  $form['comment_notify_settings'] = array();
599

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

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

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

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

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

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

    
650
  $form['comment_notify_settings']['comment_notify_default_registered_mailalert'] = array(
651
    '#type' => 'select',
652
    '#title' => t('Default state for the notification selection box for registered users'),
653
    '#return_value' => 1,
654
    '#default_value' => comment_notify_variable_registry_get('default_registered_mailalert'),
655
    '#description' => t('This flag presets the flag for the follow-up notification on the form that anon users will see when posting a comment'),
656
    '#options' => $available_options,
657
  );
658

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

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

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

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

    
693
  $form['#validate'] = array('comment_notify_settings_validate');
694

    
695
  return system_settings_form($form);
696
}
697

    
698
function comment_notify_settings_validate($form, &$form_state) {
699
  $sum_enabled = 0;
700
  foreach ($form_state['values']['comment_notify_available_alerts'] as $enabled) {
701
    $sum_enabled += $enabled;
702
  }
703
  if (!$sum_enabled) {
704
    form_set_error('comment_notify_available_alerts', 'You must enable at least one subscription mode.');
705
  }
706
}
707

    
708
/**
709
 * Get the unsubscribe link for a comment subscriber.
710
 *
711
 * @param $comment
712
 *   The subscribed comment object.
713
 *
714
 * @return
715
 *   A string with the internal path to the unsubscribe link, ready to be
716
 *   passed to the url() function.
717
 */
718
function comment_notify_get_unsubscribe_url($comment) {
719
  module_load_include('inc', 'comment_notify', 'comment_notify');
720
  if (!empty($comment->notify_hash)) {
721
    return 'comment_notify/disable/' . $comment->notify_hash;
722
  }
723
}
724
/**
725
 * Implements hook_field_extra_fields().
726
 */
727
function comment_notify_field_extra_fields() {
728
  module_load_include('inc', 'comment_notify', 'comment_notify');
729
  $extras = array();
730

    
731
  foreach (comment_notify_variable_registry_get('node_types') as $node_type) {
732
    if (isset($node_type)) {
733
      $extras['comment']['comment_node_' . $node_type]['form']['comment_notify_settings'] = array(
734
        'label' => t('Comment Notify settings'),
735
        'description' => t('@node_type settings for Comment Notify', array('@node_type' => ucwords($node_type))),
736
        'weight' => 1,
737
      );
738
    }
739
  }
740

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