Index: mail.inc
===================================================================
RCS file: /cvs/drupal/contributions/modules/project_issue/mail.inc,v
retrieving revision 1.116
diff -u -r1.116 mail.inc
--- mail.inc	22 Jan 2009 05:42:26 -0000	1.116
+++ mail.inc	23 Jan 2009 15:03:58 -0000
@@ -173,34 +173,19 @@
 }
 
 function project_mail_notify($nid) {
-  global $base_url, $user;
+  global $base_url, $user, $language;
 
   /// @TODO: re-enable this once http://drupal.org/node/361651 is done.
-  return;
+  //return;
 
   if (defined('PROJECT_NOMAIL')) {
     return;
   }
 
-  // There could be stale data in the cached node, so reset the cache.
   $node = node_load($nid, NULL, TRUE);
   $project = node_load(array('nid' => $node->project_issue['pid'], 'type' => 'project_project'));
-
-  $fields = project_issue_field_labels('email');
-
-  // Store a copy of the issue, so we can load the original issue values
-  // below.
-  $issue = drupal_clone($node);
-
-  // Load in the original issue data here, since we want a running
-  // reverse history.
-  $original_issue_data = unserialize($node->project_issue['original_issue_data']);
-  foreach ($fields as $field => $label) {
-    if ($field != 'name' && $field != 'updator') {
-      $issue->project_issue[$field] = $original_issue_data->$field;
-    }
-  }
-
+  $language = $language->language;  
+  
   // Record users that are connected to this issue.
   $uids = array();
   if (!empty($node->uid)) {
@@ -209,7 +194,7 @@
   if (!empty($node->project_issue['assigned'])) {
     $uids[$node->project_issue['assigned']] = $node->project_issue['assigned'];
   }
-
+  
   // Create complete history of the bug report.
   $history = array($issue);
   $result = db_query('SELECT u.name, c.cid, c.nid, c.subject, c.comment, c.uid, c.format, pic.* FROM {project_issue_comments} pic INNER JOIN {comments} c ON c.cid = pic.cid INNER JOIN {users} u ON u.uid = c.uid WHERE c.nid = %d AND c.status = %d ORDER BY pic.timestamp', $node->nid, COMMENT_PUBLISHED);
@@ -222,44 +207,8 @@
     if ($comment->uid) {
       $uids[$comment->uid] = $comment->uid;
     }
-    // We need the most recent cid and the next most recent cid for the
-    // message headers.  Instead of issuing another query, just keep track
-    // of them here.
-    $previous_cid = isset($cid) ? $cid : '';
-    $cid = $comment->cid;
-  }
-
-  // Create mail header
-  $sender->name = mime_header_encode(t('!name (!site)', array('!name' => $user->name, '!site' => variable_get('site_name', 'Drupal'))));
-  $sender->mail = strtr(variable_get('project_issue_reply_to', variable_get('site_mail', ini_get('sendmail_from'))), array('%project' => $project->project['uri']));
-
-  // The sender name is enclosed by double quotes below
-  // to satisfy RFC2822 <http://www.faqs.org/rfcs/rfc2822.html>,
-  // which requires double quotes when special characters (including
-  // some punctuation) are used.  See example in Appendix A.1.2.
-  $from = "\"$sender->name\" <$sender->mail>";
-  $domain = preg_replace('|.+://([a-zA-Z0-9\._-]+).*|', '\1', $base_url);
-  $header = array(
-    'Return-Path' => "<$sender->mail>",
-    'Date' => date('r'),
-    'X-Mailer' => 'Drupal Project module (http://drupal.org/project/project)',
-    'List-Id' => "$project->title <". $project->project['uri'] ."-issues-$domain>",
-    'List-Archive' => '<'. url('project/issues/'. $project->project['uri'], array('absolute' => TRUE)) .'>',
-    'List-Subscribe' => '<'. url('project/issues/subscribe-mail/'. $project->project['uri'], array('absolute' => TRUE)) .'>',
-    'List-Unsubscribe' => '<'. url('project/issues/subscribe-mail/'. $project->project['uri'], array('absolute' => TRUE)) .'>'
-  );
-
-  // Comments exist, set headers accordingly.
-  if (count($history) > 1) {
-    $header['Message-Id'] = "<type=project&nid=$node->nid&cid=$cid&host=@$domain>";
-    $header['In-Reply-To'] = "<type=project&nid=$node->nid&host=@$domain>";
-    $header['References'] = "<type=project&nid=$node->nid&host=@$domain> <type=project&nid=$node->nid&cid=$previous_cid&host=@$domain> <type=project&nid=$node->nid&revcount=1&host=@$domain>";
-  }
-  // Only original issue in this email.
-  else {
-    $header['Message-Id'] = "<type=project&nid=$node->nid&host=@$domain>";
   }
-
+  
   if (count($uids)) {
     $placeholders = implode(',', array_fill(0, count($uids), '%d'));
     array_unshift($uids, $node->project_issue['pid']);
@@ -267,17 +216,13 @@
   }
   else {
     $result = db_query('SELECT p.*, u.uid, u.name, u.mail FROM {project_subscriptions} p INNER JOIN {users} u ON p.uid = u.uid WHERE u.status = 1 AND p.nid = %d AND p.level = 2', $node->project_issue['pid']);
-  }
-
-  // Create link to related node
-  $links = t('Issue status update for !link', array('!link' => "\n". url("node/$node->nid", array('absolute' => TRUE)))) ."\n";
-  $links .= t('Post a follow up: !link', array('!link' => "\n". url("comment/reply/$node->nid", array('fragment' => 'comment-form', 'absolute' => TRUE)))) ."\n";
+  }  
 
   // To save workload, check here if either the anonymous role or the
   // authenticated role has the 'view uploaded files' permission, since
   // we only need to process each user's file access permission if this
   // is NOT the case.
-  $check_file_perms = !db_result(db_query("SELECT COUNT (*) FROM {permission} WHERE perm LIKE '%view uploaded files%' AND rid IN (%d, %d)", DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID));
+  $check_file_perms = !db_result(db_query("SELECT COUNT(*) FROM {permission} WHERE perm LIKE '%view uploaded files%' AND rid IN (%d, %d)", DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID));
 
   // We need to determine if node_access() checks are necessary.  The
   // check will be needed if any of the following is true:
@@ -290,12 +235,13 @@
     $anon_auth_access = TRUE;
   }
   $grants = module_implements('node_grants');
-  $check_node_access = $node->status != 1 || !$anon_auth_access || !empty($grants);
-
-  project_mail_output($node->title, 0);
-  $subject = t('[!short_name] [!category] !title', array('!short_name' => $project->project['uri'], '!category' => $node->project_issue['category'], '!title' => $node->title));
+  $check_node_access = $node->status != 1 || !$anon_auth_access || !empty($grants);  
 
-  while ($recipient = db_fetch_object($result)) {
+  $params['node'] = $node;
+  $params['project'] = $project;
+  $params['history'] = $history;
+  
+  while ($recipient = db_fetch_object($result)) {        
     // To save work, only go through a user_load if we need it.
     if ($check_file_perms || $check_node_access) {
       $account = user_load(array('uid' => $recipient->uid));
@@ -322,8 +268,9 @@
       else {
         $display_files = TRUE;
       }
-      $body = "$links\n". project_mail_generate_followup_mail_body($node, $history, $display_files);
-      drupal_mail('project_issue_update', $recipient->mail, $subject, $body, $from, $header);
+                  
+      $params['display_files'] = $display_files;   
+      drupal_mail('project_issue', 'project_issue_update_notification', $recipient->mail, $language, $params);      
     }
   }
 
@@ -336,8 +283,87 @@
   }
 
   if ($project->project_issue['mail_copy']) {
-    $body = "$links\n". project_mail_generate_followup_mail_body($node, $history, TRUE);
-    drupal_mail('project_issue_update', $project->project_issue['mail_copy'], $subject, $body, $from, $header);
+    $params['display_files'] = TRUE;
+    $message['body'][] = $links;
+    $message['body'][] = project_mail_generate_followup_mail_body($node, $history, TRUE);
+    drupal_mail('project_issue', 'project_issue_update_notification', $recipient->mail, $language, $params);
+  }
+}
+
+/*
+ * Implementation of hook_mail
+ */
+function project_issue_mail($key, &$message, $params) {  
+  switch($key) {
+    case "project_issue_update_notification":            
+      // There could be stale data in the cached node, so reset the cache.
+      $node    = $params['node'];
+      $project = $params['project'];
+      $history = $params['history'];
+      $fields  = project_issue_field_labels('email');      
+
+      // Store a copy of the issue, so we can load the original issue values
+      // below.
+      $issue = drupal_clone($node);
+
+      // Load in the original issue data here, since we want a running
+      // reverse history.
+      $original_issue_data = unserialize($node->project_issue['original_issue_data']);
+      foreach ($fields as $field => $label) {
+        if ($field != 'name' && $field != 'updator') {
+          $issue->project_issue[$field] = $original_issue_data->$field;
+        }
+      }
+      
+      // Create mail header      
+      $sender->name = t('!name (!site)', array('!name' => $user->name, '!site' => variable_get('site_name', 'Drupal')));
+      $sender->mail = strtr(variable_get('project_issue_reply_to', variable_get('site_mail', ini_get('sendmail_from'))), array('%project' => $project->project['uri']));
+
+      // The sender name is enclosed by double quotes below
+      // to satisfy RFC2822 <http://www.faqs.org/rfcs/rfc2822.html>,
+      // which requires double quotes when special characters (including
+      // some punctuation) are used.  See example in Appendix A.1.2.
+      $from = "\"$sender->name\" <$sender->mail>";
+      $domain = preg_replace('|.+://([a-zA-Z0-9\._-]+).*|', '\1', $base_url);
+      $message['headers'] += array(
+        'Return-Path' => "<$sender->mail>",
+        'Date' => date('r'),
+        'X-Mailer' => 'Drupal Project module (http://drupal.org/project/project)',
+        'List-Id' => "$project->title <". $project->project['uri'] ."-issues-$domain>",
+        'List-Archive' => '<'. url('project/issues/'. $project->project['uri'], array('absolute' => TRUE)) .'>',
+        'List-Subscribe' => '<'. url('project/issues/subscribe-mail/'. $project->project['uri'], array('absolute' => TRUE)) .'>',
+        'List-Unsubscribe' => '<'. url('project/issues/subscribe-mail/'. $project->project['uri'], array('absolute' => TRUE)) .'>'
+      );
+    
+      // Comments exist, set headers accordingly.
+      if (count($history) > 1) {
+        foreach ($history as $comment) {
+          // We need the most recent cid and the next most recent cid for the
+          // message headers.  Instead of issuing another query, just keep track
+          // of them here.
+          $previous_cid = isset($cid) ? $cid : '';
+          $cid = $comment->cid;
+        }
+        $message['headers']['Message-Id'] = "<type=project&nid=$node->nid&cid=$cid&host=@$domain>";
+        $message['headers']['In-Reply-To'] = "<type=project&nid=$node->nid&host=@$domain>";
+        $message['headers']['References'] = "<type=project&nid=$node->nid&host=@$domain> <type=project&nid=$node->nid&cid=$previous_cid&host=@$domain> <type=project&nid=$node->nid&revcount=1&host=@$domain>";
+      }
+      
+      // Only original issue in this email.
+      else {
+        $message['headers']['Message-Id'] = "<type=project&nid=$node->nid&host=@$domain>";
+      }
+    
+      project_mail_output($node->title, 0);
+      $message['subject'] = t('[!short_name] [!category] !title', array('!short_name' => $project->project['uri'], '!category' => $node->project_issue['category'], '!title' => $node->title));
+      
+      // Create link to related node
+      $links = t('Issue status update for !link', array('!link' => "\n". url("node/$node->nid", array('absolute' => TRUE)))) ."\n";
+      $links .= t('Post a follow up: !link', array('!link' => "\n". url("comment/reply/$node->nid", array('fragment' => 'comment-form', 'absolute' => TRUE)))) ."\n";
+      $message['body'][] = $links;
+      $message['body'][] = project_mail_generate_followup_mail_body($node, $history, $params['display_files']);
+      
+      break;        
   }
 }
 
