You are here

public function CSVExportForm::buildForm in Commerce Smart Importer 8

Builds form.

Overrides FormInterface::buildForm

File

src/Form/CSVExportForm.php, line 85

Class

CSVExportForm
Form for exporting products in CSV format.

Namespace

Drupal\commerce_smart_importer\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $options['all'] = 'All';
  $config = $this->smartImporterService
    ->getConfig();
  $taxonomies = $this->smartImporterService
    ->getReferencedTaxonomyTerms('product', $config['commerce_product_bundle']);
  foreach ($taxonomies as $taxonomy) {
    $options[$taxonomy['machine_name']] = $taxonomy['name'];
  }
  $form['export_by'] = [
    '#type' => 'select',
    '#options' => $options,
  ];
  foreach ($options as $exportBy => $option) {
    $options2 = [];
    $options2['all'] = 'All';
    if ($exportBy != 'all') {
      foreach ($taxonomies as $taxonomy) {
        if ($taxonomy['machine_name'] == $exportBy) {
          foreach ($taxonomy['target_bundles'] as $bundle) {
            $terms = $this->entityTypeManager
              ->getStorage('taxonomy_term')
              ->loadTree($bundle);
            foreach ($terms as $term) {
              $options2[$term->tid] = $term->name;
            }
          }
        }
      }
    }
    $form['export_tax_' . $exportBy] = [
      '#type' => 'select',
      '#options' => $options2,
      '#states' => [
        'visible' => [
          ':input[name="export_by"]' => [
            'value' => $exportBy,
          ],
        ],
      ],
    ];
  }
  $fields = $this->smartImporterService
    ->getFieldDefinition(TRUE);
  $identifier_fields = $this->smartImporterService
    ->getIdentifierFields();
  $product_identifiers = [];
  $product_fields = [];
  foreach ($fields['product'] as $field) {
    if (in_array($field['machine_names'], $identifier_fields['product'])) {
      $product_identifiers[$field['machine_names']] = [
        'machine_name' => $field['machine_names'],
        'field_name' => $field['label'],
      ];
    }
    else {
      $product_fields[$field['machine_names']] = [
        'machine_name' => $field['machine_names'],
        'field_name' => $field['label'],
      ];
    }
  }
  $variation_identifiers = [];
  $variation_fields = [];
  foreach ($fields['variation'] as $field) {
    if (in_array($field['machine_names'], $identifier_fields['variation'])) {
      $variation_identifiers[$field['machine_names']] = [
        'machine_name' => $field['machine_names'],
        'field_name' => $field['label'],
      ];
    }
    else {
      $variation_fields[$field['machine_names']] = [
        'machine_name' => $field['machine_names'],
        'field_name' => $field['label'],
      ];
    }
  }
  $headers = [
    'field_name' => $this
      ->t('Field name'),
    'machine_name' => $this
      ->t('Machine name'),
  ];
  $form['product'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Product Fields'),
  ];
  $form['product']['product_identifiers_fieldset'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Product identifier Fields'),
  ];
  $form['product']['product_identifiers_fieldset']['product_identifiers'] = [
    '#type' => 'tableselect',
    '#header' => $headers,
    '#options' => $product_identifiers,
  ];
  $form['product']['product_fields_fieldset'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Product Fields'),
  ];
  $form['product']['product_fields_fieldset']['product_fields'] = [
    '#type' => 'tableselect',
    '#header' => $headers,
    '#options' => $product_fields,
  ];
  $form['variation'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Variation Fields'),
  ];
  $form['variation']['variation_identifiers_fieldset'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Variation identifier Fields'),
  ];
  $form['variation']['variation_identifiers_fieldset']['variation_identifiers'] = [
    '#type' => 'tableselect',
    '#header' => $headers,
    '#options' => $variation_identifiers,
  ];
  $form['variation']['variation_fields_fieldset'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Variation Fields'),
  ];
  $form['variation']['variation_fields_fieldset']['variation_fields'] = [
    '#type' => 'tableselect',
    '#header' => $headers,
    '#options' => $variation_fields,
  ];
  $form['export'] = [
    '#value' => 'Export',
    '#type' => 'submit',
  ];
  $form['download_export'] = [
    '#value' => 'Download last export',
    '#type' => 'submit',
    '#submit' => [
      [
        $this,
        'downloadCsv',
      ],
    ],
  ];
  $form['#attached']['library'] = [
    'commerce_smart_importer/commerce-smart-importer-importer-load-library',
  ];
  if (isset($_GET['download'])) {
    $this
      ->downloadCsv();
  }
  return $form;
}