Index: commentmail.info
===================================================================
RCS file: commentmail.info
diff -N commentmail.info
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ commentmail.info	5 Jan 2007 14:54:22 -0000
@@ -0,0 +1,3 @@
+; $Id$
+name = Comment Mail
+description = Sends an email message when comments are posted to the site.
Index: commentmail.install
===================================================================
RCS file: commentmail.install
diff -N commentmail.install
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ commentmail.install	5 Jan 2007 14:54:22 -0000
@@ -0,0 +1,11 @@
+<?php
+// $Id$
+
+function date_update_1() {
+  $ret = array();
+
+  variable_set('commentmail_mode', variable_get('commentmail_opt', 'approval'));
+  variable_del('commentmail_opt');
+
+  return $ret;
+}
Index: commentmail.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/commentmail/commentmail.module,v
retrieving revision 1.10
diff -u -d -F^\s*function -r1.10 commentmail.module
--- commentmail.module	7 Oct 2006 22:15:49 -0000	1.10
+++ commentmail.module	5 Jan 2007 14:54:23 -0000
@@ -1,287 +1,314 @@
 <?php
 // $Id: commentmail.module,v 1.10 2006/10/07 22:15:49 jjeff Exp $
 
+define('COMMENTMAIL_DEFAULT_APPROVE', "An unapproved comment has been posted on @site for the post '@node'. You need to publish this comment before it will appear on your site.
+
+IP Address: @host
+Name: @user
+Email address: @mail
+URL: @homepage
+Comment:
+
+@comment
+
+
+Approve this comment: @approval_url
+Delete this comment: @delete_url
+Delete this comment and ban the user: @ban_url
+
+Edit this comment: @edit_url
+
+Comment administration: @admin_url");
+
+define('COMMENTMAIL_DEFAULT_NOTIFY', "A new comment has been posted on @site for the post '@node'.
+
+IP Address: @host
+Name: @user
+Email address: @mail
+URL: @homepage
+Comment:
+
+@comment
+
+
+Delete this comment: @delete_url
+Delete this comment and ban the user: @ban_url
+
+View this comment: @view_url
+Edit this comment: @edit_url
+
+Comment administration: @admin_url");
+
+
 /**
- * Implementation of hook_help()
+ * Implementation of hook_menu()
  */
+function commentmail_menu($may_cache) {
+  $items = array();
 
-function commentmail_help($section){
-  switch($section){
-    case 'admin/modules#description':
-      return t('Sends an email message when comments are posted to the site.');
+  if ($may_cache) {
+    $access = user_access('administer comments');
+    
+    $items[] = array(
+      'title' => t('Comment mail'), 
+      'path' => 'admin/settings/commentmail', 
+      'access' => $access,
+      'callback' => 'drupal_get_form',
+      'callback arguments' => array('commentmail_admin_settings'),
+    );
+
+    $items[] = array(
+      'title' => t('Approve comment'),
+      'path' => 'comment/approve',
+      'access' => $access,
+      'callback' => 'drupal_get_form',
+      'callback arguments' => array('commentmail_approve'),
+      'type' => MENU_CALLBACK,
+    );
+
+    $items[] = array(
+      'title' => t('Delete comment and ban user'),
+      'path' => 'comment/deleteban',
+      'access' => $access,
+      'callback' => 'drupal_get_form',
+      'callback arguments' => array('commentmail_deleteban'),
+      'type' => MENU_CALLBACK,
+    );
   }
+
+  return $items;
 }
 
+
 /**
  * Implementation of hook_settings()
  */
+function commentmail_admin_settings() {
+  $form['commentmail_to'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Send to'),
+    '#default_value' => variable_get('commentmail_to', variable_get('site_mail', '')),
+    '#description' => t('A mail message will be sent here when new comments are posted to the site. Separate multiple addresses with a comma.')
+  );
 
-function commentmail_settings(){
-  drupal_set_title(t('comment mail'));
-  $form['commentmail_to'] = array('#type' => 'textfield', '#title' => t('Send to'), '#default_value' => variable_get('commentmail_to', variable_get('site_mail', '')), '#description' => t('A mail message will be sent here when new comments are posted to the site. Separate multiple addresses with a comma.'));
-  $form['commentmail_opt'] = array(
+  $form['commentmail_mode'] = array(
     '#type' => 'radios',
     '#title' => t('Send mail for'),
-    '#default_value' => variable_get('commentmail_opt', 'approval'),
+    '#default_value' => variable_get('commentmail_mode', 'approval'),
     '#options' => array(
       'all' => t('all new comments'),
       'approval' => t('just comments needing approval'),
       'disable' => t('none (disabled)'),      
     ),
   );
-  return $form;
+  
+  $placeholders = t("The following placeholders are available:
+<dl>
+  <dt>@site</dt>
+  <dd>The name of your site.</dd>
+  
+  <dt>@node</dt>
+  <dd>The title of the post this comment was submitted for.</dd>
+  
+  <dt>@approval_url</dt>
+  <dd>The address the recipient can visit to publish the comment.</dd>
+  
+  <dt>@delete_url</dt>
+  <dd>The address the recipient can visit to delete the comment.</dd>
+  
+  <dt>@ban_url</dt>
+  <dd>The address the recipient can visit to delete the comment and ban the user who submitted the comment.</dd>
+  
+  <dt>@edit_url</dt>
+  <dd>The address the recipient can visit to edit the comment.</dd>
+  
+  <dt>@view_url</dt>
+  <dd>The address the recipient can visit to view the comment.</dd>
+  
+  <dt>@admin_url</dt>
+  <dd>The address of the comment moderation.</dd>
+  
+  <dt>@queue_url</dt>
+  <dd>The address of the comment moderation approval queue.</dd>
+  
+  <dt>@host</dt>
+  <dd>The host name of the user who submitted the comment.</dd>
+  
+  <dt>@user</dt>
+  <dd>The name of the user who submitted the comment.</dd>
+  
+  <dt>@mail</dt>
+  <dd>The e-mail address of the user who submitted the comment.</dd>
+  
+  <dt>@homepage</dt>
+  <dd>The homepage of the user who submitted the comment.</dd>
+  
+  <dt>@comment</dt>
+  <dd>The actual comment text the user submitted.</dd>");
+
+  $form['commentmail_mail_approve'] = array(
+    '#type' => 'textarea',
+    '#title' => t('Body text for approval mails'),
+    '#default_value' => variable_get('commentmail_mail_approve', COMMENTMAIL_DEFAULT_APPROVE),
+    '#description' => $placeholders,
+    '#cols' => 60,
+    '#rows' => 25,
+  );
+
+  $form['commentmail_mail_notify'] = array(
+    '#type' => 'textarea',
+    '#title' => t('Body text for notification mails'),
+    '#default_value' => variable_get('commentmail_mail_notify', COMMENTMAIL_DEFAULT_NOTIFY),
+    '#description' => $placeholders,
+    '#cols' => 60,
+    '#rows' => 25,
+  );
+
+  return system_settings_form($form);
 }
 
+
 /**
  * Implementation of hook_comment()
  *
  */
+function commentmail_comment($comment, $op) {
+  if ($op == 'insert') {
+    // Load the real comment object from the database
+    $comment_obj = _comment_load($comment['cid']);
 
-function commentmail_comment($comment, $op){
-  switch($op){
-    case 'insert':
-      $comment_obj = db_fetch_object(db_query('SELECT * FROM {comments} WHERE cid = %d', $comment['cid']));
-      $opt = variable_get('commentmail_opt', 'approval');
-      switch($opt){
-        case 'approval':
-          if($comment_obj->status == 0){
-            break;
-          }
-        case 'all':
-          $mailto = variable_get('commentmail_to', variable_get('site_mail', FALSE));
-          if($mailto){
-            $node = node_load($comment_obj->nid);
-            $site = variable_get('site_name', t('Drupal'));
-            user_mail(
-              $mailto, t(
-                "[%site] New Comment posted on'%title'",
-                array('%title' => check_plain($node->title), '%site' => $site)), 
-                theme(
-                  ($comment_obj->status == COMMENT_NOT_PUBLISHED ? 'commentmail_approve' : 'commentmail_notify'), 
-                  $comment_obj,
-                  $node
-                ),
-              ''
-            );
-          }
-          else {
-            watchdog('commentmail', t("Site mail address is not configured."), WATCHDOG_ERROR);
-          }
-      }
-  }
-}
-
-function commentmail_menu($may_cache){
-  $items = array();
-  $access = user_access('administer comments');
-  if($may_cache){
-    $items[] = array(
-      'title' => t('quick edit'), 
-      'path' => 'comment/qa', 
-      'access' => TRUE, 
-      'callback' => 'commentmail_quick_approve', 
-      'type' => MENU_CALLBACK
-    );
-    $items[] = array(
-      'title' => t('quick delete'), 
-      'path' => 'comment/quickdelete', 
-      'access' => $access, 
-      'callback' => 'commentmail_quick_delete', 
-      'type' => MENU_CALLBACK
-    );
-    $items[] = array(
-      'title' => t('quick delete and ban'), 
-      'path' => 'comment/quickdeleteban', 
-      'access' => $access, 
-      'callback' => 'commentmail_quick_deleteban', 
-      'type' => MENU_CALLBACK
-    );
-  }
-  return $items;
-}
+    dprint_r($comment_obj);
+    // Decide what to do based on the user's setting
+    switch (variable_get('commentmail_mode', 'approval')) {
+      // Mails should only be sent for unpublished comments
+      case 'approval':
+        if ($comment_obj->status == 0) {
+          // Don't send a mail because the comment is already published
+          break;
+        }
+        // Fallthrough for unpublished comments
+      // Mails should be sent for all new comments, regardless of their status
+      case 'all':
+        $recipient = variable_get('commentmail_to', variable_get('site_mail', FALSE));
 
-function commentmail_quick_approve($cid = NULL, $op = FALSE){
-  global $user;
-  if(user_access('administer comments')){
+        // Only send a mail if a recipient has been specified
+        if ($recipient) {
+          // Load the node to get the title
+          $node = node_load($comment_obj->nid);
 
-      $comment = db_fetch_object(db_query('SELECT * FROM {comments} WHERE cid = %d', $cid));
-      if ($comment){
-        if ($op == 'approve'){
-          $comment->status = 0;
-          if(comment_save((array)$comment)){
-            // link to comment on page
-            drupal_goto('node/'.$comment->nid, NULL, 'comment-'.$comment->cid);
+          if ($comment_obj->status == COMMENT_NOT_PUBLISHED) {
+            $text = variable_get('commentmail_mail_approve', COMMENTMAIL_DEFAULT_APPROVE);
           }
           else {
-            drupal_set_message(t('Comment not saved'), 'error');
+            $text = variable_get('commentmail_mail_notify', COMMENTMAIL_DEFAULT_NOTIFY);
           }
-        }
-        $comment->comment = check_markup($comment->comment, $comment->format, FALSE);
-        $node = node_load($comment->nid);
-        $output = "<h2>".t('Comment on <em>%title</em>', array('%title' => $node->title))."</h2>";
-        $confirm_delete = "return confirm('". t('Are you sure you want to delete this comment?') ."');";
-        $confirm_deleteban = "return confirm('". t('Are you sure you want to delete this comment and ban the computer that posted it?') ."');";
-        if ($comment->status) {
-          $links[] = l(t('approve'), 'comment/qa/'.$cid.'/approve');
+
+          $body = t($text, array(
+            '@site' => variable_get('site_name', 'Drupal'),
+            '@node' => $node->title,
+            '@approval_url' => url('comment/approve/'. $comment_obj->cid, NULL, NULL, TRUE),
+            '@delete_url' => url('comment/delete/'. $comment_obj->cid, NULL, NULL, TRUE),
+            '@ban_url' => url('comment/deleteban/'. $comment_obj->cid, NULL, NULL, TRUE),
+            '@edit_url' => url('comment/edit/'. $comment_obj->cid, NULL, NULL, TRUE),
+            '@queue_url' => url('admin/comment/list/approval', NULL, NULL, TRUE),
+            '@view_url' => url('node/'. $node->nid, NULL, 'comment-'. $comment_obj->cid, TRUE),
+            '@admin_url' => url('admin/content/comment', NULL, NULL, TRUE),
+            '@host' => $comment_obj->hostname,
+            '@user' => $comment_obj->name,
+            '@mail' => $comment_obj->mail,
+            '@homepage' => $comment_obj->homepage,
+            '@comment' => $comment_obj->comment,
+          ));
+
+          drupal_mail(
+            'commentmail-notify-'. $comment_obj->cid,
+            $recipient,
+            t('[@site] New Comment posted on "@title"', array('@title' => check_plain($node->title), '@site' => variable_get('site_name', 'Drupal'))),
+            $body,
+            variable_get('site_mail', NULL)
+          );
         }
         else {
-          $links[] = t('approved');
+          watchdog('commentmail', t('Site mail address is not configured.'), WATCHDOG_ERROR);
         }
-        $links[] = l(t('edit'), 'comment/edit/'.$cid);
-        $links[] = l(t('delete'), 'comment/quickdelete/'.$cid, array('onclick' => $confirm_delete));
-        $links[] = l(t('delete and ban ip'), 'comment/quickdeleteban/'.$cid, array('onclick' => $confirm_deleteban));
-        $output .= theme('comment', $comment, $links);
-      }
-      else {
-        $output = t("Comment not found.");
-      }
     }
-    elseif ($user->uid > 0) {
-      drupal_access_denied();
+  }
+}
+
+
+
+function commentmail_approve($cid) {
+  if ($comment = _comment_load($cid)) {
+    if ($comment->status == COMMENT_NOT_PUBLISHED) {
+       return confirm_form(
+          array('cid' => array('#type' => 'value', '#value' => $comment->cid)),
+          t('Are you sure you want to approve the comment %title?', array('%title' => $comment->subject)),
+          array('path' => 'node/'. $comment->nid, 'fragment' => 'comment-'. $comment->cid),
+          t('The comment will be visible to all users.'),
+          t('Approve'),
+          t('Cancel')
+        );
     }
     else {
-      // user is not logged in... goto the user login page, then come back here
-      drupal_goto('user/login', 'destination=comment/qa/'.$cid);
+      drupal_set_message(t('The comment is already published.'));
+      drupal_goto('node/'. $comment->nid, NULL, 'comment-'. $comment->cid);
     }
-  return $output;
-}
-
-function commentmail_quick_delete($cid = NULL){
-  if(is_numeric($cid) && $comment = db_fetch_object(db_query('SELECT c.*, u.name AS registered_name, u.uid FROM {comments} c INNER JOIN {users} u ON u.uid = c.uid WHERE c.cid = %d', $cid))){
-    
-    commentmail_delete($comment);
-    
-    drupal_goto("node/$comment->nid");
   }
   else {
-    drupal_not_found();
+    drupal_set_message(t('The comment no longer exists.'));
   }
 }
 
-function commentmail_quick_deleteban($cid = NULL){
-    if(is_numeric($cid) && $comment = db_fetch_object(db_query('SELECT * FROM {comments} WHERE cid = %d', $cid))){
-    
-    commentmail_delete($comment);
-    
-    $aid = db_next_id('{access}_aid');
-    db_query("INSERT INTO {access} (aid, mask, type, status) VALUES ('%s', '%s', 'host', 0)", $aid, $comment->hostname);
-    
-    drupal_set_message(t('The address <em>%ip</em> rule has been banned.', array('%ip' => $comment->hostname)));
-    
-    drupal_goto("node/$comment->nid");
+function commentmail_approve_submit($form_id, $form_values) {
+  $comment = _comment_load($form_values['cid']);
+  $comment->status = 0;
+
+  if (comment_save((array)$comment)) {
+    // link to comment on page
+    drupal_set_message(t('The comment has been approved.'));
+    drupal_goto('node/'. $comment->nid, NULL, 'comment-'. $comment->cid);
   }
   else {
-    drupal_not_found();
+    return t('There was an error during the comment approving process.');
   }
 }
 
-function commentmail_delete($comment){
-  $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
-    
-    drupal_set_message(t('The comment and all its replies have been deleted.'));
 
-    // Delete comment and its replies.
-    _comment_delete_thread($comment);
-
-    _comment_update_node_statistics($comment->nid);
 
-    // Clear the cache so an anonymous user sees that his comment was deleted.
-    cache_clear_all();
+function commentmail_deleteban($cid) {
+  if ($comment = _comment_load($cid)) {
+    return confirm_form(
+        array('cid' => array('#type' => 'value', '#value' => $comment->cid)),
+        t('Are you sure you want to delete the comment %title and ban its author?', array('%title' => $comment->subject)),
+        array('path' => 'node/'. $comment->nid, 'fragment' => 'comment-'. $comment->cid),
+        t('Any replies to this comment will be lost. This action cannot be undone. In addition, the author of the comment is no longer allowed to post comments on your site.'),
+        t('Delete and ban'),
+        t('Cancel')
+      );
+  }
+  else {
+    drupal_set_message(t('The comment no longer exists.'));
+  }
 }
 
 
-/**
- * Theme function for comments needing approval
- *
- * @param object $comment
- *  The comment object
- * @param object $node
- *  The object of the node being commented upon
- * @return string
- *  body of email
- */
-
-function theme_commentmail_approve($comment, $node){
-  $site = variable_get('site_name', t('your Drupal site'));
-  $approval_url = url('comment/qa/'. $comment->cid, NULL, NULL, TRUE);
-  //$delete_url = url('comment/delete/'. $comment->cid, $dest, NULL, TRUE);
-  //$queue_url = url('admin/comment/list/approval', NULL, NULL, TRUE);
-  $output = t(<<<EOT
-An unapproved comment has been posted on %site for the entry entitled '%node->title'. You need to publish this comment before it will appear on your site.
-
-Approve/edit/delete this comment:
-  <%approval_url>
-
-IP Address: %comment->hostname\n 
-Name: %comment->name\n
-Email Address: %comment->mail\n 
-URL: %comment->homepage\n
-Comment:
-
-%comment->comment
-
-EOT
-  , array(
-    '%site' => $site,
-    '%node->title' => $node->title,
-    '%approval_url' => $approval_url,
-    '%delete_url' => $delete_url,
-    '%queue_url' => $queue_url,
-    '%comment->hostname' => $comment->hostname,
-    '%comment->name' => $comment->name,
-    '%comment->mail' => $comment->mail,
-    '%comment->homepage' => $comment->homepage,
-    '%comment->comment' => $comment->comment,
-  ));
-  return $output;
-}
+function commentmail_deleteban_submit($form_id, $form_values) {
+  $comment = _comment_load($form_values['cid']);
 
-/**
- * Theme function for comments not needing approval
- *
- * @param object $comment
- *  The comment object
- * @param object $node
- *  The object of the node being commented upon
- * @return string
- *  body of email
- */
+  // Delete comment and its replies.
+  _comment_delete_thread($comment);
+  _comment_update_node_statistics($comment->nid);
 
-function theme_commentmail_notify($comment, $node){
-  $site = variable_get('site_name', t('your Drupal site'));
-  $view_url = url('node/'. $node->nid, NULL, 'comment-'. $comment->cid, TRUE);
-  $delete_url = url('comment/delete/'. $comment->cid, $dest, NULL, TRUE);
-  $admin_url = url('admin/comment', NULL, NULL, TRUE);
-  $output = t(<<<EOT
-A new comment has been posted on %site for the entry entitled '%node->title'.
+  drupal_set_message(t('The comment and all its replies have been deleted.'));
 
-View this comment:
-  <%view_url>
-Delete this comment:
-  <%delete_url>
-Comment administration:
-  <%admin_url>
+  // Clear the cache so an anonymous user sees that his comment was deleted.
+  cache_clear_all();
 
-IP Address: %comment->hostname\n
-Name: %comment->name\n
-Email Address: %comment->mail\n
-URL: %comment->homepage\n
-Comments:
+  // Now, ban the user
+  $aid = db_next_id('{access}_aid');
+  db_query("INSERT INTO {access} (aid, mask, type, status) VALUES ('%s', '%s', 'host', 0)", $aid, $comment->hostname);
 
-%comment->comment
+  drupal_set_message(t('The host %host has been banned.', array('%host' => $comment->hostname)));
 
-EOT
-  , array(
-    '%site' => $site,
-    '%node->title' => $node->title,
-    '%view_url' => $view_url,
-    '%delete_url' => $delete_url,
-    '%admin_url' => $admin_url,
-    '%comment->hostname' => $comment->hostname,
-    '%comment->name' => $comment->name,
-    '%comment->mail' => $comment->mail,
-    '%comment->homepage' => $comment->homepage,
-    '%comment->comment' => $comment->comment,
-  ));
-  return $output;
-}
\ No newline at end of file
+  drupal_goto('node/'. $comment->nid);
+}
