Index: download_count.info
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/download_count/Attic/download_count.info,v
retrieving revision 1.2.4.2
diff -u -p -r1.2.4.2 download_count.info
--- download_count.info	4 May 2008 16:57:20 -0000	1.2.4.2
+++ download_count.info	22 Sep 2009 16:52:29 -0000
@@ -1,6 +1,5 @@
 ; $Id: download_count.info,v 1.2.4.2 2008/05/04 16:57:20 chill35 Exp $
 name = Download counter
 description = "Increments a download counter and logs a descriptive message each time an attached file is downloaded."
-dependencies[] = upload
 core = 6.x
 
Index: download_count.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/download_count/download_count.install,v
retrieving revision 1.2.2.1
diff -u -p -r1.2.2.1 download_count.install
--- download_count.install	11 Jan 2008 01:42:14 -0000	1.2.2.1
+++ download_count.install	22 Sep 2009 16:52:29 -0000
@@ -14,18 +14,49 @@ function download_count_install() {
  * Implementation of hook_schema().
  */
 function download_count_schema() {
+  $schema['file_downloads'] = array(
+    'fields' => array(
+      'fid' => array(
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'default' => 0
+      ),
+      'timestamp' => array(
+        'type' => 'int',
+        'not null' => 1,
+        'default' => 0,
+        'disp-width' => '11'
+      ),
+      'count' => array(
+        'type' => 'int',
+        'not null' => 1,
+        'default' => 0,
+        'disp-width' => '11'
+      )
+    ),
+    'unique keys' => array('fid' => array('fid')),
+  );
 
-$schema['file_downloads'] = array(
+  $schema['file_downloads_filefield'] = array(
     'fields' => array(
-         'filename' => array('type' => 'varchar', 'length' => '255', 'not null' => 1),
-         'timestamp' => array('type' => 'int', 'not null' => 1, 'default' => 0, 'disp-width' => '11'),
-         'count' => array('type' => 'int', 'not null' => 1, 'default' => 0, 'disp-width' => '11')),
-    'unique keys' => array(
-         'filename' => array('filename')),
-);
+      'fid' => array(
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'default' => 0
+      ),
+      'filefield' => array(
+        'description' => 'Name of the filefield.',
+        'type' => 'varchar',
+        'length' => 255,
+        'not null' => TRUE,
+        'default' => ''),
+    ),
+    'unique keys' => array('fid' => array('fid')),
+  );
 
   return $schema;
-
 }
 
 /**
Index: download_count.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/download_count/download_count.module,v
retrieving revision 1.13.2.5
diff -u -p -r1.13.2.5 download_count.module
--- download_count.module	27 May 2008 04:57:42 -0000	1.13.2.5
+++ download_count.module	22 Sep 2009 16:52:29 -0000
@@ -21,7 +21,7 @@ function download_count_perm() {
   $perms[] = 'view own nodes downloads count';
   $perms[] = 'view all downloads count in nodes';
   $perms[] = 'view own nodes downloads count in nodes';
-	$perms[] = 'administer download counter';
+  $perms[] = 'administer download counter';
   return $perms;
 }
 /**
@@ -35,7 +35,7 @@ function download_count_menu() {
     'title' => 'Download counter',
     'page callback' => 'drupal_get_form',
     'page arguments' => array('download_count_admin_settings'),
-		'access arguments' => array('administer download counter'),
+    'access arguments' => array('administer download counter'),
     'type' => MENU_NORMAL_ITEM,
     );
 
@@ -127,16 +127,23 @@ function download_count_view_page() {
   $header[] = array('data' => t('last download'), 'field' => 'timestamp');
   $header[] = array('data' => t('action'));
   $rows = array();
-  $fileDirectoryPath = file_directory_path() . '/';  
-  if(user_access('view all downloads count')) {
-    $result = db_query("SELECT fd.filename, fd.count, fd.timestamp, u.nid, nt.name FROM {file_downloads} fd JOIN {files} f ON f.filepath = CONCAT('%s', fd.filename) JOIN {upload} u ON u.fid = f.fid JOIN {node} n ON n.nid = u.nid JOIN {node_type} nt ON nt.type = n.type"  . tablesort_sql($header), $fileDirectoryPath);
+  $fileDirectoryPath = file_directory_path() . '/';
+
+  //query if upload.module is installed
+  if (module_exists('upload')) {
+    $select = ', nt.name, u.nid';
+    $join = ' LEFT JOIN {upload} u ON f.fid = u.fid LEFT JOIN {node} n ON n.nid = u.nid LEFT JOIN {node_type} nt ON nt.type = n.type';
+  }
+
+  if (user_access('view all downloads count')) {
+    $result = db_query("SELECT f.filename, f.fid, fd.count, fd.timestamp". $select ." FROM {file_downloads} fd JOIN {files} f ON f.fid = fd.fid". $join . tablesort_sql($header));
   }
   else {
     $result = db_query("SELECT fd.filename, fd.count, fd.timestamp, u.nid, nt.name FROM {file_downloads} fd JOIN {files} f ON f.filepath = CONCAT('%s', fd.filename) JOIN {upload} u ON u.fid = f.fid JOIN {node} n ON n.nid = u.nid JOIN {node_type} nt ON nt.type = n.type WHERE n.uid = %d" . tablesort_sql($header), $fileDirectoryPath, $user->uid);
   }
   while ($file = db_fetch_object($result)) {
     $row = array();
-    $row[] = $file->filename;
+    $row[] = $file->filename .' ('. $file->fid .')';
     $row[] = $file->count;
     $row[] = t('@time ago', array('@time' => format_interval(time() - $file->timestamp)));
     $row[] = l(t('view @type', array('@type' => $file->name)), 'node/' . $file->nid);
@@ -153,7 +160,6 @@ function download_count_view_page() {
 /**
  * Implementation of file_download()
  */
-
 function download_count_file_download($filename) {
   $extensions = explode(' ', trim(variable_get('download_count_excluded_file_extensions', 'jpg jpeg gif png')));
   if (count($extensions)) {
@@ -162,25 +168,71 @@ function download_count_file_download($f
       return;
     }
   }
+
   $filepath = file_create_path($filename);
-  $result = db_query("SELECT u.nid, f.filepath FROM {upload} u JOIN {files} f ON f.fid = u.fid WHERE f.filepath = '%s'", $filepath);
+  // fetch fid from {files} table
+  // create db query
+  if (module_exists('upload')) {
+    $select = ', u.nid';
+    $join = ' LEFT JOIN {upload} u ON f.fid = u.fid';
+  }
+
+  $access = FALSE;
+
+  $result = db_query("SELECT f.fid, f.filepath, ff.filefield". $select ." FROM {files} f LEFT JOIN {file_downloads_filefield} ff ON f.fid = ff.fid". $join ." WHERE f.filepath = '%s'", $filepath);
+
   if ($file = db_fetch_object($result)) {
-    if (user_access('view uploaded files') && node_access('view', node_load($file->nid))) {
+    // file uploaded with upload.module
+    if ($file->nid) {
+      if (user_access('view uploaded files') && node_access('view', node_load($file->nid))) {
+        $access = TRUE;
+      }
+    }
+    // file is a filefield type
+    else if ($file->filefield) {
+      if (user_access('view '.$file->filefield)) {
+        $access = TRUE;
+      }
+    }
+    // do we need to have else here? and check for other types of files?
+
+    if ($access) {
       watchdog('download', '%file was downloaded', array('%file' => $filename), WATCHDOG_NOTICE);
       // If the file is already added, just increment the count,
       // otherwise add the file with count 1
-      if(db_result(db_query("SELECT filename FROM {file_downloads} WHERE filename = '%s'", $filename))) {
-        db_query("UPDATE {file_downloads} SET count = count+1, timestamp = %d  WHERE filename = '%s'", time(), $filename);
+      if(db_result(db_query("SELECT fid FROM {file_downloads} WHERE fid = %d", $file->fid))) {
+        db_query("UPDATE {file_downloads} SET count = count+1, timestamp = %d  WHERE fid = %d", time(), $file->fid);
+      }
+      else {
+        db_query("INSERT INTO {file_downloads} (fid, count, timestamp) VALUES (%d, 1, %d)", $file->fid, time());
       }
+    }
     else {
-        db_query("INSERT INTO {file_downloads} (filename, count, timestamp) VALUES ('%s', 1,%d)", $filename, time());
+      watchdog('download', 'Failed to download %file', array('%file' => $filename), WATCHDOG_WARNING);
     }
   }
-  else {
-    watchdog('download', 'Failed to download %file', array('%file' => $filename), WATCHDOG_WARNING);
-  }
+}
+
+/**
+* Check if the field is a cck field. 
+* @return TRUE or FALSE
+*/
+function _download_count_cck_child($key) {
+  return (!isset($key[0]) || $key[0] != '#') && substr($key, 0, 6) == 'field_';
+}
+
+/**
+* Load filefields into $node->files. 
+*/
+function download_count_load_filefield(&$node) {
+  $ccks = array_filter(array_keys((array) $node), '_download_count_cck_child');
+  foreach($ccks as $key => $cck){
+    foreach($node->$cck as $t){
+     if(array_key_exists('fid', $t))  $node->files[] = (object) $t;
+    }
   }
 }
+
 /**
  * Implementation of hook_nodeapi()
  */
@@ -189,14 +241,32 @@ function download_count_nodeapi(&$node, 
     global $user;
     if ($user->uid == 1 && variable_get('do_not_show_download_count_in_node_for_admin', FALSE)) {
         return;
-      }
-    if (!$node->teaser && count($node->files)) {
-      if (user_access('view all downloads count in nodes') || (user_access('view own nodes downloads count in nodes') && ($node->uid == $user->uid)) ) {
-        $node->body = theme('download_count_body', $node);
+    }
+    if (!$node->teaser) {
+      // Load all filefields into $node->files.
+      $filefields = download_count_load_filefield($node);
+      if (count($node->files) || count($filefields)) {
+        if (user_access('view all downloads count in nodes') || (user_access('view own nodes downloads count in nodes') && ($node->uid == $user->uid)) ) {
+          $node->body = theme('download_count_body', $node);
+        }
       }
     }
   }
 }
+
+function download_count_file_insert($file) {
+  if ($file->field['field_name']) {
+    db_query("INSERT INTO {file_downloads_filefield} (fid, filefield) VALUES (%d, '%s')", $file->fid, $file->field['field_name']);
+  }
+}
+
+function download_count_file_delete($file) {
+  if ($file->fid) {
+    db_query("DELETE FROM {file_downloads_filefield} WHERE fid = %d", $file->fid);
+    db_query("DELETE FROM {file_downloads} WHERE fid = %d", $file->fid);
+  }
+}
+
 function download_count_theme() {
   return array(
     'download_count_body' => array(
@@ -215,7 +285,7 @@ function theme_download_count_body($node
     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);
+    $pick = db_query("SELECT fid, count, timestamp FROM {file_downloads} WHERE fid = %d", $file->fid);
     if ($attach = db_fetch_object($pick)){
       $count = $attach->count;
       $last = t('@time ago', array('@time' => format_interval(time() - $attach->timestamp)));
