You are here

function mail_logger_overview in Mail Logger 7

Same name and namespace in other branches
  1. 5 mail_logger.module \mail_logger_overview()
  2. 6 mail_logger.module \mail_logger_overview()

Menu callback; displays a listing of logged mails.

1 string reference to 'mail_logger_overview'
mail_logger_menu in ./mail_logger.module
Implements hook_menu().

File

./mail_logger.pages.inc, line 10
User-facing UI components for the Mail Logger module.

Code

function mail_logger_overview() {
  drupal_add_css(drupal_get_path('module', 'mail_logger') . '/mail_logger.css');
  $form = drupal_get_form('mail_logger_form_overview');
  $output = drupal_render($form);
  $date_format = variable_get('mail_logger_log_date_format', 'short');

  // Fix the annoying lower casing by the menu module.
  drupal_set_title(t('Outgoing Mail Log'));
  $table = array(
    'header' => array(
      array(
        'data' => t('Date Sent'),
        'field' => 'date_sent',
        'sort' => 'desc',
      ),
      array(
        'data' => t('Mailkey'),
        'field' => 'mailkey',
      ),
      array(
        'data' => t('Language'),
        'field' => 'language',
      ),
      array(
        'data' => t('To'),
        'field' => 'mailto',
      ),
      array(
        'data' => t('From'),
        'field' => 'mailfrom',
      ),
      array(
        'data' => t('Subject'),
        'field' => 'subject',
      ),
      array(
        'data' => t('IP Addr'),
        'field' => 'ipaddr',
      ),
    ),
    'rows' => array(),
    'empty' => t('No outgoing email available.'),
    'attributes' => array(
      'id' => 'mail-logger-log',
    ),
  );
  $query = db_select('mail_logger', 'ml')
    ->extend('PagerDefault')
    ->limit(variable_get('mail_logger_log_lines_per_page', 50))
    ->extend('TableSort')
    ->orderByHeader($table['header'])
    ->fields('ml', array(
    'mlid',
    'mailkey',
    'language',
    'mailto',
    'mailfrom',
    'subject',
    'date_sent',
    'ipaddr',
  ));
  $type = $_SESSION['mail_logger_overview_filter'];
  if ($type != 'all') {
    $query
      ->condition('mailkey', $type);
  }
  if (!empty($_SESSION['mail_logger_overview_from_date'])) {
    $from_time = strtotime($_SESSION['mail_logger_overview_from_date']['year'] . '-' . $_SESSION['mail_logger_overview_from_date']['month'] . '-' . $_SESSION['mail_logger_overview_from_date']['day']);
  }
  else {
    $from_time = strtotime('-1 month');
  }
  if (!empty($_SESSION['mail_logger_overview_to_date'])) {
    $to_time = strtotime($_SESSION['mail_logger_overview_to_date']['year'] . '-' . $_SESSION['mail_logger_overview_to_date']['month'] . '-' . $_SESSION['mail_logger_overview_to_date']['day'] . 't23:59:59');
  }
  else {
    $to_time = strtotime('now') + 86400;
  }
  $query
    ->condition('date_sent', $from_time, '>=');
  $query
    ->condition('date_sent', $to_time, '<=');
  if (!empty($_SESSION['mail_logger_overview_search_field']) && !empty($_SESSION['mail_logger_overview_search_value'])) {
    $search_field = $_SESSION['mail_logger_overview_search_field'];
    $search_value = $_SESSION['mail_logger_overview_search_value'];
    $query
      ->condition($search_field, '%' . $search_value . '%', 'LIKE');
  }
  $result = $query
    ->execute();
  foreach ($result as $mail_logger) {

    // Alter encrypted mail log records back to their original state.
    $context = array(
      'operation' => 'read',
    );
    drupal_alter('mail_logger_record', $mail_logger, $context);
    $subject = empty($mail_logger->subject) ? t('Missing subject') : $mail_logger->subject;
    $mailto = $mail_logger->mailto;
    if (strpos($mailto, ';') !== FALSE) {
      $list = explode(';', $mailto);
    }
    else {
      $list = explode(',', $mailto);
    }
    $table['rows'][] = array(
      'data' => array(
        // Cells
        array(
          'data' => format_date($mail_logger->date_sent, $date_format),
          'class' => array(
            'date-sent',
          ),
        ),
        array(
          'data' => $mail_logger->mailkey,
          'class' => array(
            'mail-key',
          ),
        ),
        array(
          'data' => $mail_logger->language,
          'class' => array(
            'mail-language',
          ),
        ),
        array(
          'data' => implode('<br />', $list),
          'class' => array(
            'mail-to',
          ),
        ),
        array(
          'data' => $mail_logger->mailfrom,
          'class' => array(
            'mail-from',
          ),
        ),
        array(
          'data' => l(truncate_utf8($subject, 56, TRUE, TRUE), 'admin/reports/mail-logger/mail/' . $mail_logger->mlid),
          'class' => array(
            'mail-subject',
          ),
        ),
        array(
          'data' => $mail_logger->ipaddr,
          'class' => array(
            'mail-ipaddr',
          ),
        ),
      ),
    );
  }
  $output .= theme('table', $table);
  $output .= theme('pager', array());
  return $output;
}