2014-02-03 19:23:00 +00:00
|
|
|
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
2013-08-19 06:29:49 +00:00
|
|
|
|
|
|
|
module User::Search
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
search tickets
|
|
|
|
|
|
|
|
result = Ticket.search(
|
|
|
|
:current_user => User.find(123),
|
|
|
|
:query => 'search something',
|
|
|
|
:limit => 15,
|
|
|
|
)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = [ticket_model1, ticket_model2]
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
search user
|
|
|
|
|
|
|
|
result = User.search(
|
|
|
|
:query => 'some search term'
|
|
|
|
:limit => 15,
|
|
|
|
:current_user => user_model,
|
|
|
|
)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = [user_model1, user_model2, ...]
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def search(params)
|
|
|
|
|
|
|
|
# get params
|
|
|
|
query = params[:query]
|
|
|
|
limit = params[:limit] || 10
|
|
|
|
current_user = params[:current_user]
|
|
|
|
|
|
|
|
# enable search only for agents and admins
|
|
|
|
return [] if !current_user.is_role('Agent') && !current_user.is_role('Admin')
|
|
|
|
|
2014-01-29 23:55:25 +00:00
|
|
|
# try search index backend
|
2014-02-02 18:58:31 +00:00
|
|
|
if SearchIndexBackend.enabled?
|
2014-01-29 23:55:25 +00:00
|
|
|
ids = SearchIndexBackend.search( query, limit, 'User' )
|
|
|
|
users = []
|
|
|
|
ids.each { |id|
|
|
|
|
users.push User.lookup( :id => id )
|
|
|
|
}
|
|
|
|
return users
|
|
|
|
end
|
|
|
|
|
|
|
|
# fallback do sql query
|
2014-02-11 13:09:23 +00:00
|
|
|
# - stip out * we already search for *query* -
|
|
|
|
query.gsub! '*', ''
|
2014-05-20 19:49:48 +00:00
|
|
|
users = User.where(
|
|
|
|
'(firstname LIKE ? or lastname LIKE ? or email LIKE ?) AND id != 1', "%#{query}%", "%#{query}%", "%#{query}%",
|
|
|
|
).order('firstname').limit(limit)
|
2013-08-19 06:29:49 +00:00
|
|
|
return users
|
|
|
|
end
|
|
|
|
|
2014-02-03 19:23:00 +00:00
|
|
|
end
|