You are here

function simplenews_admin_news in Simplenews 6.2

Same name and namespace in other branches
  1. 5 simplenews.module \simplenews_admin_news()
  2. 6 simplenews.admin.inc \simplenews_admin_news()

Menu callback: Admin form for sent and draft newsletters.

See also

simplenews_admin_news_submit()

1 string reference to 'simplenews_admin_news'
simplenews_menu in ./simplenews.module
Implementation of hook_menu().

File

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

Code

function simplenews_admin_news(&$form_state, $action = 'sent') {
  $vid = variable_get('simplenews_vid', '');
  $form = array();
  $form['simplenews_issue_filter'] = array(
    '#type' => 'fieldset',
    '#title' => t('Show issues from'),
    '#collapsible' => FALSE,
    '#prefix' => '<div class="container-inline">',
    '#suffix' => '</div>',
  );
  if (variable_get('simplenews_last_cron', '')) {
    $form['last_sent'] = array(
      '#value' => '<p>' . format_plural(variable_get('simplenews_last_sent', 0), 'Last batch: 1 mail sent at !time.', 'Last batch: !count mails sent at !time.', array(
        '!time' => format_date(variable_get('simplenews_last_cron', ''), 'small'),
        '!count' => variable_get('simplenews_last_sent', 0),
      )) . "</p>\n",
    );
  }
  $header = array(
    t('Title'),
    t('Newsletter'),
    t('Date created'),
    t('Published'),
    t('Sent'),
    t('Operations'),
  );
  list($names, $queries) = array_values(simplenews_get_filter('simplenews_newsletters_filter'));
  $filter = isset($_SESSION['simplenews_newsletters_filter']) ? $_SESSION['simplenews_newsletters_filter'] : '';
  $form['simplenews_issue_filter']['filter'] = array(
    '#type' => 'select',
    '#options' => $names,
    '#default_value' => $filter,
  );
  $where_condition = isset($queries[$filter]) && !empty($queries[$filter]) ? 'WHERE ' . $queries[$filter] : '';
  $query = "\n    SELECT DISTINCT n.*, s.s_status, s.tid, s.sent_subscriber_count\n    FROM {node} n\n    INNER JOIN {simplenews_newsletters} s\n      ON n.nid = s.nid\n    " . $where_condition . "\n    ORDER BY n.created DESC";
  $count_query = "\n    SELECT COUNT(n.nid)\n    FROM {node} n\n    INNER JOIN {simplenews_newsletters} s\n      ON n.nid = s.nid\n    " . $where_condition;
  $result = pager_query(db_rewrite_sql($query, 'n', 'nid'), 10, 0, db_rewrite_sql($count_query, 'n', 'nid'), 0);
  $rows = array();
  module_load_include('inc', 'simplenews', 'includes/simplenews.mail');
  while ($node = db_fetch_object($result)) {
    $row = array();
    $operation = array();
    $terms = array_keys(taxonomy_node_get_terms_by_vocabulary($node, $vid, 'name'));
    $status = $node->s_status;
    $sent_subscriber_count = $node->sent_subscriber_count;
    $pending_count = simplenews_count_spool($node->nid, $node->vid);
    $already_sent = $sent_subscriber_count - $pending_count;
    $source = 'sent';
    $send_status = theme('simplenews_status', $status, 'sent', $already_sent, $sent_subscriber_count);
    $operation[] = l(t('edit'), 'node/' . $node->nid . '/edit', array(
      'query' => drupal_get_destination(),
    ));
    $row[] = l($node->title, 'node/' . $node->nid);
    $row[] = isset($terms[0]) ? $terms[0] : t('n/a');
    $row[] = format_date($node->created, 'small');
    $row[] = theme('simplenews_status', $node->status, 'published');
    $row[] = $send_status;
    $row[] = implode(' ', $operation);
    $rows[] = $row;
  }
  if ($pager = theme('pager', NULL, 10, 0)) {
    $rows[] = array(
      array(
        'data' => $pager,
        'colspan' => '8',
      ),
    );
  }
  if (!$rows) {
    $rows[] = array(
      array(
        'data' => t('No newsletters available.'),
        'colspan' => '8',
      ),
    );
  }
  $form['simplenews_issue_filter']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Filter'),
  );
  $table = theme('table', $header, $rows);
  $form['table'] = array(
    '#value' => $table,
  );
  return $form;
}