diff --git a/includes/simplenews.admin.inc b/includes/simplenews.admin.inc index 80002e7..4588f9a 100644 --- a/includes/simplenews.admin.inc +++ b/includes/simplenews.admin.inc @@ -725,6 +725,12 @@ function simplenews_subscription_list_add($form, &$form_state) { ); } + $form['resubscribe'] = array( + '#type' => 'checkbox', + '#title' => t('Force resubscription'), + '#description' => t('If checked, previously unsubscribed e-mail addresses will be resubscribed. Consider that this might be against the will of your users.'), + ); + // Include language selection when the site is multilingual. // Default value is the empty string which will result in receiving emails // in the site's default language. @@ -764,6 +770,7 @@ function simplenews_subscription_list_add($form, &$form_state) { function simplenews_subscription_list_add_submit($form, &$form_state) { $added = array(); $invalid = array(); + $unsubscribed = array(); $checked_categories = array_keys(array_filter($form_state['values']['newsletters'])); $langcode = $form_state['values']['language']; @@ -774,10 +781,18 @@ function simplenews_subscription_list_add_submit($form, &$form_state) { continue; } if (valid_email_address($email)) { + $subscriber = simplenews_subscriber_load_by_mail($email); foreach (simplenews_categories_load_multiple($checked_categories) as $category) { - - simplenews_subscribe_user($email, $category->tid, FALSE, 'mass subscribe', $langcode); - $added[] = $email; + // If there is a valid subscriber, check if there is a subscription for the current category and if this subscription has the status unsubscribed. + $is_unsubscribed = $subscriber && array_key_exists($category->tid, $subscriber->newsletter_subscription) + && $subscriber->newsletter_subscription[$category->tid]->status == SIMPLENEWS_SUBSCRIPTION_STATUS_UNSUBSCRIBED; + if ( !$is_unsubscribed || $form_state['values']['resubscribe'] == TRUE) { + simplenews_subscribe_user($email, $category->tid, FALSE, 'mass subscribe', $langcode); + $added[] = $email; + } + else { + $unsubscribed[check_plain(_simplenews_newsletter_name($category))][] = $email; + } } } else { @@ -802,6 +817,19 @@ function simplenews_subscription_list_add_submit($form, &$form_state) { drupal_set_message(t('The following addresses were invalid: %invalid.', array('%invalid' => $invalid)), 'error'); } + $show_hint = FALSE; + foreach ($unsubscribed as $name => $subscribers) { + if ( $subscribers ) { + $subscribers = implode(", ", $subscribers); + drupal_set_message(t('The following addresses were skipped because they have previously unsubscribed from %name: %unsubscribed.', array('%name' => $name, '%unsubscribed' => $subscribers)), 'warning'); + $show_hint = TRUE; + } + } + + if ($show_hint) { + drupal_set_message(t("If you would like to resubscribe them, use the 'Force resubscription' option."), 'warning'); + } + // Return to the parent page. $form_state['redirect'] = 'admin/people/simplenews'; } diff --git a/tests/simplenews.test b/tests/simplenews.test index bc459f1..ff802ed 100644 --- a/tests/simplenews.test +++ b/tests/simplenews.test @@ -1437,6 +1437,46 @@ class SimpleNewsAdministrationTestCase extends SimplenewsTestCase { $after_count = simplenews_count_subscriptions($first); $this->assertEqual($before_count - 1, $after_count, t('Blocked users are not counted in subscription count.')); + // Test mass subscribe with previously unsubscribed users. + for ($i = 0; $i < 3; $i++) { + $tested_subscribers[] = $this->randomEmail(); + } + simplenews_subscribe_user($tested_subscribers[0], $first, FALSE); + simplenews_subscribe_user($tested_subscribers[1], $first, FALSE); + simplenews_unsubscribe_user($tested_subscribers[0], $first, FALSE); + simplenews_unsubscribe_user($tested_subscribers[1], $first, FALSE); + $unsubscribed = implode(', ', array_slice($tested_subscribers, 0, 2)); + $edit = array( + 'emails' => implode(', ', $tested_subscribers), + 'newsletters[' . $first . ']' => TRUE, + ); + + $this->drupalPost('admin/people/simplenews/import', $edit, t('Subscribe')); + drupal_static_reset('simplenews_subscriber_load_multiple'); + drupal_static_reset('simplenews_user_is_subscribed'); + $this->assertFalse(simplenews_user_is_subscribed( $tested_subscribers[0], $first, t('Subscriber not resubscribed through mass subscription.'))); + $this->assertFalse(simplenews_user_is_subscribed( $tested_subscribers[1], $first, t('Subscriber not resubscribed through mass subscription.'))); + $this->assertTrue(simplenews_user_is_subscribed( $tested_subscribers[2], $first, t('Subscriber subscribed through mass subscription.'))); + $substitutes = array('@name' => check_plain(_simplenews_newsletter_name(simplenews_category_load($first))), '@mail' => $unsubscribed); + $this->assertText(t('The following addresses were skipped because they have previously unsubscribed from @name: @mail.', $substitutes)); + $this->assertText(t("If you would like to resubscribe them, use the 'Force resubscription' option.")); + + + // Test mass subscribe with previously unsubscribed users and force resubscription. + $tested_subscribers[2] = $this->randomEmail(); + $edit = array( + 'emails' => implode(', ', $tested_subscribers), + 'newsletters[' . $first . ']' => TRUE, + 'resubscribe' => TRUE, + ); + + $this->drupalPost('admin/people/simplenews/import', $edit, t('Subscribe')); + drupal_static_reset('simplenews_subscriber_load_multiple'); + drupal_static_reset('simplenews_user_is_subscribed'); + $this->assertTrue(simplenews_user_is_subscribed( $tested_subscribers[0], $first, t('Subscriber resubscribed trough mass subscription.'))); + $this->assertTrue(simplenews_user_is_subscribed( $tested_subscribers[1], $first, t('Subscriber resubscribed trough mass subscription.'))); + $this->assertTrue(simplenews_user_is_subscribed( $tested_subscribers[2], $first, t('Subscriber subscribed trough mass subscription.'))); + // Delete newsletter category. $this->drupalPost('taxonomy/term/' . $first . '/edit', array(), t('Delete')); $this->assertText(t('This taxonomy term is part of simplenews newsletter category @name. Deleting this term will delete the newsletter category and all subscriptions to category @name. This action cannot be undone.', array('@name' => $categories[$first]->name)), t('Confirmation message displayed.')); @@ -1499,7 +1539,6 @@ class SimpleNewsAdministrationTestCase extends SimplenewsTestCase { drupal_static_reset('simplenews_user_is_subscribed'); $this->assertFalse(simplenews_user_is_subscribed($subscriber->mail, reset($subscriber->tids), t('Subscriber not subscribed anymore.'))); - // @todo Test Admin subscriber edit preferred language $subscription->language }