public function DownloadCountController::downloadCountReport in Download Count 8
Builds the fields info overview page.
Return value
array Array of page elements to render.
1 string reference to 'DownloadCountController::downloadCountReport'
File
- src/
Controller/ DownloadCountController.php, line 88
Class
- DownloadCountController
- Returns responses for download_count module routes.
Namespace
Drupal\download_count\ControllerCode
public function downloadCountReport() {
$build = [];
$config = $this->configFactory
->get('download_count.settings');
$build['#title'] = $config
->get('download_count_view_page_title');
$total_downloads = 0;
$item = 1;
$limit = $config
->get('download_count_view_page_limit');
$items_per_page = $config
->get('download_count_view_page_items');
$page_header = $config
->get('download_count_view_page_header');
$page_footer = $config
->get('download_count_view_page_footer');
$output = '<div id="download-count-page">';
$header = [
[
'data' => '#',
],
[
'data' => $this
->t('Count'),
'field' => 'count',
'sort' => 'desc',
],
[
'data' => $this
->t('FID'),
'field' => 'FID',
],
[
'data' => $this
->t('Entity Type'),
'field' => 'type',
],
[
'data' => $this
->t('Entity ID'),
'field' => 'id',
],
[
'data' => $this
->t('File name'),
'fi eld' => 'filename',
],
[
'data' => $this
->t('File Size'),
'field' => 'file-size',
],
[
'data' => $this
->t('Total Size'),
'field' => 'total-size',
],
[
'data' => $this
->t('Last Downloaded'),
'field' => 'last',
],
];
$connection = Database::getConnection();
$query = $connection
->select('download_count', 'dc')
->fields('dc', [
'type',
'id',
])
->fields('f', [
'filename',
'fid',
'filesize',
])
->groupBy('dc.type')
->groupBy('dc.id')
->groupBy('dc.fid')
->groupBy('f.filename')
->groupBy('f.filesize')
->groupBy('f.fid');
$query
->addExpression('COUNT(*)', 'count');
$query
->addExpression('COUNT(*) * f.filesize', 'total-size');
$query
->addExpression('MAX(dc.timestamp)', 'last');
$query
->join('file_managed', 'f', 'dc.fid = f.fid');
if ($limit > 0) {
$query
->range(0, $limit);
}
$query
->extend('Drupal\\Core\\Database\\Query\\TableSortExtender')
->orderByHeader($header);
if ($items_per_page > 0) {
$query
->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender')
->limit($items_per_page);
}
$view_all = '';
if ($this->currentUser
->hasPermission('view all download count')) {
$view_all = Link::fromTextAndUrl($this
->t('View All'), Url::fromRoute('download_count.details', [
'download_count_entry' => 'all',
]))
->toString();
$header[] = [
'data' => $view_all,
];
}
$export_all = '';
if ($this->currentUser
->hasPermission('export all download count')) {
$export_all = Link::fromTextAndUrl($this
->t('Export All'), Url::fromRoute('download_count.export', [
'download_count_entry' => 'all',
]))
->toString();
$header[] = [
'data' => $export_all,
];
}
$reset_all = '';
if ($this->currentUser
->hasPermission('reset all download count')) {
$reset_all = Link::fromTextAndUrl($this
->t('View All'), Url::fromRoute('download_count.reset', [
'download_count_entry' => 'all',
]))
->toString();
$header[] = [
'data' => $reset_all,
];
}
$rows = [];
$result = $query
->execute();
foreach ($result as $file) {
$row = [];
$row[] = $item;
$row[] = number_format($file->count);
$row[] = $file->fid;
$row[] = Html::escape($file->type);
$row[] = $file->id;
$row[] = Html::escape($file->filename);
$row[] = format_size($file->filesize);
$row[] = format_size($file->count * $file->filesize);
$row[] = $this
->t('@time ago', [
'@time' => $this->dateFormatter
->formatInterval(REQUEST_TIME - $file->last),
]);
$query = $connection
->select('download_count', 'dc')
->fields('dc', [
'dcid',
])
->groupBy('dc.dcid')
->condition('id', $file->id)
->condition('fid', $file->fid);
$query
->addExpression('MAX(dc.timestamp)', 'last');
$dcid = $query
->execute()
->fetchField();
if ($view_all) {
$row[] = Link::fromTextAndUrl($this
->t('Details'), Url::fromRoute('download_count.details', [
'download_count_entry' => $dcid,
]))
->toString();
}
if ($export_all) {
$row[] = Link::fromTextAndUrl($this
->t('Export'), Url::fromRoute('download_count.export', [
'download_count_entry' => $dcid,
]))
->toString();
}
if ($reset_all) {
$row[] = Link::fromTextAndUrl($this
->t('Reset'), Url::fromRoute('download_count.reset', [
'download_count_entry' => $dcid,
]))
->toString();
}
$rows[] = $row;
$item++;
$total_downloads += $file->count;
}
$build['#attached'] = [
'library' => [
"download_count/global-styling-css",
],
];
if (!empty($page_header['value'])) {
$output .= '<div id="download-count-header">' . Html::escape($page_header['value'], $page_header['format']) . '</div>';
}
$output .= '<div id="download-count-total-top">' . $this
->t('Total Downloads:') . ' ' . number_format($total_downloads) . '</div>';
$table = [
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#attributes' => [
'id' => 'download-count-table',
],
'#empty' => $this
->t('No files have been downloaded.'),
];
$output .= render($table);
$output .= '<div id="download-count-total-bottom">' . $this
->t('Total Downloads:') . ' ' . number_format($total_downloads) . '</div>';
if ($items_per_page > 0) {
$pager = [
'#theme' => 'pager',
'attributes' => [
'tags' => [],
],
];
$output .= render($pager);
}
if (!empty($page_footer['value'])) {
$output .= '<div id="download-count-footer">' . Html::escape($page_footer['value'], $page_footer['format']) . '</div>';
}
$output .= '</div>';
$build['#markup'] = $output;
return $build;
}