You are here

function media_browser_plus_folder_update_file_locations_batch in Media Browser Plus 7.2

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

Batch function that updates all media URIs inside the given folders.

Parameters

$folders:

$context:

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 564
Folder manipulation functions

Code

function media_browser_plus_folder_update_file_locations_batch($folders, &$context) {
  $per_page = 25;
  $conditions = array();
  $conditions[] = array(
    'field' => array(
      'field_folder',
      'tid',
      $folders,
      'IN',
    ),
  );
  $options = array(
    'apply_filter' => FALSE,
    'count_only' => TRUE,
    'conditions' => $conditions,
  );
  $media_count = media_browser_plus_load_multiple($options);
  if (empty($context['sandbox'])) {
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['max'] = $media_count;
  }

  // Look how far we got and where we have to resume.
  $media_start = $context['sandbox']['progress'];
  $page = $media_start == 0 ? 0 : round($media_start / $per_page, 0);
  if (!isset($context['results'])) {
    $context['results'] = array(
      'success' => array(),
      'errors' => array(),
    );
  }
  $attributes = array(
    'apply_filter' => FALSE,
    'paging' => TRUE,
    'per_page' => $per_page,
    'page' => $page,
    'conditions' => $conditions,
  );
  $media_query = media_browser_plus_load_multiple($attributes);

  // Checking media.
  foreach ($media_query->results as $media) {
    if (isset($media->field_folder[LANGUAGE_NONE][0]['tid'])) {
      $source = clone $media;
      $path = media_browser_plus_construct_dir_path(taxonomy_term_load($media->field_folder[LANGUAGE_NONE][0]['tid']));
      $media->uri = $path . '/' . basename($media->uri);
      file_save($media);

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

  // Increment start.
  $media_start = $media_start + $per_page;

  // Make sure start is not above max (for progress).
  $media_start = $media_start > $media_count ? $media_count : $media_start;

  // Set sandbox value.
  $context['sandbox']['max'] = $media_count;
  $context['sandbox']['progress'] = $media_start;

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