You are here

public function LingotekImportForm::submitForm in Lingotek Translation 8

Form submission handler.

Parameters

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

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

Overrides ConfigFormBase::submitForm

File

src/Form/LingotekImportForm.php, line 121
Contains \Drupal\Lingotek\Form\LingotekManagementForm.

Class

LingotekImportForm
@file Contains \Drupal\Lingotek\Form\LingotekManagementForm.

Namespace

Drupal\lingotek\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $results = $form_state
    ->getValue('table');
  $doc_ids_to_import = array();
  foreach ($results as $id => $checked) {
    if (is_string($checked) && $checked == $id) {
      array_push($doc_ids_to_import, $id);
    }
  }
  $import_success_count = 0;
  $import_failure_count = 0;
  $import_failure_doc_ids = array();
  foreach ($doc_ids_to_import as $id) {
    $response = $this
      ->import($id);
    if ($response == 0) {
      $import_failure_count++;
      array_push($import_failure_doc_ids, $this
        ->searchForDoc($id)->properties->id);
    }
    else {
      $import_success_count++;
    }
  }
  $importedCount = $import_success_count + $import_failure_count;
  if ($import_success_count > 0) {
    $plural_or_singular = \Drupal::translation()
      ->formatPlural($importedCount, 'Successfully imported @import_success_count of @importedCount Document', 'Successfully imported @import_success_count of @importedCount Documents', array(
      '@import_success_count' => $import_success_count,
      '@importedCount' => $importedCount,
    ), array());
    drupal_set_message($plural_or_singular);
    if ($import_success_count != $importedCount) {
      $document_plurality = \Drupal::translation()
        ->formatPlural($import_failure_count, 'The following document did not import: @failed_imports', 'The following documents did not import: @failed_imports', array(
        '@failed_imports' => $this
          ->toStringUnsuccessfulImports($import_failure_doc_ids),
      ), array());
      drupal_set_message($document_plurality, 'error');
    }
  }
  else {
    if ($import_success_count == 0 && $import_failure_count == 0) {
      drupal_set_message($this
        ->t('No files were selected to import. Please check the desired documents to import.'), 'error');
    }
    else {
      $file_plurality = \Drupal::translation()
        ->formatPlural($importedCount, 'There was an error importing your file. We currently only support Wordpress, Drupal, HTML, and Text files.', 'There was an error importing your files. We currently only support Wordpress, Drupal, HTML, and Text files.', array(), array());
      drupal_set_message($file_plurality, 'error');
    }
  }
}