You are here

function filedepot_file_delete in filedepot 7

Implements hook_file_delete().

File

./filedepot.module, line 1409
filedepot.module Filedepot: File Management Module developed by Nextide www.nextide.ca Full featured document managment module with a desktop application feel. Integrated Organic Group, Role and User permissions to secure folders, automated…

Code

function filedepot_file_delete($file) {
  global $user;
  if (isset($file->version)) {
    watchdog('filedepot', "Deleting file return as version @fileversion is set", array(
      "@fileversion" => $file->version,
    ));
    return;
  }
  $fid = db_query("SELECT fid FROM {filedepot_files} WHERE drupal_fid=:dfid", array(
    ':dfid' => $file->fid,
  ))
    ->fetchField();
  if ($user->uid > 0 and $fid > 0) {
    $filedepot = filedepot_filedepot();

    // Check if user is the owner or has category admin rights
    $query = db_query("SELECT cid,drupal_fid,title,version,submitter,size FROM {filedepot_files} WHERE fid=:fid", array(
      ':fid' => $fid,
    ));
    list($cid, $drupal_fid, $title, $version, $submitter, $fsize) = array_values($query
      ->fetchAssoc());
    if ($submitter == $user->uid or $filedepot
      ->checkPermission($cid, 'admin')) {
      watchdog('filedepot', "Deleting file @fid from folder @cid", array(
        "@fid" => $fid,
        "@cid" => $cid,
      ));

      // Additional testing for the nexcloud instance because this method is also called from filedepot_uninstall()
      if (function_exists('filedepot_nexcloud')) {
        $nexcloud = filedepot_nexcloud();
      }
      else {
        module_load_include('php', 'filedepot', 'nexcloud.class');
        $nexcloud = new filedepotTagCloud();
      }
      $nexcloud
        ->clear_tags($fid);

      // Clear all tags and update metrics for this item

      /* Remove any file versions */
      $query = db_query("SELECT id, version, drupal_fid FROM {filedepot_fileversions} WHERE fid=:fid", array(
        ':fid' => $fid,
      ));
      while ($A = $query
        ->fetchAssoc()) {
        watchdog('filedepot', "Deleting file version:@version (@id), @fid from folder @cid, Drupal fid: @dfid", array(
          "@version" => $A['version'],
          "@id" => $A['id'],
          "@fid" => $fid,
          "@cid" => $cid,
          "@dfid" => $A['drupal_fid'],
        ));
        $file = file_load($A['drupal_fid']);
        if ($file) {
          file_usage_delete($file, 'filedepot');
          $file->version = $A['version'];
          file_delete($file);
        }
        db_query("DELETE FROM {filedepot_fileversions} WHERE id=:id", array(
          ':id' => $A['id'],
        ));
      }
      db_query("DELETE FROM {filedepot_files} WHERE fid=:fid", array(
        ':fid' => $fid,
      ));
      db_query("DELETE FROM {filedepot_notifications} WHERE fid=:fid", array(
        ':fid' => $fid,
      ));
      return TRUE;
    }
    else {
      watchdog('filedepot', 'Unable to delete file. User: @user, file: @fid and Folder: @folder', array(
        '@user' => $user->uid,
        '@fid' => $fid,
        '@folder' => $cid,
      ));
      $GLOBALS['alertMsg'] = 'No permission to remove selected file(s)';
      return FALSE;
    }
  }
  else {
    return FALSE;
  }
}