You are here

public function CSVExportForm::submitForm in Commerce Smart Importer 8

Submits form.

Overrides FormInterface::submitForm

File

src/Form/CSVExportForm.php, line 247

Class

CSVExportForm
Form for exporting products in CSV format.

Namespace

Drupal\commerce_smart_importer\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $values = $form_state
    ->getValues();
  $config = $this->smartImporterService
    ->getConfig();
  $export_fields_names = [];
  foreach ($values['product_fields'] as $field) {
    if (!empty($field)) {
      $export_fields_names[] = $field;
    }
  }
  foreach ($values['product_identifiers'] as $field) {
    if (!empty($field)) {
      $export_fields_names[] = $field;
    }
  }
  foreach ($values['variation_identifiers'] as $field) {
    if (!empty($field)) {
      $export_fields_names[] = $field;
    }
  }
  foreach ($values['variation_fields'] as $field) {
    if (!empty($field)) {
      $export_fields_names[] = $field;
    }
  }
  $fields = $this->smartImporterService
    ->getFieldDefinition(TRUE);
  $this
    ->leaveOnlyCheckedFields($fields, $export_fields_names);
  if ($values['export_by'] != 'all' && $values['export_tax_' . $values['export_by']] != 'all') {
    $query = $this->database
      ->query("SELECT cp.product_id FROM {commerce_product} cp JOIN {commerce_product__" . $values['export_by'] . "} tax ON cp.type = '" . $config['commerce_product_bundle'] . "' tax.entity_id=cp.product_id WHERE tax." . $values['export_by'] . "_target_id=" . $values['export_tax_' . $values['export_by']])
      ->fetchAll();
    $field_value = [
      $values['export_by'],
      $values['export_tax_' . $values['export_by']],
    ];
  }
  else {
    $query = $this->database
      ->select('commerce_product')
      ->fields('commerce_product', [
      'product_id',
    ])
      ->condition('type', $config['commerce_product_bundle'])
      ->execute()
      ->fetchAll();
    $field_value = [];
  }
  $product_number = count($query);
  $per_batch = 5;
  $number_of_batches = ceil($product_number / $per_batch);
  $batch = [
    'title' => $this
      ->t('Exporting all products'),
    'init_message' => $this
      ->t('Beginning...'),
    'progress_message' => $this
      ->t('exported @current out of @total product groups'),
    'error_message' => $this
      ->t('Something went wrong'),
    'progressive' => FALSE,
    'operations' => [],
  ];
  if ($config['store'] != 'all') {
    $store = Store::load($config['store']);
    $name = $store
      ->getName();
  }
  else {
    $name = $this
      ->config('system.site')
      ->get('name');
  }
  $file = fopen('temporary://export-' . str_replace(' ', '-', $name) . '.csv', 'w');
  $header = [];
  foreach ($fields['product'] as $product_header) {
    $header[] = $product_header['label'];
  }
  foreach ($fields['variation'] as $variation_header) {
    $header[] = $variation_header['label'];
  }
  fputcsv($file, $header);
  fputcsv($file, [
    '',
  ]);
  fclose($file);
  for ($i = 0; $i < $number_of_batches; $i++) {
    $batch['operations'][] = [
      [
        $this,
        'putExportedProductsInCsv',
      ],
      [
        $i * $per_batch,
        $per_batch,
        $fields,
        $field_value,
      ],
    ];
  }
  batch_set($batch);
}