You are here

public function WebformSubmissionExportImportUploadForm::submitUploadForm in Webform 6.x

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

Upload submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

File

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

Class

WebformSubmissionExportImportUploadForm
Upload webform submission export import CSV.

Namespace

Drupal\webform_submission_export_import\Form

Code

public function submitUploadForm(array &$form, FormStateInterface $form_state) {
  $validators = [
    'file_validate_extensions' => [
      'csv',
    ],
  ];
  $import_type = $form_state
    ->getValue('import_type');
  $file = NULL;
  switch ($import_type) {
    case 'file':
      $files = file_save_upload('import_file', $validators);
      $file = $files ? reset($files) : NULL;
      break;
    case 'url':
      $import_url = $form_state
        ->getValue('import_url');
      $file_path = tempnam($this->fileSystem
        ->getTempDirectory(), 'webform_submission_export_import_') . '.csv';
      file_put_contents($file_path, file_get_contents($import_url));
      $form_field_name = $this
        ->t('Submission CSV (Comma Separated Values) file');
      $file_size = filesize($file_path);

      // Mimic Symfony and Drupal's upload file handling.
      $file_info = new UploadedFile($file_path, basename($file_path), NULL, $file_size);
      $file = _webform_submission_export_import_file_save_upload_single($file_info, $form_field_name, $validators);
      break;
  }

  // If a managed file has been create to the file's id and rebuild the form.
  if ($file) {
    $this->importer
      ->setImportUri($file
      ->getFileUri());
    if ($this->importer
      ->getTotal() > 0) {
      $form_state
        ->set('import_fid', $file
        ->id());
      $form_state
        ->setRebuild();
    }
    else {
      $this
        ->messenger()
        ->addError($this
        ->t("Uable to parse CSV file. Please review the CSV file's formatting."));
    }
  }
}