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')
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2018-04-26 08:55:53 +00:00
|
|
|
{
|
2019-03-07 10:56:17 +00:00
|
|
|
prio: 1500,
|
2018-04-26 08:55:53 +00:00
|
|
|
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,
|
2018-07-18 14:00:06 +00:00
|
|
|
|
|
|
|
# sort single column
|
|
|
|
sort_by: 'created_at',
|
|
|
|
order_by: 'asc',
|
|
|
|
|
|
|
|
# sort multiple columns
|
|
|
|
sort_by: [ 'created_at', 'updated_at' ],
|
|
|
|
order_by: [ 'asc', 'desc' ],
|
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]
|
|
|
|
|
2020-10-30 07:59:32 +00:00
|
|
|
sql_helper = ::SqlHelper.new(object: self)
|
|
|
|
|
2019-06-20 10:45:27 +00:00
|
|
|
# check sort - positions related to order by
|
2020-10-30 07:59:32 +00:00
|
|
|
sort_by = sql_helper.get_sort_by(params, %w[active updated_at])
|
2018-07-18 14:00:06 +00:00
|
|
|
|
2019-06-20 10:45:27 +00:00
|
|
|
# check order - positions related to sort by
|
2020-10-30 07:59:32 +00:00
|
|
|
order_by = sql_helper.get_order_by(params, %w[desc desc])
|
2018-07-18 14:00:06 +00:00
|
|
|
|
2018-04-26 08:55:53 +00:00
|
|
|
# enable search only for agents and admins
|
|
|
|
return [] if !search_preferences(current_user)
|
|
|
|
|
|
|
|
# try search index backend
|
|
|
|
if SearchIndexBackend.enabled?
|
2018-12-19 17:31:51 +00:00
|
|
|
items = SearchIndexBackend.search(query, 'Organization', limit: limit,
|
|
|
|
from: offset,
|
|
|
|
sort_by: sort_by,
|
2018-11-06 16:11:10 +00:00
|
|
|
order_by: order_by)
|
2018-04-26 08:55:53 +00:00
|
|
|
organizations = []
|
|
|
|
items.each do |item|
|
|
|
|
organization = Organization.lookup(id: item[:id])
|
|
|
|
next if !organization
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2018-04-26 08:55:53 +00:00
|
|
|
organizations.push organization
|
|
|
|
end
|
|
|
|
return organizations
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2014-01-29 23:55:25 +00:00
|
|
|
|
2020-10-30 07:59:32 +00:00
|
|
|
order_select_sql = sql_helper.get_order_select(sort_by, order_by, 'organizations.updated_at')
|
|
|
|
order_sql = sql_helper.get_order(sort_by, order_by, 'organizations.updated_at ASC')
|
2018-07-18 14:00:06 +00:00
|
|
|
|
2018-04-26 08:55:53 +00:00
|
|
|
# fallback do sql query
|
|
|
|
# - stip out * we already search for *query* -
|
|
|
|
query.delete! '*'
|
2018-06-05 07:15:48 +00:00
|
|
|
organizations = Organization.where_or_cis(%i[name note], "%#{query}%")
|
2019-07-04 11:16:55 +00:00
|
|
|
.order(Arel.sql(order_sql))
|
2018-06-05 07:15:48 +00:00
|
|
|
.offset(offset)
|
|
|
|
.limit(limit)
|
|
|
|
.to_a
|
2018-04-26 08:55:53 +00:00
|
|
|
|
|
|
|
# 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
|
2018-07-19 16:54:12 +00:00
|
|
|
organizations_by_user = Organization.select("DISTINCT(organizations.id), #{order_select_sql}")
|
2018-06-05 07:15:48 +00:00
|
|
|
.joins('LEFT OUTER JOIN users ON users.organization_id = organizations.id')
|
|
|
|
.where(User.or_cis(%i[firstname lastname email], "%#{query}%"))
|
2019-07-04 11:16:55 +00:00
|
|
|
.order(Arel.sql(order_sql))
|
2018-06-05 07:15:48 +00:00
|
|
|
.limit(limit)
|
2018-04-26 08:55:53 +00:00
|
|
|
|
|
|
|
organizations_by_user.each do |organization_by_user|
|
|
|
|
|
|
|
|
organization_exists = false
|
|
|
|
organizations.each do |organization|
|
|
|
|
next if organization.id != organization_by_user.id
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2018-04-26 08:55:53 +00:00
|
|
|
organization_exists = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
# get model with full data
|
|
|
|
next if organization_exists
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2018-04-26 08:55:53 +00:00
|
|
|
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
|