You are here

function simplenews_admin_export_after_build in Simplenews 6.2

Same name and namespace in other branches
  1. 5 simplenews.module \simplenews_admin_export_after_build()
  2. 6 simplenews.admin.inc \simplenews_admin_export_after_build()
1 string reference to 'simplenews_admin_export_after_build'
simplenews_subscription_list_export in includes/simplenews.admin.inc
Menu callback: Export email address of subscriptions.

File

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

Code

function simplenews_admin_export_after_build($form, $form_element) {

  //TODO: Add (un)subscribed as filter to form.
  if (isset($form_element['values']['op']) && $form_element['values']['op'] == t('Export')) {
    $states = array_filter($form_element['values']['states']);
    $newsletters = array_filter($form_element['values']['newsletters']);

    // Build where clause for active/inactive state and newsletter selection.
    if (isset($states['active'])) {
      $where[] = 's.activated = 1';
    }
    if (isset($states['inactive'])) {
      $where[] = 's.activated = 0';
    }
    $where = isset($where) ? implode(' OR ', $where) : NULL;
    foreach (simplenews_get_newsletters(variable_get('simplenews_vid', ''), TRUE) as $newsletter) {
      if (isset($newsletters[$newsletter->tid])) {
        $where_tid[] = 't.tid = ' . $newsletter->tid;
      }
    }
    $where_tid = isset($where_tid) ? implode(' OR ', $where_tid) : NULL;

    // Get subscription data
    if (isset($where) && isset($where_tid)) {
      $query = '
        SELECT DISTINCT s.mail
        FROM {simplenews_subscriptions} s
        INNER JOIN {simplenews_snid_tid} t
        ON s.snid = t.snid
          AND t.status = 1
        WHERE (' . $where . ')
          AND (' . $where_tid . ')';
      $result = db_query($query);
      while ($mail = db_fetch_object($result)) {
        $mails[] = $mail->mail;
      }
    }

    // Build form field containing exported emails.
    // The field will be included in the form where at the ['emails'] place holder.
    if (isset($mails)) {
      $exported_mails = implode(", ", $mails);
    }
    else {
      $exported_mails = t('No addresses were found.');
    }
    $form['emails']['#value'] = $exported_mails;
    $form['emails']['#access'] = TRUE;
  }
  return $form;
}