? simplenews.token.inc
? tests
? translations
Index: simplenews.admin.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/simplenews/simplenews.admin.inc,v
retrieving revision 1.57
diff -u -r1.57 simplenews.admin.inc
--- simplenews.admin.inc	8 May 2009 07:56:25 -0000	1.57
+++ simplenews.admin.inc	30 Jul 2009 19:34:57 -0000
@@ -378,6 +378,7 @@
  * Menu callback: Mass subscribe to newsletters.
  *
  * @see simplenews_subscription_list_add_submit()
+ * @todo Add 32char description field as subsription source
  */
 function simplenews_subscription_list_add() {
   global $language;
@@ -442,7 +443,7 @@
     if (valid_email_address($email)) {
       foreach ($checked_newsletters as $tid) {
         $newsletter = taxonomy_get_term($tid);
-        simplenews_subscribe_user($email, $newsletter->tid, FALSE, $langcode);
+        simplenews_subscribe_user($email, $newsletter->tid, FALSE, 'mass subscribe', $langcode);
         $added[] = $email;
       }
     }
@@ -474,6 +475,7 @@
  * Menu callback: Export email address of subscriptions.
  *
  * @see simplenews_admin_export_after_build()
+ * @todo Add checkboxes for subscribed and unsubscribed selection.
  */
 function simplenews_subscription_list_export() {
   $form['states'] = array(
@@ -508,6 +510,7 @@
 }
 
 function simplenews_admin_export_after_build($form, $form_element) {
+//TODO: Add (un)subscribed as filter to form.
   if (isset($form_element['values']['op']) && $form_element['values']['op'] == t('Export')) {
     $states = array_filter($form_element['values']['states']);
     $newsletters = array_filter($form_element['values']['newsletters']);
@@ -561,6 +564,7 @@
  * Menu callback: Mass subscribe to newsletters.
  *
  * @see simplenews_subscription_list_remove_submit()
+ * @todo Add 32char description field as unsubsription source
  */
 function simplenews_subscription_list_remove() {
   $form['emails'] = array(
@@ -568,7 +572,7 @@
     '#title' => t('Email addresses'),
     '#cols' => 60,
     '#rows' => 5,
-    '#description' => t('Email addresses must be separated by comma, space or newline. Email addresses which are no longer subscribed to any newsletter, will be removed from the database.'),
+    '#description' => t('Email addresses must be separated by comma, space or newline.'),
   );
   $newsletters = array();
   foreach (taxonomy_get_tree(variable_get('simplenews_vid', '')) as $newsletter) {
@@ -599,7 +603,7 @@
     if (valid_email_address($email)) {
       foreach ($checked_newsletters as $tid) {
         $newsletter = taxonomy_get_term($tid);
-        simplenews_unsubscribe_user($email, $newsletter->tid, FALSE);
+        simplenews_unsubscribe_user($email, $newsletter->tid, FALSE, 'mass unsubscribe');
         $removed[] = $email;
       }
     }
@@ -609,7 +613,7 @@
   }
   if ($removed) {
     $removed = implode(", ", $removed);
-    drupal_set_message(t('The following addresses were unsubscribed or removed: %removed.', array('%removed' => $removed)));
+    drupal_set_message(t('The following addresses were unsubscribed: %removed.', array('%removed' => $removed)));
 
     $newsletter_names = array();
     foreach ($checked_newsletters as $tid) {
@@ -655,6 +659,8 @@
  * @see simplenews_subscription_list_form_submit()
  */
 function simplenews_subscription_list_form() {
+//TODO: Subscriber maintanance needs overhaul now we have more data availabale.
+
   // Table header. Used as tablesort default
   $header = array(
     array('data' => t('Email'), 'field' => 'ss.mail', 'sort' => 'asc'),
@@ -1190,7 +1196,7 @@
   if (isset($subscription_count[$tid])) {
     return $subscription_count[$tid];
   }
-  $subscription_count[$tid] = db_result(db_query('SELECT COUNT(*) FROM {simplenews_snid_tid} WHERE tid = %d', $tid));
+  $subscription_count[$tid] = db_result(db_query('SELECT COUNT(*) FROM {simplenews_snid_tid} WHERE tid = %d AND status = %d', $tid, SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED));
   
   return $subscription_count[$tid];
 }
Index: simplenews.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/simplenews/simplenews.install,v
retrieving revision 1.32
diff -u -r1.32 simplenews.install
--- simplenews.install	9 Mar 2009 07:44:46 -0000	1.32
+++ simplenews.install	30 Jul 2009 19:34:58 -0000
@@ -107,20 +107,41 @@
   );
 
   $schema['simplenews_snid_tid'] = array(
-    'description' => t('Newsletter series subscription data.'),
+    'description' => 'Newsletter series subscription data.',
     'fields' => array(
        'snid' => array(
-        'description' => t('The {simplenews_subscriptions}.snid who is subscribed.'),
+        'description' => 'The {simplenews_subscriptions}.snid who is subscribed.',
         'type' => 'int',
         'not null' => TRUE,
         'default' => 0,
       ),
-       'tid' => array(
-        'description' => t('The newsletter series ({term_data}.tid) the subscriber is subscribed to.'),
+      'tid' => array(
+        'description' => 'The newsletter series ({term_data}.tid) the subscriber is subscribed to.',
+        'type' => 'int',
+        'not null' => TRUE,
+        'default' => 0,
+      ),
+      'status' => array(
+        'description' => 'A flag indicating whether the user is subscribed (1) or unsubscribed (0).',
+        'type' => 'int',
+        'size' => 'small',
+        'not null' => TRUE,
+        'default' => 1
+      ),
+      'timestamp' => array(
+        'description' => 'UNIX timestamp of when the user is (un)subscribed.',
         'type' => 'int',
+        'unsigned' => TRUE,
         'not null' => TRUE,
         'default' => 0,
       ),
+      'source' => array(
+        'description' => 'The source via which the user is (un)subscription.',
+        'type' => 'varchar',
+        'length' => 24,
+        'not null' => TRUE,
+        'default' => '',
+      ),
     ),
     'primary key' => array('snid', 'tid'),
   );
@@ -584,4 +605,36 @@
   $vocabulary['required'] = TRUE;
   taxonomy_save_vocabulary($vocabulary);
   return array();
+}
+
+/**
+ * Add (un)subscription data to table simplenews_snid_tid.
+ */
+function simplenews_update_6100() {
+  $ret = array();
+  db_add_field($ret, 'simplenews_snid_tid', 'status', array(
+      'description' => 'A flag indicating whether the user is subscribed (1) or unsubscribed (0).',
+      'type' => 'int',
+      'size' => 'small',
+      'not null' => TRUE,
+      'default' => 1
+    )
+  );
+  db_add_field($ret, 'simplenews_snid_tid', 'timestamp', array(
+      'description' => 'UNIX timestamp of when the user is (un)subscribed.',
+      'type' => 'int',
+      'unsigned' => TRUE,
+      'not null' => TRUE,
+      'default' => 0,
+    )
+  );
+  db_add_field($ret, 'simplenews_snid_tid', 'source', array(
+      'description' => 'The source via which the user is (un)subscription.',
+      'type' => 'varchar',
+      'length' => 24,
+      'not null' => TRUE,
+      'default' => '',
+    )
+  );
+  return $ret;
 }
\ No newline at end of file
Index: simplenews.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/simplenews/simplenews.module,v
retrieving revision 1.210
diff -u -r1.210 simplenews.module
--- simplenews.module	8 May 2009 07:59:20 -0000	1.210
+++ simplenews.module	30 Jul 2009 19:34:59 -0000
@@ -34,6 +34,12 @@
 define('SIMPLENEWS_COMMAND_SEND_TEST', 2);
 
 /**
+ * NEWSLETTER SUBSCRIPTION STATUS
+ */
+define('SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED', 1);
+define('SIMPLENEWS_SUBSCRIPTION_STATUS_UNSUBSCRIBED', 0);
+
+/**
  * NEWSLETTER SEND STATUS
  */
 define('SIMPLENEWS_STATUS_SEND_NOT', 0);
@@ -665,7 +671,6 @@
   if ($op == 'delete' && $term['vid'] == variable_get('simplenews_vid', '')) {
     switch ($type) {
       case 'term':
-        //TODO subscriber is not deleted when last subscription is removed.
         db_query('DELETE FROM {simplenews_snid_tid} WHERE tid = %d', $term['tid']);
         db_query("DELETE FROM {blocks} WHERE module = '%s' AND delta = '%s'", 'simplenews', $term['tid']);
         drupal_set_message(t('All subscriptions to newsletter %newsletter have been deleted.', array('%newsletter' => $term['name'])));
@@ -717,10 +722,10 @@
       if ($category == 'newsletter' && user_access('subscribe to newsletters')) {
         foreach ($edit['newsletters'] as $tid => $checked) {
           if ($checked) {
-            simplenews_subscribe_user($account->mail, $tid, FALSE);
+            simplenews_subscribe_user($account->mail, $tid, FALSE, 'website');
           }
           else {
-            simplenews_unsubscribe_user($account->mail, $tid, FALSE);
+            simplenews_unsubscribe_user($account->mail, $tid, FALSE, 'website');
           }
         }
       }
@@ -767,7 +772,10 @@
         );
         $tree = taxonomy_get_tree(variable_get('simplenews_vid', ''));
         foreach ($tree as $newsletter) {
-          if (db_result(db_query('SELECT COUNT(s.uid) FROM {simplenews_subscriptions} s INNER JOIN {simplenews_snid_tid} t ON s.snid = t.snid WHERE s.uid = %d AND t.tid = %d', $account->uid, $newsletter->tid))) {
+          if (db_result(db_query('SELECT COUNT(s.uid) FROM {simplenews_subscriptions} s
+                                 INNER JOIN {simplenews_snid_tid} t ON s.snid = t.snid
+                                 WHERE s.uid = %d AND t.tid = %d AND t.status = %d',
+                                 $account->uid, $newsletter->tid, SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED))) {
             $subscriptions[] = l($newsletter->name, 'taxonomy/term/'. $newsletter->tid);
           }
         }
@@ -943,8 +951,14 @@
  *   The language code (i.e. 'en', 'nl') of the user preferred language.
  *   Use '' for the site default language.
  *   Use NULL for the language of the current page.
+ * @param string $source
+ *   Indication for source of subscription. Simplenews uses these sources:
+ *    website: via any website form (with or without confirmation email)
+ *    mass subscribe: mass admin UI
+ *    mass unsubscribe: mass admin UI
+ *    action: Drupal actions
  */
-function simplenews_subscribe_user($mail, $tid, $confirm = TRUE, $preferred_language = NULL) {
+function simplenews_subscribe_user($mail, $tid, $confirm = TRUE, $source = 'unknown', $preferred_language = NULL) {
   global $language;
 
   //Prevent mismatches from accidental capitals in mail address
@@ -993,8 +1007,22 @@
     drupal_mail('simplenews', 'subscribe', $mail, $subscription->language, $params, $params['from']['address']);
   }
   elseif (!isset($subscription->tids[$tid])) {
-    // OR add user to newsletter relationship if not already subscribed.
-    db_query("INSERT INTO {simplenews_snid_tid} (snid, tid) VALUES (%d, %d)", $subscription->snid, $tid);
+    // Add user to newsletter relationship if not already subscribed.
+    
+    // Check if user is (un)subscribed to this newsletter.
+    // Resubscribe or add new subscription.
+    if (isset($subscription->newsletter_subscription[$tid])) {
+      db_query("UPDATE {simplenews_snid_tid}
+               SET status = %d, timestamp = %d, source = '%s'
+               WHERE snid = %d AND tid = %d",
+               SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED, time(), $source,
+               $subscription->snid, $tid);
+    }
+    else {
+      db_query("INSERT INTO {simplenews_snid_tid} (snid, tid, status, timestamp, source)
+               VALUES (%d, %d, %d, %d, '%s')",
+               $subscription->snid, $tid, SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED, time(), $source);
+    }
 
     // Execute simplenews subscribe trigger.
     simplenews_call_actions('subscribe', $subscription);
@@ -1013,15 +1041,21 @@
  * @param string $mail The email address to unsubscribe from the newsletter.
  * @param integer $tid The term ID of the newsletter.
  * @param boolean $confirm TRUE = send confirmation mail; FALSE = unsubscribe immediate from the newsletter
+ * @param string $source
+ *   Indication for source of unsubscription. Simplenews uses these sources:
+ *    website: via any website form (with or without confirmation email)
+ *    mass subscribe: mass admin UI
+ *    mass unsubscribe: mass admin UI
+ *    action: Drupal actions
  */
-function simplenews_unsubscribe_user($mail, $tid, $confirm = TRUE) {
+function simplenews_unsubscribe_user($mail, $tid, $confirm = TRUE, $source = 'unknown') {
   //Prevent mismatches from accidental capitals in mail address
   $mail = strtolower($mail);
 
   $account = (object) array('mail' => $mail);
   $subscription = simplenews_get_subscription($account);
 
-  // The unlikely case that a user is subscribed from a non existing newsletter is logged
+  // The unlikely case that a user is unsubscribed from a non existing newsletter is logged
   if (!$newsletter = taxonomy_get_term($tid)) {
     watchdog('simplenews', 'Attempt to unsubscribe from non existing newsletter term ID %id', array('%id' => $tid), WATCHDOG_ERROR);
     return FALSE;
@@ -1037,17 +1071,15 @@
     drupal_mail('simplenews', 'unsubscribe', $mail, $subscription->language, $params, $params['from']['address']);
   }
   elseif (isset($subscription->tids[$tid])) {
-    // OR remove the user from the newsletter.
-    db_query('DELETE FROM {simplenews_snid_tid} WHERE snid = %d AND tid = %d', $subscription->snid, $tid);
-
-    // Clean up subscription account if user is not subscribed to any newsletter anymore
-    if (!db_result(db_query("SELECT COUNT(*) FROM {simplenews_snid_tid} t WHERE t.snid = %d", $subscription->snid))) {
-      db_query('DELETE FROM {simplenews_subscriptions} WHERE snid = %d', $subscription->snid);
-    }
+    // Unsubscribe the user from the newsletter.
+    db_query("UPDATE {simplenews_snid_tid}
+             SET status = %d, timestamp = %d, source = '%s'
+             WHERE snid = %d AND tid = %d",
+             SIMPLENEWS_SUBSCRIPTION_STATUS_UNSUBSCRIBED, time(), $source, $subscription->snid, $tid);
 
     // Execute simplenews unsubscribe trigger
     simplenews_call_actions('unsubscribe', $subscription);
-    }
+  }
 
   return TRUE;
 }
@@ -1060,15 +1092,19 @@
  *
  * @return boolean TRUE = email address is subscribed to given newsletter term id
  */
+//@TODO only return active subscriptions.
 function simplenews_user_is_subscribed($mail, $tid, $reset = FALSE) {
   static $subscribed = array();
 
     if ($reset) {
-      unset($subscribed);
+      $subscribed = array();
     }
 
     if (!isset($subscribed[$mail][$tid])) {
-      $subscribed[$mail][$tid] = db_result(db_query("SELECT COUNT(*) FROM {simplenews_subscriptions} s INNER JOIN {simplenews_snid_tid} t ON s.snid = t.snid WHERE s.mail = '%s' AND t.tid = %d", $mail, $tid)) ? TRUE : FALSE;
+      $subscribed[$mail][$tid] = db_result(db_query("SELECT COUNT(*) FROM {simplenews_subscriptions} s
+                                                    INNER JOIN {simplenews_snid_tid} t ON s.snid = t.snid
+                                                    WHERE s.mail = '%s' AND t.tid = %d AND t.status = %d",
+                                                    $mail, $tid, SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED)) ? TRUE : FALSE;
     }
 
   return $subscribed[$mail][$tid];
@@ -1091,7 +1127,8 @@
  * @return subscription object
  *   object(
  *     snid :  subscription id. 0 if account is not subscribed
- *     tids :  array of newsletter tid's
+ *     tids :  array of tid's of active subscriptions
+ *     newsletter_subscriptions : array of newsletter subscription objects
  *     uid  :  user id. 0 if account is anonymous user
  *     mail :  user email address. empty if email is unknown
  *     name :  always empty. Added for compatebility with user account object
@@ -1112,10 +1149,14 @@
   }
 
   if (!empty($subscription)) {
-    $result = db_query("SELECT tid FROM {simplenews_snid_tid} t WHERE t.snid = %d", $subscription->snid);
+    $result = db_query("SELECT tid, status, timestamp, source
+                        FROM {simplenews_snid_tid} t WHERE t.snid = %d", $subscription->snid);
     $subscription->tids = array();
-    while ($newsletter = db_fetch_object($result)) {
-      $subscription->tids[$newsletter->tid] = $newsletter->tid;
+    while ($newsletter_subscription = db_fetch_object($result)) {
+      if ($newsletter_subscription->status == SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED) {
+        $subscription->tids[$newsletter_subscription->tid] = $newsletter_subscription->tid;
+      }
+      $subscription->newsletter_subscription[$newsletter_subscription->tid] = $newsletter_subscription;
     }
     $subscription->name = '';
     $subscription->language = user_preferred_language($subscription);
@@ -1129,6 +1170,7 @@
     $subscription->language = language_default();
     $subscription->snid = 0;
     $subscription->tids = array();
+    $subscription->newsletter_subscription = array();
   }
   return $subscription;
 }
@@ -1314,7 +1356,7 @@
 
   switch ($form_state['values']['action']) {
     case 'subscribe':
-      simplenews_subscribe_user($form_state['values']['mail'], $tid, $confirm);
+      simplenews_subscribe_user($form_state['values']['mail'], $tid, $confirm, 'website');
       if ($confirm) {
         drupal_set_message(t('You will receive a confirmation email shortly containing further instructions on how to complete your subscription.'));
       }
@@ -1323,7 +1365,7 @@
       }
       break;
     case 'unsubscribe':
-      simplenews_unsubscribe_user($form_state['values']['mail'], $tid, $confirm);
+      simplenews_unsubscribe_user($form_state['values']['mail'], $tid, $confirm, 'website');
       if ($confirm) {
         drupal_set_message(t('You will receive a confirmation email shortly containing further instructions on how to complete the unsubscription process.'));
       }
@@ -1367,7 +1409,10 @@
 
     if (empty($accounts)) {
       // No accounts specified. Get email address of all accounts subscribed to this newsletter.
-      $result = db_query('SELECT s.mail FROM {simplenews_subscriptions} s INNER JOIN {simplenews_snid_tid} t ON s.snid = t.snid WHERE s.activated = %d AND t.tid = %d', 1, $node_data['tid']);
+      $result = db_query('SELECT s.mail FROM {simplenews_subscriptions} s
+                         INNER JOIN {simplenews_snid_tid} t ON s.snid = t.snid
+                         WHERE s.activated = %d AND t.tid = %d AND t.status = %d',
+                         1, $node_data['tid'], SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED);
       while ($account = db_fetch_object($result)) {
         $mails[] = array('mail' => $account->mail);
       }
Index: simplenews.subscription.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/simplenews/simplenews.subscription.inc,v
retrieving revision 1.12
diff -u -r1.12 simplenews.subscription.inc
--- simplenews.subscription.inc	7 Mar 2009 17:14:31 -0000	1.12
+++ simplenews.subscription.inc	30 Jul 2009 19:34:59 -0000
@@ -60,10 +60,10 @@
       arsort($form_state['values']['newsletters'], SORT_NUMERIC);
       foreach ($form_state['values']['newsletters'] as $tid => $checked) {
         if ($checked) {
-          simplenews_subscribe_user($form_state['values']['mail'], $tid, FALSE);
+          simplenews_subscribe_user($form_state['values']['mail'], $tid, FALSE, 'website');
         }
         else {
-          simplenews_unsubscribe_user($form_state['values']['mail'], $tid, FALSE);
+          simplenews_unsubscribe_user($form_state['values']['mail'], $tid, FALSE, 'website');
         }
       }
       drupal_set_message(t('The newsletter subscriptions for %mail have been updated.', array('%mail' => $form_state['values']['mail'])));
@@ -71,7 +71,7 @@
     case t('Subscribe'):
       foreach ($form_state['values']['newsletters'] as $tid => $checked) {
         if ($checked) {
-          simplenews_subscribe_user($form_state['values']['mail'], $tid);
+          simplenews_subscribe_user($form_state['values']['mail'], $tid, TRUE, 'website');
         }
       }
       drupal_set_message(t('You will receive a confirmation email shortly containing further instructions on how to complete your subscription.'));
@@ -79,7 +79,7 @@
     case t('Unsubscribe'):
       foreach ($form_state['values']['newsletters'] as $tid => $checked) {
         if ($checked) {
-          simplenews_unsubscribe_user($form_state['values']['mail'], $tid);
+          simplenews_unsubscribe_user($form_state['values']['mail'], $tid, TRUE, 'website');
         }
       }
       drupal_set_message(t('You will receive a confirmation email shortly containing further instructions on how to complete the unsubscription process.'));
@@ -144,7 +144,7 @@
 }
 
 function simplenews_confirm_add_form_submit($form, &$form_state) {
-  simplenews_subscribe_user($form_state['values']['mail'], $form_state['values']['newsletter']->tid, FALSE);
+  simplenews_subscribe_user($form_state['values']['mail'], $form_state['values']['newsletter']->tid, FALSE, 'website');
   drupal_set_message(t('%user was added to the %newsletter mailing list.', array('%user' => $form_state['values']['mail'], '%newsletter' => $form_state['values']['newsletter']->name)));
 }
 
@@ -163,15 +163,15 @@
   return confirm_form($form,
     t('Confirm unsubscription'),
     '',
-    t('This action will only remove you from the newsletter mailing list. If you are registered at our site, your account information will remain unchanged.'),
+    t('This action will unsubscribe you from the newsletter mailing list.'),
     t('Unsubscribe'),
     t('Cancel')
   );
 }
 
 function simplenews_confirm_removal_form_submit($form, &$form_state) {
-  simplenews_unsubscribe_user($form_state['values']['mail'], $form_state['values']['newsletter']->tid, FALSE);
-  drupal_set_message(t('%user was removed from the %newsletter mailing list.', array('%user' => $form_state['values']['mail'], '%newsletter' => $form_state['values']['newsletter']->name)));
+  simplenews_unsubscribe_user($form_state['values']['mail'], $form_state['values']['newsletter']->tid, FALSE, 'website');
+  drupal_set_message(t('%user was unsubscribed from the %newsletter mailing list.', array('%user' => $form_state['values']['mail'], '%newsletter' => $form_state['values']['newsletter']->name)));
 }
 
 /**
Index: simplenews_action/simplenews_action.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/simplenews/simplenews_action/simplenews_action.module,v
retrieving revision 1.5
diff -u -r1.5 simplenews_action.module
--- simplenews_action/simplenews_action.module	21 Jan 2009 08:20:44 -0000	1.5
+++ simplenews_action/simplenews_action.module	30 Jul 2009 19:34:59 -0000
@@ -187,7 +187,7 @@
     if (isset($context['tid'])) {
       // This action is only called in the context of user. User data is in $context.
       $account = $context['account'];
-      simplenews_subscribe_user($account->mail, $context['tid'], FALSE);
+      simplenews_subscribe_user($account->mail, $context['tid'], FALSE, 'action');
       drupal_set_message(t('You have been subscribed to newsletter %newsletter.', array('%newsletter' => $context['name'])));
       watchdog('action', 'User %name subscribed to newsletter %newsletter.', array('%name' => $account->name, '%newsletter' => $context['name']));
     }
@@ -247,12 +247,11 @@
       $account = $context['account'];
       //TODO: Unsubscribing should be done by simplenews_unsubscribe_user but simplenews_get_user_subscription fails because the user is already removed
       if ($result = db_fetch_object(db_query("SELECT snid FROM {simplenews_subscriptions} WHERE mail = '%s'", $account->mail))) {
-        db_query('DELETE FROM {simplenews_snid_tid} WHERE snid = %d AND tid = %d', $result->snid, $context['tid']);
-
-        // Clean up simplenews_subscriptions if no more newsletter subscriptions.
-        if (!db_result(db_query("SELECT COUNT(*) FROM {simplenews_snid_tid} t WHERE t.snid = %d", $result->snid))) {
-          db_query('DELETE FROM {simplenews_subscriptions} WHERE snid = %d', $result->snid);
-        }
+        db_query("UPDATE {simplenews_snid_tid}
+                 SET status = %d, timestamp = %d, source = '%s'
+                 WHERE snid = %d AND tid = %d",
+                 SIMPLENEWS_SUBSCRIPTION_STATUS_UNSUBSCRIBED, time(), t('Action'), $result->snid, $context['tid']
+                );
       }
       drupal_set_message(t('You have been removed from the %newsletter subscription list.', array('%newsletter' => check_plain($context['name']))));
       watchdog('action', 'User %name unsubscribed from newsletter %newsletter.', array('%name' => check_plain($account->name), '%newsletter' => check_plain($context['name'])));
