public function TrackDaFilesController::table_build in Track da files 8
1 call to TrackDaFilesController::table_build()
- TrackDaFilesController::table in src/
Controller/ TrackDaFilesController.php - Builds a table which shows number of displays by file.
File
- src/
Controller/ TrackDaFilesController.php, line 85
Class
- TrackDaFilesController
- Class TrackDaFilesController.
Namespace
Drupal\track_da_files\ControllerCode
public function table_build() {
global $base_url;
// We check avalaible optional datas.
$displays_datas = \Drupal::config('track_da_files.settings')
->get('displays_datas');
$files_datas = \Drupal::config('track_da_files.settings')
->get('files_datas');
// We prepare main report table header.
$header = array(
$this
->t('Filename'),
t('Counter'),
);
// We put displays datas in main report table header.
foreach ($displays_datas as $data => $value) {
if (!empty($value)) {
if ($value == 'total_ips') {
$header[] = array(
'data' => $this
->t('Total ips'),
'field' => $value,
'sort' => 'DESC',
);
}
elseif ($value == 'average_by_ip') {
$header[] = array(
'data' => $this
->t('Average by ip'),
'field' => $value,
'sort' => 'DESC',
);
}
elseif ($value == 'last_display') {
$header[] = array(
'data' => $this
->t('Last display'),
'field' => $value,
'sort' => 'DESC',
);
}
}
}
foreach ($files_datas as $data => $value) {
if (!empty($value)) {
if ($value == 'timestamp') {
$header[] = array(
'data' => $this
->t('Created'),
'field' => $value,
'sort' => 'DESC',
);
}
elseif ($value == 'filesize') {
$header[] = array(
'data' => $this
->t('File size'),
'field' => $value,
'sort' => 'DESC',
);
}
elseif ($value == 'filemime') {
$header[] = array(
'data' => $this
->t('File mime'),
'field' => $value,
'sort' => 'DESC',
);
}
}
}
// We prepare fields.
$fields = array(
'pid',
);
foreach ($displays_datas as $key => $value) {
if ($value == 'average_by_ip') {
$average_by_ip = $value;
}
elseif ($value == 'total_ips') {
$total_ips = $value;
}
elseif ($value == 'last_display') {
$last_display = $value;
}
else {
$fields[] = $value;
}
}
$fields2 = array(
'filename',
);
foreach ($files_datas as $key => $value) {
if ($value) {
$fields2[] = $value;
}
}
$header[] = array(
'data' => t('File datas'),
);
// We select the datas in database.
$query = \Drupal::database()
->select('track_da_files_paths', 'p');
$query
->addExpression('COUNT(recid)', 'counter');
$query
->addExpression('COUNT(DISTINCT(ip))', 'total_ips');
$query
->addExpression('MAX(time)', 'last_display');
$query
->addExpression('ROUND(COUNT(recid) / COUNT(DISTINCT(ip)))', 'average_by_ip');
$query
->join('track_da_files', 't', 't.pid = p.pid');
$query
->join('file_managed', 'f', 'p.fid = f.fid');
$query
->extend('\\Drupal\\Core\\Database\\Query\\PagerSelectExtender');
$query
->extend('\\Drupal\\Core\\Database\\Query\\TableSortExtender');
$query
->fields('f', $fields2);
$query
->fields('t', $fields);
$query
->fields('p', array(
'fid',
'path',
));
$query
->groupBy('p.fid');
$query
->groupBy('t.pid');
$query
->groupBy('f.filename');
$query
->groupBy('p.path');
$query
->groupBy('f.created');
$query
->groupBy('f.filesize');
$query
->groupBy('f.filemime');
$query
->range(0, 20);
$results = $query
->execute();
$rows = array();
// We loop into the results to build table rows for the main report table.
foreach ($results as $key => $record) {
foreach ($record as $key2 => $row) {
if ($key2 == 'filename') {
//$rows[$key][$key2] = l($row, file_create_url($record->path));
$record_uri = file_create_url($record->path);
$rows[$key][$key2] = \Drupal::l($row, Url::fromUri($record_uri));
}
elseif ($key2 == 'pid' || $key2 == 'path') {
unset($rows[$key][$key2]);
}
elseif ($key2 == 'last_display' && !empty($last_display) || $key2 == 'timestamp') {
$rows[$key][$key2] = format_date($row, 'short');
}
elseif ($key2 == 'filesize') {
$rows[$key][$key2] = format_size($row);
}
elseif ($key2 == 'filemime' || $key2 == 'counter' || !empty($total_ips) && $key2 == 'total_ips') {
$rows[$key][$key2] = $row;
}
elseif ($key2 == 'fid') {
//$rows[$key][$key2] = l(t('View more'), 'admin/reports/track_da_files/file_report/' . $record->fid . '/' . $record->pid);
// $file_uri = file_build_uri('admin/reports/track_da_files/file_report/' . $record->fid . '/' . $record->pid);
$file_url = $base_url . '/admin/reports/track_da_files/file_report/' . $record->fid . '/' . $record->pid;
$rows[$key][$key2] = \Drupal::l(t('View more'), Url::fromUri($file_url));
}
elseif (!empty($average_by_ip) && $key2 == 'average_by_ip') {
$rows[$key][$key2] = $record->average_by_ip;
}
}
}
// We reorder rows to match table headers order.
$rows_indexes_ordered = array(
'filename',
'counter',
'total_ips',
'average_by_ip',
'last_display',
'timestamp',
'filesize',
'filemime',
'fid',
);
$rows_ordered = array();
foreach ($rows as $row => $value) {
foreach ($value as $key => $value2) {
$keys[] = $key;
}
}
$rows_final = array();
$variables = array();
foreach ($rows as $row) {
foreach ($rows_indexes_ordered as $index) {
if (in_array($index, $keys)) {
$rows_ordered[$index] = $row[$index];
}
}
$rows_final[] = $rows_ordered;
}
$build['track_da_files_table'] = array(
'#type' => 'table',
'#header' => $header,
'#rows' => $rows_final,
'#attributes' => array(
'id' => 'admin-track-da-files',
'class' => array(
'admin-track-da-files',
),
),
'#attached' => array(
'library' => array(
'track_da_files/track_da_files',
),
),
);
$build['track_da_files_pager'] = array(
'#type' => 'pager',
);
return $build['track_da_files_table'];
}