You are here

function simplenews_subscription_list_add_submit in Simplenews 7

Same name and namespace in other branches
  1. 5 simplenews.module \simplenews_subscription_list_add_submit()
  2. 6.2 includes/simplenews.admin.inc \simplenews_subscription_list_add_submit()
  3. 6 simplenews.admin.inc \simplenews_subscription_list_add_submit()
  4. 7.2 includes/simplenews.admin.inc \simplenews_subscription_list_add_submit()

@todo

File

includes/simplenews.admin.inc, line 774
Newsletter admin, subscription admin, simplenews settings

Code

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'];
  $emails = preg_split("/[\\s,]+/", $form_state['values']['emails']);
  foreach ($emails as $email) {
    $email = trim($email);
    if ($email == '') {
      continue;
    }
    if (valid_email_address($email)) {
      $subscriber = simplenews_subscriber_load_by_mail($email);
      foreach (simplenews_categories_load_multiple($checked_categories) as $category) {

        // 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 {
      $invalid[] = $email;
    }
  }
  if ($added) {
    $added = implode(", ", $added);
    drupal_set_message(t('The following addresses were added or updated: %added.', array(
      '%added' => $added,
    )));
    $list_names = array();
    foreach (simplenews_categories_load_multiple($checked_categories) as $category) {
      $list_names[] = $category->name;
    }
    drupal_set_message(t('The addresses were subscribed to the following newsletters: %newsletters.', array(
      '%newsletters' => implode(', ', $list_names),
    )));
  }
  else {
    drupal_set_message(t('No addresses were added.'));
  }
  if ($invalid) {
    $invalid = implode(", ", $invalid);
    drupal_set_message(t('The following addresses were invalid: %invalid.', array(
      '%invalid' => $invalid,
    )), 'error');
  }
  foreach ($unsubscribed as $name => $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');
  }
  if (!empty($unsubscribed)) {
    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';
}