You are here

function fancy_file_delete_unmanaged_update_view in Fancy File Delete 7

Same name and namespace in other branches
  1. 8 fancy_file_delete.module \fancy_file_delete_unmanaged_update_view()

Updates the view and populates the unmanaged files table.

1 call to fancy_file_delete_unmanaged_update_view()
fancy_file_delete_views_pre_execute in ./fancy_file_delete.module
Implements of hook_views_pre_view().

File

./fancy_file_delete.module, line 155

Code

function fancy_file_delete_unmanaged_update_view() {

  // Get all files from default standard public & private directories.
  $directories = fancy_file_delete_unmanaged_get_chosen_dirs();
  $files = fancy_file_delete_unmanaged_get_files($directories);

  // Remove files from the batch that are not in our latest check.
  if (count($files)) {
    db_delete('unmanaged_files')
      ->condition('path', array(
      $files,
    ), 'NOT IN')
      ->execute();
  }
  else {
    db_delete('unmanaged_files')
      ->execute();
  }

  // Go through and add this to the batch.
  if (count($files) > 0) {

    // I changed this to use array chunk & db_query for performance
    // see issue: https://www.drupal.org/node/2637028
    $files_chunk = array_chunk($files, ceil(count($files) / 4), TRUE);
    foreach ($files_chunk as $filez) {
      $query = "SELECT path FROM {unmanaged_files} WHERE path IN (:files)";
      $result = db_query($query, array(
        ':files' => $filez,
      ))
        ->fetchAll();

      // Check if this is a first run.
      if (count($result) == 0) {
        $new = TRUE;
      }
      else {
        $umsplit[] = $result;
        $new = FALSE;
      }
    }

    // Insert if new.
    if ($new) {
      foreach ($files_chunk as $chunk) {
        foreach ($chunk as $fpath) {
          $new_files[] = $fpath;
        }
      }
      if (isset($new_files)) {
        $insert_unmanaged = db_insert('unmanaged_files')
          ->fields(array(
          'path',
        ));

        // Insert records
        foreach ($new_files as $key => $value) {
          $insert_unmanaged
            ->values(array(
            'path' => $value,
          ));
        }
        $insert_unmanaged
          ->execute();
      }
    }
    else {
      $um = array();
      foreach ($umsplit as $trill) {
        $um = array_merge($um, $trill);
      }

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

      // Again check the difference, only want ones not in the table.
      $um_final = array_diff($files, $um_check);
      if (count($um_final) > 0) {
        $insert_unmanaged = db_insert('unmanaged_files')
          ->fields(array(
          'path',
        ));

        // Insert records
        foreach ($um_final as $key => $value) {
          $insert_unmanaged
            ->values(array(
            'path' => $value,
          ));
        }
        $insert_unmanaged
          ->execute();
      }
    }
  }
}