trabajo-afectivo/app/controllers/search_controller.rb

141 lines
3.2 KiB
Ruby
Raw Normal View History

2014-02-03 19:24:49 +00:00
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
2013-05-21 22:30:09 +00:00
class SearchController < ApplicationController
before_filter :authentication_check
2014-09-19 21:35:40 +00:00
# GET /api/v1/search_user_org
def search_user_org
# enable search only for agents and admins
2015-02-15 09:12:27 +00:00
if !current_user.is_role('Agent') && !current_user.is_role(Z_ROLENAME_ADMIN)
2014-09-19 21:35:40 +00:00
response_access_deny
return true
end
# get params
query = params[:query]
limit = params[:limit] || 10
# try search index backend
assets = {}
result = []
if SearchIndexBackend.enabled?
items = SearchIndexBackend.search( query, limit, ['User', 'Organization'] )
items.each { |item|
require item[:type].to_filename
record = Kernel.const_get( item[:type] ).find( item[:id] )
assets = record.assets(assets)
result.push item
}
else
# do query
users = User.search(
:query => query,
:limit => limit,
:current_user => current_user,
)
user_result = []
users.each do |user|
item = {
:id => user.id,
:type => user.class.to_s
}
result.push item
assets = user.assets(assets)
end
organizations = Organization.search(
:query => query,
:limit => limit,
:current_user => current_user,
)
organization_result = []
organizations.each do |organization|
item = {
:id => organization.id,
:type => organization.class.to_s
}
result.push item
assets = organization.assets(assets)
end
end
render :json => {
:assets => assets,
:result => result,
}
end
# GET /api/v1/search
2013-05-21 22:30:09 +00:00
def search
2013-05-21 22:30:09 +00:00
# build result list
tickets = Ticket.search(
:limit => params[:limit],
:query => params[:term],
:current_user => current_user,
)
assets = {}
2013-05-21 22:30:09 +00:00
ticket_result = []
tickets.each do |ticket|
assets = ticket.assets(assets)
2013-05-21 22:30:09 +00:00
ticket_result.push ticket.id
end
# do query
users = User.search(
2013-05-22 01:40:24 +00:00
:query => params[:term],
:limit => params[:limit],
2013-05-21 22:30:09 +00:00
:current_user => current_user,
)
user_result = []
users.each do |user|
user_result.push user.id
assets = user.assets(assets)
2013-05-21 22:30:09 +00:00
end
organizations = Organization.search(
2013-05-22 01:40:24 +00:00
:query => params[:term],
:limit => params[:limit],
2013-05-21 22:30:09 +00:00
:current_user => current_user,
)
2013-05-22 01:40:24 +00:00
2013-05-21 22:30:09 +00:00
organization_result = []
organizations.each do |organization|
organization_result.push organization.id
assets = organization.assets(assets)
2013-05-21 22:30:09 +00:00
end
result = []
if ticket_result[0]
data = {
:name => 'Ticket',
:ids => ticket_result,
}
result.push data
end
if user_result[0]
data = {
:name => 'User',
:ids => user_result,
}
result.push data
end
if organization_result[0]
data = {
:name => 'Organization',
:ids => organization_result,
}
result.push data
end
# return result
render :json => {
2013-08-19 10:20:26 +00:00
:assets => assets,
2013-05-21 22:30:09 +00:00
:result => result,
}
end
end