You are here

function SubscriberExportForm::getEmails in Simplenews 8

Same name and namespace in other branches
  1. 8.2 src/Form/SubscriberExportForm.php \Drupal\simplenews\Form\SubscriberExportForm::getEmails()
  2. 3.x src/Form/SubscriberExportForm.php \Drupal\simplenews\Form\SubscriberExportForm::getEmails()

Implement getEmails($states, $subscribed, $newsletters)

1 call to SubscriberExportForm::getEmails()
SubscriberExportForm::buildForm in src/Form/SubscriberExportForm.php
Form constructor.

File

src/Form/SubscriberExportForm.php, line 24

Class

SubscriberExportForm
Do a mass subscription for a list of email addresses.

Namespace

Drupal\simplenews\Form

Code

function getEmails($states, $subscribed, $newsletters) {

  // Build conditions for active state, subscribed state and newsletter selection.
  if (isset($states['active'])) {
    $condition_active[] = SubscriberInterface::ACTIVE;
  }
  if (isset($states['inactive'])) {
    $condition_active[] = SubscriberInterface::INACTIVE;
  }
  if (isset($subscribed['subscribed'])) {
    $condition_subscribed[] = SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED;
  }
  if (isset($subscribed['unsubscribed'])) {
    $condition_subscribed[] = SIMPLENEWS_SUBSCRIPTION_STATUS_UNSUBSCRIBED;
  }
  if (isset($subscribed['unconfirmed'])) {
    $condition_subscribed[] = SIMPLENEWS_SUBSCRIPTION_STATUS_UNCONFIRMED;
  }

  // Get emails from the database.
  $query = \Drupal::entityQuery('simplenews_subscriber')
    ->condition('status', $condition_active, 'IN')
    ->condition('subscriptions.status', $condition_subscribed, 'IN')
    ->condition('subscriptions.target_id', (array) $newsletters, 'IN');
  $subscriber_ids = $query
    ->execute();
  $mails = array();
  foreach ($subscriber_ids as $id) {
    $subscriber = simplenews_subscriber_load($id);
    $mails[] = $subscriber
      ->getMail();
  }

  // Return comma separated array of emails or empty text.
  if ($mails) {
    return implode(", ", $mails);
  }
  return t('No addresses were found.');
}