View source
<?php
namespace Drupal\yamlform\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;
use Drupal\yamlform\Entity\YamlForm;
use Drupal\yamlform\Entity\YamlFormSubmission;
use Drupal\yamlform\YamlFormInterface;
use Drupal\yamlform\YamlFormRequestInterface;
use Drupal\yamlform\YamlFormSubmissionExporterInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class YamlFormResultsExportController extends ControllerBase implements ContainerInjectionInterface {
protected $mimeTypeGuesser;
protected $submissionExporter;
protected $requestHandler;
public function __construct(MimeTypeGuesserInterface $mime_type_guesser, YamlFormSubmissionExporterInterface $yamlform_submission_exporter, YamlFormRequestInterface $request_handler) {
$this->mimeTypeGuesser = $mime_type_guesser;
$this->submissionExporter = $yamlform_submission_exporter;
$this->requestHandler = $request_handler;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('file.mime_type.guesser'), $container
->get('yamlform_submission.exporter'), $container
->get('yamlform.request'));
}
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');
$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);
}
}
public function file(Request $request, $filename) {
list($yamlform, $source_entity) = $this->requestHandler
->getYamlFormEntities();
$this->submissionExporter
->setYamlForm($yamlform);
$this->submissionExporter
->setSourceEntity($source_entity);
$file_path = $this->submissionExporter
->getFileTempDirectory() . '/' . $filename;
if (!file_exists($file_path)) {
$route_name = $this->requestHandler
->getRouteName($yamlform, $source_entity, 'yamlform.results_export');
$route_parameters = $this->requestHandler
->getRouteParameters($yamlform, $source_entity);
$t_args = [
':href' => Url::fromRoute($route_name, $route_parameters)
->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 form</a> to create a new export.', $t_args),
];
return $build;
}
else {
return $this
->downloadFile($file_path);
}
}
public function downloadFile($file_path, $download = TRUE) {
$contents = file_get_contents($file_path);
unlink($file_path);
$content_type = $this->mimeTypeGuesser
->guess($file_path);
if ($download) {
$headers = [
'Content-Length' => strlen($contents),
'Content-Type' => $content_type,
'Content-Disposition' => 'attachment; filename="' . basename($file_path) . '"',
];
}
else {
if ($content_type != 'text/html') {
$content_type = 'text/plain';
}
$headers = [
'Content-Length' => strlen($contents),
'Content-Type' => $content_type . '; charset=utf-8',
];
}
return new Response($contents, 200, $headers);
}
public static function batchSet(YamlFormInterface $yamlform, 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('yamlform_submission.exporter');
$submission_exporter
->setYamlForm($yamlform);
$submission_exporter
->setSourceEntity($source_entity);
$submission_exporter
->setExporter($export_options);
$parameters = [
$yamlform,
$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\\yamlform\\Controller\\YamlFormResultsExportController',
'batchProcess',
],
$parameters,
],
],
'finished' => [
'\\Drupal\\yamlform\\Controller\\YamlFormResultsExportController',
'batchFinish',
],
];
batch_set($batch);
}
public static function batchProcess(YamlFormInterface $yamlform, EntityInterface $source_entity = NULL, array $export_options, &$context) {
$submission_exporter = \Drupal::service('yamlform_submission.exporter');
$submission_exporter
->setYamlForm($yamlform);
$submission_exporter
->setSourceEntity($source_entity);
$submission_exporter
->setExporter($export_options);
if (empty($context['sandbox'])) {
$context['sandbox']['progress'] = 0;
$context['sandbox']['current_sid'] = 0;
$context['sandbox']['max'] = $submission_exporter
->getQuery()
->count()
->execute();
$context['results']['yamlform_id'] = $yamlform
->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
->condition('sid', $context['sandbox']['current_sid'], '>');
$query
->range(0, $submission_exporter
->getBatchLimit());
$entity_ids = $query
->execute();
$yamlform_submissions = YamlFormSubmission::loadMultiple($entity_ids);
$submission_exporter
->writeRecords($yamlform_submissions);
$context['sandbox']['progress'] += count($yamlform_submissions);
$context['sandbox']['current_sid'] = $yamlform_submissions ? end($yamlform_submissions)
->id() : 0;
$context['message'] = t('Exported @count of @total submissions...', [
'@count' => $context['sandbox']['progress'],
'@total' => $context['sandbox']['max'],
]);
if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
}
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'];
$yamlform = YamlForm::load($yamlform_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('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();
}
$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());
}
}
}