2016-10-19 03:11:36 +00:00
|
|
|
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
|
2013-08-19 06:29:49 +00:00
|
|
|
|
2015-04-27 21:44:41 +00:00
|
|
|
class Organization
|
|
|
|
module Search
|
2018-04-26 08:55:53 +00:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
# methods defined here are going to extend the class, not the instance of it
|
|
|
|
class_methods do
|
2013-08-19 06:29:49 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2015-08-16 00:53:27 +00:00
|
|
|
search organizations preferences
|
|
|
|
|
|
|
|
result = Organization.search_preferences(user_model)
|
|
|
|
|
|
|
|
returns if user has permissions to search
|
|
|
|
|
|
|
|
result = {
|
|
|
|
prio: 1000,
|
|
|
|
direct_search_index: true
|
|
|
|
}
|
|
|
|
|
|
|
|
returns if user has no permissions to search
|
|
|
|
|
|
|
|
result = false
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2018-04-26 08:55:53 +00:00
|
|
|
def search_preferences(current_user)
|
|
|
|
return false if !current_user.permissions?('ticket.agent') && !current_user.permissions?('admin.organization')
|
|
|
|
{
|
|
|
|
prio: 1000,
|
|
|
|
direct_search_index: true,
|
|
|
|
}
|
|
|
|
end
|
2015-08-16 00:53:27 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2013-08-19 06:29:49 +00:00
|
|
|
search organizations
|
|
|
|
|
|
|
|
result = Organization.search(
|
2016-01-20 01:48:54 +00:00
|
|
|
current_user: User.find(123),
|
|
|
|
query: 'search something',
|
|
|
|
limit: 15,
|
2018-04-13 07:22:55 +00:00
|
|
|
offset: 100,
|
2013-08-19 06:29:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = [organization_model1, organization_model2]
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2018-04-26 08:55:53 +00:00
|
|
|
def search(params)
|
|
|
|
|
|
|
|
# get params
|
|
|
|
query = params[:query]
|
|
|
|
limit = params[:limit] || 10
|
|
|
|
offset = params[:offset] || 0
|
|
|
|
current_user = params[:current_user]
|
|
|
|
|
|
|
|
# enable search only for agents and admins
|
|
|
|
return [] if !search_preferences(current_user)
|
|
|
|
|
|
|
|
# try search index backend
|
|
|
|
if SearchIndexBackend.enabled?
|
|
|
|
items = SearchIndexBackend.search(query, limit, 'Organization', {}, offset)
|
|
|
|
organizations = []
|
|
|
|
items.each do |item|
|
|
|
|
organization = Organization.lookup(id: item[:id])
|
|
|
|
next if !organization
|
|
|
|
organizations.push organization
|
|
|
|
end
|
|
|
|
return organizations
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2014-01-29 23:55:25 +00:00
|
|
|
|
2018-04-26 08:55:53 +00:00
|
|
|
# fallback do sql query
|
|
|
|
# - stip out * we already search for *query* -
|
|
|
|
query.delete! '*'
|
|
|
|
organizations = Organization.where(
|
|
|
|
'name LIKE ? OR note LIKE ?', "%#{query}%", "%#{query}%"
|
|
|
|
).order('name').offset(offset).limit(limit).to_a
|
|
|
|
|
|
|
|
# use result independent of size if an explicit offset is given
|
|
|
|
# this is the case for e.g. paginated searches
|
|
|
|
return organizations if params[:offset].present?
|
|
|
|
return organizations if organizations.length > 3
|
|
|
|
|
|
|
|
# if only a few organizations are found, search for names of users
|
|
|
|
organizations_by_user = Organization.select('DISTINCT(organizations.id), organizations.name').joins('LEFT OUTER JOIN users ON users.organization_id = organizations.id').where(
|
|
|
|
'users.firstname LIKE ? or users.lastname LIKE ? or users.email LIKE ?', "%#{query}%", "%#{query}%", "%#{query}%"
|
|
|
|
).order('organizations.name').limit(limit)
|
|
|
|
|
|
|
|
organizations_by_user.each do |organization_by_user|
|
|
|
|
|
|
|
|
organization_exists = false
|
|
|
|
organizations.each do |organization|
|
|
|
|
next if organization.id != organization_by_user.id
|
|
|
|
organization_exists = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
# get model with full data
|
|
|
|
next if organization_exists
|
|
|
|
organizations.push Organization.find(organization_by_user.id)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2018-04-26 08:55:53 +00:00
|
|
|
organizations
|
2015-04-27 21:44:41 +00:00
|
|
|
end
|
2013-08-19 06:29:49 +00:00
|
|
|
end
|
|
|
|
end
|
2014-02-03 19:23:00 +00:00
|
|
|
end
|