You are here

function simplenews_unsubscribe_user_action in Simplenews 6

Same name and namespace in other branches
  1. 6.2 simplenews_action/simplenews_action.module \simplenews_unsubscribe_user_action()

A configurable Drupal action. Unsubscribe the user from a newsletter hook = user: Unsubscribe this user from selected newsletter

Available context: $context['tid'] newsletter tid $context['name'] newsletter name

See also

simplenews_unsubscribe_user_action_form()

simplenews_unsubscribe_user_action_submit()

File

simplenews_action/simplenews_action.module, line 242
simplenews_action.inc Provide actions for simplenews.

Code

function simplenews_unsubscribe_user_action(&$object, $context = array()) {
  if ($context['hook'] == 'user') {
    if (isset($context['tid'])) {

      // This action is only called in the context of user. User data is in $context.
      $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);
        }
      }
      drupal_set_message(t('You have been removed from the %newsletter subscription list.', array(
        '%newsletter' => $context['name'],
      )));
      watchdog('action', 'User %name unsubscribed from newsletter %newsletter.', array(
        '%name' => $account->name,
        '%newsletter' => $context['name'],
      ));
    }
  }
}