You are here

function countries_admin_import_form_submit in Countries 7

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

File

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

Code

function countries_admin_import_form_submit($form, &$form_state) {
  $inserted = array();
  if (!empty($form_state['inserts'])) {
    foreach (array_filter($form_state['values']['inserts']) as $iso2) {
      $country = $form_state['inserts'][$iso2];
      country_save($form_state['inserts'][$iso2]);
      $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),
    )));
  }
  $updated = array();
  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;
      }
      country_save($country, $iso2);
      $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.'));
  }
  $form_state['redirect'] = 'admin/config/regional/countries';
}