You are here

function countries_import_bulk_import_form_submit in Countries 8

File

modules/countries_import/countries_import.admin.inc, line 283

Code

function countries_import_bulk_import_form_submit($form, &$form_state) {
  $class = $form_state['values']['source'];
  $plugin = new $class();
  switch ($form_state['stage']) {
    case 'source':
      $settings = empty($form_state['values']['plugin']['settings']) ? array() : $form_state['values']['plugin']['settings'];
      $settings['langcode'] = $form_state['values']['langcode'];
      $form_state['import_data'] = $plugin
        ->import($settings, $form, $form_state);
      $form_state['stage'] = 'selection';
      $form_state['source'] = $form_state['values']['source'];
      $form_state['langcode'] = $form_state['values']['langcode'];
      $form_state['skip_disabled'] = $form_state['values']['skip_disabled'];
      $form_state['import_settings'] = $form_state['values']['plugin'];
      $form_state['rebuild'] = TRUE;
      break;
    case 'selection':
      if (empty($form_state['values']['countries'])) {
        unset($form_state['rebuild']);
      }
      $lookup_key = $plugin
        ->importKey();
      $skipped = 0;
      $errors = 0;
      $updated = 0;
      $created = 0;
      $i18n = module_exists('countries_i18n');
      $update_i18n = $i18n && $form_state['langcode'] != language_default('language');
      foreach ($form_state['values']['countries'] as $values) {
        if (!empty($values['skip'])) {
          $skipped++;
          continue;
        }
        if ($values['cid']) {
          $countries = countries_load($values['cid']);
          $country = reset($countries);
          $country->enabled = $values['enabled'];
          foreach (countries_core_properties() as $property => $label) {
            $updated_property = trim($values[$property]);
            switch ($property) {
              case 'continent':
                if (empty($updated_property)) {
                  $values[$property] = 'UN';
                }
                $country->{$property} = $updated_property;
                break;
              case 'name':
              case 'official_name':
                if ($form_state['langcode'] == language_default('language')) {
                  $country->{$property} = $updated_property;
                }
                else {

                  // Leave the default untouched but save the changes.
                  if ($update_i18n) {
                    $name = 'countries:country:' . $country->iso2 . ':' . $property;
                    list($textgroup, $context) = i18n_string_context(explode(':', $name));
                    i18n_string_textgroup($textgroup)
                      ->update_translation($context, $form_state['langcode'], $updated_property);
                  }
                }
                break;
              default:
                $country->{$property} = $updated_property;
            }
          }
        }
        else {
          $country = (object) $values;
          $country->language = language_default('language');
          if (empty($country->continent)) {
            $country->continent = 'UN';
          }
        }
        if (country_validate($country)) {
          if (country_save($country) == SAVED_NEW) {
            $created++;
            drupal_set_message(t('Created %name (%iso2)', array(
              '%name' => $country->name,
              '%iso2' => $country->iso2,
            )));
            if ($update_i18n) {
              $name = 'countries:country:' . $country->iso2 . ':name';
              list($textgroup, $context) = i18n_string_context(explode(':', $name));
              i18n_string_textgroup($textgroup)
                ->update_translation($context, $form_state['langcode'], $country->name);
              if (!empty($country->official_name)) {
                $name = 'countries:country:' . $country->iso2 . ':official_name';
                list($textgroup, $context) = i18n_string_context(explode(':', $name));
                i18n_string_textgroup($textgroup)
                  ->update_translation($context, $form_state['langcode'], $country->official_name);
              }
            }
          }
          else {
            $updated++;
            drupal_set_message(t('Updated %name (%iso2)', array(
              '%name' => country_property($country),
              '%iso2' => $country->iso2,
            )));
          }

          // Clear the form state if the country was imported.
          unset($form_state['import_data'][$country->{$lookup_key}]);
        }
        else {
          $errors++;
          drupal_set_message(t('Errors were detected while trying to import %name:<br/>!errors', array(
            '%name' => $country->name,
            '!errors' => implode('<br />', $country->_errors),
          )), 'error');
        }
      }
      if ($skipped || $errors) {
        $form_state['rebuild'] = TRUE;
      }
      drupal_set_message(t('Import complete.') . ' ' . format_plural($skipped, t('Skipped 1 country.'), t('Skipped @count countries.')) . ' ' . format_plural($created, t('Created 1 new country.'), t('Created @count new countries.')) . ' ' . format_plural($updated, t('Updated 1 country.'), t('Updated @count countries.')) . ' ' . format_plural($errors, t('1 error was detected.'), t('@count errors were detected.')));
      break;
  }
}