View source
<?php
namespace Drupal\webform\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;
use Drupal\webform\Entity\Webform;
use Drupal\webform\Entity\WebformSubmission;
use Drupal\webform\WebformInterface;
use Drupal\webform\WebformRequestInterface;
use Drupal\webform\WebformSubmissionExporterInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
class WebformResultsExportController extends ControllerBase implements ContainerInjectionInterface {
protected $mimeTypeGuesser;
protected $submissionExporter;
protected $requestHandler;
public function __construct(MimeTypeGuesserInterface $mime_type_guesser, WebformSubmissionExporterInterface $webform_submission_exporter, WebformRequestInterface $request_handler) {
$this->mimeTypeGuesser = $mime_type_guesser;
$this->submissionExporter = $webform_submission_exporter;
$this->requestHandler = $request_handler;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('file.mime_type.guesser'), $container
->get('webform_submission.exporter'), $container
->get('webform.request'));
}
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');
$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);
}
}
public function file(Request $request, $filename) {
list($webform, $source_entity) = $this->requestHandler
->getWebformEntities();
$this->submissionExporter
->setWebform($webform);
$this->submissionExporter
->setSourceEntity($source_entity);
$file_path = $this->submissionExporter
->getFileTempDirectory() . '/' . $filename;
if (!file_exists($file_path)) {
$t_args = [
':href' => $this->requestHandler
->getUrl($webform, $source_entity, 'webform.results_export')
->toString(),
];
$build = [
'#markup' => $this
->t('No export file ready for download. The file may have already been downloaded by your browser. Visit the <a href=":href">download export webform</a> to create a new export.', $t_args),
];
return $build;
}
else {
return $this
->downloadFile($file_path);
}
}
public function downloadFile($file_path, $download = TRUE) {
$headers = [];
if (!$download && preg_match('/\\.csv$/', $file_path)) {
$headers['Content-Type'] = 'text/plain';
}
$response = new BinaryFileResponse($file_path, 200, $headers, FALSE, $download ? 'attachment' : 'inline');
if (!drupal_valid_test_ua()) {
$response
->deleteFileAfterSend(TRUE);
}
return $response;
}
public static function batchSet(WebformInterface $webform, EntityInterface $source_entity = NULL, array $export_options) {
if (!empty($export_options['excluded_columns']) && is_string($export_options['excluded_columns'])) {
$excluded_columns = explode(',', $export_options['excluded_columns']);
$export_options['excluded_columns'] = array_combine($excluded_columns, $excluded_columns);
}
$submission_exporter = \Drupal::service('webform_submission.exporter');
$submission_exporter
->setWebform($webform);
$submission_exporter
->setSourceEntity($source_entity);
$submission_exporter
->setExporter($export_options);
$parameters = [
$webform,
$source_entity,
$export_options,
];
$batch = [
'title' => t('Exporting submissions'),
'init_message' => t('Creating export file'),
'error_message' => t('The export file could not be created because an error occurred.'),
'operations' => [
[
[
'\\Drupal\\webform\\Controller\\WebformResultsExportController',
'batchProcess',
],
$parameters,
],
],
'finished' => [
'\\Drupal\\webform\\Controller\\WebformResultsExportController',
'batchFinish',
],
];
batch_set($batch);
}
public static function batchProcess(WebformInterface $webform, EntityInterface $source_entity = NULL, array $export_options, &$context) {
$submission_exporter = \Drupal::service('webform_submission.exporter');
$submission_exporter
->setWebform($webform);
$submission_exporter
->setSourceEntity($source_entity);
$submission_exporter
->setExporter($export_options);
if (empty($context['sandbox'])) {
$context['sandbox']['progress'] = 0;
$context['sandbox']['offset'] = 0;
$context['sandbox']['max'] = $submission_exporter
->getQuery()
->count()
->execute();
$context['results']['webform_id'] = $webform
->id();
$context['results']['source_entity_type'] = $source_entity ? $source_entity
->getEntityTypeId() : NULL;
$context['results']['source_entity_id'] = $source_entity ? $source_entity
->id() : NULL;
$context['results']['export_options'] = $export_options;
$submission_exporter
->writeHeader();
}
$query = $submission_exporter
->getQuery();
$query
->range($context['sandbox']['offset'], $submission_exporter
->getBatchLimit());
$entity_ids = $query
->execute();
$webform_submissions = WebformSubmission::loadMultiple($entity_ids);
$submission_exporter
->writeRecords($webform_submissions);
$context['sandbox']['progress'] += count($webform_submissions);
$context['sandbox']['offset'] += $submission_exporter
->getBatchLimit();
$context['message'] = t('Exported @count of @total submissions…', [
'@count' => $context['sandbox']['progress'],
'@total' => $context['sandbox']['max'],
]);
if ($context['sandbox']['max'] > 0 && $context['sandbox']['progress'] !== $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
else {
$context['finished'] = 1;
}
}
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'];
$webform = Webform::load($webform_id);
$source_entity = $entity_type && $entity_id ? \Drupal::entityTypeManager()
->getStorage($entity_type)
->load($entity_id) : NULL;
$export_options = $results['export_options'];
$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();
}
$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());
}
}
}