You are here

public static function YamlFormResultsExportController::batchFinish in YAML Form 8

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/YamlFormResultsExportController.php, line 324

Class

YamlFormResultsExportController
Controller routines for form submission export.

Namespace

Drupal\yamlform\Controller

Code

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

  /** @var \Drupal\yamlform\YamlFormInterface $yamlform */
  $yamlform = YamlForm::load($yamlform_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\yamlform\YamlFormSubmissionExporterInterface $submission_exporter */
  $submission_exporter = \Drupal::service('yamlform_submission.exporter');
  $submission_exporter
    ->setYamlForm($yamlform);
  $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_set_message(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\yamlform\YamlFormRequestInterface $request_handler */
    $request_handler = \Drupal::service('yamlform.request');
    $route_name = $request_handler
      ->getRouteName($yamlform, $source_entity, 'yamlform.results_export');
    $route_parameters = $request_handler
      ->getRouteParameters($yamlform, $source_entity);
    $redirect_url = Url::fromRoute($route_name, $route_parameters, [
      'query' => [
        'filename' => $filename,
      ],
      'absolute' => TRUE,
    ]);
    return new RedirectResponse($redirect_url
      ->toString());
  }
}