You are here

function uc_file_user_downloads in Ubercart 7.3

Same name and namespace in other branches
  1. 5 uc_file/uc_file.module \uc_file_user_downloads()
  2. 6.2 uc_file/uc_file.pages.inc \uc_file_user_downloads()

Table builder for user downloads.

1 string reference to 'uc_file_user_downloads'
uc_file_menu in uc_file/uc_file.module
Implements hook_menu().

File

uc_file/uc_file.pages.inc, line 83
File menu items.

Code

function uc_file_user_downloads($account) {

  // Create a header and the pager it belongs to.
  $header = array(
    array(
      'data' => t('Purchased'),
      'field' => 'u.granted',
      'sort' => 'desc',
    ),
    array(
      'data' => t('Filename'),
      'field' => 'f.filename',
    ),
    array(
      'data' => t('Description'),
      'field' => 'p.description',
    ),
    array(
      'data' => t('Downloads'),
      'field' => 'u.accessed',
    ),
    array(
      'data' => t('Addresses'),
    ),
  );
  drupal_set_title(t('File downloads'));
  $files = array();
  $query = db_select('uc_file_users', 'u')
    ->extend('PagerDefault')
    ->extend('TableSort')
    ->condition('uid', $account->uid)
    ->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', array(
    'granted',
    'accessed',
    'addresses',
    'file_key',
    'download_limit',
    'address_limit',
    'expiration',
  ))
    ->fields('f', array(
    'filename',
    'fid',
  ))
    ->fields('p', array(
    'description',
  ));
  $count_query = db_select('uc_file_users')
    ->condition('uid', $account->uid);
  $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 = array(
      'attributes' => array(
        'onclick' => 'uc_file_update_download(' . $row . ', ' . $file->accessed . ', ' . (empty($download_limit) ? -1 : $download_limit) . ');',
        'id' => 'link-' . $row,
      ),
    );

    // Expiration set to 'never'.
    if ($file->expiration == FALSE) {
      $file_link = l(basename($file->filename), 'download/' . $file->fid, $onclick);
    }
    elseif (REQUEST_TIME > $file->expiration) {
      $file_link = basename($file->filename);
    }
    else {
      $file_link = l(basename($file->filename), 'download/' . $file->fid, $onclick) . ' (' . t('expires on @date', array(
        '@date' => format_date($file->expiration, 'uc_store'),
      )) . ')';
    }
    $files[] = array(
      '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'] = array(
    '#theme' => 'uc_file_user_downloads',
    '#header' => $header,
    '#files' => $files,
  );
  if (user_access('administer users')) {
    $build['admin'] = drupal_get_form('uc_file_user_form', $account);
  }
  return $build;
}