You are here

public function DownloadController::userDownloads in Ubercart 8.4

Table builder for user downloads.

1 string reference to 'DownloadController::userDownloads'
uc_file.routing.yml in uc_file/uc_file.routing.yml
uc_file/uc_file.routing.yml

File

uc_file/src/Controller/DownloadController.php, line 84

Class

DownloadController
Handles administrative view of files that may be purchased and downloaded.

Namespace

Drupal\uc_file\Controller

Code

public function userDownloads(AccountInterface $user) {

  // Create a header and the pager it belongs to.
  $header = [
    [
      'data' => $this
        ->t('Purchased'),
      'field' => 'u.granted',
      'sort' => 'desc',
    ],
    [
      'data' => $this
        ->t('Filename'),
      'field' => 'f.filename',
    ],
    [
      'data' => $this
        ->t('Description'),
      'field' => 'p.description',
    ],
    [
      'data' => $this
        ->t('Downloads'),
      'field' => 'u.accessed',
    ],
    [
      'data' => $this
        ->t('Addresses'),
    ],
  ];
  $build['#title'] = $this
    ->t('File downloads');
  $files = [];
  $query = $this->database
    ->select('uc_file_users', 'u')
    ->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender')
    ->extend('Drupal\\Core\\Database\\Query\\TableSortExtender')
    ->condition('uid', $user
    ->id())
    ->orderByHeader($header)
    ->limit(UC_FILE_PAGER_SIZE);
  $query
    ->leftJoin('uc_files', 'f', 'u.fid = f.fid');
  $query
    ->leftJoin('uc_file_products', 'p', 'p.pfid = u.pfid');
  $query
    ->fields('u', [
    'granted',
    'accessed',
    'addresses',
    'file_key',
    'download_limit',
    'address_limit',
    'expiration',
  ])
    ->fields('f', [
    'filename',
    'fid',
  ])
    ->fields('p', [
    'description',
  ]);
  $count_query = $this->database
    ->select('uc_file_users')
    ->condition('uid', $user
    ->id());
  $count_query
    ->addExpression('COUNT(*)');
  $query
    ->setCountQuery($count_query);
  $result = $query
    ->execute();
  $row = 0;
  foreach ($result as $file) {
    $download_limit = $file->download_limit;

    // Set the JS behavior when this link gets clicked.
    $onclick = [
      'attributes' => [
        'onclick' => 'Drupal.behaviors.ucFileUpdateDownload(' . $row . ', ' . $file->accessed . ', ' . (empty($download_limit) ? -1 : $download_limit) . ');',
        'id' => 'link-' . $row,
      ],
    ];

    // Expiration set to 'never'.
    if ($file->expiration == FALSE) {
      $file_link = Link::createFromRoute($this->fileSystem
        ->basename($file->filename), 'uc_file.download_file', [
        'file' => $file->fid,
      ], $onclick)
        ->toString();
    }
    elseif (REQUEST_TIME > $file->expiration) {
      $file_link = $this->fileSystem
        ->basename($file->filename);
    }
    else {
      $file_link = Link::createFromRoute($this->fileSystem
        ->basename($file->filename), 'uc_file.download_file', [
        'file' => $file->fid,
      ], $onclick)
        ->toString() . ' (' . $this
        ->t('expires on @date', [
        '@date' => \Drupal::service('date.formatter')
          ->format($file->expiration, 'uc_store'),
      ]) . ')';
    }
    $files[] = [
      'granted' => $file->granted,
      'link' => $file_link,
      'description' => $file->description,
      'accessed' => $file->accessed,
      'download_limit' => $file->download_limit,
      'addresses' => $file->addresses,
      'address_limit' => $file->address_limit,
    ];
    $row++;
  }
  $build['downloads'] = [
    '#theme' => 'uc_file_user_downloads',
    '#header' => $header,
    '#files' => $files,
  ];
  if ($this
    ->currentUser()
    ->hasPermission('administer users')) {
    $build['admin'] = $this
      ->formBuilder()
      ->getForm('Drupal\\uc_file\\Form\\UserForm', $user);
  }
  return $build;
}