You are here

public function TrackDaFilesController::table_build_file_report in Track da files 8

Build variables used to create file report.

1 call to TrackDaFilesController::table_build_file_report()
TrackDaFilesController::table in src/Controller/TrackDaFilesController.php
Builds a table which shows number of displays by file.
1 string reference to 'TrackDaFilesController::table_build_file_report'
track_da_files.routing.yml in ./track_da_files.routing.yml
track_da_files.routing.yml

File

src/Controller/TrackDaFilesController.php, line 289

Class

TrackDaFilesController
Class TrackDaFilesController.

Namespace

Drupal\track_da_files\Controller

Code

public function table_build_file_report($fid, $pid) {
  global $base_url;

  // We retrieve configuration datas.
  $single_file_datas = \Drupal::config('track_da_files.settings')
    ->get('single_file_datas');
  $user_report_enabled = \Drupal::config('track_da_files.settings')
    ->get('user_report_enabled');

  // We prepare table header for file specific report.
  $header = array(
    t('Time'),
  );

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

  // We build an array with header values to make some verifications.
  foreach ($header as $key => $value) {
    $header_values[] = $value;
  }
  if ($user_report_enabled) {
    $header[] = $this
      ->t('User datas');
  }

  // We prepare fields.
  $fields = array(
    'time',
  );
  if (isset($single_file_datas['id']) && $single_file_datas['id'] == '0') {
    unset($single_file_datas['id']);
  }
  foreach ($single_file_datas as $key => $value) {
    if (!empty($value)) {
      if ($value == 'id') {
        $fields[] = 'id';
        $fields[] = 'type';
      }
      else {
        $fields[] = $value;
      }
    }
  }
  $query = \Drupal::database()
    ->select('track_da_files', 't');
  $query
    ->extend('\\Drupal\\Core\\Database\\Query\\PagerSelectExtender');
  $query
    ->extend('\\Drupal\\Core\\Database\\Query\\TableSortExtender');
  $query
    ->fields('t', $fields);
  $query
    ->condition('pid', $pid);
  $query
    ->range(0, 20);
  $results = $query
    ->execute();
  $rows = array();

  // We loop into the results to build table rows for file report table.
  foreach ($results as $key => $record) {
    foreach ($record as $key2 => $row) {
      if ($key2 == 'time') {
        $rows[$key][$key2] = format_date($row, 'short');
      }
      elseif ($key2 == 'id') {
        if ($record->type == 'node') {
          $node = Node::load($row);
          $rows[$key][$key2] = \Drupal::l($node
            ->getTitle(), Url::fromUri($base_url . '/' . $record->type . '/' . $row));
        }
        elseif ($record->type == 'comment') {
          $comment = Comment::load($row);
          $rows[$key][$key2] = \Drupal::l($comment
            ->getsSubject(), Url::fromUri($base_url . '/' . $record->type . '/' . $row), array(
            'fragment' => 'comment-' . $row,
          ));
        }
        else {
          $rows[$key][$key2] = ' ';
        }
      }
      elseif ($key2 == 'type') {
        unset($rows[$key][$key2]);
      }
      elseif ($key2 == 'uid') {
        $uid = $row;
        $account = \Drupal\user\Entity\User::load($uid);
        $name = $account
          ->getUsername();
        if ($user_report_enabled) {

          //dpm($header_values);
          if (in_array('Username', $header_values)) {
            if (!empty($name)) {
              $rows[$key][$key2] = $name;
            }
            else {
              $rows[$key][$key2] = t('Anonymous user');
            }
          }
          $rows[$key]['view_more'] = \Drupal::l(t('View more'), Url::fromUri($base_url . '/admin/reports/track_da_files/user_report/' . $uid));
        }
        else {
          $rows[$key][$key2] = $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',
      ),
    ),
  );
  $build['track_da_files_pager'] = array(
    '#type' => 'pager',
  );
  return $build['track_da_files_table'];
}