You are here

fancy_file_delete.module in Fancy File Delete 8

Same filename and directory in other branches
  1. 7 fancy_file_delete.module
  2. 2.0.x fancy_file_delete.module

File

fancy_file_delete.module
View source
<?php

/**
 * Implements hook_form_alter().
 */
function fancy_file_delete_form_alter(&$form, \Drupal\Core\Form\FormStateInterface &$form_state, $form_id) {
  if ($form_id == 'fancy_file_delete_manual') {
    $form['actions']['submit']['#value'] = 'Engage';
  }
  if ($form_id == 'views_form_fancy_file_list_unmanaged_unmanaged') {
    $form['update'] = [
      '#type' => 'button',
      '#value' => t('Update View'),
      '#attributes' => [
        'class' => [
          'form-submit ffd-refresh',
        ],
      ],
    ];
  }
}

/**
 * Implements of hook_views_api().
 */
function fancy_file_delete_views_api() {
  return [
    'api' => 3,
    'path' => drupal_get_path('module', 'fancy_file_delete') . '/views',
  ];
}

/**
 * Implements of hook_views_pre_view().
 *
 * Used when we click on the unmanaged tab / Update View to keep it updated.
 */

// Function fancy_file_delete_views_pre_view(&$view, &$display_id, &$args) {
// If ($view->name == 'fancy_file_list_unmanaged') {
// Fancy_file_delete_unmanaged_update_view();
// }
// }

/**
 * Implements hook_entity_info().
 *
 * Add Unmanaged table as entity so we can use it with VBO.
 */

// Function fancy_file_delete_entity_info() {
// $info = array();
// $info['unmanaged_files'] = array(
// 'label' => t('Unmanaged Files'),
// 'base table' => 'unmanaged_files',
// 'entity keys' => array(
// 'id' => 'unfid',
// 'label' => 'path',
// ),
// 'module' => 'unmanaged_files',
// );
// return $info;
// }.

/**
 * Implements hook_action_info().
 */
function fancy_file_delete_action_info() {
  return [
    'fancy_file_delete_files' => [
      'type' => 'entity',
      'label' => t('Delete Files'),
      'configurable' => FALSE,
      'pass rows' => TRUE,
      'permissions' => [
        'administer fancy file delete',
      ],
    ],
    'fancy_file_delete_files_force' => [
      'type' => 'entity',
      'label' => t('FORCE Delete Files (No Turning Back!)'),
      'configurable' => FALSE,
      'pass rows' => TRUE,
      'permissions' => [
        'administer fancy file delete',
      ],
    ],
  ];
}

/**
 * Normal File Delete Action for hook_action_info.
 */
function fancy_file_delete_files(&$entity, $context) {

  // Set entities to batch our way.
  $operations = [];
  foreach ($entity as $key => $value) {
    if ($key == 'fid' || $key == 'path') {
      $operations[] = [
        'fancy_file_delete_batch',
        [
          $value,
          FALSE,
        ],
      ];
    }
  }

  // Send to batch.
  _fancy_file_delete_batch_run($operations);
}

/**
 * Force File Delete Action for hook_action_info.
 */
function fancy_file_delete_files_force(&$entity, $context) {

  // Set entities to batch our way.
  $operations = [];
  foreach ($entity as $key => $value) {
    if ($key == 'fid') {
      $operations[] = [
        'fancy_file_delete_batch',
        [
          $value,
          TRUE,
        ],
      ];
    }
  }

  // Send to batch.
  _fancy_file_delete_batch_run($operations);
}

/**
 * Updates the view and populates the unmanaged files table.
 */
function fancy_file_delete_unmanaged_update_view() {

  // Get all files from default standard file dir.
  $dir = \Drupal::state()
    ->get('file_public_path') ?: 'sites/default/files';
  $files = file_scan_directory($dir, '(.*?)');

  // Go through each one and replace this with a proper uri.
  foreach ($files as $file) {
    $file_check[] = str_replace($dir . '/', 'public://', $file->uri);
  }

  // All the files in the file_managed table.
  $query = \Drupal::database()
    ->select('file_managed', 'fm')
    ->fields('fm', [
    'uri',
  ])
    ->execute();

  // Set this to a numeric keyed array so we can check this easier.
  foreach ($query
    ->fetchAll() as $result) {
    $db_check[] = $result->uri;
  }

  // Get the files not in the file_managed table.
  $results = array_diff($file_check, $db_check);

  // Go through and add this to the batch.
  if (count($results) > 0) {
    $um = \Drupal::database()
      ->select('unmanaged_files', 'um')
      ->fields('um', [
      'path',
    ])
      ->condition('path', [
      $results,
    ], 'IN')
      ->execute()
      ->fetchAll();

    // Go in and check it and set it as an array to check.
    $um_check = [];
    foreach ($um as $res) {
      $um_check[] = $res->path;
    }

    // Again check the difference, only want ones not in the table.
    $um_final = array_diff($results, $um_check);
    if (count($um_final) > 0) {
      $insert_unmanaged = \Drupal::database()
        ->insert('unmanaged_files')
        ->fields([
        'path',
      ]);
      foreach ($um_final as $key => $value) {
        $insert_unmanaged
          ->values([
          'path' => $value,
        ]);
      }
      $insert_unmanaged
        ->execute();
    }
  }
}

/**
 * The batch operations to run for either function.
 *
 * @param object $operations
 *   The operations array from each submit handler.
 */
function _fancy_file_delete_batch_run($operations) {
  $batch = [
    'title' => t('Fun Stuff is Happening...'),
    'operations' => $operations,
    'finished' => 'fancy_file_delete_batch_finished',
    'file' => drupal_get_path('module', 'fancy_file_delete') . '/fancy_file_delete.batch.inc',
  ];
  batch_set($batch);
}

Functions

Namesort descending Description
fancy_file_delete_action_info Implements hook_action_info().
fancy_file_delete_files Normal File Delete Action for hook_action_info.
fancy_file_delete_files_force Force File Delete Action for hook_action_info.
fancy_file_delete_form_alter Implements hook_form_alter().
fancy_file_delete_unmanaged_update_view Updates the view and populates the unmanaged files table.
fancy_file_delete_views_api Implements of hook_views_api().
_fancy_file_delete_batch_run The batch operations to run for either function.