You are here

function drush_yamlform_export in YAML Form 8

Implements drush_hook_COMMAND().

File

drush/yamlform.drush.inc, line 217
YAML Form module drush commands.

Code

function drush_yamlform_export($yamlform_id = NULL) {
  if (!$yamlform_id) {
    $yamlforms = array_keys(YamlForm::loadMultiple());
    $choices = array_combine($yamlforms, $yamlforms);
    $yamlform_id = drush_choice($choices, dt("Choose a form to export submissions from."));
    if ($yamlform_id === FALSE) {
      return drush_user_abort();
    }
  }
  $yamlform = YamlForm::load($yamlform_id);

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

  /** @var \Drupal\yamlform\YamlFormSubmissionExporterInterface $submission_exporter */
  $submission_exporter = \Drupal::service('yamlform_submission.exporter');
  $submission_exporter
    ->setYamlForm($yamlform);
  $submission_exporter
    ->setSourceEntity($source_entity);

  // Get command options as export options.
  $export_options = drush_redispatch_get_options();

  // Convert dashes to underscores.
  foreach ($export_options as $key => $value) {
    unset($export_options[$key]);
    $export_options[str_replace('-', '_', $key)] = $value;
  }
  $export_options += $submission_exporter
    ->getDefaultExportOptions();
  $submission_exporter
    ->setExporter($export_options);
  YamlFormResultsExportController::batchSet($yamlform, $source_entity, $export_options);
  drush_backend_batch_process();
  $file_path = $submission_exporter
    ->isArchive() ? $submission_exporter
    ->getArchiveFilePath() : $submission_exporter
    ->getExportFilePath();
  if (isset($export_options['destination'])) {
    drush_print(dt('Created @destination', [
      '@destination' => $export_options['destination'],
    ]));
    file_unmanaged_copy($file_path, $export_options['destination'], FILE_EXISTS_REPLACE);
  }
  else {
    drush_print(file_get_contents($file_path));
  }
  @unlink($file_path);
  return NULL;
}