You are here

public function CSVUpdateForm::buildForm in Commerce Smart Importer 8

Builds form.

Overrides FormInterface::buildForm

File

src/Form/CSVUpdateForm.php, line 98

Class

CSVUpdateForm
Class CSVUpdateForm.

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',
      ];
    }
    elseif ($_GET['action'] == 'load') {
      $save = CommerceSmartImporterConstants::TEMP_DIR . '/' . $_GET['import_name'];
      $errors = json_decode(file_get_contents($save . '/log.json'), TRUE);
      $form['submit'] = [
        '#type' => 'submit',
        '#value' => $this
          ->t('Update everything that is accepted'),
      ];
      $form['image_action'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Append images'),
        '#description' => $this
          ->t('If this box is checked images provided in file will be appended to current images if allowed number of values is not reached, else it will just be replaced'),
      ];
      $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,
        '#log_type' => 'update',
      ];
      $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',
        ],
      ];
      $form['#attached']['library'] = [
        'commerce_smart_importer/commerce-smart-importer-import-library',
      ];
    }
  }
  return $form;
}