t('All comments'), COMMENT_NOTIFY_COMMENT => t('Replies to my comment') ); $available_options = array(); $options = variable_get('comment_notify_available_alerts', drupal_map_assoc(array(COMMENT_NOTIFY_NODE, COMMENT_NOTIFY_COMMENT))); foreach ($options as $key => $available) { if ($key == $available) { $available_options[$available] = $total_options[$available]; } } return $available_options; } function comment_notify_form_comment_form_alter(&$form, &$form_state, $form_id) { global $user; if (!(user_access('subscribe to comments') || user_access('administer comments'))) { return; } // 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; } $available_options = _comment_notify_options(); drupal_add_css(drupal_get_path('module', 'comment_notify') .'/comment_notify.css'); drupal_add_js(drupal_get_path('module', 'comment_notify') .'/comment_notify.js'); // Add the checkbox for anonymous users and set the default based on admin settings. if ($user->uid == 0) { // If anonymous user's can't enter their e-mail don't tempt them with the checkbox. if (empty($form['mail'])) { return; } $preference = variable_get('comment_notify_default_anon_mailalert', COMMENT_NOTIFY_DISABLED); } else { $preference = comment_notify_get_user_comment_notify_preference($user->uid); } // If you want to hide this on your site see http://drupal.org/node/322482 $form['notify_settings'] = array( '#prefix' => '
', '#suffix' => '
', ); $form['notify_settings']['notify'] = array( '#type' => 'checkbox', '#title' => t('Notify me when new comments are posted'), '#default_value' => $preference, ); if (count($available_options) > 1) { $form['notify_settings']['notify_type'] = array( '#type' => 'radios', '#options' => $available_options, '#default_value' => $preference, ); } else { $form['notify_settings']['notify_type'] = array( '#type' => 'hidden', '#value' => key($available_options), ); } $form['notify_settings']['notify_type']['#default_value'] = $preference; // 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']); $form['notify_settings']['notify']['#default_value'] = $notify; if (count($available_options) > 1) { $form['notify_settings']['notify_type']['#default_value'] = $notify; } else { $form['notify_settings']['notify_type']['#default_value'] = key($available_options); } } } /** * Implement of hook_permission(). */ 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.', ), ); } /** * Implement of hook_menu(). */ function comment_notify_menu() { $items['admin/config/people/comment_notify'] = array( 'title' => 'Comment notify', 'description' => 'Configure settings for e-mails about new comments.', 'page callback' => 'drupal_get_form', 'page arguments' => array('comment_notify_settings'), 'access arguments' => array('administer comment notify'), 'type' => MENU_NORMAL_ITEM, ); $items['admin/config/people/comment_notify/settings'] = array( 'title' => 'Settings', 'description' => 'Configure settings for e-mails about new comments.', 'page callback' => 'drupal_get_form', 'page arguments' => array('comment_notify_settings'), 'access arguments' => array('administer comment notify'), 'type' => MENU_DEFAULT_LOCAL_TASK, ); $items['admin/config/people/comment_notify/unsubscribe'] = array( 'title' => 'Unsubscribe', 'description' => 'Unsubscribe an email from all notifications.', 'weight' => 2, 'page callback' => 'drupal_get_form', 'page arguments' => array('comment_notify_unsubscribe'), 'access arguments' => array('administer comment notify'), 'type' => MENU_LOCAL_TASK, ); $items['comment_notify/disable/%'] = array( 'title' => 'Disable comment notification', 'page callback' => 'comment_notify_disable_page', 'page arguments' => array(2), 'access arguments' => array('access content'), 'type' => MENU_CALLBACK ); return $items; } /** * Page callback to allow users to unsubscribe simply by visiting the page. */ function comment_notify_disable_page($hash) { if (comment_notify_unsubscribe_by_hash($hash)) { drupal_set_message(t('Your comment follow-up notification for this post was disabled. Thanks.')); } else { drupal_set_message(t('Sorry, there was a problem unsubscribing from notifications.')); } return ' '; } function comment_notify_comment_validate($comment) { global $user; // 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. if (!$user->uid && $comment->notify && empty($comment->mail)) { form_set_error('mail', t('If you want to subscribe to comments you must supply a valid e-mail address.')); } } function comment_notify_comment_publish($comment) { // And send notifications - the real purpose of the module. _comment_notify_mailalert($comment); } function comment_notify_comment_update($comment) { // In case they have changed their status, save it in the database. comment_notify_update_notification($comment->cid, $comment->notify_type); // And send notifications - the real purpose of the module. if ($comment->status == COMMENT_PUBLISHED) { _comment_notify_mailalert($comment); } } function comment_notify_comment_insert($comment) { global $user; // For new comments, we first build up a string to be used as the identifier for the alert // JS: This is terribly broken. What if the user changes their email??!? $mail = empty($comment->mail) ? $user->mail : $comment->mail; $notify_hash = drupal_get_token($mail . $comment->cid); if ($comment->notify) { $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); } } function comment_notify_comment_delete($comment) { comment_notify_remove_all_notifications($comment->cid); } /** * Implement hook_form_alter(). */ function comment_notify_form_alter(&$form, &$form_state, $form_id) { if (!($form_id == 'user_register_form' || $form_id == 'user_profile_form')) { return; } elseif ($form['#user_category'] != 'account'){ 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( '#type' => 'fieldset', '#title' => t('Comment follow-up notification settings'), '#weight' => 4, '#collapsible' => TRUE ); // Only show the node followup UI if the user has permission to create nodes. $nodes = FALSE; foreach (node_type_get_names() as $type) { if (user_access('create '. $type .' content')) { $nodes = TRUE; break; } } if (user_access('administer nodes') || $nodes) { $form['comment_notify_settings']['node_notify'] = array( '#type' => 'checkbox', '#title' => t('Receive node follow-up notification e-mails'), '#default_value' => isset($node_notify) ? $node_notify : comment_notify_variable_registry_get('node_notify_default_mailalert'), '#description' => t('Check this box to receive an e-mail notification for follow-ups on your nodes (pages, forum topics, etc). You can not disable notifications for individual threads.') ); } else { $form['comment_notify_settings']['node_notify'] = array( '#type' => 'hidden', '#value' => COMMENT_NOTIFY_DISABLED, ); } $available_options[COMMENT_NOTIFY_DISABLED] = t('No notifications'); $available_options += _comment_notify_options(); $form['comment_notify_settings']['comment_notify'] = array( '#type' => 'select', '#title' => t('Receive comment follow-up notification e-mails'), '#default_value' => isset($comment_notify) ? array($comment_notify) : array(comment_notify_variable_registry_get('comment_notify_default_registered_mailalert')), '#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.") ); return $form; // Construct the user form } function comment_notify_user_update(&$edit, $account, $category) { if ($category != 'account') { return; } if (isset($edit['node_notify']) && isset($edit['comment_notify'])) { // 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']); } function comment_notify_user_load(&$users) { // Why would we want to load this on every user load? foreach ($users as &$user) { $user->comment_notify_settings = comment_notify_get_user_notification_setting($user->uid); } return; // @todo: Fix this naming weirdness. foreach ($users as $user) { $result = db_select('comment_notify_user_settings', 'cnus') ->fields('cnus', array('node_notify' => 'node_notify_mailalert', 'comment_notify' => 'comment_notify_mailalert')) ->condition('uid', $user->uid) ->execute(); } } function comment_notify_user_cancel(&$edit, $account, $method) { comment_notify_delete_user_notification_setting($account->uid); } /** * Private function to send the notifications. * * @param $comment * The comment array as found in hook_comment $op = publish. */ function _comment_notify_mailalert($comment) { $comment = (object) $comment; global $language; global $base_url; global $user; $nid = $comment->nid; $cid = $comment->cid; // Check to see if a notification has already been sent for this // comment so that edits to a comment don't trigger an additional // notification. if (comment_notify_is_notification_already_sent($cid)) { return; } $initial_language = $language; if (function_exists('locale')) { $languages = locale_language_list(); $languages = $languages['name']; } $node = node_load($nid); // 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; } if (!isset($comment->mail)) { $comment_account = user_load(array('name' => $comment->name)); $comment_mail = $comment_account->mail; } else { $comment_mail = $comment->mail; } $sent_to = array(); // Send to a subscribed author if they are not the current commenter $author = user_load(array('uid' => $node->uid)); if (!empty($author->comment_notify_settings->node_notify) && $author->node_notify_mailalert == 1 && $user->uid != $author->uid && node_access('view', $node, $author)) { // Get the author's language. $language = user_preferred_language($author); $raw_values = array ( 'subject' => comment_notify_variable_registry_get('author_subject'), 'body' => comment_notify_variable_registry_get('node_notify_default_mailtext'), //JS @todo:change this. ); foreach ($raw_values as $k => $v) { $message[$k] = comment_notify_tokenize(t($v), $comment, $node, $user); } drupal_mail('comment_notify', 'comment_notify_mail', $author->mail, $language, $message); $sent_to[] = $author->mail; } // For "reply to my comments" notifications, figure out what thread this is. $thread = comment_notify_get_thread($cid); // TODO? the original big query had stuff making sure the mail was populated and contained .+@.+ Perhaps check for that here and set notify = 0 if that is the case for this cid // Get the list of commenters to notify. $watchers = comment_notify_get_watchers($nid); foreach ($watchers as $alert) { $umail = empty($alert->umail) ? $alert->uinit : $alert->umail; $mail = empty($alert->cmail) ? $umail : $alert->cmail; $relevant_thread = substr($thread, 0, drupal_strlen($alert->thread) -1); if ($alert->notify == COMMENT_NOTIFY_COMMENT && strcmp($relevant_thread . '/', $alert->thread) != 0) { continue; } if ($mail != $comment_mail && !in_array($mail, $sent_to) && ($alert->uid != $comment->uid || $alert->uid == 0)) { $message = array(); if (!empty($alert->uid)) { $recipient_user = user_load(array('uid' => $alert->uid)); $language = user_preferred_language($recipient_user); } else { $language = language_default(); $recipient_user = drupal_anonymous_user(); } // Make sure they have access to this node before showing a bunch of node information. if (!node_access('view', $node, $recipient_user)) { continue; } $raw_values = array ( 'subject' => comment_notify_variable_registry_get('watcher_subject'), 'body' => comment_notify_variable_registry_get('comment_notify_default_mailtext'), //JS @todo:change this var name. ); foreach ($raw_values as $k => $v) { $message[$k] = comment_notify_tokenize(t($v), $comment, $node, $user); } // array( // '!link1' => url('comment_notify/disable/'. $alert->notify_hash, array('absolute' => TRUE)) // ) drupal_mail('comment_notify', 'comment_notify_mail', $mail, $language, $message); $sent_to[] = $mail; // Make the mail link to user's /edit, unless it's an anonymous user. if ($alert->uid != 0) { $user_mail = l($mail, 'user/'. $alert->uid .'/edit'); } else { $user_mail = check_plain($mail); } // Add an entry to the watchdog log. watchdog( 'comment_notify', 'Notified: !user_mail', array('!user_mail' => $user_mail), WATCHDOG_NOTICE, l(t('source comment'), 'node/'. $nid, array( 'fragment' => 'comment-'. $alert->cid, )) ); // revert to previous (site default) locale $language = $initial_language; } } // Record that a notification was sent for this comment so that // notifications aren't sent again if the comment is later edited. comment_notify_mark_comment_as_notified($cid); } /** * Implement of hook_mail(). */ function comment_notify_mail($key, &$message, $params) { $message['subject'] = $params['subject']; $message['body'][] = $params['body']; } /** * Callback for an administrative form to unsubscribe users by e-mail address. */ function comment_notify_unsubscribe($form, &$form_state) { $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; } /** * Based on admin submit, do the actual unsubscribe from notifications. */ function comment_notify_unsubscribe_submit($form, &$form_state) { $email = trim($form_state['values']['email_to_unsubscribe']); $comments = comment_notify_unsubscribe_by_email($email); // 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.")); } } /* * Page callback for administrative settings form. */ function comment_notify_settings() { $form['comment_notify_settings'] = array(); // Only perform comment_notify for certain node types. $enabled_types = comment_notify_variable_registry_get('node_types'); $anonymous_problems = ''; foreach (node_type_get_names() as $type => $name) { $checkboxes[$type] = check_plain($name); $default[] = $type; // If they don't have the ability to leave contact info, then we make a report if (isset($enabled_types[$type]) && $enabled_types[$type] && variable_get('comment_anonymous_'. $type, COMMENT_ANONYMOUS_MAYNOT_CONTACT) == COMMENT_ANONYMOUS_MAYNOT_CONTACT) { $account = drupal_anonymous_user(); if (user_access('subscribe to comments', $account)) { $anonymous_problems[] = l(t('@content-type', array('@content-type' => $name)), 'admin/content/node-type/'. $type); } } } if (!empty($anonymous_problems)) { drupal_set_message(t('Anonymous commenters have the permission to subscribe to comments but cannot leave their contact infromation on the following content types: !types. You should either disable subscriptions on those types here, revoke the permission for anonymous users, or enable anonymous users to leave their contact information in the comment settings.', array('!types' => implode(', ', $anonymous_problems))), 'status', FALSE); } $form['comment_notify_settings']['comment_notify_node_types'] = array( '#type' => 'checkboxes', '#title' => t('Content types to enable for comment notification'), '#default_value' => $enabled_types, '#options' => $checkboxes, '#description' => t('Comments on content types enabled here will have the option of comment notification.'), ); $form['comment_notify_settings']['comment_notify_available_alerts'] = array( '#type' => 'checkboxes', '#title' => t('Available subscription modes'), '#return_value' => 1, '#default_value' => comment_notify_variable_registry_get('available_alerts'), '#description' => t('Choose which notification subscription styles are available for users'), '#options' => array( COMMENT_NOTIFY_NODE => t('All comments'), COMMENT_NOTIFY_COMMENT => t('Replies to my comment') ) ); $available_options[COMMENT_NOTIFY_DISABLED] = t('No notifications'); $available_options += _comment_notify_options(); $form['comment_notify_settings']['comment_notify_default_anon_mailalert'] = array( '#type' => 'select', '#title' => t('Default state for the notification selection box for anonymous users'), '#return_value' => 1, '#default_value' => comment_notify_variable_registry_get('default_anon_mailalert'), '#options' => $available_options, ); $form['comment_notify_settings']['comment_notify_default_registered_mailalert'] = array( '#type' => 'select', '#title' => t('Default state for the notification selection box for registered users'), '#return_value' => 1, '#default_value' => comment_notify_variable_registry_get('default_registered_mailalert'), '#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, ); $form['comment_notify_settings']['comment_notify_node_notify_default_mailalert'] = array( '#type' => 'checkbox', '#title' => t('Subscribe users to their node follow-up notification emails by default'), '#default_value' => comment_notify_variable_registry_get('node_notify_default_mailalert'), '#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.'), ); $token_help = "Need a theme_token_info :("; $form['comment_notify_settings']['comment_notify_comment_notify_default_mailtext'] = array( '#type' => 'textarea', '#title' => t('Default mail text for sending out notifications to commenters'), '#suffix' => $token_help, '#default_value' => comment_notify_variable_registry_get('comment_notify_default_mailtext'), '#return_value' => 1, '#cols' => 80, '#rows' => 15 ); $form['comment_notify_settings']['comment_notify_node_notify_default_mailtext'] = array( '#type' => 'textarea', '#title' => t('Default mail text for sending out the notifications to node authors'), '#suffix' => $token_help, '#default_value' => comment_notify_variable_registry_get('node_notify_default_mailtext'), '#return_value' => 1, '#cols' => 80, '#rows' => 15 ); $form['#validate'] = array('comment_notify_settings_validate'); return system_settings_form($form); } 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.'); } } function comment_notify_tokens($type, $tokens, array $data = array(), array $options = array()) { // Add the tokens for unsubscribe links if ($type != "comment" ||!isset($data['comment'])) { return; } $cid = $data['comment']->cid; $hash = comment_notify_get_notify_hash($cid); if (!$hash) { return; } $replacements = array(); $replacements['comment:unsubscribe_url'] = url('comment_notify/disable/'. $hash, array('absolute' => TRUE)); return $replacements; }