You are here

public function DownloadCountController::downloadCountDetails in Download Count 8

Download_count details page callback.

1 string reference to 'DownloadCountController::downloadCountDetails'
download_count.routing.yml in ./download_count.routing.yml
download_count.routing.yml

File

src/Controller/DownloadCountController.php, line 264

Class

DownloadCountController
Returns responses for download_count module routes.

Namespace

Drupal\download_count\Controller

Code

public function downloadCountDetails($download_count_entry = NULL) {
  $config = $this->configFactory
    ->get('download_count.settings');
  $build = [];
  $build['#attached'] = [
    'library' => [
      "download_count/global-styling-css",
    ],
  ];
  $last_cron = $config
    ->get('download_count_last_cron');
  if ($download_count_entry != NULL) {
    $connection = Database::getConnection();
    $query = $connection
      ->select('download_count', 'dc');
    $query
      ->innerjoin('file_managed', 'f', 'dc.fid = f.fid');
    $query
      ->fields('dc', [
      'dcid',
      'fid',
      'uid',
      'type',
      'id',
      'ip_address',
      'referrer',
      'timestamp',
    ]);
    $query
      ->fields('f', [
      'filename',
      'uri',
      'filemime',
      'filesize',
    ]);
    $query
      ->condition('dc.dcid', $download_count_entry);
    $dc_entry = $query
      ->execute()
      ->fetchObject();
  }
  else {
    $dc_entry = 'all';
  }
  $output = Link::fromTextAndUrl($this
    ->t('« Back to summary'), Url::fromRoute('download_count.reports'))
    ->toString();
  $connection = Database::getConnection();
  $query = $connection
    ->select('download_count_cache', 'dc');
  $query
    ->addExpression('COUNT(dc.count)', 'count');
  if (!is_object($dc_entry)) {
    $build['#title'] = $this
      ->t('Download Count Details - All Files');
  }
  else {
    $build['#title'] = $this
      ->t('Download Count Details - @filename from @type @id', [
      '@filename' => $dc_entry->filename,
      '@type' => $dc_entry->type,
      '@id' => $dc_entry->id,
    ]);
    $query
      ->condition('dc.type', $dc_entry->type);
    $query
      ->condition('dc.id', $dc_entry->id);
    $query
      ->condition('dc.fid', $dc_entry->fid);
  }
  $result = $query
    ->execute()
    ->fetchField();
  $total = number_format($result);
  if ($last_cron > 0) {
    $output .= '<p>Current as of ' . $this->dateFormatter
      ->format($last_cron, 'long') . ' with ' . number_format($this->queue
      ->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>' . $this
    ->t('Total Downloads:') . '</strong> ' . $total . '</div>';

  // Determine first day of week (from date module if set, 'Sunday' if not).
  if ($config
    ->get('date_first_day') == 0) {
    $week_format = '%U';
  }
  else {
    $week_format = '%u';
  }
  $sparkline_type = $config
    ->get('download_count_sparklines');

  // Base query for all files for all intervals.
  $query = $connection
    ->select('download_count_cache', 'dc')
    ->groupBy('time_interval');
  $query
    ->addExpression('SUM(dc.count)', 'count');
  $query
    ->orderBy('dc.date', 'DESC');

  // 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, $config
    ->get('download_count_details_daily_limit'));
  $result = $query
    ->execute();
  $daily = $this
    ->downloadCountDetailsTable($result, 'Daily', 'Day');
  $output .= render($daily['output']);
  if ($sparkline_type != 'none') {
    $values['daily'] = implode(',', array_reverse($daily['values']));
    $output .= '<div class="download-count-sparkline-daily">' . $this
      ->t('Rendering Sparkline...') . '</div>';
  }
  $expressions =& $query
    ->getExpressions();

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

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

  // Yearly data.
  $expressions['time_interval']['expression'] = "FROM_UNIXTIME(date, '%Y')";
  $query
    ->range(0, $config
    ->get('download_count_details_yearly_limit'));
  $result = $query
    ->execute();
  $yearly = $this
    ->downloadCountDetailsTable($result, 'Yearly', 'Year');
  $output .= render($yearly['output']);
  if ($sparkline_type != 'none') {
    $values['yearly'] = implode(',', array_reverse($yearly['values']));
    $output .= '<div class="download-count-sparkline-yearly">' . $this
      ->t('Rendering Sparkline...') . '</div>';
  }
  $output .= '<div id="download-count-total-bottom"><strong>' . $this
    ->t('Total Downloads:') . '</strong> ' . $total . '</div>';
  if ($sparkline_type != 'none') {
    $build['#attached']['library'][] = "download_count/sparkline";
    $build['#attached']['drupalSettings']['download_count'] = [
      'values' => $values,
      'type' => $sparkline_type,
      'min' => $config
        ->get('download_count_sparkline_min'),
      'height' => $config
        ->get('download_count_sparkline_height'),
      'width' => $config
        ->get('download_count_sparkline_width'),
    ];
  }
  $build['#markup'] = $output;
  return $build;
}