trabajo-afectivo/app/controllers/search_controller.rb

141 lines
3 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_action :authentication_check
2013-05-21 22:30:09 +00:00
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 10:01:12 +00:00
if !current_user.is_role(Z_ROLENAME_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, %w(User Organization) )
2014-09-19 21:35:40 +00:00
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,
2014-09-19 21:35:40 +00:00
)
user_result = []
users.each do |user|
item = {
id: user.id,
type: user.class.to_s
2014-09-19 21:35:40 +00:00
}
result.push item
assets = user.assets(assets)
end
organizations = Organization.search(
query: query,
limit: limit,
current_user: current_user,
2014-09-19 21:35:40 +00:00
)
organization_result = []
organizations.each do |organization|
item = {
id: organization.id,
type: organization.class.to_s
2014-09-19 21:35:40 +00:00
}
result.push item
assets = organization.assets(assets)
end
end
render json: {
assets: assets,
result: result,
2014-09-19 21:35:40 +00:00
}
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,
2013-05-21 22:30:09 +00:00
)
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(
query: params[:term],
limit: params[:limit],
current_user: current_user,
2013-05-21 22:30:09 +00:00
)
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(
query: params[:term],
limit: params[:limit],
current_user: current_user,
2013-05-21 22:30:09 +00:00
)
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,
2013-05-21 22:30:09 +00:00
}
result.push data
end
if user_result[0]
data = {
name: 'User',
ids: user_result,
2013-05-21 22:30:09 +00:00
}
result.push data
end
if organization_result[0]
data = {
name: 'Organization',
ids: organization_result,
2013-05-21 22:30:09 +00:00
}
result.push data
end
# return result
render json: {
assets: assets,
result: result,
2013-05-21 22:30:09 +00:00
}
end
end