View source
<?php
namespace Drupal\webform\Plugin;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\webform\EntityStorage\WebformEntityStorageTrait;
use Drupal\webform\WebformSubmissionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class WebformExporterBase extends PluginBase implements WebformExporterInterface {
use WebformEntityStorageTrait;
protected $logger;
protected $configFactory;
protected $elementManager;
protected $tokenManager;
protected $archive;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = new static($configuration, $plugin_id, $plugin_definition);
$instance->logger = $container
->get('logger.factory')
->get('webform');
$instance->configFactory = $container
->get('config.factory');
$instance->entityTypeManager = $container
->get('entity_type.manager');
$instance->elementManager = $container
->get('plugin.manager.webform.element');
$instance->tokenManager = $container
->get('webform.token_manager');
$instance
->setConfiguration($configuration);
return $instance;
}
public function label() {
return $this->pluginDefinition['label'];
}
public function description() {
return $this->pluginDefinition['description'];
}
public function getStatus() {
return TRUE;
}
public function isExcluded() {
return $this->configFactory
->get('webform.settings')
->get('export.excluded_exporters.' . $this->pluginDefinition['id']) ? TRUE : FALSE;
}
public function isArchive() {
return $this->pluginDefinition['archive'];
}
public function hasFiles() {
return $this->pluginDefinition['files'];
}
public function hasOptions() {
return $this->pluginDefinition['options'];
}
public function getConfiguration() {
return $this->configuration;
}
public function setConfiguration(array $configuration) {
$this->configuration = $configuration + $this
->defaultConfiguration();
return $this;
}
public function defaultConfiguration() {
return [
'webform' => NULL,
'source_entity' => NULL,
];
}
protected function getWebform() {
return $this->configuration['webform'];
}
protected function getSourceEntity() {
return $this->configuration['source_entity'];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
return $form;
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
}
public function createExport() {
}
public function openExport() {
}
public function closeExport() {
}
public function writeHeader() {
}
public function writeSubmission(WebformSubmissionInterface $webform_submission) {
}
public function writeFooter() {
}
public function getFileTempDirectory() {
return $this->configFactory
->get('webform.settings')
->get('export.temp_directory') ?: \Drupal::service('file_system')
->getTempDirectory();
}
public function getSubmissionBaseName(WebformSubmissionInterface $webform_submission) {
$export_options = $this
->getConfiguration();
$file_name = $export_options['file_name'];
$file_name = $this->tokenManager
->replace($file_name, $webform_submission);
$file_name = preg_replace('([^\\w\\s\\d\\-_~,;:\\[\\]\\(\\].]|[\\.]{2,})', '', $file_name);
$file_name = preg_replace('/\\s+/', '-', $file_name);
return $file_name;
}
public function getFileExtension() {
return 'txt';
}
public function getBaseFileName() {
$webform = $this
->getWebform();
$source_entity = $this
->getSourceEntity();
if ($source_entity) {
return $webform
->id() . '.' . $source_entity
->getEntityTypeId() . '.' . $source_entity
->id();
}
else {
return $webform
->id();
}
}
public function getExportFileName() {
return $this
->getBaseFileName() . '.' . $this
->getFileExtension();
}
public function getExportFilePath() {
return $this
->getFileTempDirectory() . '/' . $this
->getExportFileName();
}
public function getArchiveFilePath() {
return $this
->getFileTempDirectory() . '/' . $this
->getArchiveFileName();
}
public function getArchiveFileName() {
return $this
->getBaseFileName() . '.' . $this
->getArchiveFileExtension();
}
public function getArchiveType() {
return $this->configuration['archive_type'] === WebformExporterInterface::ARCHIVE_ZIP && class_exists('\\ZipArchive') ? WebformExporterInterface::ARCHIVE_ZIP : WebformExporterInterface::ARCHIVE_TAR;
}
public function getArchiveFileExtension() {
return $this
->getArchiveType() === WebformExporterInterface::ARCHIVE_ZIP ? 'zip' : 'tar.gz';
}
public function addToArchive($path, $name, array $options = []) {
$options += [
'remove_path' => '',
'close' => FALSE,
];
if ($this
->getArchiveType() === WebformExporterInterface::ARCHIVE_ZIP) {
$this
->addToZipFile($path, $name, $options);
}
else {
$this
->addToTarArchive($path, $name, $options);
}
}
public function getBatchLimit() {
return $this->configFactory
->get('webform.settings')
->get('batch.default_batch_export_size') ?: 500;
}
protected function addToTarArchive($path, $name, array $options = []) {
if (!isset($this->archive)) {
$this->archive = new \Archive_Tar($this
->getArchiveFilePath(), 'gz');
}
if (@file_exists($path)) {
if (is_dir($path)) {
$this->archive
->addModify((array) $path, $name, $options['remove_path']);
}
else {
$this->archive
->addModify((array) $path, $name, $options['remove_path']);
}
}
else {
$this->archive
->addString($name, $path);
}
if (!empty($options['close'])) {
$this->archive = NULL;
}
}
protected function addToZipFile($path, $name, array $options = []) {
if (!isset($this->archive)) {
$this->archive = new \ZipArchive();
$flags = !file_exists($this
->getArchiveFilePath()) ? \ZipArchive::CREATE : NULL;
$this->archive
->open($this
->getArchiveFilePath(), $flags);
}
if (@file_exists($path)) {
if (is_dir($path)) {
$options += [
'add_path' => $name . '/',
];
$this->archive
->addPattern('/\\.[a-z0-9]+$/', $path, $options);
}
else {
$file_name = $path;
if ($options['remove_path']) {
$file_name = preg_replace('#^' . $options['remove_path'] . '#', '', $file_name);
}
$file_name = ltrim($file_name, '/');
$this->archive
->addFile($path, $name . '/' . $file_name);
}
}
else {
$this->archive
->addFromString($name, $path);
}
if (!empty($options['close'])) {
$this->archive
->close();
$this->archive = NULL;
}
}
}