You are here

function download_count_view_details in Download Count 7.3

Download_count details page callback.

1 string reference to 'download_count_view_details'
download_count_menu in ./download_count.module
Implements hook_menu().

File

includes/download_count.pages.inc, line 239
Administrative page callbacks for the download_count module.

Code

function download_count_view_details($dc_entry = NULL) {
  drupal_add_css(drupal_get_path('module', 'download_count') . '/download_count.css');
  $last_cron = variable_get('download_count_last_cron', 0);
  $output = l(t('« Back to summary'), 'admin/reports/download-count', array(
    'html' => TRUE,
  ));
  if ($dc_entry == 'all') {
    drupal_set_title(t('Download Count Details - All Files'));
    $total = number_format(db_query('SELECT SUM(count) FROM {download_count_cache}')
      ->fetchField());
  }
  else {
    drupal_set_title(t('Download Count Details - @filename from @type @id', array(
      '@filename' => $dc_entry->filename,
      '@type' => $dc_entry->type,
      '@id' => $dc_entry->id,
    )));
    $total = number_format(db_query('SELECT SUM(count) FROM {download_count_cache} WHERE type = :type AND id = :id AND fid = :fid', array(
      ':type' => $dc_entry->type,
      ':id' => $dc_entry->id,
      ':fid' => $dc_entry->fid,
    ))
      ->fetchField());
  }
  if ($last_cron > 0) {
    $output .= '<p>Current as of ' . format_date($last_cron, 'long') . ' with ' . number_format(DrupalQueue::get('download_count')
      ->numberOfItems()) . ' items still queued to cache.</p>';
  }
  else {
    $output .= '<p>No download count data has been cached. You may want to check Drupal cron.</p>';
  }
  $output .= '<div id="download-count-total-top"><strong>' . t('Total Downloads:') . '</strong> ' . $total . '</div>';

  // determine first day of week (from date module if set, 'Sunday' if not).
  if (variable_get('date_first_day', '0') == 0) {
    $week_format = '%U';
  }
  else {
    $week_format = '%u';
  }
  $sparkline_type = variable_get('download_count_sparklines', 'line');

  //base query for all files for all intervals
  $query = db_select('download_count_cache')
    ->fields(NULL, array(
    'date',
  ))
    ->groupBy('time_interval');
  $query
    ->addExpression('SUM(count)', 'count');
  $query
    ->orderBy('date', 'DESC');
  $expressions =& $query
    ->getExpressions();

  // Details for a specific download and entity.
  if ($dc_entry != 'all') {
    $query
      ->condition('type', $dc_entry->type, '=');
    $query
      ->condition('id', $dc_entry->id, '=');
    $query
      ->condition('fid', $dc_entry->fid, '=');
  }

  // daily data
  $query
    ->addExpression("FROM_UNIXTIME(date, '%Y-%m-%d')", 'time_interval');
  $query
    ->range(0, variable_get('download_count_details_daily_limit', 30));
  $result = $query
    ->execute();
  $daily = _download_count_details_table($result, 'Daily', 'Day');
  $output .= $daily['output'];
  if ($sparkline_type != 'none') {
    $values['daily'] = implode(',', array_reverse($daily['values']));
    $output .= '<div class="download-count-sparkline-daily">' . t('Rendering Sparkline...') . '</div>';
  }

  // weekly data
  $expressions['time_interval']['expression'] = "FROM_UNIXTIME(date, '{$week_format}')";
  $query
    ->range(0, variable_get('download_count_details_weekly_limit', 25));
  $result = $query
    ->execute();
  $weekly = _download_count_details_table($result, 'Weekly', 'Week');
  $output .= $weekly['output'];
  if ($sparkline_type != 'none') {
    $values['weekly'] = implode(',', array_reverse($weekly['values']));
    $output .= '<div class="download-count-sparkline-weekly">' . t('Rendering Sparkline...') . '</div>';
  }

  // monthly data
  $expressions['time_interval']['expression'] = "FROM_UNIXTIME(date, '%Y-%m')";
  $query
    ->range(0, variable_get('download_count_details_monthly_limit', 12));
  $result = $query
    ->execute();
  $monthly = _download_count_details_table($result, 'Monthly', 'Month');
  $output .= $monthly['output'];
  if ($sparkline_type != 'none') {
    $values['monthly'] = implode(',', array_reverse($monthly['values']));
    $output .= '<div class="download-count-sparkline-monthly">' . t('Rendering Sparkline...') . '</div>';
  }

  // yearly data
  $expressions['time_interval']['expression'] = "FROM_UNIXTIME(date, '%Y')";
  $query
    ->range(0, variable_get('download_count_details_yearly_limit', 5));
  $result = $query
    ->execute();
  $yearly = _download_count_details_table($result, 'Yearly', 'Year');
  $output .= $yearly['output'];
  if ($sparkline_type != 'none') {
    $values['yearly'] = implode(',', array_reverse($yearly['values']));
    $output .= '<div class="download-count-sparkline-yearly">' . t('Rendering Sparkline...') . '</div>';
  }
  $output .= '<div id="download-count-total-bottom"><strong>' . t('Total Downloads:') . '</strong> ' . $total . '</div>';
  if ($sparkline_type != 'none') {
    drupal_add_library('download_count', 'jquery.sparkline');
    drupal_add_js(drupal_get_path('module', 'download_count') . '/js/download_count_sparklines.js');
    $settings['download_count'] = array(
      'values' => $values,
      'type' => $sparkline_type,
      'min' => variable_get('download_count_sparkline_min', '0'),
      'height' => variable_get('download_count_sparkline_height', '150px'),
      'width' => variable_get('download_count_sparkline_width', '50%'),
    );
    drupal_add_js($settings, array(
      'type' => 'setting',
    ));
  }
  return $output;
}