You are here

function download_count_file_download in Download Count 5

Same name and namespace in other branches
  1. 6.2 download_count.module \download_count_file_download()
  2. 6 download_count.module \download_count_file_download()
  3. 7.2 download_count.module \download_count_file_download()

Implementation of file_download()

File

./download_count.module, line 185
Download counter

Code

function download_count_file_download($filename) {
  $extensions = explode(' ', trim(variable_get('download_count_excluded_file_extensions', '')));
  if (count($extensions)) {
    $pathinfo = pathinfo($filename);
    if (in_array($pathinfo['extension'], $extensions)) {
      return;
    }
  }
  $file = file_create_path($filename);
  $result = db_query("SELECT f.* FROM {files} f WHERE filepath = '%s'", $file);
  if ($file = db_fetch_object($result)) {
    if (user_access('view uploaded files') && node_access('view', node_load($file->nid))) {
      $message = t('%file was downloaded', array(
        '%file' => $filename,
      ));
      watchdog('download', $message, 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);
      }
      else {
        db_query("INSERT INTO {file_downloads} (filename, count, timestamp) VALUES ('%s', 1,%d)", $filename, time());
      }
    }
    else {
      $message = t('Failed to download %file', array(
        '%file' => $filename,
      ));
      watchdog('download', $message, WATCHDOG_WARNING);
    }
  }
}