You are here

public function WebformResultsExportController::index in Webform 6.x

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

Returns webform 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 'WebformResultsExportController::index'
webform.routing.yml in ./webform.routing.yml
webform.routing.yml
webform_node.routing.yml in modules/webform_node/webform_node.routing.yml
modules/webform_node/webform_node.routing.yml

File

src/Controller/WebformResultsExportController.php, line 63

Class

WebformResultsExportController
Controller routines for webform submission export.

Namespace

Drupal\webform\Controller

Code

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

    // Redirect to file export.
    $file_path = $this->submissionExporter
      ->getFileTempDirectory() . '/' . $query['filename'];
    if (file_exists($file_path)) {
      $route_name = $this->requestHandler
        ->getRouteName($webform, $source_entity, 'webform.results_export_file');
      $route_parameters = $this->requestHandler
        ->getRouteParameters($webform, $source_entity) + [
        'filename' => $query['filename'],
      ];
      $file_url = Url::fromRoute($route_name, $route_parameters, [
        'absolute' => TRUE,
      ])
        ->toString();
      $this
        ->messenger()
        ->addStatus($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,
          ],
        ],
        'webform_results_export_download_file_refresh',
      ];
    }
    return $build;
  }
  elseif ($query && empty($query['ajax_form']) && isset($query['download'])) {
    $default_options = $this->submissionExporter
      ->getDefaultExportOptions();
    foreach ($query as $key => $value) {
      if (isset($default_options[$key]) && is_array($default_options[$key]) && is_string($value)) {
        $query[$key] = explode(',', $value);
      }
    }
    if (!empty($query['excluded_columns'])) {
      $query['excluded_columns'] = array_combine($query['excluded_columns'], $query['excluded_columns']);
    }
    $export_options = $query + $default_options;
    $this->submissionExporter
      ->setExporter($export_options);
    if ($this->submissionExporter
      ->isBatch()) {
      static::batchSet($webform, $source_entity, $export_options);
      return batch_process($this->requestHandler
        ->getUrl($webform, $source_entity, 'webform.results_export'));
    }
    else {
      $this->submissionExporter
        ->generate();
      $file_path = $this->submissionExporter
        ->getExportFilePath();
      return $this
        ->downloadFile($file_path, $export_options['download']);
    }
  }
  else {
    return $this
      ->formBuilder()
      ->getForm('Drupal\\webform\\Form\\WebformResultsExportForm', $webform);
  }
}