You are here

function media_browser_plus_folder_update_file_locations_batch in Media Browser Plus 7.3

Same name and namespace in other branches
  1. 7 includes/media_browser_plus.folders.inc \media_browser_plus_folder_update_file_locations_batch()
  2. 7.2 includes/media_browser_plus.folders.inc \media_browser_plus_folder_update_file_locations_batch()

Batch function to move files to the location according the assigned folder.

Adjust the file uri to match the path given by the assigned folder. If the uri is adjusted hook_file_move() is invoked. If the file isn't located in the related directory on the disk it's moved. The legacy path of the file is logged in $context['handled_directories'].

Parameters

array $folders: A list of folder ids (term ids).

array $context: Batch context array.

1 call to media_browser_plus_folder_update_file_locations_batch()
media_browser_plus_rebuild_folder_structure_process in includes/media_browser_plus.folders.inc
Batch process of folder rebuild moves files and delete leftover directories.
1 string reference to 'media_browser_plus_folder_update_file_locations_batch'
media_browser_plus_move_subfolder in includes/media_browser_plus.folders.inc
Helper function to move a subfolder - and update the related files.

File

includes/media_browser_plus.folders.inc, line 135
Folder manipulation functions.

Code

function media_browser_plus_folder_update_file_locations_batch($folders, &$context) {
  $step_size = 25;
  $file_query = new EntityFieldQuery();
  $file_query
    ->entityCondition('entity_type', 'file');
  if (!empty($folders)) {
    $file_query
      ->fieldCondition('field_folder', 'tid', $folders, 'IN');
  }
  if (empty($context['sandbox'])) {
    $context['sandbox']['progress'] = 0;
    $files = $file_query
      ->execute();
    $context['sandbox']['max'] = !empty($files['file']) ? count($files['file']) : 0;
    $context['local_stream_wrappers'] = media_get_local_stream_wrappers();
  }
  if (!isset($context['results'])) {
    $context['results'] = array(
      'success' => array(),
      'errors' => array(),
    );
  }
  $file_query
    ->range($context['sandbox']['progress'], $step_size);
  $files = $file_query
    ->execute();
  $file_entities = array();
  if (!empty($files['file'])) {
    $file_entities = file_load_multiple(array_keys($files['file']));
  }

  // Checking media.
  foreach ($file_entities as $file) {

    // Only handle locale files with a folder id.
    if (isset($context['local_stream_wrappers'][file_uri_scheme($file->uri)]) && isset($file->field_folder[LANGUAGE_NONE][0]['tid'])) {

      // Update file path.
      $source = clone $file;
      $path = media_browser_plus_construct_dir_path(taxonomy_term_load($file->field_folder[LANGUAGE_NONE][0]['tid']));
      $file->uri = file_stream_wrapper_uri_normalize(file_uri_scheme($file->uri) . '://' . $path . '/' . drupal_basename($file->uri));

      // Check if the uri has changed.
      if ($file->uri !== $source->uri) {

        // Check if the source file still exists, if so move the file.
        clearstatcache();
        if (file_exists(drupal_realpath($source->uri))) {
          $context['handled_directories'][drupal_dirname($source->uri)] = drupal_dirname($source->uri);
          $destination = drupal_dirname($file->uri);
          file_prepare_directory($destination, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
          file_unmanaged_move($source->uri, $destination, FILE_EXISTS_RENAME);
        }

        // Save all the changes and inform other modules.
        file_save($file);

        // Inform modules that the file has been moved.
        module_invoke_all('file_move', $file, $source);
      }
    }
  }

  // Increment progress but make sure start is not above max (for progress).
  $context['sandbox']['progress'] = min($context['sandbox']['max'], $context['sandbox']['progress'] + $step_size);

  // Set other context values.
  $context['message'] = t('Relocating files') . '...(' . $context['sandbox']['progress'] . '/' . $context['sandbox']['max'] . ') ';
  $context['finished'] = 1;
  if ($context['sandbox']['progress'] < $context['sandbox']['max']) {
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  }
}