2016-10-19 03:11:36 +00:00
|
|
|
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
|
2013-08-06 10:29:35 +00:00
|
|
|
|
2021-03-01 08:18:40 +00:00
|
|
|
# Perform geo data lookup on user changes.
|
|
|
|
module User::PerformsGeoLookup
|
|
|
|
extend ActiveSupport::Concern
|
2013-08-06 10:29:35 +00:00
|
|
|
|
2021-03-01 08:18:40 +00:00
|
|
|
included do
|
|
|
|
before_create :user_check_geo_location
|
|
|
|
before_update :user_check_geo_location
|
2013-08-06 10:29:35 +00:00
|
|
|
end
|
2015-05-07 10:27:12 +00:00
|
|
|
|
2021-03-01 08:18:40 +00:00
|
|
|
private
|
2013-08-06 10:29:35 +00:00
|
|
|
|
2021-03-01 08:18:40 +00:00
|
|
|
def user_check_geo_location
|
2013-08-06 10:29:35 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
location = %w[address street zip city country]
|
2013-08-06 10:29:35 +00:00
|
|
|
|
|
|
|
# check if geo update is needed based on old/new location
|
2021-03-01 08:18:40 +00:00
|
|
|
if id
|
|
|
|
current = User.find_by(id: id)
|
2014-02-03 19:23:00 +00:00
|
|
|
return if !current
|
2013-08-06 10:29:35 +00:00
|
|
|
|
|
|
|
current_location = {}
|
2017-10-01 12:25:52 +00:00
|
|
|
location.each do |item|
|
2013-08-06 10:29:35 +00:00
|
|
|
current_location[item] = current[item]
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2013-08-06 10:29:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# get full address
|
|
|
|
next_location = {}
|
2017-10-01 12:25:52 +00:00
|
|
|
location.each do |item|
|
2021-03-01 08:18:40 +00:00
|
|
|
next_location[item] = attributes[item]
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2013-08-06 10:29:35 +00:00
|
|
|
|
|
|
|
# return if address hasn't changed and geo data is already available
|
2021-03-01 08:18:40 +00:00
|
|
|
return if (current_location == next_location) && preferences['lat'] && preferences['lng']
|
2013-08-06 10:29:35 +00:00
|
|
|
|
|
|
|
# geo update
|
2021-03-01 08:18:40 +00:00
|
|
|
user_update_geo_location
|
2013-08-06 10:29:35 +00:00
|
|
|
end
|
|
|
|
|
2021-03-01 08:18:40 +00:00
|
|
|
def user_update_geo_location
|
2013-08-06 10:29:35 +00:00
|
|
|
address = ''
|
2017-11-23 08:09:44 +00:00
|
|
|
location = %w[address street zip city country]
|
2017-10-01 12:25:52 +00:00
|
|
|
location.each do |item|
|
2021-03-01 08:18:40 +00:00
|
|
|
next if attributes[item].blank?
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-09-29 16:03:09 +00:00
|
|
|
if address.present?
|
|
|
|
address += ', '
|
2013-08-06 10:29:35 +00:00
|
|
|
end
|
2021-03-01 08:18:40 +00:00
|
|
|
address += attributes[item]
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2013-08-06 10:29:35 +00:00
|
|
|
|
|
|
|
# return if no address is given
|
2017-09-29 16:03:09 +00:00
|
|
|
return if address.blank?
|
2013-08-06 10:29:35 +00:00
|
|
|
|
2013-08-15 20:40:22 +00:00
|
|
|
# lookup
|
2016-03-08 06:32:58 +00:00
|
|
|
latlng = Service::GeoLocation.geocode(address)
|
2021-03-01 08:18:40 +00:00
|
|
|
|
2013-08-06 10:29:35 +00:00
|
|
|
return if !latlng
|
|
|
|
|
|
|
|
# store data
|
2021-03-01 08:18:40 +00:00
|
|
|
preferences['lat'] = latlng[0]
|
|
|
|
preferences['lng'] = latlng[1]
|
2013-08-06 10:29:35 +00:00
|
|
|
end
|
2014-02-03 19:23:00 +00:00
|
|
|
end
|