You are here

public static function WebformResultsExportController::batchFinish in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Controller/WebformResultsExportController.php \Drupal\webform\Controller\WebformResultsExportController::batchFinish()

Batch API callback; Completed export.

Parameters

bool $success: TRUE if batch successfully completed.

array $results: Batch results.

array $operations: An array of function calls (not used in this function).

Return value

\Symfony\Component\HttpFoundation\RedirectResponse Redirect to download the exported results.

File

src/Controller/WebformResultsExportController.php, line 318

Class

WebformResultsExportController
Controller routines for webform submission export.

Namespace

Drupal\webform\Controller

Code

public static function batchFinish($success, array $results, array $operations) {
  $webform_id = $results['webform_id'];
  $entity_type = $results['source_entity_type'];
  $entity_id = $results['source_entity_id'];

  /** @var \Drupal\webform\WebformInterface $webform */
  $webform = Webform::load($webform_id);

  /** @var \Drupal\Core\Entity\EntityInterface|null $source_entity */
  $source_entity = $entity_type && $entity_id ? \Drupal::entityTypeManager()
    ->getStorage($entity_type)
    ->load($entity_id) : NULL;

  /** @var array $export_options */
  $export_options = $results['export_options'];

  /** @var \Drupal\webform\WebformSubmissionExporterInterface $submission_exporter */
  $submission_exporter = \Drupal::service('webform_submission.exporter');
  $submission_exporter
    ->setWebform($webform);
  $submission_exporter
    ->setSourceEntity($source_entity);
  $submission_exporter
    ->setExporter($export_options);
  if (!$success) {
    $file_path = $submission_exporter
      ->getExportFilePath();
    @unlink($file_path);
    $archive_path = $submission_exporter
      ->getArchiveFilePath();
    @unlink($archive_path);
    \Drupal::messenger()
      ->addStatus(t('Finished with an error.'));
  }
  else {
    $submission_exporter
      ->writeFooter();
    $filename = $submission_exporter
      ->getExportFileName();
    if ($submission_exporter
      ->isArchive()) {
      $submission_exporter
        ->writeExportToArchive();
      $filename = $submission_exporter
        ->getArchiveFileName();
    }

    /** @var \Drupal\webform\WebformRequestInterface $request_handler */
    $request_handler = \Drupal::service('webform.request');
    $redirect_url = $request_handler
      ->getUrl($webform, $source_entity, 'webform.results_export', [
      'query' => [
        'filename' => $filename,
      ],
      'absolute' => TRUE,
    ]);
    return new RedirectResponse($redirect_url
      ->toString());
  }
}