You are here

function simplenews_unsubscribe_user_action in Simplenews 6.2

Same name and namespace in other branches
  1. 6 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 263
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("\n        SELECT snid\n        FROM {simplenews_subscriptions}\n        WHERE mail = '%s'", $account->mail))) {
        db_query("\n          UPDATE {simplenews_snid_tid}\n          SET status = %d,\n            timestamp = %d,\n            source = '%s'\n          WHERE snid = %d\n            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' => $context['name'],
      )));
      watchdog('action', 'User %name unsubscribed from newsletter %newsletter.', array(
        '%name' => $account->name,
        '%newsletter' => $context['name'],
      ));
    }
  }
}