You are here

function ip2country_admin_settings_submit in IP-based Determination of a Visitor's Country 7

Same name and namespace in other branches
  1. 6 ip2country.admin.inc \ip2country_admin_settings_submit()

Processes forms submitted by IP to Country administration page.

See also

ip2country_admin_settings()

1 string reference to 'ip2country_admin_settings_submit'
ip2country_admin_settings in ./ip2country.admin.inc
Default IP to Country administration settings.

File

./ip2country.admin.inc, line 286
Determination of user's Country based on IP address.

Code

function ip2country_admin_settings_submit($form, &$form_state) {
  global $user;

  // Exclude unnecessary elements from being saved in variable table.
  unset($form_state['values']['ip2country_update_database'], $form_state['values']['ip2country_lookup'], $form_state['values']['ip2country_lookup_button']);

  // Check to see if debug set.
  if ($form_state['values']['ip2country_debug']) {

    // Debug on.
    if ($form_state['values']['ip2country_test_type']) {

      // Dummy IP Address.
      $ip = $form_state['values']['ip2country_test_ip_address'];
      $country_code = ip2country_get_country($ip);
    }
    else {

      // Dummy Country.
      $country_code = $form_state['values']['ip2country_test_country'];
    }
    $country_list = country_get_list();
    $country_name = $country_list[$country_code];
    drupal_set_message(t('Using DEBUG value for Country - @country (@code)', array(
      '@country' => $country_name,
      '@code' => $country_code,
    )));
  }
  else {

    // Debug off - make sure we set/reset IP/Country to their real values.
    $ip = ip_address();
    $country_code = ip2country_get_country($ip);
    drupal_set_message(t('Using ACTUAL value for Country - @country', array(
      '@country' => $country_code,
    )));
  }

  // Finally, save country, if it has been determined.
  if ($country_code) {

    // Store the ISO country code in the $user object.
    user_save($user, array(
      'country_iso_code_2' => $country_code,
    ));
  }
}