You are here

function _download_count_block_contents in Download Count 7.2

Same name and namespace in other branches
  1. 6.2 download_count.module \_download_count_block_contents()
  2. 7.3 download_count.module \_download_count_block_contents()
1 call to _download_count_block_contents()
download_count_block_view in ./download_count.module
Implements hook_block_view().

File

./download_count.module, line 433
Tracks file downloads for files stored in the drupal files table using the private files setting or custom private filefield.

Code

function _download_count_block_contents($block) {
  $result = '';
  $limit = variable_get('download_count_' . $block . '_block_limit', 10);
  $show_size = variable_get('download_count_' . $block . '_show_size', 0);
  $show_last = variable_get('download_count_' . $block . '_show_last', 0);
  $block == 'files' ? $file_links = variable_get('download_count_files_file_links', 1) : NULL;
  switch ($block) {
    case 'files':
      $rows = array();
      $header[] = array(
        'data' => t('Filename'),
        'class' => 'filename',
      );
      $header[] = $show_size ? array(
        'data' => t('Size'),
        'class' => 'size',
      ) : '';
      $header[] = array(
        'data' => t('Count'),
        'class' => 'count',
      );
      $header[] = $show_last ? array(
        'data' => t('Last Downloaded'),
        'class' => 'last',
      ) : '';
      if (user_access('view all download counts')) {
        $result = db_query('SELECT COUNT(dc.dcid) AS count, f.filename, f.filepath, f.fid, SUM(f.filesize) AS size, MAX(dc.timestamp) as last FROM {download_count} dc JOIN {files} f ON dc.fid = f.fid GROUP BY f.filename ORDER BY count DESC LIMIT %d', (int) $limit);
      }
      while ($file = db_fetch_object($result)) {
        $row = array();
        $row[] = $file_links && (user_access('view uploaded files') || _download_count_is_accessible_by_filefield($file->filepath)) ? l(t('@filename', array(
          '@filename' => $file->filename,
        )), function_exists('_private_upload_create_url') ? _private_upload_create_url($file) : file_create_url($file->filepath)) : check_plain($file->filename);
        $row[] = $show_size ? format_size($file->size) : '';
        $row[] = $file->count;
        $row[] = $show_last ? t('@time ago', array(
          '@time' => format_interval(REQUEST_TIME - $file->last),
        )) : '';
        $rows[] = $row;
      }
      if (count($rows)) {
        return theme('table', array(
          'header' => $header,
          'rows' => $rows,
          'attributes' => array(
            'class' => 'no-sticky',
          ),
        ));
      }
      return;
    case 'downloaders':
      $rows = array();
      $header[] = array(
        'data' => t('User'),
        'class' => 'user',
      );
      $header[] = $show_size ? array(
        'data' => t('Size'),
        'class' => 'size',
      ) : '';
      $header[] = array(
        'data' => t('Count'),
        'class' => 'count',
      );
      $header[] = $show_last ? array(
        'data' => t('Last Downloaded'),
        'class' => 'last',
      ) : '';
      if (user_access('view all download counts')) {
        $result = db_query('SELECT COUNT(dc.dcid) AS count, SUM(f.filesize) AS size, u.name, u.uid, MAX(dc.timestamp) as last FROM {download_count} dc JOIN {users} u ON dc.uid = u.uid JOIN {files} f on dc.fid = f.fid GROUP BY u.name ORDER BY count DESC LIMIT %d', (int) $limit);
      }
      while ($file = db_fetch_object($result)) {
        $row = array();
        $row[] = l($file->name, 'user/' . $file->uid);
        $row[] = $show_size ? format_size($file->size) : '';
        $row[] = $file->count;
        $row[] = $show_last ? t('@time ago', array(
          '@time' => format_interval(REQUEST_TIME - $file->last),
        )) : '';
        $rows[] = $row;
      }
      if (count($rows)) {
        return theme('table', array(
          'header' => $header,
          'rows' => $rows,
          'attributes' => array(
            'class' => 'no-sticky',
          ),
        ));
      }
      return;
    case 'users':
      $rows = array();
      $header[] = array(
        'data' => t('User'),
        'class' => 'user',
      );
      $header[] = $show_size ? array(
        'data' => t('Size'),
        'class' => 'size',
      ) : '';
      $header[] = array(
        'data' => t('Count'),
        'class' => 'count',
      );
      $header[] = $show_last ? array(
        'data' => t('Last Downloaded'),
        'class' => 'last',
      ) : '';
      if (user_access('view all download counts')) {
        $result = db_query('SELECT COUNT(dc.dcid) AS count, SUM(f.filesize) AS size, u.name, u.uid, MAX(dc.timestamp) as last FROM {download_count} dc JOIN {files} f on dc.fid = f.fid JOIN {users} u ON f.uid = u.uid GROUP BY u.name ORDER BY count DESC LIMIT %d', (int) $limit);
      }
      while ($file = db_fetch_object($result)) {
        $row = array();
        $row[] = l($file->name, 'user/' . $file->uid);
        $row[] = $show_size ? format_size($file->size) : '';
        $row[] = $file->count;
        $row[] = $show_last ? t('@time ago', array(
          '@time' => format_interval(REQUEST_TIME - $file->last),
        )) : '';
        $rows[] = $row;
      }
      if (count($rows)) {
        return theme('table', array(
          'header' => $header,
          'rows' => $rows,
          'attributes' => array(
            'class' => 'no-sticky',
          ),
        ));
      }
      return;
    case 'nodes':
      $rows = array();
      $header[] = array(
        'data' => t('Page'),
        'class' => 'node',
      );
      $header[] = $show_size ? array(
        'data' => t('Size'),
        'class' => 'size',
      ) : '';
      $header[] = array(
        'data' => t('Count'),
        'class' => 'count',
      );
      $header[] = $show_last ? array(
        'data' => t('Last Downloaded'),
        'class' => 'last',
      ) : '';
      if (user_access('view all download counts')) {
        $result = db_query('SELECT COUNT(dc.dcid) AS count, SUM(f.filesize) AS size, dc.nid, MAX(dc.timestamp) as last FROM {download_count} dc JOIN {files} f on dc.fid = f.fid GROUP BY dc.nid ORDER BY count DESC LIMIT %d', (int) $limit);
      }
      while ($file = db_fetch_object($result)) {
        $row = array();
        $node = node_load($file->nid);
        $row[] = node_access('view', $node) ? l($node->title, 'node/' . $node->nid) : check_plain($node->title);
        $row[] = $show_size ? format_size($file->size) : '';
        $row[] = $file->count;
        $row[] = $show_last ? t('@time ago', array(
          '@time' => format_interval(REQUEST_TIME - $file->last),
        )) : '';
        $rows[] = $row;
      }
      if (count($rows)) {
        return theme('table', array(
          'header' => $header,
          'rows' => $rows,
          'attributes' => array(
            'class' => 'no-sticky',
          ),
        ));
      }
      return;
    default:
      return;
  }
}