comment_notify.module

Tracking 7.x-1.x branch
  1. drupal
    1. 7 contributions/comment_notify/comment_notify.module

This module provides comment follow-up e-mail notification for anonymous and registered users.

Constants

NameDescription
AUTHOR_MAILTEXT
COMMENT_NOTIFY_COMMENT
COMMENT_NOTIFY_DISABLED
COMMENT_NOTIFY_NODE
DEFAULT_MAILTEXT

Functions & methods

NameDescription
comment_notify_comment_delete
comment_notify_comment_insertImplements hook_comment_insert().
comment_notify_comment_loadImplements hook_comment_load().
comment_notify_comment_publish
comment_notify_comment_updateImplements hook_comment_update().
comment_notify_comment_validate
comment_notify_disable_pagePage callback to allow users to unsubscribe simply by visiting the page.
comment_notify_field_extra_fieldsImplements hook_field_extra_fields().
comment_notify_form_alterImplement hook_form_alter().
comment_notify_form_comment_form_alter
comment_notify_get_unsubscribe_urlGet the unsubscribe link for a comment subscriber.
comment_notify_initImplements hook_init().
comment_notify_mailImplements hook_mail().
comment_notify_menuImplements hook_menu().
comment_notify_permissionImplements hook_permission().
comment_notify_settings
comment_notify_settings_validate
comment_notify_unsubscribeCallback for an administrative form to unsubscribe users by e-mail address.
comment_notify_unsubscribe_submitBased on admin submit, do the actual unsubscribe from notifications.
comment_notify_user_cancel
comment_notify_user_load
comment_notify_user_update
_comment_notify_mailalertPrivate function to send the notifications.
_comment_notify_optionsProvide an array of available options for notification on a comment.

File

View source
  1. <?php
  2. /**
  3. * @file
  4. *
  5. * This module provides comment follow-up e-mail notification for anonymous and registered users.
  6. */
  7. define('COMMENT_NOTIFY_DISABLED', 0);
  8. define('COMMENT_NOTIFY_NODE', 1);
  9. define('COMMENT_NOTIFY_COMMENT', 2);
  10. define('AUTHOR_MAILTEXT',
  11. 'Hi [comment:node:author],
  12. You have received a comment on: "[comment:node:title]"
  13. ----
  14. [comment:title]
  15. [comment:body]
  16. ----
  17. You can view the comment at the following url
  18. [comment:url]
  19. You will receive emails like this for all replies to your posts. You can
  20. disable this by logging in and changing the settings on your user account at
  21. [comment:node:author:edit-url].
  22. -- [site:name] team
  23. [site:url]');
  24. define('DEFAULT_MAILTEXT',
  25. 'Hi [comment-subscribed:author],
  26. [comment:author] has commented on: "[comment:node:title]"
  27. ----
  28. [comment:title]
  29. [comment:body]
  30. ----
  31. You can view the comment at the following url
  32. [comment:url]
  33. You can stop receiving emails when someone replies to this post,
  34. by going to [comment-subscribed:unsubscribe-url]
  35. You can set up auto-following feature for all future posts
  36. by creating your own user with a few clicks here [site:login-url]
  37. -- [site:name] team
  38. [site:url]');
  39. /**
  40. * Implements hook_init().
  41. */
  42. function comment_notify_init() {
  43. // Add on every page - they are both very small so it's better to add
  44. // everywhere than force a second file on some pages.
  45. $options = array('every_page' => TRUE);
  46. $path = drupal_get_path('module', 'comment_notify');
  47. drupal_add_css($path . '/comment_notify.css', $options);
  48. // We only add the JS if more than one subscription mode is enabled.
  49. $available_options = _comment_notify_options();
  50. if (count($available_options) > 1) {
  51. drupal_add_js($path . '/comment_notify.js', $options);
  52. }
  53. }
  54. /**
  55. * Provide an array of available options for notification on a comment.
  56. */
  57. function _comment_notify_options() {
  58. $total_options = array(
  59. COMMENT_NOTIFY_NODE => t('All comments'),
  60. COMMENT_NOTIFY_COMMENT => t('Replies to my comment')
  61. );
  62. $available_options = array();
  63. $options = variable_get('comment_notify_available_alerts', drupal_map_assoc(array(COMMENT_NOTIFY_NODE, COMMENT_NOTIFY_COMMENT)));
  64. foreach ($options as $key => $available) {
  65. if ($key == $available) {
  66. $available_options[$available] = $total_options[$available];
  67. }
  68. }
  69. return $available_options;
  70. }
  71. function comment_notify_form_comment_form_alter(&$form, &$form_state, $form_id) {
  72. global $user;
  73. if (!(user_access('subscribe to comments') || user_access('administer comments'))) {
  74. return;
  75. }
  76. // Only add the checkbox if this is an enabled content type
  77. $node = node_load($form['nid']['#value'] ? $form['nid']['#value'] : $form['nid']['#default_value']);
  78. $enabled_types = variable_get('comment_notify_node_types', drupal_map_assoc(array($node->type)));
  79. if (empty($enabled_types[$node->type])) {
  80. return;
  81. }
  82. $available_options = _comment_notify_options();
  83. // Add the checkbox for anonymous users.
  84. if ($user->uid == 0) {
  85. // If anonymous users can't enter their e-mail don't tempt them with the checkbox.
  86. if (empty($form['author']['mail'])) {
  87. return;
  88. }
  89. $form['#validate'][] = 'comment_notify_comment_validate';
  90. }
  91. module_load_include('inc', 'comment_notify', 'comment_notify');
  92. $preference = comment_notify_get_user_comment_notify_preference($user->uid);
  93. // If you want to hide this on your site see http://drupal.org/node/322482
  94. $form['notify_settings']['notify'] = array(
  95. '#type' => 'checkbox',
  96. '#title' => t('Notify me when new comments are posted'),
  97. '#default_value' => (bool) $preference,
  98. );
  99. $form['notify_settings']['notify_type'] = array(
  100. '#type' => 'radios',
  101. '#options' => $available_options,
  102. '#default_value' => $preference ? $preference : 1,
  103. );
  104. if (count($available_options) == 1) {
  105. $form['notify_settings']['notify_type']['#type'] = 'hidden';
  106. $form['notify_settings']['notify_type']['#value'] = key($available_options);
  107. }
  108. // If this is an existing comment we set the default value based on their selection last time.
  109. if ($form['cid']['#value'] != '') {
  110. $notify = comment_notify_get_notification_type($form['cid']['#value']);
  111. $form['notify_settings']['notify']['#default_value'] = (bool) $notify;
  112. if (count($available_options) > 1) {
  113. $form['notify_settings']['notify_type']['#default_value'] = empty($notify) ? COMMENT_NOTIFY_NODE : $notify;
  114. }
  115. else {
  116. $form['notify_settings']['notify_type']['#default_value'] = key($available_options);
  117. }
  118. }
  119. }
  120. /**
  121. * Implements hook_permission().
  122. */
  123. function comment_notify_permission() {
  124. return array(
  125. 'administer comment notify' => array(
  126. 'title' => 'Administer Comment Notify',
  127. 'description' => 'Change global comment notification settings.',
  128. ),
  129. 'subscribe to comments' => array(
  130. 'title' => 'Subscribe to comment notifications',
  131. 'description' => 'Subscribe to recieve notifications when new comments are posted.',
  132. ),
  133. );
  134. }
  135. /**
  136. * Implements hook_menu().
  137. */
  138. function comment_notify_menu() {
  139. $items['admin/config/people/comment_notify'] = array(
  140. 'title' => 'Comment Notify',
  141. 'description' => 'Configure settings for e-mails about new comments.',
  142. 'page callback' => 'drupal_get_form',
  143. 'page arguments' => array('comment_notify_settings'),
  144. 'access arguments' => array('administer comment notify'),
  145. 'type' => MENU_NORMAL_ITEM,
  146. );
  147. $items['admin/config/people/comment_notify/settings'] = array(
  148. 'title' => 'Settings',
  149. 'description' => 'Configure settings for e-mails about new comments.',
  150. 'page callback' => 'drupal_get_form',
  151. 'page arguments' => array('comment_notify_settings'),
  152. 'access arguments' => array('administer comment notify'),
  153. 'type' => MENU_DEFAULT_LOCAL_TASK,
  154. );
  155. $items['admin/config/people/comment_notify/unsubscribe'] = array(
  156. 'title' => 'Unsubscribe',
  157. 'description' => 'Unsubscribe an email from all notifications.',
  158. 'weight' => 2,
  159. 'page callback' => 'drupal_get_form',
  160. 'page arguments' => array('comment_notify_unsubscribe'),
  161. 'access arguments' => array('administer comment notify'),
  162. 'type' => MENU_LOCAL_TASK,
  163. );
  164. $items['comment_notify/disable/%'] = array(
  165. 'title' => 'Disable comment notification',
  166. 'page callback' => 'comment_notify_disable_page',
  167. 'page arguments' => array(2),
  168. 'access arguments' => array('access content'),
  169. 'type' => MENU_CALLBACK
  170. );
  171. return $items;
  172. }
  173. /**
  174. * Page callback to allow users to unsubscribe simply by visiting the page.
  175. */
  176. function comment_notify_disable_page($hash) {
  177. module_load_include('inc', 'comment_notify', 'comment_notify');
  178. if (comment_notify_unsubscribe_by_hash($hash)) {
  179. return(t('Your comment follow-up notification for this post was disabled. Thanks.'));
  180. }
  181. else {
  182. return(t('Sorry, there was a problem unsubscribing from notifications.'));
  183. }
  184. }
  185. function comment_notify_comment_validate($comment) {
  186. global $user;
  187. // We assume that if they are non-anonymous then they have a valid mail.
  188. // For anonymous users, though, we verify that they entered a mail and let comment.module validate it is real.
  189. if (!$user->uid && $comment['notify_settings']['notify']['#value'] && empty($comment['author']['mail']['#value'])) {
  190. form_set_error('mail', t('If you want to subscribe to comments you must supply a valid e-mail address.'));
  191. }
  192. }
  193. function comment_notify_comment_publish($comment) {
  194. // And send notifications - the real purpose of the module.
  195. _comment_notify_mailalert($comment);
  196. }
  197. /**
  198. * Implements hook_comment_update().
  199. */
  200. function comment_notify_comment_update($comment) {
  201. module_load_include('inc', 'comment_notify', 'comment_notify');
  202. // Take the status of the "notify" checkbox if they unchecked it.
  203. if (empty($comment->notify)) {
  204. $status = COMMENT_NOTIFY_DISABLED;
  205. }
  206. else {
  207. $status = $comment->notify_type;
  208. }
  209. // In case they have changed their status, save it in the database.
  210. if (isset($status)) {
  211. comment_notify_update_notification($comment->cid, $status);
  212. }
  213. // And send notifications - the real purpose of the module.
  214. if ($comment->status == COMMENT_PUBLISHED) {
  215. _comment_notify_mailalert($comment);
  216. }
  217. }
  218. /**
  219. * Implements hook_comment_insert().
  220. */
  221. function comment_notify_comment_insert($comment) {
  222. module_load_include('inc', 'comment_notify', 'comment_notify');
  223. global $user;
  224. // For new comments, we first build up a string to be used as the identifier for the alert.
  225. // This identifier is used to later unsubscribe the user or allow them to
  226. // potentially edit their comment / preferences if they are anonymous.
  227. // The string is built with token and their host and comment identifier.
  228. // It is stored and referenced, we really just need something unique/unguessable.
  229. $hostname = isset($comment->hostname) ? $comment->hostname : (isset($user->hostname) ? $user->hostname : '');
  230. $notify_hash = drupal_get_token($hostname . $comment->cid);
  231. if (!empty($comment->notify)) {
  232. $notify = $comment->notify_type;
  233. // If they don't have a preference, save one.
  234. $current = comment_notify_get_user_comment_notify_preference($user->uid);
  235. if ($current == 0 && $user->uid) {
  236. comment_notify_set_user_notification_setting($user->uid, NULL, $comment->notify_type);
  237. }
  238. }
  239. else {
  240. $notify = 0;
  241. }
  242. // And then save the data.
  243. comment_notify_add_notification($comment->cid, $notify, $notify_hash);
  244. // And send notifications - the real purpose of the module.
  245. if ($comment->status == COMMENT_PUBLISHED) {
  246. _comment_notify_mailalert($comment);
  247. }
  248. }
  249. function comment_notify_comment_delete($comment) {
  250. module_load_include('inc', 'comment_notify', 'comment_notify');
  251. comment_notify_remove_all_notifications($comment->cid);
  252. }
  253. /**
  254. * Implement hook_form_alter().
  255. */
  256. function comment_notify_form_alter(&$form, &$form_state, $form_id) {
  257. module_load_include('inc', 'comment_notify', 'comment_notify');
  258. if (!($form_id == 'user_register_form' || $form_id == 'user_profile_form')) {
  259. return;
  260. }
  261. elseif ($form['#user_category'] != 'account') {
  262. return;
  263. }
  264. $user = $form['#user'];
  265. if (!empty($user->comment_notify_settings)) {
  266. $node_notify = $user->comment_notify_settings->node_notify;
  267. $comment_notify = $user->comment_notify_settings->comment_notify;
  268. }
  269. $form['comment_notify_settings'] = array(
  270. '#type' => 'fieldset',
  271. '#title' => t('Comment follow-up notification settings'),
  272. '#weight' => 4,
  273. '#collapsible' => TRUE
  274. );
  275. // Only show the node followup UI if the user has permission to create nodes.
  276. $nodes = FALSE;
  277. foreach (node_type_get_names() as $type => $name) {
  278. if (node_access('create', $type)) {
  279. $nodes = TRUE;
  280. break;
  281. }
  282. }
  283. if (user_access('administer nodes') || $nodes) {
  284. $form['comment_notify_settings']['node_notify'] = array(
  285. '#type' => 'checkbox',
  286. '#title' => t('Receive content follow-up notification e-mails'),
  287. '#default_value' => isset($node_notify) ? $node_notify : comment_notify_variable_registry_get('node_notify_default_mailalert'),
  288. '#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.')
  289. );
  290. }
  291. else {
  292. $form['comment_notify_settings']['node_notify'] = array(
  293. '#type' => 'hidden',
  294. '#value' => COMMENT_NOTIFY_DISABLED,
  295. );
  296. }
  297. $available_options[COMMENT_NOTIFY_DISABLED] = t('No notifications');
  298. $available_options += _comment_notify_options();
  299. $form['comment_notify_settings']['comment_notify'] = array(
  300. '#type' => 'select',
  301. '#title' => t('Receive comment follow-up notification e-mails'),
  302. '#default_value' => isset($comment_notify) ? array($comment_notify) : array(comment_notify_variable_registry_get('default_registered_mailalert')),
  303. '#options' => $available_options,
  304. '#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.")
  305. );
  306. return $form;
  307. // Construct the user form
  308. }
  309. function comment_notify_user_update(&$edit, $account, $category) {
  310. if ($category != 'account') {
  311. return;
  312. }
  313. if (isset($edit['node_notify']) && isset($edit['comment_notify'])) {
  314. module_load_include('inc', 'comment_notify', 'comment_notify');
  315. // Save the values of node_notify_mailalert and comment_notify_mailalert
  316. // to {comment_notify_user_settings}.
  317. comment_notify_set_user_notification_setting($account->uid, $edit['node_notify'], $edit['comment_notify']);
  318. }
  319. // Unset them from $user so they don't also get saved into {users}.data.
  320. unset($edit['node_notify']);
  321. unset($edit['comment_notify']);
  322. }
  323. function comment_notify_user_load($users) {
  324. module_load_include('inc', 'comment_notify', 'comment_notify');
  325. // @todo: Why would we want to load this on every user load?
  326. foreach ($users as &$user) {
  327. $user->comment_notify_settings = comment_notify_get_user_notification_setting($user->uid);
  328. }
  329. return;
  330. }
  331. function comment_notify_user_cancel($edit, $account, $method) {
  332. module_load_include('inc', 'comment_notify', 'comment_notify');
  333. comment_notify_delete_user_notification_setting($account->uid);
  334. }
  335. /**
  336. * Implements hook_comment_load().
  337. */
  338. function comment_notify_comment_load($comments) {
  339. // Load some comment_notify specific information into the comment object.
  340. $query = db_select('comment_notify', 'cn');
  341. $query->join('comment', 'c', 'c.cid = cn.cid');
  342. $query->leftJoin('users', 'u', 'c.uid = u.uid');
  343. $query->condition('c.cid', array_keys($comments));
  344. $query->fields('cn', array('cid', 'notify', 'notify_hash', 'notified'));
  345. $query->addField('c', 'mail', 'cmail');
  346. $query->addField('u', 'init', 'uinit');
  347. $query->addField('u', 'mail', 'umail');
  348. $records = $query->execute()->fetchAllAssoc('cid');
  349. foreach ($records as $cid => $record) {
  350. $comments[$cid]->notify = $record->notify;
  351. $comments[$cid]->notify_type = $record->notify;
  352. $comments[$cid]->notify_hash = $record->notify_hash;
  353. $comments[$cid]->notified = $record->notified;
  354. $comments[$cid]->cmail = $record->cmail;
  355. $comments[$cid]->uinit = $record->uinit;
  356. $comments[$cid]->umail = $record->umail;
  357. }
  358. }
  359. /**
  360. * Private function to send the notifications.
  361. *
  362. * @param $comment
  363. * The comment array as found in hook_comment $op = publish.
  364. */
  365. function _comment_notify_mailalert($comment) {
  366. module_load_include('inc', 'comment_notify', 'comment_notify');
  367. $comment = (object) $comment;
  368. global $language;
  369. global $base_url;
  370. global $user;
  371. $initial_language = $language;
  372. $nid = $comment->nid;
  373. $cid = $comment->cid;
  374. // Check to see if a notification has already been sent for this
  375. // comment so that edits to a comment don't trigger an additional
  376. // notification.
  377. if (!empty($comment->notified)) {
  378. return;
  379. }
  380. $node = node_load($nid);
  381. // No mails if this is not an enabled content type.
  382. $enabled_types = variable_get('comment_notify_node_types', array($node->type => TRUE));
  383. if (empty($enabled_types[$node->type])) {
  384. return;
  385. }
  386. if (empty($comment->mail)) {
  387. $comment_account = user_load_by_name($comment->name);
  388. $comment_mail = isset($comment_account->mail) ? $comment_account->mail : '';
  389. }
  390. else {
  391. $comment_mail = $comment->mail;
  392. }
  393. $sent_to = array();
  394. // Send to a subscribed author if they are not the current commenter.
  395. $author = user_load($node->uid);
  396. // Do they explicitly want this? Or is it default to send to users?
  397. // Is the comment author not the node author? Do they have access? Do they have an email (e.g. anonymous)?
  398. 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) && !empty($author->mail)) {
  399. // Get the author's language.
  400. $language = user_preferred_language($author);
  401. $raw_values = array(
  402. 'subject' => comment_notify_variable_registry_get('author_subject'),
  403. 'body' => comment_notify_variable_registry_get('node_notify_default_mailtext'), //JS @todo:change this.
  404. );
  405. foreach ($raw_values as $k => $v) {
  406. $message[$k] = token_replace(t($v), array('comment' => $comment), array('sanitize' => FALSE));
  407. }
  408. drupal_mail('comment_notify', 'comment_notify_mail', $author->mail, $language, $message);
  409. $sent_to[] = strtolower($author->mail);
  410. }
  411. // For "reply to my comments" notifications, figure out what thread this is.
  412. $thread = isset($comment->thread) ? $comment->thread : '';
  413. // Get the list of commenters to notify.
  414. $watchers = comment_notify_get_watchers($nid);
  415. foreach ($watchers as $alert) {
  416. // If the user is not anonymous, always load the current e-mail address
  417. // from his or her user account instead of trusting $comment->mail.
  418. $recipient_user = !empty($alert->uid) ? user_load($alert->uid) : drupal_anonymous_user();
  419. $mail = !empty($recipient_user->mail) ? $recipient_user->mail : $alert->cmail;
  420. $relevant_thread = drupal_substr($thread, 0, drupal_strlen($alert->thread) -1);
  421. if ($alert->notify == COMMENT_NOTIFY_COMMENT && strcmp($relevant_thread . '/', $alert->thread) != 0) {
  422. continue;
  423. }
  424. if ($mail != $comment_mail && !in_array(strtolower($mail), $sent_to) && ($alert->uid != $comment->uid || $alert->uid == 0)) {
  425. $message = array();
  426. $language = !empty($alert->uid) ? user_preferred_language($recipient_user) : language_default();
  427. // Make sure they have access to this node before showing a bunch of node information.
  428. if (!node_access('view', $node, $recipient_user)) {
  429. continue;
  430. }
  431. $raw_values = array(
  432. 'subject' => comment_notify_variable_registry_get('watcher_subject'),
  433. 'body' => comment_notify_variable_registry_get('comment_notify_default_mailtext'), //JS @todo:change this var name.
  434. );
  435. foreach ($raw_values as $k => $v) {
  436. $message[$k] = token_replace(t($v), array('comment' => $comment, 'comment-subscribed' => $alert), array('sanitize' => FALSE));
  437. }
  438. drupal_mail('comment_notify', 'comment_notify_mail', $mail, $language, $message);
  439. $sent_to[] = strtolower($mail);
  440. // Make the mail link to user's /edit, unless it's an anonymous user.
  441. if ($alert->uid != 0) {
  442. $user_mail = l($mail, 'user/' . $alert->uid . '/edit');
  443. }
  444. else {
  445. $user_mail = check_plain($mail);
  446. }
  447. // Add an entry to the watchdog log.
  448. watchdog(
  449. 'comment_notify',
  450. 'Notified: @user_mail',
  451. array('@user_mail' => $user_mail),
  452. WATCHDOG_NOTICE,
  453. l(t('source comment'), 'node/' . $nid, array(
  454. 'fragment' => 'comment-' . $alert->cid,
  455. ))
  456. );
  457. // Revert to previous (site default) locale.
  458. $language = $initial_language;
  459. }
  460. }
  461. // Record that a notification was sent for this comment so that
  462. // notifications aren't sent again if the comment is later edited.
  463. comment_notify_mark_comment_as_notified($comment);
  464. }
  465. /**
  466. * Implements hook_mail().
  467. */
  468. function comment_notify_mail($key, &$message, $params) {
  469. $message['subject'] = $params['subject'];
  470. $message['body'][] = $params['body'];
  471. }
  472. /**
  473. * Callback for an administrative form to unsubscribe users by e-mail address.
  474. */
  475. function comment_notify_unsubscribe($form, &$form_state) {
  476. $form['comment_notify_unsubscribe'] = array();
  477. $form['comment_notify_unsubscribe']['email_to_unsubscribe'] = array(
  478. '#type' => 'textfield',
  479. '#title' => t('Email to unsubscribe'),
  480. );
  481. $form['comment_notify_unsubscribe']['submit'] = array(
  482. '#type' => 'submit',
  483. '#value' => t('Unsubscribe this e-mail'),
  484. );
  485. return $form;
  486. }
  487. /**
  488. * Based on admin submit, do the actual unsubscribe from notifications.
  489. */
  490. function comment_notify_unsubscribe_submit($form, &$form_state) {
  491. module_load_include('inc', 'comment_notify', 'comment_notify');
  492. $email = trim($form_state['values']['email_to_unsubscribe']);
  493. $comments = comment_notify_unsubscribe_by_email($email);
  494. // Update the admin about the state of this comment notification subscription.
  495. if ($comments == 0) {
  496. drupal_set_message(t("There were no active comment notifications for that email."));
  497. }
  498. else {
  499. drupal_set_message(format_plural($comments, "Email unsubscribed from 1 comment notification.",
  500. "Email unsubscribed from @count comment notifications."));
  501. }
  502. }
  503. /*
  504. * Page callback for administrative settings form.
  505. */
  506. function comment_notify_settings() {
  507. module_load_include('inc', 'comment_notify', 'comment_notify');
  508. $form['comment_notify_settings'] = array();
  509. // Only perform comment_notify for certain node types.
  510. $enabled_types = comment_notify_variable_registry_get('node_types');
  511. $anonymous_problems = '';
  512. foreach (node_type_get_names() as $type => $name) {
  513. $checkboxes[$type] = check_plain($name);
  514. $default[] = $type;
  515. // If they don't have the ability to leave contact info, then we make a report
  516. if (isset($enabled_types[$type]) && $enabled_types[$type] && variable_get('comment_anonymous_' . $type, COMMENT_ANONYMOUS_MAYNOT_CONTACT) == COMMENT_ANONYMOUS_MAYNOT_CONTACT) {
  517. $account = drupal_anonymous_user();
  518. if (user_access('subscribe to comments', $account)) {
  519. $anonymous_problems[] = l(t('@content-type', array('@content-type' => $name)), 'admin/structure/types/manage/' . $type);
  520. }
  521. }
  522. }
  523. if (!empty($anonymous_problems)) {
  524. 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);
  525. }
  526. $form['comment_notify_settings']['comment_notify_node_types'] = array(
  527. '#type' => 'checkboxes',
  528. '#title' => t('Content types to enable for comment notification'),
  529. '#default_value' => $enabled_types,
  530. '#options' => $checkboxes,
  531. '#description' => t('Comments on content types enabled here will have the option of comment notification.'),
  532. );
  533. $form['comment_notify_settings']['comment_notify_available_alerts'] = array(
  534. '#type' => 'checkboxes',
  535. '#title' => t('Available subscription modes'),
  536. '#return_value' => 1,
  537. '#default_value' => comment_notify_variable_registry_get('available_alerts'),
  538. '#description' => t('Choose which notification subscription styles are available for users'),
  539. '#options' => array(
  540. COMMENT_NOTIFY_NODE => t('All comments'),
  541. COMMENT_NOTIFY_COMMENT => t('Replies to my comment')
  542. )
  543. );
  544. $available_options[COMMENT_NOTIFY_DISABLED] = t('No notifications');
  545. $available_options += _comment_notify_options();
  546. $form['comment_notify_settings']['comment_notify_default_anon_mailalert'] = array(
  547. '#type' => 'select',
  548. '#title' => t('Default state for the notification selection box for anonymous users'),
  549. '#return_value' => 1,
  550. '#default_value' => comment_notify_variable_registry_get('default_anon_mailalert'),
  551. '#options' => $available_options,
  552. );
  553. $form['comment_notify_settings']['comment_notify_default_registered_mailalert'] = array(
  554. '#type' => 'select',
  555. '#title' => t('Default state for the notification selection box for registered users'),
  556. '#return_value' => 1,
  557. '#default_value' => comment_notify_variable_registry_get('default_registered_mailalert'),
  558. '#description' => t('This flag presets the flag for the follow-up notification on the form that anon users will see when posting a comment'),
  559. '#options' => $available_options,
  560. );
  561. $form['comment_notify_settings']['comment_notify_node_notify_default_mailalert'] = array(
  562. '#type' => 'checkbox',
  563. '#title' => t('Subscribe users to their node follow-up notification emails by default'),
  564. '#default_value' => comment_notify_variable_registry_get('node_notify_default_mailalert'),
  565. '#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.'),
  566. );
  567. $form['comment_notify_settings']['comment_notify_comment_notify_default_mailtext'] = array(
  568. '#type' => 'textarea',
  569. '#title' => t('Default mail text for sending out notifications to commenters'),
  570. '#default_value' => comment_notify_variable_registry_get('comment_notify_default_mailtext'),
  571. '#return_value' => 1,
  572. '#cols' => 80,
  573. '#rows' => 15,
  574. '#token_types' => array('comment'),
  575. '#element_validate' => array('token_element_validate'),
  576. );
  577. $form['comment_notify_settings']['comment_notify_node_notify_default_mailtext'] = array(
  578. '#type' => 'textarea',
  579. '#title' => t('Default mail text for sending out the notifications to node authors'),
  580. '#default_value' => comment_notify_variable_registry_get('node_notify_default_mailtext'),
  581. '#return_value' => 1,
  582. '#cols' => 80,
  583. '#rows' => 15,
  584. '#token_types' => array('comment'),
  585. '#element_validate' => array('token_element_validate'),
  586. );
  587. $form['comment_notify_settings']['token_help'] = array(
  588. '#theme' => 'token_tree',
  589. '#token_types' => array('comment'),
  590. );
  591. $form['#validate'] = array('comment_notify_settings_validate');
  592. return system_settings_form($form);
  593. }
  594. function comment_notify_settings_validate($form, &$form_state) {
  595. $sum_enabled = 0;
  596. foreach ($form_state['values']['comment_notify_available_alerts'] as $enabled) {
  597. $sum_enabled += $enabled;
  598. }
  599. if (!$sum_enabled) {
  600. form_set_error('comment_notify_available_alerts', 'You must enable at least one subscription mode.');
  601. }
  602. }
  603. /**
  604. * Get the unsubscribe link for a comment subscriber.
  605. *
  606. * @param $comment
  607. * The subscribed comment object.
  608. *
  609. * @return
  610. * A string with the internal path to the unsubscribe link, ready to be
  611. * passed to the url() function.
  612. */
  613. function comment_notify_get_unsubscribe_url($comment) {
  614. module_load_include('inc', 'comment_notify', 'comment_notify');
  615. if (!empty($comment->notify_hash)) {
  616. return 'comment_notify/disable/' . $comment->notify_hash;
  617. }
  618. }
  619. /**
  620. * Implements hook_field_extra_fields().
  621. */
  622. function comment_notify_field_extra_fields() {
  623. module_load_include('inc', 'comment_notify', 'comment_notify');
  624. $extras = array();
  625. foreach (comment_notify_variable_registry_get('node_types') as $node_type) {
  626. if (isset($node_type)) {
  627. $extras['comment']['comment_node_' . $node_type]['form']['comment_notify_settings'] = array(
  628. 'label' => t('Comment Notify settings'),
  629. 'description' => t('@node_type settings for Comment Notify', array('@node_type' => ucwords($node_type))),
  630. 'weight' => 1,
  631. );
  632. }
  633. }
  634. $extras['user']['user']['form']['comment_notify_settings'] = array(
  635. 'label' => t('Comment Notify settings'),
  636. 'description' => t('User settings for Comment Notify'),
  637. 'weight' => 4,
  638. );
  639. return $extras;
  640. }