You are here

function uc_file_user_downloads in Ubercart 5

Same name and namespace in other branches
  1. 6.2 uc_file/uc_file.pages.inc \uc_file_user_downloads()
  2. 7.3 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
Implementation of hook_menu().

File

uc_file/uc_file.module, line 884
Allows products to be associated with downloadable files.

Code

function uc_file_user_downloads($uid) {
  drupal_set_title(t('File downloads'));
  uc_add_js(drupal_get_path('module', 'uc_file') . '/uc_file.js');
  $header = array(
    array(
      'data' => t('Purchased'),
      'field' => 'u.granted',
      'sort' => 'desc',
    ),
    array(
      'data' => t('Filename'),
      'field' => 'f.filename',
    ),
    array(
      'data' => t('Size'),
    ),
    array(
      'data' => t('Description'),
      'field' => 'p.description',
    ),
    array(
      'data' => t('Downloads'),
      'field' => 'u.accessed',
    ),
  );
  $sql = "SELECT granted, filename, accessed, description, `file_key`, f.fid FROM {uc_file_users} as u LEFT JOIN {uc_files} as f ON u.fid = f.fid LEFT JOIN {uc_file_products} as p ON p.pfid = u.pfid WHERE uid = %d";
  $count_query = "SELECT COUNT(*) FROM {uc_file_users} WHERE uid = %d";
  $download_limit = variable_get('uc_file_download_limit_number', NULL);
  $file_ids = array();
  $rows = array();
  $files = pager_query($sql . tablesort_sql($header), UC_FILE_PAGER_SIZE, 0, $count_query, $uid);
  while ($file = db_fetch_object($files)) {
    $row = count($rows);
    $file_path = variable_get('uc_file_base_dir', NULL) . '/' . $file->filename;
    $bytesize = format_size(filesize($file_path));
    $expiration = _file_expiration_date($file->granted);
    $onclick = array(
      'onclick' => 'uc_file_update_download(' . $row . ', ' . $file->accessed . ', ' . (empty($download_limit) ? -1 : $download_limit) . ');',
      'id' => 'link-' . $row,
    );
    if (!$expiration) {
      $file_link = l(basename($file->filename), 'download/' . $file->fid . '/' . $file->file_key, $onclick);
    }
    else {
      if (time() > $expiration) {
        $file_link = basename($file->filename);
      }
      else {
        $file_link = l(basename($file->filename), 'download/' . $file->fid . '/' . $file->file_key, $onclick) . ' (' . t('expires on @date', array(
          '@date' => format_date($expiration, 'custom', variable_get('uc_date_format_default', 'm/d/Y')),
        )) . ')';
      }
    }
    $rows[] = array(
      array(
        'data' => format_date($file->granted, 'custom', variable_get('uc_date_format_default', 'm/d/Y')),
        'class' => 'date-row',
        'id' => 'date-' . $row,
      ),
      array(
        'data' => $file_link,
        'class' => 'filename-row',
        'id' => 'filename-' . $row,
      ),
      array(
        'data' => $bytesize,
        'class' => 'filename-row',
        'id' => 'filesize-' . $row,
      ),
      array(
        'data' => $file->description,
        'class' => 'description-row',
        'id' => 'description-' . $row,
      ),
      array(
        'data' => $file->accessed,
        'class' => 'download-row',
        'id' => 'download-' . $row,
      ),
    );
  }
  if (empty($rows)) {
    $rows[] = array(
      array(
        'data' => t('No downloads found'),
        'colspan' => 4,
      ),
    );
  }
  $output = theme('table', $header, $rows) . theme('pager', NULL, UC_FILE_PAGER_SIZE, 0);
  return $output;
}