Index: coherent_access.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/coherent_access/coherent_access.module,v
retrieving revision 1.2.2.5
diff -u -p -r1.2.2.5 coherent_access.module
--- coherent_access.module	2 Feb 2009 21:47:19 -0000	1.2.2.5
+++ coherent_access.module	27 Oct 2009 00:37:50 -0000
@@ -144,20 +144,20 @@ function coherent_access_admin_settings(
   
   $form['coherent_access_send_email'] = array(
     '#type' => 'checkbox',
-    '#title' => t('Send email when viewers or editors are added'),
+    '#title' => t('Send email when viewers or editors are added or deleted'),
     '#default_value' => variable_get('coherent_access_send_email', FALSE)
   );
   
   $form['coherent_access_subject'] = array(
     '#type' => 'textarea',
     '#title' => t('Email notification subject'),
-    '#default_value' => variable_get('coherent_access_subject', 'You have been added as a %role  for %nodetitle by %sender'),
+    '#default_value' => variable_get('coherent_access_subject', 'You have been %action a %role  for %nodetitle by %sender'),
   );
   
   $form['coherent_access_body'] = array(
     '#type' => 'textarea',
     '#title' => t('Email notification body'),
-    '#default_value' => variable_get('coherent_access_body', 'You have been added as a %role for %nodetitle by %sender. You can access this by going to %nodeurl'),
+    '#default_value' => variable_get('coherent_access_body', 'You have been %action a %role for %nodetitle by %sender. You can access this by going to %nodeurl'),
   );
 
   return system_settings_form($form);
@@ -471,6 +471,8 @@ function coherent_access_nodeapi(&$node,
         }
         db_query('DELETE FROM {coherent_access_user} WHERE gid IN('. implode(',', array_fill(0, count($gids), '%d')) .')', $gids);
       case 'insert':
+        $existing = empty($existing) ? array() : $existing;
+
         if (isset($node->shared_editing['edit']['editor_list'])) {
           $editors = unserialize($node->shared_editing['edit']['editor_list']);
         }
@@ -508,17 +510,49 @@ function coherent_access_nodeapi(&$node,
         if (variable_get('coherent_access_send_email', FALSE)) {
           $email = array();
           foreach ($inserts as $uid => $mode) {
-            if (empty($existing) || !array_key_exists($uid, $existing)) {
+            if ((empty($existing) || !array_key_exists($uid, $existing) || $existing[$uid] != $inserts[$uid]) && $params['sender']->uid != $uid) {
+
               $params['recipient'] = user_load(array('uid' => $uid));
-              if ($mode == COHERENT_ACCESS_EDIT | COHERENT_ACCESS_VIEW) {
+              $params['action'] = t('added as');
+              if ($mode == (COHERENT_ACCESS_EDIT | COHERENT_ACCESS_VIEW)) {
                 $params['role'] = 'editor';
+
+                // This is a special case where a user may already be a 
+                // 'viewer' but is being changed to an editor.
+                if (!empty($existing[$uid]) && $existing[$uid] == COHERENT_ACCESS_VIEW) {
+                  $params['action'] = t('changed to');
+                }
               }
               else {
                 $params['role'] = 'viewer';
+
+                // This is a special case where a user may already be a 
+                // 'viewer' but is being changed to an editor.
+                if (!empty($existing[$uid]) && $existing[$uid] == (COHERENT_ACCESS_EDIT | COHERENT_ACCESS_VIEW)) {
+                  $params['action'] = t('changed to');
+                }
               }
+
+              $mailkey = 'coherent_access'. $node->nid .'_'. $uid;
+              drupal_mail('coherent_access', $mailkey, $params['recipient']->mail, user_preferred_language($params['recipient']), $params, $params['sender']->mail);
+            }
+          }
+
+          // Send out messages regarding reovcation as well.
+          foreach ($existing as $uid => $mode) {
+            if (!array_key_exists($uid, $inserts)) {
+              $params['recipient'] = user_load(array('uid' => $uid));
+              if ($mode == (COHERENT_ACCESS_EDIT | COHERENT_ACCESS_VIEW)) {
+                $params['role'] = 'editor';
+              }
+              else {
+                $params['role'] = 'viewer';
+              }
+
+              $params['action'] = t('revoked as');
+
               $mailkey = 'coherent_access'. $node->nid .'_'. $uid;
-              coherent_access_mail($key, $message, $params);
-              drupal_mail($mailkey, $params['recipient']->mail, $message['subject'], $message['body']);
+              drupal_mail('coherent_access', $mailkey, $params['recipient']->mail, user_preferred_language($params['recipient']), $params, $params['sender']->mail);
             }
           }
         }
@@ -705,14 +739,16 @@ function coherent_access_mail($key, &$me
   $recipient = $params['recipient'];
   $role = $params['role'];
   $node = $params['node'];
+  $action = $params['action'];
   
-  $subject = variable_get('coherent_access_subject', 'You have been added as a %role for %nodetitle by %sender. You can access this by going to %nodeurl');
-  $body = variable_get('coherent_access_body', 'You have been added as a %role for %nodetitle by %sender. You can access this by going to %nodeurl');
+  $subject = variable_get('coherent_access_subject', 'You have been %action a %role for %nodetitle by %sender. You can access this by going to %nodeurl');
+  $body = variable_get('coherent_access_body', 'You have been %action a %role for %nodetitle by %sender. You can access this by going to %nodeurl');
   
   $urlpath = 'node/'. $node->nid;
   
   $replace = array('%sender' => $sender->name,
                   '%recipient' => $recipient->name,
+                  '%action' => $action,
                   '%role' => $role,
                   '%nodetitle' => $node->title, 
                   '%nodeurl' => url($urlpath, array('absolute' => TRUE)),
