You are here

public static function WebformSubmissionExportImportUploadForm::batchProcess in Webform 8.5

Same name and namespace in other branches
  1. 6.x modules/webform_submission_export_import/src/Form/WebformSubmissionExportImportUploadForm.php \Drupal\webform_submission_export_import\Form\WebformSubmissionExportImportUploadForm::batchProcess()

Batch API callback; Write the header and rows of the export to the export file.

Parameters

\Drupal\webform\WebformInterface $webform: The webform.

\Drupal\Core\Entity\EntityInterface|null $source_entity: A webform source entity.

string $import_uri: The URI of the CSV import file.

array $import_options: An associative array of import options.

mixed|array $context: The batch current context.

File

modules/webform_submission_export_import/src/Form/WebformSubmissionExportImportUploadForm.php, line 579

Class

WebformSubmissionExportImportUploadForm
Upload webform submission export import CSV.

Namespace

Drupal\webform_submission_export_import\Form

Code

public static function batchProcess(WebformInterface $webform, EntityInterface $source_entity = NULL, $import_uri = '', array $import_options = [], &$context) {

  /** @var \Drupal\webform_submission_export_import\WebformSubmissionExportImportImporterInterface $importer */
  $importer = \Drupal::service('webform_submission_export_import.importer');
  $importer
    ->setWebform($webform);
  $importer
    ->setSourceEntity($source_entity);
  $importer
    ->setImportUri($import_uri);
  $importer
    ->setImportOptions($import_options);
  if (empty($context['sandbox'])) {
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['offset'] = 0;
    $context['sandbox']['max'] = $importer
      ->getTotal();

    // Drush is losing the results so we are going to track theme
    // via the sandbox.
    $context['sandbox']['stats'] = [
      'created' => 0,
      'updated' => 0,
      'skipped' => 0,
      'total' => 0,
      'warnings' => [],
      'errors' => [],
    ];
    $context['results'] = [
      'import_uri' => $import_uri,
    ];
  }

  // Import CSV records.
  $import_stats = $importer
    ->import($context['sandbox']['offset'], $importer
    ->getBatchLimit());

  // Append import stats and errors to results.
  foreach ($import_stats as $import_stat => $value) {
    if (is_array($value)) {

      // Convert translatable markup into strings to save memory.
      $context['sandbox']['stats'][$import_stat] += WebformOptionsHelper::convertOptionsToString($value);
    }
    else {
      $context['sandbox']['stats'][$import_stat] += $value;
    }
  }

  // Track progress.
  $context['sandbox']['progress'] += $import_stats['total'];
  $context['sandbox']['offset'] += $importer
    ->getBatchLimit();

  // Display message.
  $context['message'] = t('Imported @count of @total submissions…', [
    '@count' => $context['sandbox']['progress'],
    '@total' => $context['sandbox']['max'],
  ]);

  // Track finished.
  if ($context['sandbox']['progress'] !== $context['sandbox']['max']) {
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  }

  // Context results are not being passed to batchFinish via Drush,
  // therefor we are going to show them when this is finished.
  if ($context['finished'] >= 1) {
    static::displayStats($context['sandbox']['stats']);
  }
}