You are here

public function CSVIntroductionForm::csvDownload in Commerce Smart Importer 8

Downloads importer template.

File

src/Form/CSVIntroductionForm.php, line 186

Class

CSVIntroductionForm
Introduction form.

Namespace

Drupal\commerce_smart_importer\Form

Code

public function csvDownload() {
  $fields = $this->smartImporterService
    ->getFieldDefinition();
  $ordered = [];
  foreach ($fields['product'] as $product_field) {
    $ordered[] = $product_field['label'];
  }
  $leading_header = $this
    ->reorderBasedOnPrefered($ordered);
  $ordered = [];
  foreach ($fields['variation'] as $variation_field) {
    $ordered[] = $variation_field['label'];
  }
  $leading_header = array_merge($leading_header, $this
    ->reorderBasedOnPrefered($ordered));
  $config = $this->smartImporterService
    ->getConfig();
  if ($config['store'] != 'all') {
    $store = Store::load($config['store']);
    $name = $store
      ->getName();
  }
  else {
    $name = $this
      ->config('system.site')
      ->get('name');
  }
  $path = "public://csv-" . str_replace(' ', '-', $name) . ".csv";
  $file = fopen($path, 'w');
  fputcsv($file, $leading_header);
  fputcsv($file, [
    'Your product data starts in next row',
  ]);
  fclose($file);
  $csv_file = file_get_contents($path);
  header('Content-Description: File Transfer');
  header('Expires: 0');
  header('Cache-Control: must-revalidate');
  header('Pragma: public');
  header('Content-Type: application/csv');
  header("Content-length: " . filesize($path));
  header('Content-Disposition: attachment; filename="' . basename($path) . '"');
  if (file_exists($path)) {
    unlink($path);
  }
  echo $csv_file;
  exit;
}