You are here

public function WebformCliService::drush_webform_export in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Commands/WebformCliService.php \Drupal\webform\Commands\WebformCliService::drush_webform_export()

Implements drush_hook_COMMAND().

Overrides WebformCliServiceInterface::drush_webform_export

File

src/Commands/WebformCliService.php, line 331

Class

WebformCliService
Drush version agnostic commands.

Namespace

Drupal\webform\Commands

Code

public function drush_webform_export($webform_id = NULL) {
  if (!$webform_id) {
    $webforms = array_keys(Webform::loadMultiple());
    $choices = array_combine($webforms, $webforms);
    $webform_id = $this
      ->drush_choice($choices, $this
      ->dt("Choose a webform to export submissions from."));
    if ($webform_id === FALSE) {
      return $this
        ->drush_user_abort();
    }
  }
  $webform = Webform::load($webform_id);

  // @todd Determine if we should get source entity from options entity type
  // and id.
  $source_entity = NULL;

  /** @var \Drupal\webform\WebformSubmissionExporterInterface $submission_exporter */
  $submission_exporter = \Drupal::service('webform_submission.exporter');
  $submission_exporter
    ->setWebform($webform);
  $submission_exporter
    ->setSourceEntity($source_entity);

  // Get command options as export options.
  $default_options = $submission_exporter
    ->getDefaultExportOptions();
  $export_options = $this
    ->drush_redispatch_get_options();

  // Convert dashes to underscores.
  foreach ($export_options as $key => $value) {
    unset($export_options[$key]);
    if (isset($default_options[$key]) && is_array($default_options[$key])) {
      $value = explode(',', $value);
    }
    $export_options[str_replace('-', '_', $key)] = $value;
  }
  $export_options += $submission_exporter
    ->getDefaultExportOptions();
  $submission_exporter
    ->setExporter($export_options);
  WebformResultsExportController::batchSet($webform, $source_entity, $export_options);
  $this
    ->drush_backend_batch_process();
  $file_path = $submission_exporter
    ->isArchive() ? $submission_exporter
    ->getArchiveFilePath() : $submission_exporter
    ->getExportFilePath();
  if (isset($export_options['destination'])) {
    $this
      ->drush_print($this
      ->dt('Created @destination', [
      '@destination' => $export_options['destination'],
    ]));
    \Drupal::service('file_system')
      ->copy($file_path, $export_options['destination'], FileSystemInterface::EXISTS_REPLACE);
  }
  else {
    $this
      ->drush_print(file_get_contents($file_path));
  }
  @unlink($file_path);
  return NULL;
}