You are here

public function IpGeoLocAPI::storeLocation in IP Geolocation Views & Maps 8

Store the supplied IP geolocation info on the database. This will overwrite any existing info for the IP address in question.

Parameters

array $location: Array with up to 13 location info fields; must at least contain a non-empty $location['ip_address'] and a non-empty $location['formatted_address'] for anything to happen.

Return value

mixed Returns 0, when no insert or update was necessary SAVED_NEW (=1), when a new location record was inserted into the db SAVED_UPDATED (=2), when an existing location record was updated FALSE, when a db insert or db update failed

File

src/Services/IpGeoLocAPI.php, line 58

Class

IpGeoLocAPI
Class IpGeoAPI to interact with other modules.

Namespace

Drupal\ip_geoloc\Services

Code

public function storeLocation(array $location) {

  // Give contributed modules a chance to add their 2 cents by implementing
  // hook_get_ip_geolocation_alter()
  // @TODO migrate drupal_alter
  // drupal_alter('get_ip_geolocation', $location);.
  $config = \Drupal::config('ip_geloc.settings');
  $stores_addresses = $this->config
    ->get('ip_geoloc_store_addresses') ? $this->config
    ->get('ip_geoloc_store_addresses') : TRUE;
  if (!$stores_addresses) {
    return;
  }
  if (empty($location['ip_address']) || empty($location['formatted_address'])) {

    // ip_geoloc_debug('IPGV&M: location object must contain both IP address
    // and formatted address -- not stored.');.
    return 0;
  }
  if ($location['ip_address'] != '127.0.0.1' && (!isset($location['latitude']) || !isset($location['latitude']))) {
    $this->logger
      ->get('IPGV&M')
      ->warning('latitude or longitude missing for IP address %ip (location still stored)', [
      '%ip' => $location['ip_address'],
    ]);
  }

  // See if this IP is already on the db.
  $connection = \Drupal::database();
  $result = $connection
    ->query('SELECT * FROM {ip_geoloc} WHERE ip_address = :ip', [
    ':ip' => $location['ip_address'],
  ]);
  $existing_location = $result
    ->fetchAssoc();
  if (!$existing_location) {

    // New entry, insert.
    $location['city'] = utf8_encode($location['city']);
    $location['formatted_address'] = utf8_encode($location['formatted_address']);

    // @TODO move debug to other location
    // ip_geoloc_debug(t('IP Geolocaton: adding new record to db: !location', array('!location' => ip_geoloc_pretty_print($location))));
    $full_location =& $location;
  }
  else {

    // When updating, drupal_write_record() does not erase fields not present
    // in $location.
    $empty_location['latitude'] = '';
    $empty_location['longitude'] = '';
    $empty_location['country'] = '';
    $empty_location['country_code'] = '';
    $empty_location['region'] = '';
    $empty_location['region_code'] = '';
    $empty_location['city'] = '';
    $empty_location['locality'] = '';
    $empty_location['route'] = '';
    $empty_location['street_number'] = '';
    $empty_location['postal_code'] = '';
    $empty_location['administrative_area_level_1'] = '';
    $empty_location['formatted_address'] = '';
    $location['id'] = $existing_location['id'];
    $full_location = array_merge($empty_location, $location);

    // @TODO move debug to other location
    // ip_geoloc_debug(t('IPGV&M: updating db with above location'));
  }
  try {
    $result = $connection
      ->merge('ip_geoloc')
      ->fields($full_location);
    if ($existing_location) {
      $result
        ->key([
        'id',
      ]);
    }
    $result
      ->execute();
  } catch (PDOException $e) {

    // May happen when a fields contains illegal characters.
    $this->messenger
      ->addMessage(Html::escape($e
      ->getMessage()), 'error');
    $result = FALSE;
  }
  if ($result === FALSE) {
    $this->messenger
      ->addMessage($this->stringTranslation
      ->translate('IPGV&M: could not save location to db: !location', [
      '!location' => ip_geoloc_pretty_print($full_location),
    ]), 'error');
  }
  return $result;
}