You are here

function countries_admin_import_form_submit in Countries 7.2

Same name and namespace in other branches
  1. 7 countries.admin.inc \countries_admin_import_form_submit()

Submit handler.

File

./countries.admin.inc, line 367
Admin page callbacks for the Countries module.

Code

function countries_admin_import_form_submit($form, &$form_state) {
  $inserted = array();
  $updated = array();
  $skipped = array();
  if (!empty($form_state['inserts'])) {
    foreach (array_filter($form_state['values']['inserts']) as $iso2) {
      $country = $form_state['inserts'][$iso2];
      if ($duplicates = country_duplicate_field_check($country)) {
        $skipped[] = t('Skipped @iso2 due to duplicate fields in:<br />!duplicates', array(
          '@iso2' => $iso2,
          '!duplicates' => implode('<br />', $duplicates),
        ));
      }
      else {
        country_save($country, FALSE);
        $inserted[] = l($country->name, 'admin/config/regional/countries/' . $country->iso2);
      }
    }
  }
  if (count($inserted)) {
    drupal_set_message(t('The newly imported countries were: !countries', array(
      '!countries' => implode('; ', $inserted),
    )));
  }
  if (!empty($form_state['updates'])) {
    $changes = array();
    foreach (array_filter($form_state['values']['updates']) as $change) {
      list($iso2, $key) = explode('-', $change);
      $changes[$iso2][$key] = $form_state['updates'][$iso2][$key]['new'];
    }
    foreach ($changes as $iso2 => $values) {
      $country = country_load($iso2);
      foreach ($values as $key => $value) {
        $country->{$key} = $value;
      }
      if ($duplicates = country_duplicate_field_check($country)) {
        $skipped[] = t('Skipped @iso2 due to duplicate fields in:<br />!duplicates', array(
          '@iso2' => $iso2,
          '!duplicates' => implode('<br />', $duplicates),
        ));
      }
      else {
        country_save($country, FALSE);
        $updated[] = l($country->name, 'admin/config/regional/countries/' . $country->iso2);
      }
    }
  }
  if (count($updated)) {
    drupal_set_message(t('The updated countries were: !countries', array(
      '!countries' => implode('; ', $updated),
    )));
  }
  if (empty($inserted) && empty($updated)) {
    drupal_set_message(t('No changes to the countries database were done.'));
  }
  else {
    countries_clear_caches();
  }
  if (!empty($skipped)) {
    foreach ($skipped as $warning) {
      drupal_set_message($warning, 'warning');
    }
  }
  $form_state['redirect'] = 'admin/config/regional/countries';
}