You are here

function media_bulk_upload_import_batch_import_files in D7 Media 7.4

Same name and namespace in other branches
  1. 7.2 modules/media_bulk_upload/includes/media_bulk_upload.admin.inc \media_bulk_upload_import_batch_import_files()
  2. 7.3 modules/media_bulk_upload/includes/media_bulk_upload.admin.inc \media_bulk_upload_import_batch_import_files()

BatchAPI callback op for media import.

1 string reference to 'media_bulk_upload_import_batch_import_files'
media_bulk_upload_import_submit in modules/media_bulk_upload/includes/media_bulk_upload.admin.inc
Submit handler for media_import().

File

modules/media_bulk_upload/includes/media_bulk_upload.admin.inc, line 123
This file contains the admin functions for the Media Bulk Upload module.

Code

function media_bulk_upload_import_batch_import_files($files, $params, &$context) {
  if (!isset($context['sandbox']['files'])) {

    // This runs the first time the batch runs.
    // This is stupid, but otherwise, I don't think it will work...
    $context['results'] = array(
      'success' => array(),
      'errors' => array(),
    );
    $context['sandbox']['max'] = count($files);
    $context['sandbox']['files'] = $files;
  }
  $files =& $context['sandbox']['files'];

  // Take a cut of files.  Let's do 10 at a time.
  $import_batch_size = variable_get('media_bulk_upload_import_batch_size', 20);
  $length = count($files) > $import_batch_size ? $import_batch_size : count($files);
  $to_process = array_splice($files, 0, $length);
  $image_in_message = '';
  foreach ($to_process as $file) {
    try {
      $file_obj = media_parse_to_file($file, $params);
      $context['results']['success'][] = $file;
      if (!$image_in_message) {

        // @todo Is this load step really necessary? When there's time, test
        //   this, and either remove it, or comment why it's needed.
        $loaded_file = file_load($file_obj->fid);
        $image_in_message = file_view_file($loaded_file, 'preview');
      }
    } catch (Exception $e) {
      $context['results']['errors'][] = $file . " Reason: " . $e
        ->getMessage();
    }
  }
  $context['message'] = "Importing " . theme('item_list', array(
    'items' => $to_process,
  ));

  // Show the image that is being imported.
  $context['message'] .= drupal_render($image_in_message);
  $context['finished'] = ($context['sandbox']['max'] - count($files)) / $context['sandbox']['max'];
}