You are here

public function filedepot::deleteFile in filedepot 7

Same name and namespace in other branches
  1. 6 filedepot.class.php \filedepot::deleteFile()

File

./filedepot.class.php, line 1139
filedepot.class.php Main class for the Filedepot module

Class

filedepot
@file filedepot.class.php Main class for the Filedepot module

Code

public function deleteFile($fid, &$out_pid = NULL) {
  $query = db_query("SELECT cid,drupal_fid FROM {filedepot_files} WHERE fid=:fid", array(
    ':fid' => $fid,
  ));
  list($cid, $dfid) = array_values($query
    ->fetchAssoc());
  if ($out_pid !== NULL) {
    $out_pid = $cid;
  }
  if ($this
    ->checkPermission($cid, 'admin')) {
    $file = file_load($dfid);

    // Drupal is not updating the node when the file (attachment) is deleted
    // Need to cycle thru the attachments and remove it then save the node
    $nid = db_query("SELECT nid FROM {filedepot_categories} WHERE cid=:cid", array(
      'cid' => $cid,
    ))
      ->fetchField();
    $foldernode = node_load($nid);
    foreach ($foldernode->filedepot_folder_file[LANGUAGE_NONE] as $delta => $attachment) {
      if ($attachment['fid'] == $dfid) {
        unset($foldernode->filedepot_folder_file[LANGUAGE_NONE][$delta]);
        node_save($foldernode);
        break;
      }
    }
    if ($file) {
      file_usage_delete($file, 'filedepot');
      file_delete($file);
    }
    return TRUE;
  }
  else {
    return FALSE;
  }
}