You are here

public function YamlFormResultsExportController::index in YAML Form 8

Returns form submission as a CSV.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The current request.

Return value

array|null|\Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response A response that renders or redirects to the CSV file.

2 string references to 'YamlFormResultsExportController::index'
yamlform.routing.yml in ./yamlform.routing.yml
yamlform.routing.yml
yamlform_node.routing.yml in modules/yamlform_node/yamlform_node.routing.yml
modules/yamlform_node/yamlform_node.routing.yml

File

src/Controller/YamlFormResultsExportController.php, line 82

Class

YamlFormResultsExportController
Controller routines for form submission export.

Namespace

Drupal\yamlform\Controller

Code

public function index(Request $request) {
  list($yamlform, $source_entity) = $this->requestHandler
    ->getYamlFormEntities();
  $this->submissionExporter
    ->setYamlForm($yamlform);
  $this->submissionExporter
    ->setSourceEntity($source_entity);
  $query = $request->query
    ->all();
  unset($query['destination']);
  if (isset($query['filename'])) {
    $build = $this
      ->formBuilder()
      ->getForm('Drupal\\yamlform\\Form\\YamlFormResultsExportForm');

    // Redirect to file export.
    $file_path = $this->submissionExporter
      ->getFileTempDirectory() . '/' . $query['filename'];
    if (file_exists($file_path)) {
      $route_name = $this->requestHandler
        ->getRouteName($yamlform, $source_entity, 'yamlform.results_export_file');
      $route_parameters = $this->requestHandler
        ->getRouteParameters($yamlform, $source_entity) + [
        'filename' => $query['filename'],
      ];
      $file_url = Url::fromRoute($route_name, $route_parameters, [
        'absolute' => TRUE,
      ])
        ->toString();
      drupal_set_message($this
        ->t('Export creation complete. Your download should begin now. If it does not start, <a href=":href">download the file here</a>. This file may only be downloaded once.', [
        ':href' => $file_url,
      ]));
      $build['#attached']['html_head'][] = [
        [
          '#tag' => 'meta',
          '#attributes' => [
            'http-equiv' => 'refresh',
            'content' => '0; url=' . $file_url,
          ],
        ],
        'yamlform_results_export_download_file_refresh',
      ];
    }
    return $build;
  }
  elseif ($query && empty($query['ajax_form'])) {
    if (!empty($query['excluded_columns']) && is_string($query['excluded_columns'])) {
      $excluded_columns = explode(',', $query['excluded_columns']);
      $query['excluded_columns'] = array_combine($excluded_columns, $excluded_columns);
    }
    $export_options = $query + $this->submissionExporter
      ->getDefaultExportOptions();
    $this->submissionExporter
      ->setExporter($export_options);
    if ($this->submissionExporter
      ->isBatch()) {
      self::batchSet($yamlform, $source_entity, $export_options);
      $route_name = $this->requestHandler
        ->getRouteName($yamlform, $source_entity, 'yamlform.results_export');
      $route_parameters = $this->requestHandler
        ->getRouteParameters($yamlform, $source_entity);
      return batch_process(Url::fromRoute($route_name, $route_parameters));
    }
    else {
      $this->submissionExporter
        ->generate();
      $file_path = $this->submissionExporter
        ->getExportFilePath();
      return $this
        ->downloadFile($file_path, $export_options['download']);
    }
  }
  else {
    return $this
      ->formBuilder()
      ->getForm('Drupal\\yamlform\\Form\\YamlFormResultsExportForm', $yamlform);
  }
}