You are here

function media_browser_plus_media_import_batch_import_files in Media Browser Plus 7

Same name and namespace in other branches
  1. 7.2 media_browser_plus.module \media_browser_plus_media_import_batch_import_files()

Batch process that only differs in the ability to apply the field values to the items

Parameters

$files:

$form_values:

$context:

1 string reference to 'media_browser_plus_media_import_batch_import_files'
media_browser_plus_media_import_submit in ./media_browser_plus.module
Changing the media import standard submit to use our own batch process.

File

./media_browser_plus.module, line 464
Adds fields to the media browser forms for better UX

Code

function media_browser_plus_media_import_batch_import_files($files, $form_values, &$context) {

  // Split up attributes.
  // list($files, $form_values) = $attributes;
  // Need to repeat a lot of code here just to add the fields :-(
  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.
  $length = media_variable_get('import_batch_size', 0);
  if ($length > count($files)) {
    $length = 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);
      $context['results']['success'][] = $file;
      if (!$image_in_message) {
        $media = file_load($file_obj->fid);
        $image_in_message = field_view_field('file', $media, 'file', 'media_preview');
      }

      // adding fields here
      $media = file_load($file_obj->fid);
      $media->field_folder[LANGUAGE_NONE][0] = array(
        'tid' => $form_values['field_folder'],
      );
      $media->field_tags[LANGUAGE_NONE] = $form_values['field_tags'];
      media_browser_plus_move_file($form_values['field_folder'], $media);
    } catch (Exception $e) {
      $context['results']['errors'][] = $file . ' Reason: ' . $e
        ->getMessage();
    }
  }
  $context['message'] = t('Importing') . theme('item_list', array(
    'items' => $to_process,
  ));

  // Just for kicks, show an image we are importing.
  $context['message'] .= drupal_render($image_in_message);
  $context['finished'] = ($context['sandbox']['max'] - count($files)) / $context['sandbox']['max'];
}