You are here

public function TrackDaFilesController::table_build_user_report in Track da files 8

Build variables used to create table which shows datas for a specific user.

1 call to TrackDaFilesController::table_build_user_report()
TrackDaFilesController::table_user_report in src/Controller/TrackDaFilesController.php
* Builds a table which shows datas for a specific user.

File

src/Controller/TrackDaFilesController.php, line 439

Class

TrackDaFilesController
Class TrackDaFilesController.

Namespace

Drupal\track_da_files\Controller

Code

public function table_build_user_report($uid) {
  $user_report_enabled = \Drupal::config('track_da_files.settings')
    ->get('user_report_enabled');
  $single_user_datas = \Drupal::config('track_da_files.settings')
    ->get('single_user_datas');
  if ($user_report_enabled) {
    $header = array(
      t('Filename'),
      t('time'),
    );

    // Header datas.
    foreach ($single_user_datas as $data => $value) {
      if (!empty($value)) {
        if ($value == 'referer') {
          $header[] = t('Displayed from');
        }
        elseif ($value == 'browser') {
          $header[] = t('Browser');
        }
        elseif ($value == 'browser_version') {
          $header[] = t('Browser version');
        }
        elseif ($value == 'browser_platform') {
          $header[] = t('Platform');
        }
        elseif ($value == 'ip') {
          $header[] = t('Ip');
        }
      }
    }

    // We prepare fields.
    $fields = array(
      'time',
    );
    foreach ($single_user_datas as $key => $value) {
      if ($value) {
        $fields[] = $value;
      }
    }
    $query = \Drupal::database()
      ->select('track_da_files', 't');
    $query
      ->join('track_da_files_paths', 'p', 't.pid = p.pid');
    $query
      ->extend('\\Drupal\\Core\\Database\\Query\\PagerSelectExtender');
    $query
      ->extend('\\Drupal\\Core\\Database\\Query\\TableSortExtender');
    $query
      ->fields('p', array(
      'fid',
    ));
    $query
      ->fields('t', $fields);
    $query
      ->condition('uid', $uid);
    $query
      ->range(0, 20);
    $results = $query
      ->execute();
    $rows = array();

    // We loop into results to build rows for user report table.
    foreach ($results as $key => $record) {
      foreach ($record as $key2 => $row) {
        if ($key2 == 'time') {
          $rows[$key][$key2] = format_date($row, 'short');
        }
        elseif ($key2 == 'fid') {

          //$file = file_load($row);
          $file = File::load($row);
          if (isset($file->filename)) {
            $rows[$key][$key2] = $file
              ->getFilename();
          }
          else {
            $rows[$key][$key2] = t('No name');
          }
        }
        else {
          $rows[$key][$key2] = $row;
        }
      }
    }
    $build['track_da_files_table'] = array(
      '#type' => 'table',
      '#header' => $header,
      '#rows' => $rows,
      '#attributes' => array(
        'id' => 'admin-track-da-files',
        'class' => array(
          'admin-track-da-files',
        ),
      ),
      '#attached' => array(
        'library' => array(
          'track_da_files/track_da_files',
        ),
      ),
    );
    return $build['track_da_files_table'];
  }
  else {
    return FALSE;
  }
}