I would like to use geouser to auto fill $user->location based on their current IP address if user has not entered it.

Geouser looks like a great way to do this.

That way i am not always asking the user to fill in their location information.

I was thinking of something along these lines

global $user;
if geouser module is installed then {
if ($user->location['city'] == '') $user->location['city'] = $user->content['geouser']['city'];
if ($user->location['region'] == '') $user->location['region'] = $user->content['geouser']['region'];
if ($user->location['country'] == '') $user->location['country'] = $user->->content['geouser']['country'];
drupal_set_message ('Your location has been set to '. $user->location['city'] .' ,'. $user->location['region'] .' ,'.$user->location['country']);

}

Does this seem like the right start? I could use some real programmers to just help flesh it out.

Thanks,
Chris

Geouser defines this:
$account->content['geouser']['country']
$account->content['geouser']['region']
$account->content['geouser']['city']

Comments

acdtrp’s picture

I would like to see this solved too!

tinohuda’s picture

subcribe

deggertsen’s picture

subscribe

bryancasler’s picture

subscribe

alexbk66-’s picture

Title: Default user location given by Geouser » Default user location given by Geouser, GeoIP
rooby’s picture

Status: Active » Fixed

Yep, that sort of solution would work.

There are a number of ways you could do it depending on the functionality desired.
If you just want it so that when a user saves their account page it checks for empty location values and fills them based on geouser data you could use hook_user()
It would be something like this (assuming your geouser fields are these):

<?php
/**
 * Implementation of hook_user().
 */
function mymodule_user($op, &$edit, &$account, $category = NULL) {
  if (!empty($edit['locations'])) {
    foreach ($edit['locations'] as $key => $location) {
      $changed = FALSE;
      if (empty($location['city'])) {
        $changed = TRUE;
        $edit['locations'][$key]['city'] = $account->content['geouser']['city'];
      }
      if (empty($location['province'])) {
        $changed = TRUE;
        $edit['locations'][$key]['province'] = $account->content['geouser']['region'];
      }
      if (empty($location['country'])) {
        $changed = TRUE;
        $edit['locations'][$key]['country'] = $account->content['geouser']['country'];
      }
      if ($changed) {
        drupal_set_message(t('Your location has been set to %city, %province, %country.', array(
          '%city' => $edit['locations'][$key]['city'],
          '%province' => $edit['locations'][$key]['province'],
          '%country' => $edit['locations'][$key]['country'])));
      }
    }
  }
}
?>

However this is tied to the user edit form so it will only occur when that form is saved.
If a user is saved programmatically via a custom module or views_bulk_operations or something this won't be called.

There are other ways you could do it using hook user for different cases and there is also hook_locationapi() in the location module (using the field_expand op), which allows you to override the default values of the location fields when they are loaded on the screen (then the user could change them if they wanted to before saving).

It is not something that will ever be a part of the location module but it would be fairly straight forward to write a module that bridges location with geouser, ip2cc, smart_ip or whatever other module you can get the location data from.

Feel free to reopen this if you have further questions.

Status: Fixed » Closed (fixed)
Issue tags: -default location, -geouser, -ip

Automatically closed -- issue fixed for 2 weeks with no activity.