Index: casetracker_mail.info
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/casetracker/casetracker_mail.info,v
retrieving revision 1.2
diff -u -p -r1.2 casetracker_mail.info
--- casetracker_mail.info	9 Mar 2008 14:50:55 -0000	1.2
+++ casetracker_mail.info	11 Mar 2009 14:55:14 -0000
@@ -1,5 +1,6 @@
-; $Id: casetracker_mail.info,v 1.2 2008/03/09 14:50:55 zero2one Exp $
+; $Id: casetracker_mail.info,v 1.2 2007/11/23 07:51:33 zero2one Exp $
 name = CT Mail
 package = Case Tracker
-dependencies = casetracker
-description = Enables mail sending and Mailhandler integration for Case Tracker.
+dependencies[] = casetracker
+description = Enables mail sending and Mailhandler intregation for Case Tracker.
+core = 6.x
Index: casetracker_mail.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/casetracker/casetracker_mail.install,v
retrieving revision 1.2
diff -u -p -r1.2 casetracker_mail.install
--- casetracker_mail.install	23 Nov 2007 07:51:33 -0000	1.2
+++ casetracker_mail.install	11 Mar 2009 14:55:14 -0000
@@ -2,45 +2,58 @@
 // $Id: casetracker_mail.install,v 1.2 2007/11/23 07:51:33 zero2one Exp $
 
 /**
+ * @file
+ * The install file for casetracker_mail
+ */
+
+/**
  * Implementation of hook_install().
- *
- * Database schema last updated 2006-12 by Morbus Iff.
- *
+ */
+function casetracker_mail_install() {
+  drupal_install_schema('casetracker_mail');
+}
+
+/**
+ * Implementation of hook_schema().
  *   casetracker_mail:
  *     msg_id: the message ID of an outgoing mail notification.
  *     nid: the node ID to which this outgoing mail refers.
  *     cid: the comment ID to which this outgoing mail refers.
  */
-function casetracker_mail_install() {
-  switch ($GLOBALS['db_type']) {
-    case 'mysql':
-    case 'mysqli':
-      db_query("CREATE TABLE {casetracker_mail} (
-        msg_id varchar(250) NOT NULL,
-        nid int(11) NOT NULL,
-        cid int(11) NOT NULL,
-        PRIMARY KEY (msg_id)
-      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
-      break;
-     
-    case 'pgsql':
-       db_query("CREATE TABLE {casetracker_mail} (
-        msg_id varchar(250) NOT NULL,
-        nid numeric(11) NOT NULL,
-        cid numeric(11) NOT NULL,
-        PRIMARY KEY  (msg_id)
-      )");
-      break;
-  }
+
+function casetracker_mail_schema() {
+  $schema['casetracker_mail'] = array(
+    'fields' => array(
+      'msg_id' => array(
+        'type' => 'varchar',
+        'length' => 255,
+        'not null' => TRUE,
+        'default' => '',
+      ),
+      'nid' => array(
+        'type' => 'int',
+        'not null' => TRUE,
+        'default' => 0,
+      ),
+      'cid' => array(
+        'type' => 'int',
+        'not null' => TRUE,
+        'default' => 0,
+      ),
+    ),
+    'primary key' => array('msg_id'),
+  );
+  
+  return $schema;
 }
 
 /**
  * Implementation of hook_uninstall().
  */
 function casetracker_mail_uninstall() {
-  db_query('DROP TABLE {casetracker_mail}');
+  drupal_uninstall_schema('casetracker_mail');
   variable_del('casetracker_mail_address');
   variable_del('casetracker_mail_subject');
   variable_del('casetracker_mail_case_message');
   variable_del('casetracker_mail_comment_message');
-}
\ No newline at end of file
+}
Index: casetracker_mail.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/casetracker/casetracker_mail.module,v
retrieving revision 1.20
diff -u -p -r1.20 casetracker_mail.module
--- casetracker_mail.module	12 Feb 2009 21:40:03 -0000	1.20
+++ casetracker_mail.module	11 Mar 2009 14:55:14 -0000
@@ -1,46 +1,82 @@
 <?php
-// $Id: casetracker_mail.module,v 1.20 2009/02/12 21:40:03 jmiccolis Exp $
+// $Id: casetracker_mail.module,v 1.16 2007/11/19 07:36:57 zero2one Exp $
 
 /**
  * @file
- * Enables mail sending and Mailhandler integration for Case Tracker.
+ * Enables mail sending and Mailhandler intregation for Case Tracker.
  */
 
 /**
  * Implementation of hook_help().
  */
-function casetracker_mail_help($section) {
-  switch ($section) {
+function casetracker_mail_help($path, $arg) {
+  switch ($path) {
     case 'admin/settings/casetracker_mail':
-      return '<p>'.t('Configure the various Case Tracker mail options with these settings.').'</p>';
+      return '<p>'. t('Configure the various Case Tracker mail options with these settings.') .'</p>';
   }
 }
 
 /**
  * Implementation of hook_menu().
  */
-function casetracker_mail_menu($may_cache) {
+function casetracker_mail_menu() {
   $items = array();
+  $items['admin/settings/casetracker/mail'] = array(
+    'access arguments'   => array('administer case tracker'),
+    'page callback'      => 'drupal_get_form',
+    'page arguments'     => array('casetracker_mail_settings'),
+    'description'        => 'Configure the various Case Tracker mail options with these settings.',
+    'title'              => 'CT Mail',
+    'type'               => MENU_LOCAL_TASK,
+  );
 
-  if ($may_cache) {
-    $items[] = array(
-      'access'             => user_access('administer case tracker'),
-      'callback'           => 'drupal_get_form',
-      'callback arguments' => 'casetracker_mail_settings',
-      'description'        => t('Configure the various Case Tracker mail options with these settings.'),
-      'path'               => 'admin/settings/casetracker/mail',
-      'title'              => t('CT Mail'),
-      'type'               => MENU_LOCAL_TASK,
-    );
+  return $items;
+}
+
+/**
+ * Hook implementation for mail
+ */
+function casetracker_mail_mail($key, &$message, $params) {
+  $variables = array(
+    '%project_id'       => $params['pid'],
+    '%project_title'    => $params['project']->title,
+    '%case_id'          => $params['case']->nid,
+    '%case_number'      => $params['pid'] .'-'. $params['case']->nid,
+    '%case_title'       => $params['is_comment'] ? $params['comment']['subject'] : $params['case']->title,
+    '%case_type'        => casetracker_case_state_load($params['is_comment'] ? $params['comment']['casetracker']['case_type_id'] : $params['case']->casetracker->case_type_id, 'type'),
+    '%case_priority'    => casetracker_case_state_load($params['is_comment'] ? $params['comment']['casetracker']['case_priority_id'] : $params['case']->casetracker->case_priority_id, 'priority'),
+    '%case_status'      => casetracker_case_state_load($params['is_comment'] ? $params['comment']['casetracker']['case_status_id'] : $params['case']->casetracker->case_status_id, 'status'),
+    '%case_assigned'    => $params['assign_to_name'],
+    '%case_author'      => casetracker_get_name($params['case']->uid),
+    '%case_created'     => format_date($params['case']->created, 'large'),
+    '%case_changed'     => format_date($params['case']->changed, 'large'),
+    '%case_url'         => url('node/'. $params['case']->nid, array('absolute' => TRUE)),
+    // @todo fails for CCK or non-body cases.
+    '%case_description' => _casetracker_mail_plain_description($params['case']->body),
+    '%comment'          => NULL,
+  );
+  if ($params['is_comment']) {
+    $variables['%comment'] = strtr(variable_get('casetracker_mail_comment_message', _casetracker_mail_comment_message()), array(
+      '%comment_author'      => casetracker_get_name($params['comment']['uid']),
+      '%comment_created'     => format_date($params['comment']['date'], 'large'),
+      '%comment_title'       => $params['comment']['subject'],
+      '%comment_description' => _casetracker_mail_plain_description($params['comment']['comment']),
+    ));
   }
 
-  return $items;
+  $subject = strtr(variable_get('casetracker_mail_subject', _casetracker_mail_subject()), $variables);
+  $body    = strtr(variable_get('casetracker_mail_case_message', _casetracker_mail_case_message()), $variables);
+
+  $message['subject'] = t($subject);
+  $message['body'] = t($body, $variables, $params['language']);
+//  $message['headers'] = $params['headers'];
+
 }
 
 /**
  * Sends out emails. Woot! Do people still say woot? Man, I'm old.
  *
- * @param $node
+ * @param $case
  *   The case $node object that is being inserted or modified.
  * @param $comment
  *   The $comment array, passed only if this a comment has been left.
@@ -49,105 +85,66 @@ function casetracker_mail_send($case, $c
   global $user;
 
   // is it a comment post
-  $isComment = !is_null($comment);
+  $is_comment = !is_null($comment);
 
   // get the project data
-  $pid = ($isComment)
-      ? $comment['prid']
-      : $case->pid;
-  $project = db_fetch_object(db_query("SELECT cp.project_number, n.title FROM {casetracker_project} cp LEFT JOIN {node} n ON (n.vid = cp.vid) WHERE cp.nid = %d", $pid));
+  $pid = ($is_comment) ? $comment['casetracker']['pid'] : $case->casetracker->pid;
+  $project = db_fetch_object(db_query("SELECT title FROM {node} WHERE nid = %d", $pid));
 
   // get the assigned to name
-  $assignToId = ($isComment)
-            ? $comment['assign_to']
-            : $case->assign_to;
-  $assignToName =  (is_numeric($assignToId))
-              ? casetracker_get_name($assignToId)
-              : $assignToId;
-
-  $variables = array(
-    '%project_id'       => $pid,
-    '%project_number'   => $project->project_number,
-    '%project_title'    => $project->title,
-    '%case_id'          => $case->nid,
-    '%case_number'      => $project->project_number .'-'. $case->case_number,
-    '%case_title'       => $isComment
-                ? $comment['case_title']
-                : $case->title,
-    '%case_type'        => casetracker_case_state_load(
-                'type',
-                $isComment
-                  ? $comment['case_type_id']
-                  : $case->case_type_id),
-    '%case_priority'    => casetracker_case_state_load(
-                'priority',
-                $isComment
-                  ? $comment['case_priority_id']
-                  : $case->case_priority_id),
-    '%case_status'      => casetracker_case_state_load(
-                'status',
-                $isComment
-                  ? $comment['case_status_id']
-                  : $case->case_status_id),
-    '%case_assigned'    => $assignToName,
-    '%case_author'      => casetracker_get_name($case->uid),
-    '%case_created'     => format_date($case->created, 'large'),
-    '%case_changed'     => format_date($case->changed, 'large'),
-    '%case_url'         => url('node/'.$case->nid, NULL, NULL, TRUE),
-    // @todo fails for CCK or non-body cases.
-    '%case_description' => _casetracker_mail_plain_description($case->body),
-    '%comment'          => NULL,
-  );
-
-
-
-  if (isset($comment)) { // make a master %comment variable that contains the values of this specific comment.
-    // @todo it'd be nice if we could display all the previous comments, as the project.module currently does.
-    $variables['%comment'] = strtr(variable_get('casetracker_mail_comment_message', _casetracker_mail_comment_message()), array(
-      '%comment_author'      => casetracker_get_name($comment['uid']),
-      '%comment_created'     => format_date($comment['date'], 'large'),
-      '%comment_title'       => $comment['subject'],
-      '%comment_description' => _casetracker_mail_plain_description($comment['comment']),
-    ));
-  }
+  $assign_to_id = ($is_comment) ? $comment['casetracker']['assign_to'] : $case->casetracker->assign_to;
+  $assign_to_name =  (is_numeric($assign_to_id)) ? casetracker_get_name($assign_to_id) : $assign_to_id;
 
   // make our own message ID so we can log it and allow responses via mailhandler.
   $msg_id = '<'. time() .'.'. mt_rand() .'@'. drupal_strtolower($_SERVER['SERVER_NAME']) .'>';
   $from    = variable_get('casetracker_mail_address', variable_get('site_mail', ini_get('sendmail_from')));
-  $subject = strtr(variable_get('casetracker_mail_subject', _casetracker_mail_subject()), $variables);
-  $body    = strtr(variable_get('casetracker_mail_case_message', _casetracker_mail_case_message()), $variables);
   db_query("INSERT INTO {casetracker_mail} (msg_id, nid, cid) VALUES ('%s', %d, %d)", $msg_id, $case->nid, isset($comment['cid']) ? $comment['cid'] : 0);
 
   // @todo this currently sends to only author and assigned. there needs to be
   // finer-grain control here, like an OG subscribers or all commenters, etc.
-  $results = db_query(
-    "SELECT uid, name, mail FROM {users} WHERE uid IN (%d, %d, %d)",
-    $comment['uid'],
-    $case->uid,
-    is_numeric($assignToId)
-      ? $assignToId
-      : casetracker_get_uid($assignToId)
-  );
+  $results = db_query("SELECT uid, name, mail FROM {users} WHERE uid IN (%d, %d, %d)", $comment['uid'], $case->uid, is_numeric($assign_to_id) ? $assign_to_id : casetracker_get_uid($assign_to_name));
+  drupal_set_message('<pre>'. print_r(db_fetch_array($results), TRUE) .'</pre>');
+
+  $params = array();
+  $params['case'] = $case;
+  $params['comment'] = $comment;
+  $params['is_comment'] = $is_comment;
+  $params['pid'] = $pid;
+  $params['project'] = $project;
+  $params['assign_to_name'] = $assign_to_name;
+  $params['headers'] = array('Message-ID' => $msg_id);
 
   while ($result = db_fetch_object($results)) {
-    if ($result->uid == $user->uid) { continue; } // don't fire to currently commenting user.
-    if (!$result->mail) { continue; } // don't fire blanks.
+    if ($result->uid == $user->uid) { // don't fire to currently commenting user.
+      continue; 
+    }
+    if (!$result->mail) { // don't fire blanks.
+      continue; 
+    }
+
+    // get the language for the user
+    $account = user_load(array('uid' => $result->uid));
+    $language = user_preferred_language($account);
+    $params['language'] = $language->language;
 
     // if we get here a mail is send
-    $mail_status = drupal_mail('casetracker_mail', $result->mail, $subject, $body, $from, array('Message-ID' => $msg_id));
+    $mail_status = drupal_mail('casetracker_mail', 'case', $result->mail, $language, $params, $from);
     if (!$mail_status) { // mail failure doesn't actually tell us much, since PHP returns no error string, but hey, feel good, right?
-      watchdog('casetracker_mail', t('E-mail notification failed for %address.', array('%address' => $result->mail)));
+      watchdog('casetracker_mail', 'E-mail notification failed for %address.', array('%address' => $result->mail));
+    }
+    else {
+      watchdog('casetracker_mail', 'E-mail notification succeeded for %address.', array('%address' => $result->mail));
     }
   }
 }
 
+
 /**
  * Implementation of hook_comment().
  */
 function casetracker_mail_comment(&$comment, $op) {
   switch ($op) {
     case 'insert':
-    case 'update':
       $node = node_load($comment['nid']); // checks type and compares existing case meta to submitted.
       if (!in_array($node->type, variable_get('casetracker_case_node_types', array('casetracker_basic_case')), TRUE)) {
         return; // if this isn't a casetracker case node type, return without sullying our miserable code. MISERY!
@@ -168,9 +165,7 @@ function casetracker_mail_comment(&$comm
 function casetracker_mail_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
   switch ($op) {
     case 'insert':
-    case 'update':
       if (in_array($node->type, variable_get('casetracker_case_node_types', array('casetracker_basic_case')), TRUE)) {
-        dvm('send');
         casetracker_mail_send($node);
       } break;
 
@@ -185,7 +180,7 @@ function casetracker_mail_nodeapi(&$node
  * Implementation of hook_mailhandler().
  *
  *   project_number: 500
- *   type: casetracker_case
+ *   type: casetracker_basic_case
  *   case_title: This is a case title! Yes!
  *   assign_to: Morbus Iff
  *   case_status: open
@@ -203,9 +198,10 @@ function casetracker_mailhandler($node, 
   $node->assign_to = $node->assign_to  ? $node->assign_to  : $node->uid;
   foreach (array('priority', 'status', 'type') as $state) {
     $options = casetracker_case_state_load($state);
-    $node->{'case_'.$state.'_id'} = $node->{'case_'.$state}
-      ? db_result(db_query("SELECT csid FROM {casetracker_case_states} WHERE case_state_name = LOWER('%s') AND case_state_realm = '%s'", drupal_strtolower($node->{'case_'.$state}), $state))
-      : variable_get('casetracker_default_case_'.$state, array_shift(array_keys($options)));
+    $temp_keys = array_keys($options);
+    $node->{'case_'. $state .'_id'} = $node->{'case_'. $state}
+      ? db_result(db_query("SELECT csid FROM {casetracker_case_states} WHERE case_state_name = LOWER('%s') AND case_state_realm = '%s'", drupal_strtolower($node->{'case_'. $state}), $state))
+      : variable_get('casetracker_default_case_'. $state, array_shift($temp_keys));
   }
 
   // @todo potential hack for CCK fields: explode on [ and make
@@ -228,15 +224,11 @@ function casetracker_mailhandler($node, 
     $comment['case_type_id']     = $case->case_type_id;
     $comment['case_status_id']   = $case->case_status_id;
     $comment['case_title']       = $case->title;
-    $comment['pid']              = $case->pid;
+    $comment['prid']              = $case->pid;
     $comment['assign_to']        = casetracker_get_name($case->assign_to);
     // @todo allow emailed comments to change these states.
     comment_save($comment);
   }
-  // else (if no pid) then pass the node back for default processing)
-  else {
-    return $node;
-  }
 }
 
 /**
@@ -250,7 +242,7 @@ function casetracker_mail_settings() {
     '#title'         => t('E-mail settings'),
     '#collapsible'   => TRUE,
     '#collapsed'     => FALSE,
-    '#description'   => t('Enter the From address, subject, case message, and comment message for Case Tracker generated mails. Available variables are %project_id (the node ID), %project_number, %project_title, %case_id (the node ID), %case_number, %case_title.'),
+    '#description'   => t('Enter the From address, subject, case message, and comment message for Case Tracker generated mails. Available variables are %project_id (the node ID), %project_title, %case_id (the node ID), %case_number, %case_title.'),
   );
   $form['casetracker_mail']['casetracker_mail_address'] = array(
     '#type'          => 'textfield',
@@ -332,11 +324,10 @@ function _casetracker_mail_plain_descrip
     '</code>', '</ul>', '</ol>', '</li>', '</dl>', '</dd>',
     '</dt>',
   );
-  foreach($tags AS $tag)
-  {
-    $_description = str_replace($tag, $tag . "\n", $_description);
+  foreach ($tags AS $tag) {
+    $_description = str_replace($tag, $tag ."\n", $_description);
   }
   $result = filter_xss($_description, array('dd'));
 
   return $result;
-}
\ No newline at end of file
+}
