You are here

public function CSVImportForm::buildForm in Commerce Smart Importer 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/CSVImportForm.php, line 95

Class

CSVImportForm
Class CSVImportForm enales you upload CSV and import products from it.

Namespace

Drupal\commerce_smart_importer\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $sql = $this->database
    ->query("SELECT store_id FROM {commerce_store_field_data} LIMIT 1")
    ->fetchAll();
  if (count($sql) != 0) {
    if ($_GET['action'] == 'check') {
      $form['csv_file'] = [
        '#type' => 'managed_file',
        '#title' => $this
          ->t('Upload CSV files here'),
        '#upload_validators' => [
          'file_validate_extensions' => [
            'csv',
          ],
        ],
        '#required' => TRUE,
      ];
      $form['image_upload'] = [
        '#markup' => '<div id="dropzone" class="dropzone needsclick dz-clickable dz-started"></div>',
      ];
      $form['submit'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Upload'),
      ];
      $form['#attached']['library'] = [
        'commerce_smart_importer/commerce-smart-importer-dropzone-library',
        'commerce_smart_importer/commerce-smart-importer-import-library',
      ];
    }
    elseif ($_GET['action'] == 'load') {
      $form['#attached']['library'] = [
        'commerce_smart_importer/commerce-smart-importer-importer-load-library',
      ];
      $form['importing_rules'] = [
        '#type' => 'checkboxes',
        '#options' => [
          'duplicates' => $this
            ->t('Products will be imported but duplicate values will be eliminated'),
          'cardinality' => $this
            ->t('If allowed numbers of values are exceeded, only part will be used'),
          'default_value' => $this
            ->t('Import products with system default values(if invalid or empty)'),
          'incorrect_value' => $this
            ->t('Import products with invalid - not mandatory fields'),
          'variations' => $this
            ->t('Create product even if some variations are not valid'),
          'sku' => $this
            ->t('If checked Importer will generate new sku for products where sku is already taken, else will skip'),
        ],
        '#title' => $this
          ->t('Select options'),
        '#default_value' => [
          'duplicates',
          'cardinality',
          'default_value',
          'incorrect_value',
          'variations',
        ],
      ];
      $form['import'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Import'),
      ];
      $save = CommerceSmartImporterConstants::TEMP_DIR . '/' . $_GET['import_name'];
      $errors = json_decode(file_get_contents($save . '/log.json'), TRUE);
      $field_defintions = json_decode(file_get_contents($save . '/field_definitions.json'), TRUE);
      $log = [
        '#theme' => 'commerce_smart_importer_error_logger',
        '#error_log' => $errors,
        '#field_definitions' => $field_defintions,
      ];
      $form['log'] = [
        '#markup' => '<div class="log-section" id="log-section" align="center"> <h2 class="log-section__title">Notices and errors in CSV</h2>' . $this->renderer
          ->render($log) . '</div>',
        '#allowed_tags' => [
          'div',
          'section',
          'input',
          'span',
          'b',
          'br',
          'button',
          'fieldset',
          'legend',
          'p',
          'label',
          'strong',
          'h2',
        ],
      ];
    }
  }
  else {
    $this
      ->messenger()
      ->addStatus($this
      ->t("In order to use this module you'll have to create at least one store"));
  }
  return $form;
}