? comment_notify_cleanup.patch
Index: comment_notify.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/comment_notify/comment_notify.module,v
retrieving revision 1.8
diff -u -p -r1.8 comment_notify.module
--- comment_notify.module	29 Jul 2008 20:41:21 -0000	1.8
+++ comment_notify.module	4 Aug 2008 01:53:34 -0000
@@ -47,7 +47,7 @@ function comment_notify_help($section) {
 
 /**
  * Insert our checkbox, and populate fields.
- * set validation hook.
+ * Set a validation hook.
  */
 function comment_notify_form_alter($form_id, &$form) {
   global $user;
@@ -64,6 +64,7 @@ function comment_notify_form_alter($form
     return;
   }
 
+  // If this is a POST then it is a preview and we remind people that there post isn't done yet.
   $op = isset($_POST['op']) ? $_POST['op'] : '';
 
   if ($op == t('Preview comment')) {
@@ -86,6 +87,7 @@ function comment_notify_form_alter($form
     }
   }
 
+  // Add the checkbox for anonymous users and set the default based on admin settings.
   if ($user->uid == 0) {
     $form['notify'] = array(
       '#type' => 'checkbox',
@@ -93,6 +95,7 @@ function comment_notify_form_alter($form
       '#default_value' => ($user->uid != 0) ? $user->comment_notify_mailalert : variable_get('comment_notify_default_anon_mailalert', TRUE),
     );
   }
+  // For registered users and the admin wants them to see the checkbox.
   elseif (variable_get('comment_notify_regged_checkbox', TRUE)) {
     $form['notify'] = array(
       '#type' => 'checkbox',
@@ -101,6 +104,7 @@ function comment_notify_form_alter($form
       '#description' => t('You can change the default for this field in "Comment follow-up notification settings" on <a href="!uri">your account edit page</a>', array('!uri' => url('user/'. $user->uid .'/edit'))),
     );
   }
+  // For the registered users where the admin wants the checkbox hidden.
   else {
     $form['notify'] = array(
       '#type' => 'hidden',
@@ -108,7 +112,7 @@ function comment_notify_form_alter($form
       '#default_value' => $user->comment_notify_mailalert,
     );
   }
-
+  // If this is an existing comment we set the default value based on their selection last time.
   if ($form['cid']['#value'] != '') {
     $notify = db_result(db_query("SELECT notify FROM {comment_notify} WHERE cid = %d", $form['cid']['#value']));
     $form['notify']['#default_value'] = $notify;
@@ -116,7 +120,7 @@ function comment_notify_form_alter($form
 }
 
 /**
- * Implementation of hook_perm
+ * Implementation of hook_perm().
  */
 function comment_notify_perm() {
   return array('Administer comment notify', 'Subscribe to comments');
@@ -170,10 +174,12 @@ function comment_notify_menu($may_cache)
   return $items;
 }
 
+/**
+ * Page callback to allow users to unsubscribe simply by visiting the page.
+ */
 function comment_notify_page() {
   global $user;
 
-  $breadcrumb = NULL;
   $op = $_POST['op'];
   $edit = $_POST['edit'];
 
@@ -197,53 +203,38 @@ function comment_notify_page() {
   }
 
   drupal_set_title($title);
-  drupal_set_breadcrumb($breadcrumb);
   print theme('page', $page_content);
 }
 
 /**
- * save our data.
- */
-function comment_notify_validate($form_id, $form_values) {
-
-  if ($form_values['optin']) {
-    foreach (array('optin') as $field) {
-      $_SESSION['comment_notify'][$field] = $form_values[$field];
-    }
-  }
-  else {
-    foreach (array('optin') as $field) {
-      unset($_SESSION['comment_notify'][$field]);
-    }
-  }
-}
-
-/**
- * implement hook_comment and check the publish status
+ * Implementation of hook_comment().
  */
 function comment_notify_comment($comment, $op) {
   global $user;
 
   switch ($op) {
     case 'validate':
-      // We assume that if they are non-anonymous then they have a valid mail
+      // 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.'));
       }
       break;
     case 'publish':
+      // The real meat of the module.
       _comment_notify_mailalert($comment);
       break;
     case 'update':
+      // In case they have changed their status, save it in the database.
       $sql = 'UPDATE {comment_notify} SET notify = %d WHERE cid = %d';
       db_query($sql, $comment['notify'], $comment['cid']);
       break;
-
     case 'insert':
+      // For new comments, we first build up a string to be used as the md5 identifier for the alert
       $mail = empty($comment['mail']) ? $user->mail : $comment['mail'];
       $md5_string = $mail . $user->uid . $comment['name'] . $comment['nid'];
-      $sql = "INSERT INTO {comment_notify} (cid, notify, notify_hash) values (%d, %d, '%s')";
-      db_query($sql, $comment['cid'], $comment['notify'], md5($md5_string));
+      // And then save the data.
+      db_query("INSERT INTO {comment_notify} (cid, notify, notify_hash) values (%d, %d, '%s')", $comment['cid'], $comment['notify'], md5($md5_string));
       break;
   }
 }
@@ -267,9 +258,8 @@ function comment_notify_user($type, &$ed
           '#type' => 'checkbox',
           '#title' => t('Receive comment follow-up notification e-mails'),
           '#default_value' => isset($edit['comment_notify_mailalert']) ? $edit['comment_notify_mailalert'] : 0,
-          '#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.')
+          '#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;
       }
 
@@ -277,6 +267,12 @@ function comment_notify_user($type, &$ed
   }
 }
 
+/**
+ * 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 $locale;
@@ -291,12 +287,9 @@ function _comment_notify_mailalert($comm
 
   $nid = $comment->nid;
   $cid = $comment->cid;
-  $commname = $comment->name;
-  $commtext = $comment->comment;
-  $commsubj = $comment->subject;
   $node = node_load($nid);
   if (!isset($comment->mail)) {
-    $comment_account = user_load(array('name' => $commname));
+    $comment_account = user_load(array('name' => $comment->name));
     $comment_mail = $comment_account->mail;
   }
   else {
@@ -327,9 +320,9 @@ function _comment_notify_mailalert($comm
       $message = t(
         variable_get('comment_notify_default_mailtext', DEFAULT_MAILTEXT),
         array(
-          '!commname' => $commname,
-          '!commtext' => $commtext,
-          '!commsubj' => $commsubj,
+          '!commname' => $comment->name,
+          '!commtext' => $comment->comment,
+          '!commsubj' => $comment->subject,
           '!comment_url' => url('node/'. $nid, NULL, NULL, 1) .'#comment-'. $cid,
           '!node_title' =>  $node->title,
           '!node_teaser' => $node->teaser,
@@ -353,7 +346,6 @@ function _comment_notify_mailalert($comm
       if ($alert->uid != 0) {
         $watchdog_message = t('Notified: <a href="!url">@user_mail</a>',
           array('!url' => url('user/'. $alert->uid .'/edit'), '@user_mail' => $mail)) ;
-
       }
       else {
         $watchdog_message = t('Notified @user_mail', array('@user_mail' => $mail));
@@ -370,7 +362,7 @@ function _comment_notify_mailalert($comm
 }
 
 /**
- * Callback for a form to unsubscribe users.
+ * Callback for an administrative form to unsubscribe users by e-mail address.
  */
 function comment_notify_unsubscribe() {
   $form['comment_notify_unsubscribe'] = array();
@@ -386,7 +378,7 @@ function comment_notify_unsubscribe() {
 }
 
 /**
- * Actual unsubscribe of users.
+ * Based on admin submit, do the actual unsubscribe from notifications.
  */
 function comment_notify_unsubscribe_submit($form_id, $form_values) {
   $email = trim($form_values['email_to_unsubscribe']);
