You are here

function theme_download_count_body in Download Count 5

Same name and namespace in other branches
  1. 6 download_count.module \theme_download_count_body()
1 theme call to theme_download_count_body()
download_count_nodeapi in ./download_count.module
Implementation of hook_nodeapi()

File

./download_count.module, line 233
Download counter

Code

function theme_download_count_body($node) {
  $header[] = array(
    'data' => t('Attachment'),
  );
  $header[] = array(
    'data' => t('Size'),
  );
  $header[] = array(
    'data' => t('Hits'),
  );
  $header[] = array(
    'data' => t('Last download'),
  );
  $rows = array();
  $fileDirectoryPath = file_directory_path() . '/';
  foreach ($node->files as $file) {
    if ($file->list) {
      $href = $file->fid ? file_create_url($file->filepath) : url(file_create_filename($file->filename, file_create_path()));
      $text = $file->description ? $file->description : $file->filename;
      $pick = db_query("SELECT filename, count, timestamp FROM {file_downloads} WHERE CONCAT('%s', filename) = '%s'", $fileDirectoryPath, $file->filepath);
      if ($attach = db_fetch_object($pick)) {
        $count = $attach->count;
        $last = format_interval(time() - $attach->timestamp) . ' ago';
      }
      else {
        $count = 0;
        $last = t('Not yet downloaded');
      }
      if (user_access('view uploaded files')) {
        $rows[] = array(
          l($text, $href),
          format_size($file->filesize),
          $count,
          $last,
        );
      }
      else {
        $rows[] = array(
          $file->filename,
          format_size($file->filesize),
          $count,
          $last,
        );
      }
    }
  }
  if (count($rows)) {
    $attachments = theme('table', $header, $rows, array(
      'id' => 'attachments',
    ));
  }
  else {
    $attachments = '';
  }
  if (strstr($node->body, '<table id="attachments"')) {
    $start = strpos($node->body, '<table id="attachments"');
    $end = strpos($node->body, '</table>', $start);

    // return substr_replace($node->body, '', $start, $end - $start + 8) . $attachments;
    return substr_replace($node->body, $attachments, $start, $end - $start + 8);
  }
  else {
    return $node->body . $attachments;
  }
}