You are here

function fancy_file_delete_batch_delete in Fancy File Delete 7

Batch function.

4 string references to 'fancy_file_delete_batch_delete'
fancy_file_delete_drush_batch_delete_process in ./fancy_file_delete.drush.inc
Run fancy file delete.
fancy_file_delete_files in ./fancy_file_delete.module
Normal File Delete Action for hook_action_info.
fancy_file_delete_files_force in ./fancy_file_delete.module
Force File Delete Action for hook_action_info.
fancy_file_delete_manual_submit in includes/fancy_file_delete.admin.inc
Submit handler for manual form.

File

includes/fancy_file_delete.batch_delete.inc, line 12
Batch function.

Code

function fancy_file_delete_batch_delete($fid, $force, &$context) {

  // Update our progress information.
  if (!isset($context['sandbox']['progress'])) {
    $context['sandbox']['progress'] = 0;
  }
  $context['sandbox']['progress']++;

  // Remove line endings to prevent fail and convert to integer.
  // @see https://www.drupal.org/node/2719619 for related issue.
  $fid = str_replace(array(
    "\r",
    "\n",
  ), '', $fid);

  // Manual / Orphan Delete.
  if ((int) $fid) {
    $fid = (int) $fid;
    $file = file_load($fid);
    if ($file) {
      if ($force) {
        db_delete('file_managed')
          ->condition('fid', $file->fid)
          ->execute();
        db_delete('file_usage')
          ->condition('fid', $file->fid)
          ->execute();
        file_unmanaged_delete($file->uri);
      }
      else {
        $result = file_delete($file);
        if (is_array($result)) {

          // The file is still being referenced and not set to be forcefully deleted.
          // Notify the user instead.
          $context['results']['error'][] = array(
            'fid' => $fid,
            'message' => t('The file with fid#%fid cannot be delete because it
            is still referenced in the file_usage table. %file_usage', array(
              '%fid' => $fid,
              '%file_usage' => print_r($result, TRUE),
            )),
          );
        }
        else {
          $context['results'][] = $fid;
        }
      }
    }
  }
  else {
    db_delete('unmanaged_files')
      ->condition('path', $fid)
      ->execute();
    file_unmanaged_delete($fid);
    $context['results'][] = $fid;
  }

  // Set the processing message.
  $context['message'] = t('Now cleansing the system of fid#%fid', array(
    '%fid' => $fid,
  ));
}