You are here

function simplenews_admin_export_after_build in Simplenews 6

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

File

./simplenews.admin.inc, line 510
Newsletter admin, subscription admin, simplenews settings

Code

function simplenews_admin_export_after_build($form, $form_element) {
  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;
    if ($tree = taxonomy_get_tree(variable_get('simplenews_vid', ''))) {
      foreach ($tree 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 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'] = array(
      '#type' => 'textarea',
      '#title' => t('Export results'),
      '#cols' => 60,
      '#rows' => 5,
      '#value' => $exported_mails,
    );
  }
  return $form;
}