You are here

public function WebformSubmissionExportImportImporter::import in Webform 6.x

Same name and namespace in other branches
  1. 8.5 modules/webform_submission_export_import/src/WebformSubmissionExportImportImporter.php \Drupal\webform_submission_export_import\WebformSubmissionExportImportImporter::import()

Import records from CSV import file.

Parameters

int $offset: Line to be begin importing from.

int|null $limit: The number of records to be imported.

Return value

array An associate array containing imports states including total, created, updated, skipped, and errors.

Overrides WebformSubmissionExportImportImporterInterface::import

File

modules/webform_submission_export_import/src/WebformSubmissionExportImportImporter.php, line 375

Class

WebformSubmissionExportImportImporter
Webform submission export importer.

Namespace

Drupal\webform_submission_export_import

Code

public function import($offset = 0, $limit = NULL) {
  if ($limit === NULL) {
    $limit = $this
      ->getBatchLimit();
  }
  $import_options = $this
    ->getImportOptions();

  // Open CSV file.
  $handle = fopen($this
    ->getImportUri(), 'r');

  // Get the column names.
  $column_names = fgetcsv($handle);
  foreach ($column_names as $index => $name) {
    $column_names[$index] = $name;
  }

  // Fast forward CSV file to offset.
  $index = 0;
  while ($index < $offset && !feof($handle)) {
    fgets($handle);
    $index++;
  }

  // Collect import stats.
  $stats = [
    'created' => 0,
    'updated' => 0,
    'skipped' => 0,
    'total' => 0,
    'warnings' => [],
    'errors' => [],
  ];

  // Import submission records.
  while ($stats['total'] < $limit && !feof($handle)) {

    // Get CSV values.
    $values = fgetcsv($handle);

    // Complete ignored empty rows.
    if (empty($values) || $values === [
      '',
    ]) {
      continue;
    }
    $index++;
    $stats['total']++;

    // Track row specific warnings and errors.
    $stats['warnings'][$index] = [];
    $stats['errors'][$index] = [];
    $row_warnings =& $stats['warnings'][$index];
    $row_errors =& $stats['errors'][$index];

    // Make sure expected number of columns and values are equal.
    if (count($column_names) !== count($values)) {
      $t_args = [
        '@expected' => count($column_names),
        '@found' => count($values),
      ];
      $error = $this
        ->t('@expected values expected and only @found found.', $t_args);
      if (!empty($import_options['treat_warnings_as_errors'])) {
        $row_errors[] = $error;
      }
      else {
        $row_warnings[] = $error;
      }
      continue;
    }

    // Create record and trim all values.
    $record = array_combine($column_names, $values);
    foreach ($record as $key => $value) {
      $record[$key] = trim($value);
    }

    // Track original record.
    $original_record = $record;

    // Map.
    $record = $this
      ->importMapRecord($record);

    // Token: Generate token from the original CSV record.
    if (empty($record['token'])) {
      $record['token'] = Crypt::hashBase64(Settings::getHashSalt() . serialize($original_record));
    }

    // Prepare.
    $webform_submission = $this
      ->importLoadSubmission($record);
    if ($errors = $this
      ->importPrepareRecord($record, $webform_submission)) {
      if (!empty($import_options['treat_warnings_as_errors'])) {
        $row_errors = array_merge($row_warnings, array_values($errors));
      }
      else {
        $row_warnings = array_merge($row_warnings, array_values($errors));
      }
    }

    // Validate.
    if (empty($import_options['skip_validation'])) {
      if ($errors = $this
        ->importValidateRecord($record)) {
        $row_errors = array_merge($row_errors, array_values($errors));
      }
    }

    // Skip import if there are row errors.
    if ($row_errors) {
      $stats['skipped']++;
      continue;
    }

    // Save.
    $this
      ->importSaveSubmission($record, $webform_submission);
    $stats[$webform_submission ? 'updated' : 'created']++;
  }
  fclose($handle);
  return $stats;
}