You are here

function media_browser_plus_folder_media_import in Media Browser Plus 7

@todo Document what this function does.

Parameters

$files:

$context:

1 string reference to 'media_browser_plus_folder_media_import'
media_browser_plus_enable in ./media_browser_plus.install
Implements hook_enable().

File

includes/media_browser_plus.admin.inc, line 14
This file contains the admin functions for the Media browser plus module.

Code

function media_browser_plus_folder_media_import($root, $dir, &$context) {
  $per_page = 50;
  $media_count = media_browser_plus_load_multiple(array(
    'apply_filter' => FALSE,
    'count_only' => TRUE,
  ));
  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 : bcdiv($media_start, $per_page, 0);
  if (!isset($context['results'])) {
    $context['results'] = array(
      'success' => array(),
      'errors' => array(),
    );
  }
  $order = array(
    array(
      'property' => array(
        'fid',
        'ASC',
      ),
    ),
  );
  $attributes = array(
    'apply_filter' => FALSE,
    'paging' => TRUE,
    'per_page' => $per_page,
    'page' => $page,
    'order' => $order,
  );
  $media_query = media_browser_plus_load_multiple($attributes);

  // Checking media.
  foreach ($media_query->results as $media) {
    if (!isset($media->field_folder[LANGUAGE_NONE])) {
      $media->field_folder = array(
        LANGUAGE_NONE => array(
          array(
            'tid' => $root,
          ),
        ),
      );
      $context['results']['success'] = $media->fid;
      $destination = $dir . $media->filename;
      file_move($media, $destination, FILE_EXISTS_RENAME);
    }
  }

  // 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;

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