You are here

function newsletter_subscriber_list in Newsletter 7

Menu callback; present an administrative subscriber listing.

1 string reference to 'newsletter_subscriber_list'
newsletter_menu in ./newsletter.module
Implements hook_menu().

File

includes/newsletter.admin.inc, line 815
Admin page callbacks for the newsletter module.

Code

function newsletter_subscriber_list($form, &$form_state) {
  if (isset($form_state['storage']['confirm'])) {
    $form['operation'] = array(
      '#type' => 'hidden',
      '#value' => 'delete',
    );
    $form['subscriber'] = array(
      '#type' => 'hidden',
      '#value' => $form_state['values']['subscriber'],
    );
    $output = t('Are you sure you want to delete the following newsletter subscribers?');
    $output .= '<ul>';
    $subscribers = newsletter_subscriber_load($form_state['values']['subscriber']);
    foreach ($subscribers as $subscriber) {
      $output .= !empty($subscriber) ? '<li>' . check_plain($subscriber->email) . '</li>' : '';
    }
    $output .= '</ul>';
    $output .= t('This action cannot be undone.');
    return confirm_form($form, t('Delete the following?'), 'admin/config/media/newsletter/subscribers', filter_xss($output));
  }
  $form['options'] = array(
    '#type' => 'fieldset',
    '#title' => t('Options'),
    '#attributes' => array(
      'class' => array(
        'container-inline',
      ),
    ),
  );
  $options['delete'] = t('Delete the selected subscribers');
  $form['options']['operation'] = array(
    '#type' => 'select',
    '#title' => t('Operation'),
    '#title_display' => 'invisible',
    '#options' => $options,
    '#default_value' => 'delete',
  );
  $form['options']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Update'),
  );
  $header = array(
    'id' => array(
      'data' => t('ID'),
      'field' => 'nsid',
    ),
    'email' => array(
      'data' => t('Email'),
      'field' => 'email',
    ),
    'info' => array(
      'data' => t('Personal Info'),
    ),
    'list_title' => array(
      'data' => t('Subscribed to'),
    ),
    'created' => array(
      'data' => t('Created'),
      'field' => 'created',
      'sort' => 'desc',
    ),
    'confirmed' => array(
      'data' => t('Confirmed'),
    ),
    'operations' => array(
      'data' => t('Operations'),
    ),
  );
  $query = db_select('newsletter_subscriber', 'subscribers')
    ->extend('PagerDefault')
    ->extend('TableSort');
  $result = $query
    ->fields('subscribers')
    ->limit(50)
    ->orderByHeader($header)
    ->execute();
  $options = array();
  $destination = drupal_get_destination();
  foreach ($result as $row) {
    $info = array(
      'data' => array(),
    );
    $info['data'][] = !empty($row->firstname) ? array(
      '#markup' => t('Firstname:') . check_plain($row->firstname) . '<br />',
    ) : NULL;
    $info['data'][] = !empty($row->lastname) ? array(
      '#markup' => t('Lastname:') . check_plain($row->lastname) . '<br />',
    ) : NULL;
    $info['data'][] = !empty($row->age_group) ? array(
      '#markup' => t('Age group:') . check_plain($row->age_group) . '<br />',
    ) : NULL;
    $info['data'][] = !empty($row->job) ? array(
      '#markup' => t('Job:') . check_plain($row->job) . '<br />',
    ) : NULL;
    $info['data'][] = !empty($row->gender) ? array(
      '#markup' => t('Gender:') . check_plain($row->gender) . '<br />',
    ) : NULL;
    $info['data'][] = !empty($row->receive_format) ? array(
      '#markup' => t('Preferred format:') . check_plain($row->receive_format) . '<br />',
    ) : NULL;
    $info['data'][] = !empty($row->language) ? array(
      '#markup' => t('Language:') . check_plain($row->language) . '<br />',
    ) : NULL;
    $lists = db_query('SELECT title
      FROM {newsletter_list} list
      JOIN {field_data_field_newsletter_list} sub_index
      ON sub_index.field_newsletter_list_target_id = list.nlid
      WHERE sub_index.entity_id = :id', array(
      ':id' => $row->nsid,
    ))
      ->fetchCol();
    $lists = implode(', ', $lists);
    $options[$row->nsid] = array(
      'id' => (int) $row->nsid,
      'email' => check_plain($row->email),
      'info' => $info,
      'list_title' => $lists,
      'created' => format_date($row->created, 'short'),
      'confirmed' => $row->confirmed ? t('Yes, on') . '<br/>' . format_date($row->confirmation_timestamp, 'short') : t('No'),
      'operations' => array(
        'data' => array(
          array(
            '#type' => 'link',
            '#title' => t('edit'),
            '#href' => 'admin/config/media/newsletter/subscribers/edit/' . $row->nsid,
            '#options' => array(
              'query' => $destination,
            ),
          ),
          array(
            '#markup' => ' | ',
          ),
          array(
            '#type' => 'link',
            '#title' => t('delete'),
            '#href' => 'admin/config/media/newsletter/subscribers/delete/' . $row->nsid,
            '#options' => array(
              'query' => $destination,
            ),
          ),
        ),
      ),
    );
  }
  $form['subscriber'] = array(
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $options,
    '#empty' => t('No subscribers yet.'),
  );
  $form['pager'] = array(
    '#theme' => 'pager',
  );
  return $form;
}